Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1479)

Side by Side Diff: third_party/grpc/src/compiler/ruby_generator.cc

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 *
3 * Copyright 2015, 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>
35 #include <map>
36 #include <vector>
37
38 #include "src/compiler/config.h"
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
44 using grpc::protobuf::FileDescriptor;
45 using grpc::protobuf::ServiceDescriptor;
46 using grpc::protobuf::MethodDescriptor;
47 using grpc::protobuf::io::Printer;
48 using grpc::protobuf::io::StringOutputStream;
49 using std::map;
50 using std::vector;
51
52 namespace grpc_ruby_generator {
53 namespace {
54
55 // Prints out the method using the ruby gRPC DSL.
56 void PrintMethod(const MethodDescriptor *method, const grpc::string &package,
57 Printer *out) {
58 grpc::string input_type = RubyTypeOf(method->input_type()->name(), package);
59 if (method->client_streaming()) {
60 input_type = "stream(" + input_type + ")";
61 }
62 grpc::string output_type = RubyTypeOf(method->output_type()->name(), package);
63 if (method->server_streaming()) {
64 output_type = "stream(" + output_type + ")";
65 }
66 std::map<grpc::string, grpc::string> method_vars =
67 ListToDict({"mth.name", method->name(), "input.type", input_type,
68 "output.type", output_type, });
69 out->Print(method_vars, "rpc :$mth.name$, $input.type$, $output.type$\n");
70 }
71
72 // Prints out the service using the ruby gRPC DSL.
73 void PrintService(const ServiceDescriptor *service, const grpc::string &package,
74 Printer *out) {
75 if (service->method_count() == 0) {
76 return;
77 }
78
79 // Begin the service module
80 std::map<grpc::string, grpc::string> module_vars =
81 ListToDict({"module.name", CapitalizeFirst(service->name()), });
82 out->Print(module_vars, "module $module.name$\n");
83 out->Indent();
84
85 // TODO(temiola): add documentation
86 grpc::string doc = "TODO: add proto service documentation here";
87 std::map<grpc::string, grpc::string> template_vars =
88 ListToDict({"Documentation", doc, });
89 out->Print("\n");
90 out->Print(template_vars, "# $Documentation$\n");
91 out->Print("class Service\n");
92
93 // Write the indented class body.
94 out->Indent();
95 out->Print("\n");
96 out->Print("include GRPC::GenericService\n");
97 out->Print("\n");
98 out->Print("self.marshal_class_method = :encode\n");
99 out->Print("self.unmarshal_class_method = :decode\n");
100 std::map<grpc::string, grpc::string> pkg_vars =
101 ListToDict({"service.name", service->name(), "pkg.name", package, });
102 out->Print(pkg_vars, "self.service_name = '$pkg.name$.$service.name$'\n");
103 out->Print("\n");
104 for (int i = 0; i < service->method_count(); ++i) {
105 PrintMethod(service->method(i), package, out);
106 }
107 out->Outdent();
108
109 out->Print("end\n");
110 out->Print("\n");
111 out->Print("Stub = Service.rpc_stub_class\n");
112
113 // End the service module
114 out->Outdent();
115 out->Print("end\n");
116 }
117
118 } // namespace
119
120 grpc::string GetServices(const FileDescriptor *file) {
121 grpc::string output;
122 {
123 // Scope the output stream so it closes and finalizes output to the string.
124
125 StringOutputStream output_stream(&output);
126 Printer out(&output_stream, '$');
127
128 // Don't write out any output if there no services, to avoid empty service
129 // files being generated for proto files that don't declare any.
130 if (file->service_count() == 0) {
131 return output;
132 }
133
134 // Write out a file header.
135 std::map<grpc::string, grpc::string> header_comment_vars = ListToDict(
136 {"file.name", file->name(), "file.package", file->package(), });
137 out.Print("# Generated by the protocol buffer compiler. DO NOT EDIT!\n");
138 out.Print(header_comment_vars,
139 "# Source: $file.name$ for package '$file.package$'\n");
140
141 out.Print("\n");
142 out.Print("require 'grpc'\n");
143 // Write out require statemment to import the separately generated file
144 // that defines the messages used by the service. This is generated by the
145 // main ruby plugin.
146 std::map<grpc::string, grpc::string> dep_vars =
147 ListToDict({"dep.name", MessagesRequireName(file), });
148 out.Print(dep_vars, "require '$dep.name$'\n");
149
150 // Write out services within the modules
151 out.Print("\n");
152 std::vector<grpc::string> modules = Split(file->package(), '.');
153 for (size_t i = 0; i < modules.size(); ++i) {
154 std::map<grpc::string, grpc::string> module_vars =
155 ListToDict({"module.name", CapitalizeFirst(modules[i]), });
156 out.Print(module_vars, "module $module.name$\n");
157 out.Indent();
158 }
159 for (int i = 0; i < file->service_count(); ++i) {
160 auto service = file->service(i);
161 PrintService(service, file->package(), &out);
162 }
163 for (size_t i = 0; i < modules.size(); ++i) {
164 out.Outdent();
165 out.Print("end\n");
166 }
167 }
168 return output;
169 }
170
171 } // namespace grpc_ruby_generator
OLDNEW
« no previous file with comments | « third_party/grpc/src/compiler/ruby_generator.h ('k') | third_party/grpc/src/compiler/ruby_generator_helpers-inl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698