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
19 #include "src/compiler/ruby_generator.h"
20
21 #include <cctype>
22 #include <map>
23 #include <vector>
24
25 #include "src/compiler/config.h"
26 #include "src/compiler/ruby_generator_helpers-inl.h"
27 #include "src/compiler/ruby_generator_map-inl.h"
28 #include "src/compiler/ruby_generator_string-inl.h"
29
30 using grpc::protobuf::FileDescriptor;
31 using grpc::protobuf::MethodDescriptor;
32 using grpc::protobuf::ServiceDescriptor;
33 using grpc::protobuf::io::Printer;
34 using grpc::protobuf::io::StringOutputStream;
35 using std::map;
36 using std::vector;
37
38 namespace grpc_ruby_generator {
39 namespace {
40
41 // Prints out the method using the ruby gRPC DSL.
PrintMethod(const MethodDescriptor * method,Printer * out)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.
PrintService(const ServiceDescriptor * service,Printer * out)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/63895855d7b1298bee97591cbafced49f23902da/src/google/protobuf/compiler/ruby/ruby_generator.cc#L312
114 // TODO: keep up to date with protoc code generation, though this behavior isn't
115 // expected to change
116
117 // Locale-agnostic utility functions.
IsLower(char ch)118 bool IsLower(char ch) { return ch >= 'a' && ch <= 'z'; }
119
IsUpper(char ch)120 bool IsUpper(char ch) { return ch >= 'A' && ch <= 'Z'; }
121
IsAlpha(char ch)122 bool IsAlpha(char ch) { return IsLower(ch) || IsUpper(ch); }
123
UpperChar(char ch)124 char UpperChar(char ch) { return IsLower(ch) ? (ch - 'a' + 'A') : ch; }
125
126 // Package names in protobuf are snake_case by convention, but Ruby module
127 // names must be PascalCased.
128 //
129 // foo_bar_baz -> FooBarBaz
PackageToModule(const std::string & name)130 std::string PackageToModule(const std::string& name) {
131 bool next_upper = true;
132 std::string result;
133 result.reserve(name.size());
134
135 for (std::string::size_type i = 0; i < name.size(); i++) {
136 if (name[i] == '_') {
137 next_upper = true;
138 } else {
139 if (next_upper) {
140 result.push_back(UpperChar(name[i]));
141 } else {
142 result.push_back(name[i]);
143 }
144 next_upper = false;
145 }
146 }
147
148 return result;
149 }
150
151 // Class and enum names in protobuf should be PascalCased by convention, but
152 // since there is nothing enforcing this we need to ensure that they are valid
153 // Ruby constants. That mainly means making sure that the first character is
154 // an upper-case letter.
RubifyConstant(const std::string & name)155 std::string RubifyConstant(const std::string& name) {
156 std::string ret = name;
157 if (!ret.empty()) {
158 if (IsLower(ret[0])) {
159 // If it starts with a lowercase letter, capitalize it.
160 ret[0] = UpperChar(ret[0]);
161 } else if (!IsAlpha(ret[0])) {
162 // Otherwise (e.g. if it begins with an underscore), we need to come up
163 // with some prefix that starts with a capital letter. We could be smarter
164 // here, e.g. try to strip leading underscores, but this may cause other
165 // problems if the user really intended the name. So let's just prepend a
166 // well-known suffix.
167 ret = "PB_" + ret;
168 }
169 }
170
171 return ret;
172 }
173 // end copying of protoc generator for ruby code
174
GetServices(const FileDescriptor * file)175 std::string GetServices(const FileDescriptor* file) {
176 std::string output;
177 {
178 // Scope the output stream so it closes and finalizes output to the string.
179
180 StringOutputStream output_stream(&output);
181 Printer out(&output_stream, '$');
182
183 // Don't write out any output if there no services, to avoid empty service
184 // files being generated for proto files that don't declare any.
185 if (file->service_count() == 0) {
186 return output;
187 }
188
189 std::string package_name = RubyPackage(file);
190
191 // Write out a file header.
192 std::map<std::string, std::string> header_comment_vars = ListToDict({
193 "file.name",
194 file->name(),
195 "file.package",
196 package_name,
197 });
198 out.Print("# Generated by the protocol buffer compiler. DO NOT EDIT!\n");
199 out.Print(header_comment_vars,
200 "# Source: $file.name$ for package '$file.package$'\n");
201
202 std::string leading_comments = GetRubyComments(file, true);
203 if (!leading_comments.empty()) {
204 out.Print("# Original file comments:\n");
205 out.PrintRaw(leading_comments.c_str());
206 }
207
208 out.Print("\n");
209 out.Print("require 'grpc'\n");
210 // Write out require statemment to import the separately generated file
211 // that defines the messages used by the service. This is generated by the
212 // main ruby plugin.
213 std::map<std::string, std::string> dep_vars = ListToDict({
214 "dep.name",
215 MessagesRequireName(file),
216 });
217 out.Print(dep_vars, "require '$dep.name$'\n");
218
219 // Write out services within the modules
220 out.Print("\n");
221 std::vector<std::string> modules = Split(package_name, '.');
222 for (size_t i = 0; i < modules.size(); ++i) {
223 std::map<std::string, std::string> module_vars = ListToDict({
224 "module.name",
225 PackageToModule(modules[i]),
226 });
227 out.Print(module_vars, "module $module.name$\n");
228 out.Indent();
229 }
230 for (int i = 0; i < file->service_count(); ++i) {
231 auto service = file->service(i);
232 PrintService(service, &out);
233 }
234 for (size_t i = 0; i < modules.size(); ++i) {
235 out.Outdent();
236 out.Print("end\n");
237 }
238
239 out.Print(GetRubyComments(file, false).c_str());
240 }
241 return output;
242 }
243
244 } // namespace grpc_ruby_generator
245