aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/compiler/ruby_generator.cc
diff options
context:
space:
mode:
authorGravatar murgatroid99 <mlumish@google.com>2016-10-28 11:48:22 -0700
committerGravatar murgatroid99 <mlumish@google.com>2016-10-28 11:48:22 -0700
commit5d6ae930ab446fdb96346dbd8d2241b5498e8234 (patch)
tree064b024ffd9d2215345795732ad9c52b0545913d /src/compiler/ruby_generator.cc
parent51fc01dffabf778f8dc4697db4bc33461a29683b (diff)
parent6040b471bcd1d6bb05b25c126b6545180a1d3528 (diff)
Merge remote-tracking branch 'upstream/v1.0.x' into v1.0.1_upmerge
Diffstat (limited to 'src/compiler/ruby_generator.cc')
-rw-r--r--src/compiler/ruby_generator.cc40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/compiler/ruby_generator.cc b/src/compiler/ruby_generator.cc
index 02202568cb..42ea3586db 100644
--- a/src/compiler/ruby_generator.cc
+++ b/src/compiler/ruby_generator.cc
@@ -119,6 +119,43 @@ void PrintService(const ServiceDescriptor *service, const grpc::string &package,
} // namespace
+// The following functions are copied directly from the source for the protoc
+// ruby generator
+// to ensure compatibility (with the exception of int and string type changes).
+// See
+// https://github.com/google/protobuf/blob/master/src/google/protobuf/compiler/ruby/ruby_generator.cc#L250
+// TODO: keep up to date with protoc code generation, though this behavior isn't
+// expected to change
+bool IsLower(char ch) { return ch >= 'a' && ch <= 'z'; }
+
+char ToUpper(char ch) { return IsLower(ch) ? (ch - 'a' + 'A') : ch; }
+
+// Package names in protobuf are snake_case by convention, but Ruby module
+// names must be PascalCased.
+//
+// foo_bar_baz -> FooBarBaz
+grpc::string PackageToModule(const grpc::string &name) {
+ bool next_upper = true;
+ grpc::string result;
+ result.reserve(name.size());
+
+ for (grpc::string::size_type i = 0; i < name.size(); i++) {
+ if (name[i] == '_') {
+ next_upper = true;
+ } else {
+ if (next_upper) {
+ result.push_back(ToUpper(name[i]));
+ } else {
+ result.push_back(name[i]);
+ }
+ next_upper = false;
+ }
+ }
+
+ return result;
+}
+// end copying of protoc generator for ruby code
+
grpc::string GetServices(const FileDescriptor *file) {
grpc::string output;
{
@@ -164,6 +201,9 @@ grpc::string GetServices(const FileDescriptor *file) {
std::map<grpc::string, grpc::string> module_vars = ListToDict({
"module.name", CapitalizeFirst(modules[i]),
});
+ std::map<grpc::string, grpc::string> module_vars = ListToDict({
+ "module.name", PackageToModule(modules[i]),
+ });
out.Print(module_vars, "module $module.name$\n");
out.Indent();
}