blob: 393f8f3b5936e1b0f8d1b1f1fac4e149b4344623 [file] [log] [blame]
nnobleebebb7e2014-12-10 16:31:01 -08001/*
2 *
3 * Copyright 2014, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include <cctype>
Nicolas "Pixel" Noble36f53232015-01-16 06:39:58 +010035#include <string>
nnobleebebb7e2014-12-10 16:31:01 -080036#include <map>
37#include <vector>
38
39#include "src/compiler/ruby_generator.h"
40#include "src/compiler/ruby_generator_helpers-inl.h"
41#include "src/compiler/ruby_generator_map-inl.h"
42#include "src/compiler/ruby_generator_string-inl.h"
43#include <google/protobuf/io/printer.h>
44#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
45#include <google/protobuf/descriptor.pb.h>
46#include <google/protobuf/descriptor.h>
47
48using google::protobuf::FileDescriptor;
49using google::protobuf::ServiceDescriptor;
50using google::protobuf::MethodDescriptor;
51using google::protobuf::io::Printer;
52using google::protobuf::io::StringOutputStream;
53using std::map;
54using std::vector;
55
56namespace grpc_ruby_generator {
57namespace {
58
59// Prints out the method using the ruby gRPC DSL.
Nicolas Noblef5c5d802015-01-15 16:36:13 -080060void PrintMethod(const MethodDescriptor* method, const std::string& package,
nnobleebebb7e2014-12-10 16:31:01 -080061 Printer* out) {
Nicolas Noblef5c5d802015-01-15 16:36:13 -080062 std::string input_type = RubyTypeOf(method->input_type()->name(), package);
nnoblec78b3402014-12-11 16:06:57 -080063 if (method->client_streaming()) {
nnobleebebb7e2014-12-10 16:31:01 -080064 input_type = "stream(" + input_type + ")";
65 }
Nicolas Noblef5c5d802015-01-15 16:36:13 -080066 std::string output_type = RubyTypeOf(method->output_type()->name(), package);
nnoblec78b3402014-12-11 16:06:57 -080067 if (method->server_streaming()) {
nnobleebebb7e2014-12-10 16:31:01 -080068 output_type = "stream(" + output_type + ")";
69 }
Nicolas Noblef5c5d802015-01-15 16:36:13 -080070 std::map<std::string, std::string> method_vars = ListToDict({
Craig Tillerb5dcec52015-01-13 11:13:42 -080071 "mth.name", method->name(), "input.type", input_type, "output.type",
72 output_type,
nnobleebebb7e2014-12-10 16:31:01 -080073 });
74 out->Print(method_vars, "rpc :$mth.name$, $input.type$, $output.type$\n");
75}
76
77// Prints out the service using the ruby gRPC DSL.
Nicolas Noblef5c5d802015-01-15 16:36:13 -080078void PrintService(const ServiceDescriptor* service, const std::string& package,
nnobleebebb7e2014-12-10 16:31:01 -080079 Printer* out) {
80 if (service->method_count() == 0) {
81 return;
82 }
83
84 // Begin the service module
Nicolas Noblef5c5d802015-01-15 16:36:13 -080085 std::map<std::string, std::string> module_vars = ListToDict({
temiola54535552015-01-06 17:50:59 -080086 "module.name", CapitalizeFirst(service->name()),
nnobleebebb7e2014-12-10 16:31:01 -080087 });
88 out->Print(module_vars, "module $module.name$\n");
89 out->Indent();
90
91 // TODO(temiola): add documentation
Nicolas Noblef5c5d802015-01-15 16:36:13 -080092 std::string doc = "TODO: add proto service documentation here";
93 std::map<std::string, std::string> template_vars = ListToDict({
nnobleebebb7e2014-12-10 16:31:01 -080094 "Documentation", doc,
95 });
96 out->Print("\n");
97 out->Print(template_vars, "# $Documentation$\n");
98 out->Print("class Service\n");
99
100 // Write the indented class body.
101 out->Indent();
102 out->Print("\n");
temiola3d726cf2015-01-06 18:25:06 -0800103 out->Print("include GRPC::GenericService\n");
nnobleebebb7e2014-12-10 16:31:01 -0800104 out->Print("\n");
105 out->Print("self.marshal_class_method = :encode\n");
106 out->Print("self.unmarshal_class_method = :decode\n");
Nicolas Noblef5c5d802015-01-15 16:36:13 -0800107 std::map<std::string, std::string> pkg_vars = ListToDict({
Craig Tillerb5dcec52015-01-13 11:13:42 -0800108 "service.name", service->name(), "pkg.name", package,
temiolad6fd84a2015-01-08 09:59:08 -0800109 });
110 out->Print(pkg_vars, "self.service_name = '$pkg.name$.$service.name$'\n");
nnobleebebb7e2014-12-10 16:31:01 -0800111 out->Print("\n");
112 for (int i = 0; i < service->method_count(); ++i) {
113 PrintMethod(service->method(i), package, out);
114 }
115 out->Outdent();
116
117 out->Print("end\n");
118 out->Print("\n");
119 out->Print("Stub = Service.rpc_stub_class\n");
120
121 // End the service module
122 out->Outdent();
123 out->Print("end\n");
124}
125
126} // namespace
127
Nicolas Noblef5c5d802015-01-15 16:36:13 -0800128std::string GetServices(const FileDescriptor* file) {
129 std::string output;
nnobleebebb7e2014-12-10 16:31:01 -0800130 StringOutputStream output_stream(&output);
131 Printer out(&output_stream, '$');
132
temiolae5206aa2015-01-07 11:43:05 -0800133 // Don't write out any output if there no services, to avoid empty service
134 // files being generated for proto files that don't declare any.
135 if (file->service_count() == 0) {
136 return output;
137 }
138
139 // Write out a file header.
Nicolas Noblef5c5d802015-01-15 16:36:13 -0800140 std::map<std::string, std::string> header_comment_vars = ListToDict({
Craig Tillerb5dcec52015-01-13 11:13:42 -0800141 "file.name", file->name(), "file.package", file->package(),
142 });
temiola54535552015-01-06 17:50:59 -0800143 out.Print("# Generated by the protocol buffer compiler. DO NOT EDIT!\n");
nnobleebebb7e2014-12-10 16:31:01 -0800144 out.Print(header_comment_vars,
temiola54535552015-01-06 17:50:59 -0800145 "# Source: $file.name$ for package '$file.package$'\n");
nnobleebebb7e2014-12-10 16:31:01 -0800146
147 out.Print("\n");
148 out.Print("require 'grpc'\n");
149 // Write out require statemment to import the separately generated file
150 // that defines the messages used by the service. This is generated by the
151 // main ruby plugin.
Nicolas Noblef5c5d802015-01-15 16:36:13 -0800152 std::map<std::string, std::string> dep_vars = ListToDict({
nnobleebebb7e2014-12-10 16:31:01 -0800153 "dep.name", MessagesRequireName(file),
154 });
155 out.Print(dep_vars, "require '$dep.name$'\n");
156
157 // Write out services within the modules
158 out.Print("\n");
Nicolas Noblef5c5d802015-01-15 16:36:13 -0800159 std::vector<std::string> modules = Split(file->package(), '.');
nnobleebebb7e2014-12-10 16:31:01 -0800160 for (size_t i = 0; i < modules.size(); ++i) {
Nicolas Noblef5c5d802015-01-15 16:36:13 -0800161 std::map<std::string, std::string> module_vars = ListToDict({
temiola54535552015-01-06 17:50:59 -0800162 "module.name", CapitalizeFirst(modules[i]),
nnobleebebb7e2014-12-10 16:31:01 -0800163 });
164 out.Print(module_vars, "module $module.name$\n");
165 out.Indent();
166 }
167 for (int i = 0; i < file->service_count(); ++i) {
168 auto service = file->service(i);
169 PrintService(service, file->package(), &out);
170 }
171 for (size_t i = 0; i < modules.size(); ++i) {
172 out.Outdent();
173 out.Print("end\n");
174 }
175
176 return output;
177}
178
179} // namespace grpc_ruby_generator