From d69c0d34da555e8a953bbf949b324e4ada633899 Mon Sep 17 00:00:00 2001 From: Alexander Polcyn Date: Mon, 3 Oct 2016 10:33:02 -0700 Subject: [PATCH 1/3] convert snake case protos package names to camel case in ruby modules --- src/compiler/ruby_generator.cc | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/compiler/ruby_generator.cc b/src/compiler/ruby_generator.cc index 1501c3f3e01..6e5419cb47b 100644 --- a/src/compiler/ruby_generator.cc +++ b/src/compiler/ruby_generator.cc @@ -115,6 +115,16 @@ void PrintService(const ServiceDescriptor *service, const grpc::string &package, } // namespace +grpc::string SnakeCaseToCamelCase(grpc::string input) { + grpc::string output; + std::vector words = Split(input, '_'); + for(size_t i = 0; i < words.size(); i++) { + output.append(CapitalizeFirst(words[i])); + } + return output; +} + + grpc::string GetServices(const FileDescriptor *file) { grpc::string output; { @@ -156,7 +166,7 @@ grpc::string GetServices(const FileDescriptor *file) { std::vector modules = Split(file->package(), '.'); for (size_t i = 0; i < modules.size(); ++i) { std::map module_vars = - ListToDict({"module.name", CapitalizeFirst(modules[i]), }); + ListToDict({"module.name", SnakeCaseToCamelCase(modules[i]), }); out.Print(module_vars, "module $module.name$\n"); out.Indent(); } From c429d7830f2a5a32c9543ee3c362f4061c9f0129 Mon Sep 17 00:00:00 2001 From: Alexander Polcyn Date: Mon, 3 Oct 2016 11:23:20 -0700 Subject: [PATCH 2/3] change ruby proto plugin to copy package name conversion from protoc --- src/compiler/ruby_generator.cc | 36 +++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/compiler/ruby_generator.cc b/src/compiler/ruby_generator.cc index 6e5419cb47b..3a7b89bf69e 100644 --- a/src/compiler/ruby_generator.cc +++ b/src/compiler/ruby_generator.cc @@ -124,6 +124,40 @@ grpc::string SnakeCaseToCamelCase(grpc::string input) { return output; } +// The following functions are copied directly from the source for the protoc ruby generator +// to ensure compatibility ('int i' changed to 'uint i' is the only change). +// 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 +std::string PackageToModule(const std::string& name) { + bool next_upper = true; + std::string result; + result.reserve(name.size()); + + for (uint 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; @@ -166,7 +200,7 @@ grpc::string GetServices(const FileDescriptor *file) { std::vector modules = Split(file->package(), '.'); for (size_t i = 0; i < modules.size(); ++i) { std::map module_vars = - ListToDict({"module.name", SnakeCaseToCamelCase(modules[i]), }); + ListToDict({"module.name", PackageToModule(modules[i]), }); out.Print(module_vars, "module $module.name$\n"); out.Indent(); } From 5ba82e5d9205d024c249e41581e3c7fc61c5ae32 Mon Sep 17 00:00:00 2001 From: Alexander Polcyn Date: Mon, 3 Oct 2016 16:31:14 -0700 Subject: [PATCH 3/3] remove unused function and switch std::string to grpc::string --- src/compiler/ruby_generator.cc | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/compiler/ruby_generator.cc b/src/compiler/ruby_generator.cc index 3a7b89bf69e..ee2502e150a 100644 --- a/src/compiler/ruby_generator.cc +++ b/src/compiler/ruby_generator.cc @@ -115,17 +115,8 @@ void PrintService(const ServiceDescriptor *service, const grpc::string &package, } // namespace -grpc::string SnakeCaseToCamelCase(grpc::string input) { - grpc::string output; - std::vector words = Split(input, '_'); - for(size_t i = 0; i < words.size(); i++) { - output.append(CapitalizeFirst(words[i])); - } - return output; -} - // The following functions are copied directly from the source for the protoc ruby generator -// to ensure compatibility ('int i' changed to 'uint i' is the only change). +// 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'; } @@ -137,9 +128,9 @@ char ToUpper(char ch) { return IsLower(ch) ? (ch - 'a' + 'A') : ch; } // names must be PascalCased. // // foo_bar_baz -> FooBarBaz -std::string PackageToModule(const std::string& name) { +grpc::string PackageToModule(const grpc::string& name) { bool next_upper = true; - std::string result; + grpc::string result; result.reserve(name.size()); for (uint i = 0; i < name.size(); i++) {