ruby_generator.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <cctype>
  19. #include <map>
  20. #include <vector>
  21. #include "src/compiler/config.h"
  22. #include "src/compiler/ruby_generator.h"
  23. #include "src/compiler/ruby_generator_helpers-inl.h"
  24. #include "src/compiler/ruby_generator_map-inl.h"
  25. #include "src/compiler/ruby_generator_string-inl.h"
  26. using grpc::protobuf::FileDescriptor;
  27. using grpc::protobuf::MethodDescriptor;
  28. using grpc::protobuf::ServiceDescriptor;
  29. using grpc::protobuf::io::Printer;
  30. using grpc::protobuf::io::StringOutputStream;
  31. using std::map;
  32. using std::vector;
  33. namespace grpc_ruby_generator {
  34. namespace {
  35. // Prints out the method using the ruby gRPC DSL.
  36. void PrintMethod(const MethodDescriptor* method, const grpc::string& package,
  37. Printer* out) {
  38. grpc::string input_type = RubyTypeOf(method->input_type(), package);
  39. if (method->client_streaming()) {
  40. input_type = "stream(" + input_type + ")";
  41. }
  42. grpc::string output_type = RubyTypeOf(method->output_type(), package);
  43. if (method->server_streaming()) {
  44. output_type = "stream(" + output_type + ")";
  45. }
  46. std::map<grpc::string, grpc::string> method_vars = ListToDict({
  47. "mth.name",
  48. method->name(),
  49. "input.type",
  50. input_type,
  51. "output.type",
  52. output_type,
  53. });
  54. out->Print(GetRubyComments(method, true).c_str());
  55. out->Print(method_vars, "rpc :$mth.name$, $input.type$, $output.type$\n");
  56. out->Print(GetRubyComments(method, false).c_str());
  57. }
  58. // Prints out the service using the ruby gRPC DSL.
  59. void PrintService(const ServiceDescriptor* service, const grpc::string& package,
  60. Printer* out) {
  61. if (service->method_count() == 0) {
  62. return;
  63. }
  64. // Begin the service module
  65. std::map<grpc::string, grpc::string> module_vars = ListToDict({
  66. "module.name",
  67. Modularize(service->name()),
  68. });
  69. out->Print(module_vars, "module $module.name$\n");
  70. out->Indent();
  71. out->Print(GetRubyComments(service, true).c_str());
  72. out->Print("class Service\n");
  73. // Write the indented class body.
  74. out->Indent();
  75. out->Print("\n");
  76. out->Print("include GRPC::GenericService\n");
  77. out->Print("\n");
  78. out->Print("self.marshal_class_method = :encode\n");
  79. out->Print("self.unmarshal_class_method = :decode\n");
  80. std::map<grpc::string, grpc::string> pkg_vars =
  81. ListToDict({"service_full_name", service->full_name()});
  82. out->Print(pkg_vars, "self.service_name = '$service_full_name$'\n");
  83. out->Print("\n");
  84. for (int i = 0; i < service->method_count(); ++i) {
  85. PrintMethod(service->method(i), package, out);
  86. }
  87. out->Outdent();
  88. out->Print("end\n");
  89. out->Print("\n");
  90. out->Print("Stub = Service.rpc_stub_class\n");
  91. // End the service module
  92. out->Outdent();
  93. out->Print("end\n");
  94. out->Print(GetRubyComments(service, false).c_str());
  95. }
  96. } // namespace
  97. // The following functions are copied directly from the source for the protoc
  98. // ruby generator
  99. // to ensure compatibility (with the exception of int and string type changes).
  100. // See
  101. // https://github.com/google/protobuf/blob/master/src/google/protobuf/compiler/ruby/ruby_generator.cc#L250
  102. // TODO: keep up to date with protoc code generation, though this behavior isn't
  103. // expected to change
  104. bool IsLower(char ch) { return ch >= 'a' && ch <= 'z'; }
  105. char ToUpper(char ch) { return IsLower(ch) ? (ch - 'a' + 'A') : ch; }
  106. // Package names in protobuf are snake_case by convention, but Ruby module
  107. // names must be PascalCased.
  108. //
  109. // foo_bar_baz -> FooBarBaz
  110. grpc::string PackageToModule(const grpc::string& name) {
  111. bool next_upper = true;
  112. grpc::string result;
  113. result.reserve(name.size());
  114. for (grpc::string::size_type i = 0; i < name.size(); i++) {
  115. if (name[i] == '_') {
  116. next_upper = true;
  117. } else {
  118. if (next_upper) {
  119. result.push_back(ToUpper(name[i]));
  120. } else {
  121. result.push_back(name[i]);
  122. }
  123. next_upper = false;
  124. }
  125. }
  126. return result;
  127. }
  128. // end copying of protoc generator for ruby code
  129. grpc::string GetServices(const FileDescriptor* file) {
  130. grpc::string output;
  131. {
  132. // Scope the output stream so it closes and finalizes output to the string.
  133. StringOutputStream output_stream(&output);
  134. Printer out(&output_stream, '$');
  135. // Don't write out any output if there no services, to avoid empty service
  136. // files being generated for proto files that don't declare any.
  137. if (file->service_count() == 0) {
  138. return output;
  139. }
  140. std::string package_name = RubyPackage(file);
  141. // Write out a file header.
  142. std::map<grpc::string, grpc::string> header_comment_vars = ListToDict({
  143. "file.name",
  144. file->name(),
  145. "file.package",
  146. package_name,
  147. });
  148. out.Print("# Generated by the protocol buffer compiler. DO NOT EDIT!\n");
  149. out.Print(header_comment_vars,
  150. "# Source: $file.name$ for package '$file.package$'\n");
  151. grpc::string leading_comments = GetRubyComments(file, true);
  152. if (!leading_comments.empty()) {
  153. out.Print("# Original file comments:\n");
  154. out.PrintRaw(leading_comments.c_str());
  155. }
  156. out.Print("\n");
  157. out.Print("require 'grpc'\n");
  158. // Write out require statemment to import the separately generated file
  159. // that defines the messages used by the service. This is generated by the
  160. // main ruby plugin.
  161. std::map<grpc::string, grpc::string> dep_vars = ListToDict({
  162. "dep.name",
  163. MessagesRequireName(file),
  164. });
  165. out.Print(dep_vars, "require '$dep.name$'\n");
  166. // Write out services within the modules
  167. out.Print("\n");
  168. std::vector<grpc::string> modules = Split(package_name, '.');
  169. for (size_t i = 0; i < modules.size(); ++i) {
  170. std::map<grpc::string, grpc::string> module_vars = ListToDict({
  171. "module.name",
  172. PackageToModule(modules[i]),
  173. });
  174. out.Print(module_vars, "module $module.name$\n");
  175. out.Indent();
  176. }
  177. for (int i = 0; i < file->service_count(); ++i) {
  178. auto service = file->service(i);
  179. PrintService(service, file->package(), &out);
  180. }
  181. for (size_t i = 0; i < modules.size(); ++i) {
  182. out.Outdent();
  183. out.Print("end\n");
  184. }
  185. out.Print(GetRubyComments(file, false).c_str());
  186. }
  187. return output;
  188. }
  189. } // namespace grpc_ruby_generator