blob: 6128b816a4da2b1a75c67200b36874c93a5082d9 [file] [log] [blame]
nnobleebebb7e2014-12-10 16:31:01 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
nnobleebebb7e2014-12-10 16:31:01 -08004 * 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// Generates cpp gRPC service interface out of Protobuf IDL.
35//
36
37#include <memory>
yang-g9efec8e2016-04-14 14:34:55 -070038#include <sstream>
Nicolas Nobled446eb82015-03-12 17:22:33 -070039
40#include "src/compiler/config.h"
Nicolas "Pixel" Noble36f53232015-01-16 06:39:58 +010041
nnobleebebb7e2014-12-10 16:31:01 -080042#include "src/compiler/cpp_generator.h"
43#include "src/compiler/cpp_generator_helpers.h"
yang-g9efec8e2016-04-14 14:34:55 -070044#include "src/compiler/generator_helpers.h"
45
46grpc::string GenerateComments(const std::vector<grpc::string> &in) {
47 std::ostringstream oss;
48 for (const grpc::string &elem : in) {
49 if (elem.empty()) {
50 oss << "//\n";
51 } else if (elem[0] == ' ') {
52 oss << "//" << elem << "\n";
53 } else {
54 oss << "// " << elem << "\n";
55 }
56 }
57 return oss.str();
58}
59
60// Get leading or trailing comments in a string. Comment lines start with "// ".
61// Leading detached comments are put in in front of leading comments.
62template <typename DescriptorType>
63grpc::string GetComments(const DescriptorType *desc, bool leading) {
64 std::vector<grpc::string> out;
65 if (leading) {
66 grpc_generator::GetComment(
67 desc, grpc_generator::COMMENTTYPE_LEADING_DETACHED, &out);
68 std::vector<grpc::string> leading;
69 grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_LEADING,
70 &leading);
71 out.insert(out.end(), leading.begin(), leading.end());
72 } else {
73 grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_TRAILING,
74 &out);
75 }
76 return GenerateComments(out);
77}
nnobleebebb7e2014-12-10 16:31:01 -080078
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -080079class ProtoBufMethod : public grpc_cpp_generator::Method {
80 public:
81 ProtoBufMethod(const grpc::protobuf::MethodDescriptor *method)
82 : method_(method) {}
83
84 grpc::string name() const { return method_->name(); }
85
86 grpc::string input_type_name() const {
87 return grpc_cpp_generator::ClassName(method_->input_type(), true);
88 }
89 grpc::string output_type_name() const {
90 return grpc_cpp_generator::ClassName(method_->output_type(), true);
91 }
92
93 bool NoStreaming() const {
94 return !method_->client_streaming() && !method_->server_streaming();
95 }
96
97 bool ClientOnlyStreaming() const {
98 return method_->client_streaming() && !method_->server_streaming();
99 }
100
101 bool ServerOnlyStreaming() const {
102 return !method_->client_streaming() && method_->server_streaming();
103 }
104
105 bool BidiStreaming() const {
106 return method_->client_streaming() && method_->server_streaming();
107 }
108
yang-g9efec8e2016-04-14 14:34:55 -0700109 grpc::string GetLeadingComments() const { return GetComments(method_, true); }
110
111 grpc::string GetTrailingComments() const {
112 return GetComments(method_, false);
113 }
114
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800115 private:
116 const grpc::protobuf::MethodDescriptor *method_;
117};
118
119class ProtoBufService : public grpc_cpp_generator::Service {
120 public:
121 ProtoBufService(const grpc::protobuf::ServiceDescriptor *service)
122 : service_(service) {}
123
124 grpc::string name() const { return service_->name(); }
125
126 int method_count() const { return service_->method_count(); };
127 std::unique_ptr<const grpc_cpp_generator::Method> method(int i) const {
128 return std::unique_ptr<const grpc_cpp_generator::Method>(
129 new ProtoBufMethod(service_->method(i)));
130 };
131
yang-g9efec8e2016-04-14 14:34:55 -0700132 grpc::string GetLeadingComments() const {
133 return GetComments(service_, true);
134 }
135
136 grpc::string GetTrailingComments() const {
137 return GetComments(service_, false);
138 }
139
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800140 private:
141 const grpc::protobuf::ServiceDescriptor *service_;
142};
143
144class ProtoBufPrinter : public grpc_cpp_generator::Printer {
145 public:
146 ProtoBufPrinter(grpc::string *str)
147 : output_stream_(str), printer_(&output_stream_, '$') {}
148
149 void Print(const std::map<grpc::string, grpc::string> &vars,
150 const char *string_template) {
151 printer_.Print(vars, string_template);
152 }
153
154 void Print(const char *string) { printer_.Print(string); }
155 void Indent() { printer_.Indent(); }
156 void Outdent() { printer_.Outdent(); }
157
158 private:
159 grpc::protobuf::io::StringOutputStream output_stream_;
160 grpc::protobuf::io::Printer printer_;
161};
162
163class ProtoBufFile : public grpc_cpp_generator::File {
164 public:
165 ProtoBufFile(const grpc::protobuf::FileDescriptor *file) : file_(file) {}
166
167 grpc::string filename() const { return file_->name(); }
168 grpc::string filename_without_ext() const {
169 return grpc_generator::StripProto(filename());
170 }
171
172 grpc::string package() const { return file_->package(); }
173 std::vector<grpc::string> package_parts() const {
174 return grpc_generator::tokenize(package(), ".");
175 }
176
177 int service_count() const { return file_->service_count(); };
178 std::unique_ptr<const grpc_cpp_generator::Service> service(int i) const {
179 return std::unique_ptr<const grpc_cpp_generator::Service> (
180 new ProtoBufService(file_->service(i)));
181 }
182
183 std::unique_ptr<grpc_cpp_generator::Printer> CreatePrinter(grpc::string *str) const {
184 return std::unique_ptr<grpc_cpp_generator::Printer>(
185 new ProtoBufPrinter(str));
186 }
187
yang-g9efec8e2016-04-14 14:34:55 -0700188 grpc::string GetLeadingComments() const { return GetComments(file_, true); }
189
190 grpc::string GetTrailingComments() const { return GetComments(file_, false); }
191
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800192 private:
193 const grpc::protobuf::FileDescriptor *file_;
194};
195
Nicolas Nobled446eb82015-03-12 17:22:33 -0700196class CppGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
nnobleebebb7e2014-12-10 16:31:01 -0800197 public:
198 CppGrpcGenerator() {}
199 virtual ~CppGrpcGenerator() {}
200
Nicolas Nobled446eb82015-03-12 17:22:33 -0700201 virtual bool Generate(const grpc::protobuf::FileDescriptor *file,
202 const grpc::string &parameter,
203 grpc::protobuf::compiler::GeneratorContext *context,
204 grpc::string *error) const {
nnobleebebb7e2014-12-10 16:31:01 -0800205 if (file->options().cc_generic_services()) {
Craig Tillerb5dcec52015-01-13 11:13:42 -0800206 *error =
207 "cpp grpc proto compiler plugin does not work with generic "
208 "services. To generate cpp grpc APIs, please set \""
209 "cc_generic_service = false\".";
nnobleebebb7e2014-12-10 16:31:01 -0800210 return false;
211 }
212
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100213 grpc_cpp_generator::Parameters generator_parameters;
Nicolas "Pixel" Noble931bdce2016-01-12 03:08:11 +0100214 generator_parameters.use_system_headers = true;
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100215
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800216 ProtoBufFile pbfile(file);
217
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100218 if (!parameter.empty()) {
219 std::vector<grpc::string> parameters_list =
220 grpc_generator::tokenize(parameter, ",");
Nicolas "Pixel" Noble1e475142015-03-25 23:35:09 +0100221 for (auto parameter_string = parameters_list.begin();
222 parameter_string != parameters_list.end();
223 parameter_string++) {
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100224 std::vector<grpc::string> param =
Nicolas "Pixel" Noble1e475142015-03-25 23:35:09 +0100225 grpc_generator::tokenize(*parameter_string, "=");
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100226 if (param[0] == "services_namespace") {
227 generator_parameters.services_namespace = param[1];
Nicolas "Pixel" Noble931bdce2016-01-12 03:08:11 +0100228 } else if (param[0] == "use_system_headers") {
229 if (param[1] == "true") {
230 generator_parameters.use_system_headers = true;
231 } else if (param[1] == "false") {
232 generator_parameters.use_system_headers = false;
233 } else {
234 *error = grpc::string("Invalid parameter: ") + *parameter_string;
235 return false;
236 }
237 } else if (param[0] == "grpc_search_path") {
238 generator_parameters.grpc_search_path = param[1];
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100239 } else {
Nicolas "Pixel" Noble1e475142015-03-25 23:35:09 +0100240 *error = grpc::string("Unknown parameter: ") + *parameter_string;
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100241 return false;
242 }
243 }
244 }
245
Nicolas Nobled446eb82015-03-12 17:22:33 -0700246 grpc::string file_name = grpc_generator::StripProto(file->name());
nnobleebebb7e2014-12-10 16:31:01 -0800247
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200248 grpc::string header_code =
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800249 grpc_cpp_generator::GetHeaderPrologue(&pbfile, generator_parameters) +
250 grpc_cpp_generator::GetHeaderIncludes(&pbfile, generator_parameters) +
251 grpc_cpp_generator::GetHeaderServices(&pbfile, generator_parameters) +
252 grpc_cpp_generator::GetHeaderEpilogue(&pbfile, generator_parameters);
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200253 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> header_output(
254 context->Open(file_name + ".grpc.pb.h"));
255 grpc::protobuf::io::CodedOutputStream header_coded_out(
256 header_output.get());
257 header_coded_out.WriteRaw(header_code.data(), header_code.size());
258
259 grpc::string source_code =
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800260 grpc_cpp_generator::GetSourcePrologue(&pbfile, generator_parameters) +
261 grpc_cpp_generator::GetSourceIncludes(&pbfile, generator_parameters) +
262 grpc_cpp_generator::GetSourceServices(&pbfile, generator_parameters) +
263 grpc_cpp_generator::GetSourceEpilogue(&pbfile, generator_parameters);
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200264 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> source_output(
265 context->Open(file_name + ".grpc.pb.cc"));
266 grpc::protobuf::io::CodedOutputStream source_coded_out(
267 source_output.get());
268 source_coded_out.WriteRaw(source_code.data(), source_code.size());
nnobleebebb7e2014-12-10 16:31:01 -0800269
270 return true;
271 }
272
273 private:
274 // Insert the given code into the given file at the given insertion point.
Nicolas Nobled446eb82015-03-12 17:22:33 -0700275 void Insert(grpc::protobuf::compiler::GeneratorContext *context,
276 const grpc::string &filename, const grpc::string &insertion_point,
277 const grpc::string &code) const {
278 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output(
nnobleebebb7e2014-12-10 16:31:01 -0800279 context->OpenForInsert(filename, insertion_point));
Nicolas Nobled446eb82015-03-12 17:22:33 -0700280 grpc::protobuf::io::CodedOutputStream coded_out(output.get());
nnobleebebb7e2014-12-10 16:31:01 -0800281 coded_out.WriteRaw(code.data(), code.size());
282 }
283};
284
Craig Tillerecd49342015-01-18 14:36:47 -0800285int main(int argc, char *argv[]) {
nnobleebebb7e2014-12-10 16:31:01 -0800286 CppGrpcGenerator generator;
Nicolas Nobled446eb82015-03-12 17:22:33 -0700287 return grpc::protobuf::compiler::PluginMain(argc, argv, &generator);
Craig Tiller190d3602015-02-18 09:23:38 -0800288}