ruby_generator.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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::ServiceDescriptor;
  28. using grpc::protobuf::MethodDescriptor;
  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 =
  39. RubyTypeOf(method->input_type()->full_name(), package);
  40. if (method->client_streaming()) {
  41. input_type = "stream(" + input_type + ")";
  42. }
  43. grpc::string output_type =
  44. RubyTypeOf(method->output_type()->full_name(), package);
  45. if (method->server_streaming()) {
  46. output_type = "stream(" + output_type + ")";
  47. }
  48. std::map<grpc::string, grpc::string> method_vars = ListToDict({
  49. "mth.name", method->name(), "input.type", input_type, "output.type",
  50. output_type,
  51. });
  52. out->Print(GetRubyComments(method, true).c_str());
  53. out->Print(method_vars, "rpc :$mth.name$, $input.type$, $output.type$\n");
  54. out->Print(GetRubyComments(method, false).c_str());
  55. }
  56. // Prints out the service using the ruby gRPC DSL.
  57. void PrintService(const ServiceDescriptor *service, const grpc::string &package,
  58. Printer *out) {
  59. if (service->method_count() == 0) {
  60. return;
  61. }
  62. // Begin the service module
  63. std::map<grpc::string, grpc::string> module_vars = ListToDict({
  64. "module.name", CapitalizeFirst(service->name()),
  65. });
  66. out->Print(module_vars, "module $module.name$\n");
  67. out->Indent();
  68. out->Print(GetRubyComments(service, true).c_str());
  69. out->Print("class Service\n");
  70. // Write the indented class body.
  71. out->Indent();
  72. out->Print("\n");
  73. out->Print("include GRPC::GenericService\n");
  74. out->Print("\n");
  75. out->Print("self.marshal_class_method = :encode\n");
  76. out->Print("self.unmarshal_class_method = :decode\n");
  77. std::map<grpc::string, grpc::string> pkg_vars =
  78. ListToDict({"service_full_name", service->full_name()});
  79. out->Print(pkg_vars, "self.service_name = '$service_full_name$'\n");
  80. out->Print("\n");
  81. for (int i = 0; i < service->method_count(); ++i) {
  82. PrintMethod(service->method(i), package, out);
  83. }
  84. out->Outdent();
  85. out->Print("end\n");
  86. out->Print("\n");
  87. out->Print("Stub = Service.rpc_stub_class\n");
  88. // End the service module
  89. out->Outdent();
  90. out->Print("end\n");
  91. out->Print(GetRubyComments(service, false).c_str());
  92. }
  93. } // namespace
  94. // The following functions are copied directly from the source for the protoc
  95. // ruby generator
  96. // to ensure compatibility (with the exception of int and string type changes).
  97. // See
  98. // https://github.com/google/protobuf/blob/master/src/google/protobuf/compiler/ruby/ruby_generator.cc#L250
  99. // TODO: keep up to date with protoc code generation, though this behavior isn't
  100. // expected to change
  101. bool IsLower(char ch) { return ch >= 'a' && ch <= 'z'; }
  102. char ToUpper(char ch) { return IsLower(ch) ? (ch - 'a' + 'A') : ch; }
  103. // Package names in protobuf are snake_case by convention, but Ruby module
  104. // names must be PascalCased.
  105. //
  106. // foo_bar_baz -> FooBarBaz
  107. grpc::string PackageToModule(const grpc::string &name) {
  108. bool next_upper = true;
  109. grpc::string result;
  110. result.reserve(name.size());
  111. for (grpc::string::size_type i = 0; i < name.size(); i++) {
  112. if (name[i] == '_') {
  113. next_upper = true;
  114. } else {
  115. if (next_upper) {
  116. result.push_back(ToUpper(name[i]));
  117. } else {
  118. result.push_back(name[i]);
  119. }
  120. next_upper = false;
  121. }
  122. }
  123. return result;
  124. }
  125. // end copying of protoc generator for ruby code
  126. grpc::string GetServices(const FileDescriptor *file) {
  127. grpc::string output;
  128. {
  129. // Scope the output stream so it closes and finalizes output to the string.
  130. StringOutputStream output_stream(&output);
  131. Printer out(&output_stream, '$');
  132. // Don't write out any output if there no services, to avoid empty service
  133. // files being generated for proto files that don't declare any.
  134. if (file->service_count() == 0) {
  135. return output;
  136. }
  137. // Write out a file header.
  138. std::map<grpc::string, grpc::string> header_comment_vars = ListToDict({
  139. "file.name", file->name(), "file.package", file->package(),
  140. });
  141. out.Print("# Generated by the protocol buffer compiler. DO NOT EDIT!\n");
  142. out.Print(header_comment_vars,
  143. "# Source: $file.name$ for package '$file.package$'\n");
  144. grpc::string leading_comments = GetRubyComments(file, true);
  145. if (!leading_comments.empty()) {
  146. out.Print("# Original file comments:\n");
  147. out.Print(leading_comments.c_str());
  148. }
  149. out.Print("\n");
  150. out.Print("require 'grpc'\n");
  151. // Write out require statemment to import the separately generated file
  152. // that defines the messages used by the service. This is generated by the
  153. // main ruby plugin.
  154. std::map<grpc::string, grpc::string> dep_vars = ListToDict({
  155. "dep.name", MessagesRequireName(file),
  156. });
  157. out.Print(dep_vars, "require '$dep.name$'\n");
  158. // Write out services within the modules
  159. out.Print("\n");
  160. std::vector<grpc::string> modules = Split(file->package(), '.');
  161. for (size_t i = 0; i < modules.size(); ++i) {
  162. std::map<grpc::string, grpc::string> module_vars = ListToDict({
  163. "module.name", PackageToModule(modules[i]),
  164. });
  165. out.Print(module_vars, "module $module.name$\n");
  166. out.Indent();
  167. }
  168. for (int i = 0; i < file->service_count(); ++i) {
  169. auto service = file->service(i);
  170. PrintService(service, file->package(), &out);
  171. }
  172. for (size_t i = 0; i < modules.size(); ++i) {
  173. out.Outdent();
  174. out.Print("end\n");
  175. }
  176. out.Print(GetRubyComments(file, false).c_str());
  177. }
  178. return output;
  179. }
  180. } // namespace grpc_ruby_generator