blob: f1a1d809396102f1fdbf8caaa851f98b9249854b [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
yang-g25df28e2016-04-20 16:36:12 -070046using grpc_generator::GetComments;
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-g9efec8e2016-04-14 14:34:55 -070078 grpc::string GetLeadingComments() const { return GetComments(method_, true); }
79
80 grpc::string GetTrailingComments() const {
81 return GetComments(method_, false);
82 }
83
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -080084 private:
85 const grpc::protobuf::MethodDescriptor *method_;
86};
87
88class ProtoBufService : public grpc_cpp_generator::Service {
89 public:
90 ProtoBufService(const grpc::protobuf::ServiceDescriptor *service)
91 : service_(service) {}
92
93 grpc::string name() const { return service_->name(); }
94
95 int method_count() const { return service_->method_count(); };
96 std::unique_ptr<const grpc_cpp_generator::Method> method(int i) const {
97 return std::unique_ptr<const grpc_cpp_generator::Method>(
98 new ProtoBufMethod(service_->method(i)));
99 };
100
yang-g9efec8e2016-04-14 14:34:55 -0700101 grpc::string GetLeadingComments() const {
102 return GetComments(service_, true);
103 }
104
105 grpc::string GetTrailingComments() const {
106 return GetComments(service_, false);
107 }
108
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800109 private:
110 const grpc::protobuf::ServiceDescriptor *service_;
111};
112
113class ProtoBufPrinter : public grpc_cpp_generator::Printer {
114 public:
115 ProtoBufPrinter(grpc::string *str)
116 : output_stream_(str), printer_(&output_stream_, '$') {}
117
118 void Print(const std::map<grpc::string, grpc::string> &vars,
119 const char *string_template) {
120 printer_.Print(vars, string_template);
121 }
122
123 void Print(const char *string) { printer_.Print(string); }
124 void Indent() { printer_.Indent(); }
125 void Outdent() { printer_.Outdent(); }
126
127 private:
128 grpc::protobuf::io::StringOutputStream output_stream_;
129 grpc::protobuf::io::Printer printer_;
130};
131
132class ProtoBufFile : public grpc_cpp_generator::File {
133 public:
134 ProtoBufFile(const grpc::protobuf::FileDescriptor *file) : file_(file) {}
135
136 grpc::string filename() const { return file_->name(); }
137 grpc::string filename_without_ext() const {
138 return grpc_generator::StripProto(filename());
139 }
140
141 grpc::string package() const { return file_->package(); }
142 std::vector<grpc::string> package_parts() const {
143 return grpc_generator::tokenize(package(), ".");
144 }
145
146 int service_count() const { return file_->service_count(); };
147 std::unique_ptr<const grpc_cpp_generator::Service> service(int i) const {
148 return std::unique_ptr<const grpc_cpp_generator::Service> (
149 new ProtoBufService(file_->service(i)));
150 }
151
152 std::unique_ptr<grpc_cpp_generator::Printer> CreatePrinter(grpc::string *str) const {
153 return std::unique_ptr<grpc_cpp_generator::Printer>(
154 new ProtoBufPrinter(str));
155 }
156
yang-g9efec8e2016-04-14 14:34:55 -0700157 grpc::string GetLeadingComments() const { return GetComments(file_, true); }
158
159 grpc::string GetTrailingComments() const { return GetComments(file_, false); }
160
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800161 private:
162 const grpc::protobuf::FileDescriptor *file_;
163};
164
Nicolas Nobled446eb82015-03-12 17:22:33 -0700165class CppGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
nnobleebebb7e2014-12-10 16:31:01 -0800166 public:
167 CppGrpcGenerator() {}
168 virtual ~CppGrpcGenerator() {}
169
Nicolas Nobled446eb82015-03-12 17:22:33 -0700170 virtual bool Generate(const grpc::protobuf::FileDescriptor *file,
171 const grpc::string &parameter,
172 grpc::protobuf::compiler::GeneratorContext *context,
173 grpc::string *error) const {
nnobleebebb7e2014-12-10 16:31:01 -0800174 if (file->options().cc_generic_services()) {
Craig Tillerb5dcec52015-01-13 11:13:42 -0800175 *error =
176 "cpp grpc proto compiler plugin does not work with generic "
177 "services. To generate cpp grpc APIs, please set \""
178 "cc_generic_service = false\".";
nnobleebebb7e2014-12-10 16:31:01 -0800179 return false;
180 }
181
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100182 grpc_cpp_generator::Parameters generator_parameters;
Nicolas "Pixel" Noble931bdce2016-01-12 03:08:11 +0100183 generator_parameters.use_system_headers = true;
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100184
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800185 ProtoBufFile pbfile(file);
186
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100187 if (!parameter.empty()) {
188 std::vector<grpc::string> parameters_list =
189 grpc_generator::tokenize(parameter, ",");
Nicolas "Pixel" Noble1e475142015-03-25 23:35:09 +0100190 for (auto parameter_string = parameters_list.begin();
191 parameter_string != parameters_list.end();
192 parameter_string++) {
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100193 std::vector<grpc::string> param =
Nicolas "Pixel" Noble1e475142015-03-25 23:35:09 +0100194 grpc_generator::tokenize(*parameter_string, "=");
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100195 if (param[0] == "services_namespace") {
196 generator_parameters.services_namespace = param[1];
Nicolas "Pixel" Noble931bdce2016-01-12 03:08:11 +0100197 } else if (param[0] == "use_system_headers") {
198 if (param[1] == "true") {
199 generator_parameters.use_system_headers = true;
200 } else if (param[1] == "false") {
201 generator_parameters.use_system_headers = false;
202 } else {
203 *error = grpc::string("Invalid parameter: ") + *parameter_string;
204 return false;
205 }
206 } else if (param[0] == "grpc_search_path") {
207 generator_parameters.grpc_search_path = param[1];
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100208 } else {
Nicolas "Pixel" Noble1e475142015-03-25 23:35:09 +0100209 *error = grpc::string("Unknown parameter: ") + *parameter_string;
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100210 return false;
211 }
212 }
213 }
214
Nicolas Nobled446eb82015-03-12 17:22:33 -0700215 grpc::string file_name = grpc_generator::StripProto(file->name());
nnobleebebb7e2014-12-10 16:31:01 -0800216
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200217 grpc::string header_code =
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800218 grpc_cpp_generator::GetHeaderPrologue(&pbfile, generator_parameters) +
219 grpc_cpp_generator::GetHeaderIncludes(&pbfile, generator_parameters) +
220 grpc_cpp_generator::GetHeaderServices(&pbfile, generator_parameters) +
221 grpc_cpp_generator::GetHeaderEpilogue(&pbfile, generator_parameters);
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200222 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> header_output(
223 context->Open(file_name + ".grpc.pb.h"));
224 grpc::protobuf::io::CodedOutputStream header_coded_out(
225 header_output.get());
226 header_coded_out.WriteRaw(header_code.data(), header_code.size());
227
228 grpc::string source_code =
Wouter van Oortmerssenaf09a732016-03-09 17:03:21 -0800229 grpc_cpp_generator::GetSourcePrologue(&pbfile, generator_parameters) +
230 grpc_cpp_generator::GetSourceIncludes(&pbfile, generator_parameters) +
231 grpc_cpp_generator::GetSourceServices(&pbfile, generator_parameters) +
232 grpc_cpp_generator::GetSourceEpilogue(&pbfile, generator_parameters);
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200233 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> source_output(
234 context->Open(file_name + ".grpc.pb.cc"));
235 grpc::protobuf::io::CodedOutputStream source_coded_out(
236 source_output.get());
237 source_coded_out.WriteRaw(source_code.data(), source_code.size());
nnobleebebb7e2014-12-10 16:31:01 -0800238
239 return true;
240 }
241
242 private:
243 // Insert the given code into the given file at the given insertion point.
Nicolas Nobled446eb82015-03-12 17:22:33 -0700244 void Insert(grpc::protobuf::compiler::GeneratorContext *context,
245 const grpc::string &filename, const grpc::string &insertion_point,
246 const grpc::string &code) const {
247 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output(
nnobleebebb7e2014-12-10 16:31:01 -0800248 context->OpenForInsert(filename, insertion_point));
Nicolas Nobled446eb82015-03-12 17:22:33 -0700249 grpc::protobuf::io::CodedOutputStream coded_out(output.get());
nnobleebebb7e2014-12-10 16:31:01 -0800250 coded_out.WriteRaw(code.data(), code.size());
251 }
252};
253
Craig Tillerecd49342015-01-18 14:36:47 -0800254int main(int argc, char *argv[]) {
nnobleebebb7e2014-12-10 16:31:01 -0800255 CppGrpcGenerator generator;
Nicolas Nobled446eb82015-03-12 17:22:33 -0700256 return grpc::protobuf::compiler::PluginMain(argc, argv, &generator);
Craig Tiller190d3602015-02-18 09:23:38 -0800257}