Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small extract method refactoring to improve CodeGenerator #22

Merged
merged 4 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## [Unreleased]

- Add `skip-empty` option to prevent generating empty scaffolding for proto files without services - [#21](https://github.com/collectiveidea/protoc-gen-twirp_ruby/pull/21)
- Refactor code generator to improve internal readability - [#12](https://github.com/collectiveidea/protoc-gen-twirp_ruby/pull/12), [#13](https://github.com/collectiveidea/protoc-gen-twirp_ruby/pull/13)
- Refactor code generator to improve internal readability - [#12](https://github.com/collectiveidea/protoc-gen-twirp_ruby/pull/12), [#13](https://github.com/collectiveidea/protoc-gen-twirp_ruby/pull/13), [#22](https://github.com/collectiveidea/protoc-gen-twirp_ruby/pull/22)
- Remove unnecessary extra files from packaged gem - [#11](https://github.com/collectiveidea/protoc-gen-twirp_ruby/pull/11)

## [1.0.0] - 2024-05-10
Expand Down
13 changes: 3 additions & 10 deletions lib/twirp/protoc_plugin/code_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_relative "../../google/protobuf/compiler/plugin_pb"
require_relative "../../core_ext/string/camel_case"
require_relative "../../core_ext/string/snake_case"
require_relative "descriptor_ext/service_descriptor_proto_ext"
require "stringio"

module Twirp
Expand Down Expand Up @@ -52,14 +53,7 @@ def generate
output << "\n" if index > 0

service_name = service.name
# The generated service class name should end in "Service"; Only append the
# suffix if the service is not already well-named.
service_class_name = if service_name.end_with?("Service")
service_name
else
service_name + "Service"
end
service_class_name = service_class_name.camel_case
service_class_name = service.service_class_name
danielmorrison marked this conversation as resolved.
Show resolved Hide resolved

# Generate service class
output << line("class #{service_class_name} < ::Twirp::Service", indent_level)
Expand All @@ -76,8 +70,7 @@ def generate

# Generate client class

# Strip the "Service" suffix if present for better readability.
client_class_name = (service_name.delete_suffix("Service") + "Client").camel_case
client_class_name = service.client_class_name

output << "\n"
output << line("class #{client_class_name} < ::Twirp::Client", indent_level)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require_relative "../../../core_ext/string/camel_case"

class Google::Protobuf::ServiceDescriptorProto
def service_class_name
# The generated service class name should end in "Service"; A well-named
# service may already end with "Service" but we can't guarantee it. Use
# class_name_without_service_suffix to #avoid "ServiceService"
class_name_without_service_suffix + "Service"
end

def client_class_name
class_name_without_service_suffix + "Client"
end

private

def class_name_without_service_suffix
name.delete_suffix("Service").camel_case
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

RSpec.describe Google::Protobuf::ServiceDescriptorProto do
describe "#service_class_name" do
it "appends \"Service\" when the service is not well-named" do
service = Google::Protobuf::ServiceDescriptorProto.new(name: "HelloWorld")
expect(service.service_class_name).to eq("HelloWorldService")
end

it "does not alter the service name when the service is well-named" do
service = Google::Protobuf::ServiceDescriptorProto.new(name: "HelloWorldService")
expect(service.service_class_name).to eq("HelloWorldService")
end
end

describe "#client_class_name" do
it "appends \"Client\" when the service is not well-named" do
service = Google::Protobuf::ServiceDescriptorProto.new(name: "HelloWorld")
expect(service.client_class_name).to eq("HelloWorldClient")
end

it "trims \"Service\" and appends \"Client\" when the service is well-named" do
service = Google::Protobuf::ServiceDescriptorProto.new(name: "HelloWorldService")
expect(service.client_class_name).to eq("HelloWorldClient")
end
end
end