aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/compiler
diff options
context:
space:
mode:
authorGravatar apolcyn <apolcyn@google.com>2018-03-29 16:13:07 -0700
committerGravatar GitHub <noreply@github.com>2018-03-29 16:13:07 -0700
commita9d16ccb3dd9b9e8946b091f2524f3abc63ec3fe (patch)
treed47e08b67412a59ce05a6f1cffbf836bd9714bfa /src/compiler
parentc1b3f0c6a7507a93775c67cd6321613149083f28 (diff)
parent8b7007ad7b99494d449b8345a2bf25461ea8ce3e (diff)
Merge pull request #13634 from hassox/ruby-module-name
Updates the ruby generator RubyTypeOf to correctly account for underscores in packages
Diffstat (limited to 'src/compiler')
-rw-r--r--src/compiler/ruby_generator.cc2
-rw-r--r--src/compiler/ruby_generator_string-inl.h20
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];
}