From 8b7007ad7b99494d449b8345a2bf25461ea8ce3e Mon Sep 17 00:00:00 2001 From: Daniel Neighman Date: Mon, 4 Dec 2017 17:51:44 -0800 Subject: Updates the ruby generator RubyAsType to correctly account for underscores in packages Prior to this change, when the ruby generator tried to reference an entity that was not part of the same package (or a direct parent package) and the package contains underscores, the result would simply uppercase the first character. It should however uppercase each letter that proceeds an underscore and remove underscores. i.e. ``` package my_package.service; import "my_package/data.proto"; service MyService { rpc Test (data.Request) returns data.Response {} } ``` Was ```ruby # ... rpc :Test, My_package::Data::REquest, My_package::Data::Response # ... ``` Should be: ```ruby # ... rpc :Test, MyPackage::Data::REquest, My_package::Data::Response # ... ``` --- src/compiler/ruby_generator.cc | 2 +- src/compiler/ruby_generator_string-inl.h | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) (limited to 'src/compiler') diff --git a/src/compiler/ruby_generator.cc b/src/compiler/ruby_generator.cc index e81dea603b..c7af9c38fa 100644 --- a/src/compiler/ruby_generator.cc +++ b/src/compiler/ruby_generator.cc @@ -73,7 +73,7 @@ void PrintService(const ServiceDescriptor* service, const grpc::string& package, // Begin the service module std::map module_vars = ListToDict({ "module.name", - CapitalizeFirst(service->name()), + Modularize(service->name()), }); out->Print(module_vars, "module $module.name$\n"); out->Indent(); diff --git a/src/compiler/ruby_generator_string-inl.h b/src/compiler/ruby_generator_string-inl.h index fb429784bb..ecfe796e7a 100644 --- a/src/compiler/ruby_generator_string-inl.h +++ b/src/compiler/ruby_generator_string-inl.h @@ -81,13 +81,23 @@ inline bool ReplacePrefix(grpc::string* s, const grpc::string& from, return true; } -// CapitalizeFirst capitalizes the first char in a string. -inline grpc::string CapitalizeFirst(grpc::string s) { +// Modularize converts a string into a ruby module compatible name +inline grpc::string Modularize(grpc::string s) { if (s.empty()) { return s; } - s[0] = ::toupper(s[0]); - return s; + grpc::string new_string = ""; + bool was_last_underscore = false; + new_string.append(1, ::toupper(s[0])); + for (grpc::string::size_type i = 1; i < s.size(); ++i) { + if (was_last_underscore && s[i] != '_') { + new_string.append(1, ::toupper(s[i])); + } else if (s[i] != '_') { + new_string.append(1, s[i]); + } + was_last_underscore = s[i] == '_'; + } + return new_string; } // RubyTypeOf updates a proto type to the required ruby equivalent. @@ -106,7 +116,7 @@ inline grpc::string RubyTypeOf(const grpc::string& a_type, res += "::"; // switch '.' to the ruby module delim } if (i < prefixes_and_type.size() - 1) { - res += CapitalizeFirst(prefixes_and_type[i]); // capitalize pkgs + res += Modularize(prefixes_and_type[i]); // capitalize pkgs } else { res += prefixes_and_type[i]; } -- cgit v1.2.3