From 5322b13857c1df7645af352175f655fd80ad7811 Mon Sep 17 00:00:00 2001 From: darronschall Date: Fri, 6 Jan 2023 09:34:17 -0500 Subject: [PATCH 1/2] Only append "Service" to the generated service class name when services are not properly named. Protobuf services should end in `Service`. This is a [buf lint rule](https://github.com/bufbuild/buf-examples/blob/main/linting/bad/acme/weather/v1/weather.proto#L39), recommended via [the protobuf style guide](https://developers.google.com/protocol-buffers/docs/style#services), and demonstrated in [Twirp Best Practices](https://twitchtv.github.io/twirp/docs/best_practices.html)) This change avoids generating class names such as `MessagesServiceService`. --- protoc-gen-twirp_ruby/main.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/protoc-gen-twirp_ruby/main.go b/protoc-gen-twirp_ruby/main.go index 2a872af..122f149 100644 --- a/protoc-gen-twirp_ruby/main.go +++ b/protoc-gen-twirp_ruby/main.go @@ -109,7 +109,14 @@ func (g *generator) generateRubyCode(file *descriptor.FileDescriptorProto, pbFil for i, service := range file.Service { svcName := service.GetName() - print(b, "%sclass %sService < ::Twirp::Service", indent, camelCase(svcName)) + // The generated service class name should end in "Service"; Only append the + // suffix if the service is not already well-named. + svcClassName := svcName + if !strings.HasSuffix(svcClassName, "Service") { + svcClassName += "Service" + } + + print(b, "%sclass %s < ::Twirp::Service", indent, camelCase(svcClassName)) if pkgName != "" { print(b, "%s package '%s'", indent, pkgName) } @@ -125,7 +132,7 @@ func (g *generator) generateRubyCode(file *descriptor.FileDescriptorProto, pbFil print(b, "") print(b, "%sclass %sClient < ::Twirp::Client", indent, camelCase(svcName)) - print(b, "%s client_for %sService", indent, camelCase(svcName)) + print(b, "%s client_for %s", indent, camelCase(svcClassName)) print(b, "%send", indent) if i < len(file.Service)-1 { print(b, "") From 3c0927bf17b221a91bc4f50e25329a6eeafcee9f Mon Sep 17 00:00:00 2001 From: darronschall Date: Fri, 6 Jan 2023 09:50:31 -0500 Subject: [PATCH 2/2] Remove `Service` from generated client class names for well-named services. Similar to 5322b138, for well-named services that end in "Service", this change prevents the generated clients from being named like "MessagesServiceClient". --- protoc-gen-twirp_ruby/main.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/protoc-gen-twirp_ruby/main.go b/protoc-gen-twirp_ruby/main.go index 122f149..adba270 100644 --- a/protoc-gen-twirp_ruby/main.go +++ b/protoc-gen-twirp_ruby/main.go @@ -131,7 +131,10 @@ func (g *generator) generateRubyCode(file *descriptor.FileDescriptorProto, pbFil print(b, "%send", indent) print(b, "") - print(b, "%sclass %sClient < ::Twirp::Client", indent, camelCase(svcName)) + // Strip the "Service" suffix if present for better readability. + clientClassName := strings.TrimSuffix(svcName, "Service") + "Client" + + print(b, "%sclass %s < ::Twirp::Client", indent, camelCase(clientClassName)) print(b, "%s client_for %s", indent, camelCase(svcClassName)) print(b, "%send", indent) if i < len(file.Service)-1 {