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