| 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. |
| 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 |
| 124 |
std::string PackageToModule(const std::string& name) { |
| 125 |
bool next_upper = true; |
| 126 |
std::string result; |
| 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 |
|
| 146 |
std::string GetServices(const FileDescriptor* file) { |
| 147 |
std::string output; |
| 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", |
| 186 |
MessagesRequireName(file), |
| 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", |
| 196 |
PackageToModule(modules[i]), |
| 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 |