diff options
author | Daniel Neighman <dneighman@gmail.com> | 2017-12-04 17:51:44 -0800 |
---|---|---|
committer | Connor Jacobsen <jacobsen.connor@gmail.com> | 2018-03-26 16:00:05 -0700 |
commit | 8b7007ad7b99494d449b8345a2bf25461ea8ce3e (patch) | |
tree | 12f2de072bf45a6a8c4ad1cd32ed022eff32ec43 /src/compiler | |
parent | 6fa206de8f8a1444fff19a84945a424c0cabb41c (diff) |
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
# ...
```
Diffstat (limited to 'src/compiler')
-rw-r--r-- | src/compiler/ruby_generator.cc | 2 | ||||
-rw-r--r-- | src/compiler/ruby_generator_string-inl.h | 20 |
2 files changed, 16 insertions, 6 deletions
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<grpc::string, grpc::string> 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]; } |