blob: fc0296cd282edb3ea8e5c81cbc191a282ef89560 [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
murgatroid99210f3c42016-05-20 13:24:59 -070046using grpc_cpp_generator::GetCppComments;
nnobleebebb7e2014-12-10 16:31:01 -080047
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -080048class ProtoBufMethod : public grpc_cpp_generator::Method {
49 public:
50 ProtoBufMethod(const grpc::protobuf::MethodDescriptor *method)
51 : method_(method) {}
52
53 grpc::string name() const { return method_->name(); }
54
55 grpc::string input_type_name() const {
56 return grpc_cpp_generator::ClassName(method_->input_type(), true);
57 }
58 grpc::string output_type_name() const {
59 return grpc_cpp_generator::ClassName(method_->output_type(), true);
60 }
61
62 bool NoStreaming() const {
63 return !method_->client_streaming() && !method_->server_streaming();
64 }
65
66 bool ClientOnlyStreaming() const {
67 return method_->client_streaming() && !method_->server_streaming();
68 }
69
70 bool ServerOnlyStreaming() const {
71 return !method_->client_streaming() && method_->server_streaming();
72 }
73
74 bool BidiStreaming() const {
75 return method_->client_streaming() && method_->server_streaming();
76 }
77
yang-g57d1e082016-04-25 15:31:39 -070078 grpc::string GetLeadingComments() const {
79 return GetCppComments(method_, true);
80 }
yang-g9efec8e2016-04-14 14:34:55 -070081
82 grpc::string GetTrailingComments() const {
yang-g57d1e082016-04-25 15:31:39 -070083 return GetCppComments(method_, false);
yang-g9efec8e2016-04-14 14:34:55 -070084 }
85
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -080086 private:
87 const grpc::protobuf::MethodDescriptor *method_;
88};
89
90class ProtoBufService : public grpc_cpp_generator::Service {
91 public:
92 ProtoBufService(const grpc::protobuf::ServiceDescriptor *service)
93 : service_(service) {}
94
95 grpc::string name() const { return service_->name(); }
96
97 int method_count() const { return service_->method_count(); };
98 std::unique_ptr<const grpc_cpp_generator::Method> method(int i) const {
99 return std::unique_ptr<const grpc_cpp_generator::Method>(
100 new ProtoBufMethod(service_->method(i)));
101 };
102
yang-g9efec8e2016-04-14 14:34:55 -0700103 grpc::string GetLeadingComments() const {
yang-g57d1e082016-04-25 15:31:39 -0700104 return GetCppComments(service_, true);
yang-g9efec8e2016-04-14 14:34:55 -0700105 }
106
107 grpc::string GetTrailingComments() const {
yang-g57d1e082016-04-25 15:31:39 -0700108 return GetCppComments(service_, false);
yang-g9efec8e2016-04-14 14:34:55 -0700109 }
110
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800111 private:
112 const grpc::protobuf::ServiceDescriptor *service_;
113};
114
115class ProtoBufPrinter : public grpc_cpp_generator::Printer {
116 public:
117 ProtoBufPrinter(grpc::string *str)
118 : output_stream_(str), printer_(&output_stream_, '$') {}
119
120 void Print(const std::map<grpc::string, grpc::string> &vars,
121 const char *string_template) {
122 printer_.Print(vars, string_template);
123 }
124
125 void Print(const char *string) { printer_.Print(string); }
126 void Indent() { printer_.Indent(); }
127 void Outdent() { printer_.Outdent(); }
128
129 private:
130 grpc::protobuf::io::StringOutputStream output_stream_;
131 grpc::protobuf::io::Printer printer_;
132};
133
134class ProtoBufFile : public grpc_cpp_generator::File {
135 public:
136 ProtoBufFile(const grpc::protobuf::FileDescriptor *file) : file_(file) {}
137
138 grpc::string filename() const { return file_->name(); }
139 grpc::string filename_without_ext() const {
140 return grpc_generator::StripProto(filename());
141 }
142
Wouter van Oortmerssenb695b9b2016-04-13 18:18:08 -0700143 grpc::string message_header_ext() const { return ".pb.h"; }
144 grpc::string service_header_ext() const { return ".grpc.pb.h"; }
145
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800146 grpc::string package() const { return file_->package(); }
147 std::vector<grpc::string> package_parts() const {
148 return grpc_generator::tokenize(package(), ".");
149 }
150
Wouter van Oortmerssenb695b9b2016-04-13 18:18:08 -0700151 grpc::string additional_headers() const { return ""; }
152
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800153 int service_count() const { return file_->service_count(); };
154 std::unique_ptr<const grpc_cpp_generator::Service> service(int i) const {
155 return std::unique_ptr<const grpc_cpp_generator::Service> (
156 new ProtoBufService(file_->service(i)));
157 }
158
159 std::unique_ptr<grpc_cpp_generator::Printer> CreatePrinter(grpc::string *str) const {
160 return std::unique_ptr<grpc_cpp_generator::Printer>(
161 new ProtoBufPrinter(str));
162 }
163
yang-g57d1e082016-04-25 15:31:39 -0700164 grpc::string GetLeadingComments() const {
165 return GetCppComments(file_, true);
166 }
yang-g9efec8e2016-04-14 14:34:55 -0700167
yang-g57d1e082016-04-25 15:31:39 -0700168 grpc::string GetTrailingComments() const {
169 return GetCppComments(file_, false);
170 }
yang-g9efec8e2016-04-14 14:34:55 -0700171
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800172 private:
173 const grpc::protobuf::FileDescriptor *file_;
174};
175
Nicolas Nobled446eb82015-03-12 17:22:33 -0700176class CppGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
nnobleebebb7e2014-12-10 16:31:01 -0800177 public:
178 CppGrpcGenerator() {}
179 virtual ~CppGrpcGenerator() {}
180
Nicolas Nobled446eb82015-03-12 17:22:33 -0700181 virtual bool Generate(const grpc::protobuf::FileDescriptor *file,
182 const grpc::string &parameter,
183 grpc::protobuf::compiler::GeneratorContext *context,
184 grpc::string *error) const {
nnobleebebb7e2014-12-10 16:31:01 -0800185 if (file->options().cc_generic_services()) {
Craig Tillerb5dcec52015-01-13 11:13:42 -0800186 *error =
187 "cpp grpc proto compiler plugin does not work with generic "
188 "services. To generate cpp grpc APIs, please set \""
189 "cc_generic_service = false\".";
nnobleebebb7e2014-12-10 16:31:01 -0800190 return false;
191 }
192
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100193 grpc_cpp_generator::Parameters generator_parameters;
Nicolas "Pixel" Noble931bdce2016-01-12 03:08:11 +0100194 generator_parameters.use_system_headers = true;
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100195
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800196 ProtoBufFile pbfile(file);
197
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100198 if (!parameter.empty()) {
199 std::vector<grpc::string> parameters_list =
200 grpc_generator::tokenize(parameter, ",");
Nicolas "Pixel" Noble1e475142015-03-25 23:35:09 +0100201 for (auto parameter_string = parameters_list.begin();
202 parameter_string != parameters_list.end();
203 parameter_string++) {
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100204 std::vector<grpc::string> param =
Nicolas "Pixel" Noble1e475142015-03-25 23:35:09 +0100205 grpc_generator::tokenize(*parameter_string, "=");
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100206 if (param[0] == "services_namespace") {
207 generator_parameters.services_namespace = param[1];
Nicolas "Pixel" Noble931bdce2016-01-12 03:08:11 +0100208 } else if (param[0] == "use_system_headers") {
209 if (param[1] == "true") {
210 generator_parameters.use_system_headers = true;
211 } else if (param[1] == "false") {
212 generator_parameters.use_system_headers = false;
213 } else {
214 *error = grpc::string("Invalid parameter: ") + *parameter_string;
215 return false;
216 }
217 } else if (param[0] == "grpc_search_path") {
218 generator_parameters.grpc_search_path = param[1];
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100219 } else {
Nicolas "Pixel" Noble1e475142015-03-25 23:35:09 +0100220 *error = grpc::string("Unknown parameter: ") + *parameter_string;
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100221 return false;
222 }
223 }
224 }
225
Nicolas Nobled446eb82015-03-12 17:22:33 -0700226 grpc::string file_name = grpc_generator::StripProto(file->name());
nnobleebebb7e2014-12-10 16:31:01 -0800227
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200228 grpc::string header_code =
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800229 grpc_cpp_generator::GetHeaderPrologue(&pbfile, generator_parameters) +
230 grpc_cpp_generator::GetHeaderIncludes(&pbfile, generator_parameters) +
231 grpc_cpp_generator::GetHeaderServices(&pbfile, generator_parameters) +
232 grpc_cpp_generator::GetHeaderEpilogue(&pbfile, generator_parameters);
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200233 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> header_output(
234 context->Open(file_name + ".grpc.pb.h"));
235 grpc::protobuf::io::CodedOutputStream header_coded_out(
236 header_output.get());
237 header_coded_out.WriteRaw(header_code.data(), header_code.size());
238
239 grpc::string source_code =
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800240 grpc_cpp_generator::GetSourcePrologue(&pbfile, generator_parameters) +
241 grpc_cpp_generator::GetSourceIncludes(&pbfile, generator_parameters) +
242 grpc_cpp_generator::GetSourceServices(&pbfile, generator_parameters) +
243 grpc_cpp_generator::GetSourceEpilogue(&pbfile, generator_parameters);
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200244 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> source_output(
245 context->Open(file_name + ".grpc.pb.cc"));
246 grpc::protobuf::io::CodedOutputStream source_coded_out(
247 source_output.get());
248 source_coded_out.WriteRaw(source_code.data(), source_code.size());
nnobleebebb7e2014-12-10 16:31:01 -0800249
250 return true;
251 }
252
253 private:
254 // Insert the given code into the given file at the given insertion point.
Nicolas Nobled446eb82015-03-12 17:22:33 -0700255 void Insert(grpc::protobuf::compiler::GeneratorContext *context,
256 const grpc::string &filename, const grpc::string &insertion_point,
257 const grpc::string &code) const {
258 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output(
nnobleebebb7e2014-12-10 16:31:01 -0800259 context->OpenForInsert(filename, insertion_point));
Nicolas Nobled446eb82015-03-12 17:22:33 -0700260 grpc::protobuf::io::CodedOutputStream coded_out(output.get());
nnobleebebb7e2014-12-10 16:31:01 -0800261 coded_out.WriteRaw(code.data(), code.size());
262 }
263};
264
Craig Tillerecd49342015-01-18 14:36:47 -0800265int main(int argc, char *argv[]) {
nnobleebebb7e2014-12-10 16:31:01 -0800266 CppGrpcGenerator generator;
Nicolas Nobled446eb82015-03-12 17:22:33 -0700267 return grpc::protobuf::compiler::PluginMain(argc, argv, &generator);
Craig Tiller190d3602015-02-18 09:23:38 -0800268}