src/compiler/ruby_generator.cc
Go to the documentation of this file.
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 
20 
21 #include <cctype>
22 #include <map>
23 #include <vector>
24 
25 #include "src/compiler/config.h"
29 
35 using std::map;
36 using std::vector;
37 
39 namespace {
40 
41 // Prints out the method using the ruby gRPC DSL.
42 void PrintMethod(const MethodDescriptor* method, Printer* out) {
43  std::string input_type = RubyTypeOf(method->input_type());
44  if (method->client_streaming()) {
45  input_type = "stream(" + input_type + ")";
46  }
47  std::string output_type = RubyTypeOf(method->output_type());
48  if (method->server_streaming()) {
49  output_type = "stream(" + output_type + ")";
50  }
51  std::map<std::string, std::string> method_vars = ListToDict({
52  "mth.name",
53  method->name(),
54  "input.type",
55  input_type,
56  "output.type",
57  output_type,
58  });
59  out->Print(GetRubyComments(method, true).c_str());
60  out->Print(method_vars, "rpc :$mth.name$, $input.type$, $output.type$\n");
61  out->Print(GetRubyComments(method, false).c_str());
62 }
63 
64 // Prints out the service using the ruby gRPC DSL.
65 void PrintService(const ServiceDescriptor* service, Printer* out) {
66  if (service->method_count() == 0) {
67  return;
68  }
69 
70  // Begin the service module
71  std::map<std::string, std::string> module_vars = ListToDict({
72  "module.name",
73  Modularize(service->name()),
74  });
75  out->Print(module_vars, "module $module.name$\n");
76  out->Indent();
77 
78  out->Print(GetRubyComments(service, true).c_str());
79  out->Print("class Service\n");
80 
81  // Write the indented class body.
82  out->Indent();
83  out->Print("\n");
84  out->Print("include ::GRPC::GenericService\n");
85  out->Print("\n");
86  out->Print("self.marshal_class_method = :encode\n");
87  out->Print("self.unmarshal_class_method = :decode\n");
88  std::map<std::string, std::string> pkg_vars =
89  ListToDict({"service_full_name", service->full_name()});
90  out->Print(pkg_vars, "self.service_name = '$service_full_name$'\n");
91  out->Print("\n");
92  for (int i = 0; i < service->method_count(); ++i) {
93  PrintMethod(service->method(i), out);
94  }
95  out->Outdent();
96 
97  out->Print("end\n");
98  out->Print("\n");
99  out->Print("Stub = Service.rpc_stub_class\n");
100 
101  // End the service module
102  out->Outdent();
103  out->Print("end\n");
104  out->Print(GetRubyComments(service, false).c_str());
105 }
106 
107 } // namespace
108 
109 // The following functions are copied directly from the source for the protoc
110 // ruby generator
111 // to ensure compatibility (with the exception of int and string type changes).
112 // See
113 // https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/compiler/ruby/ruby_generator.cc#L250
114 // TODO: keep up to date with protoc code generation, though this behavior isn't
115 // expected to change
116 bool IsLower(char ch) { return ch >= 'a' && ch <= 'z'; }
117 
118 char ToUpper(char ch) { return IsLower(ch) ? (ch - 'a' + 'A') : ch; }
119 
120 // Package names in protobuf are snake_case by convention, but Ruby module
121 // names must be PascalCased.
122 //
123 // foo_bar_baz -> FooBarBaz
125  bool next_upper = true;
127  result.reserve(name.size());
128 
129  for (std::string::size_type i = 0; i < name.size(); i++) {
130  if (name[i] == '_') {
131  next_upper = true;
132  } else {
133  if (next_upper) {
134  result.push_back(ToUpper(name[i]));
135  } else {
136  result.push_back(name[i]);
137  }
138  next_upper = false;
139  }
140  }
141 
142  return result;
143 }
144 // end copying of protoc generator for ruby code
145 
148  {
149  // Scope the output stream so it closes and finalizes output to the string.
150 
151  StringOutputStream output_stream(&output);
152  Printer out(&output_stream, '$');
153 
154  // Don't write out any output if there no services, to avoid empty service
155  // files being generated for proto files that don't declare any.
156  if (file->service_count() == 0) {
157  return output;
158  }
159 
160  std::string package_name = RubyPackage(file);
161 
162  // Write out a file header.
163  std::map<std::string, std::string> header_comment_vars = ListToDict({
164  "file.name",
165  file->name(),
166  "file.package",
167  package_name,
168  });
169  out.Print("# Generated by the protocol buffer compiler. DO NOT EDIT!\n");
170  out.Print(header_comment_vars,
171  "# Source: $file.name$ for package '$file.package$'\n");
172 
173  std::string leading_comments = GetRubyComments(file, true);
174  if (!leading_comments.empty()) {
175  out.Print("# Original file comments:\n");
176  out.PrintRaw(leading_comments.c_str());
177  }
178 
179  out.Print("\n");
180  out.Print("require 'grpc'\n");
181  // Write out require statemment to import the separately generated file
182  // that defines the messages used by the service. This is generated by the
183  // main ruby plugin.
184  std::map<std::string, std::string> dep_vars = ListToDict({
185  "dep.name",
187  });
188  out.Print(dep_vars, "require '$dep.name$'\n");
189 
190  // Write out services within the modules
191  out.Print("\n");
192  std::vector<std::string> modules = Split(package_name, '.');
193  for (size_t i = 0; i < modules.size(); ++i) {
194  std::map<std::string, std::string> module_vars = ListToDict({
195  "module.name",
197  });
198  out.Print(module_vars, "module $module.name$\n");
199  out.Indent();
200  }
201  for (int i = 0; i < file->service_count(); ++i) {
202  auto service = file->service(i);
203  PrintService(service, &out);
204  }
205  for (size_t i = 0; i < modules.size(); ++i) {
206  out.Outdent();
207  out.Print("end\n");
208  }
209 
210  out.Print(GetRubyComments(file, false).c_str());
211  }
212  return output;
213 }
214 
215 } // namespace grpc_ruby_generator
grpc_ruby_generator::Modularize
std::string Modularize(std::string s)
Definition: ruby_generator_string-inl.h:85
grpc_ruby_generator::RubyPackage
std::string RubyPackage(const grpc::protobuf::FileDescriptor *file)
Definition: ruby_generator_string-inl.h:104
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
grpc::protobuf::FileDescriptor
GRPC_CUSTOM_FILEDESCRIPTOR FileDescriptor
Definition: include/grpcpp/impl/codegen/config_protobuf.h:85
grpc_ruby_generator::IsLower
bool IsLower(char ch)
Definition: src/compiler/ruby_generator.cc:116
grpc_ruby_generator::PackageToModule
std::string PackageToModule(const std::string &name)
Definition: src/compiler/ruby_generator.cc:124
grpc_ruby_generator::MessagesRequireName
std::string MessagesRequireName(const grpc::protobuf::FileDescriptor *file)
Definition: ruby_generator_helpers-inl.h:44
config.h
FileDescriptor
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:128
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
ruby_generator_string-inl.h
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
grpc::protobuf::io::Printer
GRPC_CUSTOM_PRINTER Printer
Definition: src/compiler/config.h:54
setup.name
name
Definition: setup.py:542
grpc_ruby_generator::ListToDict
std::map< std::string, std::string > ListToDict(const initializer_list< std::string > &values)
Definition: ruby_generator_map-inl.h:38
map
zval * map
Definition: php/ext/google/protobuf/encode_decode.c:480
ruby_generator_helpers-inl.h
grpc::protobuf::io::StringOutputStream
GRPC_CUSTOM_STRINGOUTPUTSTREAM StringOutputStream
Definition: src/compiler/config.h:56
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
grpc::protobuf::MethodDescriptor
GRPC_CUSTOM_METHODDESCRIPTOR MethodDescriptor
Definition: include/grpcpp/impl/codegen/config_protobuf.h:87
grpc_ruby_generator::GetServices
std::string GetServices(const FileDescriptor *file)
Definition: src/compiler/ruby_generator.cc:146
regress.modules
modules
Definition: regress/regress.py:20
ruby_generator_map-inl.h
grpc_ruby_generator::ToUpper
char ToUpper(char ch)
Definition: src/compiler/ruby_generator.cc:118
grpc_ruby_generator
Definition: src/compiler/ruby_generator.cc:38
grpc_ruby_generator::GetRubyComments
std::string GetRubyComments(const DescriptorType *desc, bool leading)
Definition: ruby_generator_helpers-inl.h:52
ruby_generator.h
ch
char ch
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3621
service
__attribute__((deprecated("Please use GRPCProtoMethod."))) @interface ProtoMethod NSString * service
Definition: ProtoMethod.h:25
file::name
char * name
Definition: bloaty/third_party/zlib/examples/gzappend.c:176
method
NSString * method
Definition: ProtoMethod.h:28
grpc_ruby_generator::Split
std::vector< std::string > & Split(const std::string &s, char delim, std::vector< std::string > *elems)
Definition: ruby_generator_string-inl.h:34
grpc::protobuf::ServiceDescriptor
GRPC_CUSTOM_SERVICEDESCRIPTOR ServiceDescriptor
Definition: include/grpcpp/impl/codegen/config_protobuf.h:88
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
grpc_ruby_generator::RubyTypeOf
std::string RubyTypeOf(const grpc::protobuf::Descriptor *descriptor)
Definition: ruby_generator_string-inl.h:119


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:13