blob: 7bf05483415abb1ce8a84e39779e2c8e8398c8da [file] [log] [blame]
Jan Tattermusch2d924952015-05-06 10:23:17 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Jan Tattermusch2d924952015-05-06 10:23:17 -07004 * 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>
yang-gc3ee1d52015-08-28 11:33:52 -070036#include <sstream>
Jan Tattermusch2d924952015-05-06 10:23:17 -070037#include <vector>
38
Jan Tattermusch741e64c2015-08-03 08:08:56 -070039#include "src/compiler/csharp_generator.h"
Jan Tattermusch2d924952015-05-06 10:23:17 -070040#include "src/compiler/config.h"
Jan Tattermuschb5897bf2015-05-07 15:45:37 -070041#include "src/compiler/csharp_generator_helpers.h"
Jan Tattermusch2d924952015-05-06 10:23:17 -070042#include "src/compiler/csharp_generator.h"
43
Jan Tattermusch741e64c2015-08-03 08:08:56 -070044
45using google::protobuf::compiler::csharp::GetFileNamespace;
46using google::protobuf::compiler::csharp::GetClassName;
Jan Tattermuschda717f42016-01-20 13:12:35 -080047using google::protobuf::compiler::csharp::GetReflectionClassName;
Jan Tattermusch2d924952015-05-06 10:23:17 -070048using grpc::protobuf::FileDescriptor;
49using grpc::protobuf::Descriptor;
50using grpc::protobuf::ServiceDescriptor;
51using grpc::protobuf::MethodDescriptor;
52using grpc::protobuf::io::Printer;
53using grpc::protobuf::io::StringOutputStream;
Jan Tattermuschb5897bf2015-05-07 15:45:37 -070054using grpc_generator::MethodType;
55using grpc_generator::GetMethodType;
56using grpc_generator::METHODTYPE_NO_STREAMING;
57using grpc_generator::METHODTYPE_CLIENT_STREAMING;
58using grpc_generator::METHODTYPE_SERVER_STREAMING;
59using grpc_generator::METHODTYPE_BIDI_STREAMING;
Jan Tattermusch41f9f332015-05-20 08:52:00 -070060using grpc_generator::StringReplace;
Jan Tattermusch2d924952015-05-06 10:23:17 -070061using std::map;
62using std::vector;
63
Jan Tattermusch741e64c2015-08-03 08:08:56 -070064
Jan Tattermusch2d924952015-05-06 10:23:17 -070065namespace grpc_csharp_generator {
66namespace {
67
Jan Tattermusch59c20ed2016-04-28 09:12:13 -070068// This function is a massaged version of
69// https://github.com/google/protobuf/blob/master/src/google/protobuf/compiler/csharp/csharp_doc_comment.cc
70// Currently, we cannot easily reuse the functionality as
71// google/protobuf/compiler/csharp/csharp_doc_comment.h is not a public header.
72// TODO(jtattermusch): reuse the functionality from google/protobuf.
73void GenerateDocCommentBodyImpl(grpc::protobuf::io::Printer* printer, grpc::protobuf::SourceLocation location) {
74 grpc::string comments = location.leading_comments.empty() ?
75 location.trailing_comments : location.leading_comments;
76 if (comments.empty()) {
77 return;
78 }
79 // XML escaping... no need for apostrophes etc as the whole text is going to be a child
80 // node of a summary element, not part of an attribute.
81 comments = grpc_generator::StringReplace(comments, "&", "&amp;", true);
82 comments = grpc_generator::StringReplace(comments, "<", "&lt;", true);
83
84 std::vector<grpc::string> lines;
85 grpc_generator::Split(comments, '\n', &lines);
86 // TODO: We really should work out which part to put in the summary and which to put in the remarks...
87 // but that needs to be part of a bigger effort to understand the markdown better anyway.
88 printer->Print("/// <summary>\n");
89 bool last_was_empty = false;
90 // We squash multiple blank lines down to one, and remove any trailing blank lines. We need
91 // to preserve the blank lines themselves, as this is relevant in the markdown.
92 // Note that we can't remove leading or trailing whitespace as *that's* relevant in markdown too.
93 // (We don't skip "just whitespace" lines, either.)
94 for (std::vector<grpc::string>::iterator it = lines.begin(); it != lines.end(); ++it) {
95 grpc::string line = *it;
96 if (line.empty()) {
97 last_was_empty = true;
98 } else {
99 if (last_was_empty) {
100 printer->Print("///\n");
101 }
102 last_was_empty = false;
103 printer->Print("/// $line$\n", "line", *it);
104 }
105 }
106 printer->Print("/// </summary>\n");
107}
108
109template <typename DescriptorType>
110void GenerateDocCommentBody(
111 grpc::protobuf::io::Printer* printer, const DescriptorType* descriptor) {
112 grpc::protobuf::SourceLocation location;
113 if (descriptor->GetSourceLocation(&location)) {
114 GenerateDocCommentBodyImpl(printer, location);
115 }
116}
117
Jan Tattermusch2d924952015-05-06 10:23:17 -0700118std::string GetServiceClassName(const ServiceDescriptor* service) {
119 return service->name();
120}
121
122std::string GetClientInterfaceName(const ServiceDescriptor* service) {
123 return "I" + service->name() + "Client";
124}
125
126std::string GetClientClassName(const ServiceDescriptor* service) {
127 return service->name() + "Client";
128}
129
130std::string GetServerInterfaceName(const ServiceDescriptor* service) {
131 return "I" + service->name();
132}
133
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700134std::string GetServerClassName(const ServiceDescriptor* service) {
135 return service->name() + "Base";
136}
137
Jan Tattermusch2d924952015-05-06 10:23:17 -0700138std::string GetCSharpMethodType(MethodType method_type) {
139 switch (method_type) {
140 case METHODTYPE_NO_STREAMING:
141 return "MethodType.Unary";
142 case METHODTYPE_CLIENT_STREAMING:
143 return "MethodType.ClientStreaming";
144 case METHODTYPE_SERVER_STREAMING:
145 return "MethodType.ServerStreaming";
146 case METHODTYPE_BIDI_STREAMING:
147 return "MethodType.DuplexStreaming";
148 }
149 GOOGLE_LOG(FATAL)<< "Can't get here.";
150 return "";
151}
152
153std::string GetServiceNameFieldName() {
154 return "__ServiceName";
155}
156
157std::string GetMarshallerFieldName(const Descriptor *message) {
158 return "__Marshaller_" + message->name();
159}
160
161std::string GetMethodFieldName(const MethodDescriptor *method) {
162 return "__Method_" + method->name();
163}
164
Jan Tattermusch8cd4ae92016-03-21 18:46:32 -0700165std::string GetMethodRequestParamMaybe(const MethodDescriptor *method,
166 bool invocation_param=false) {
Jan Tattermusch2d924952015-05-06 10:23:17 -0700167 if (method->client_streaming()) {
168 return "";
169 }
Jan Tattermusch8cd4ae92016-03-21 18:46:32 -0700170 if (invocation_param) {
171 return "request, ";
172 }
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700173 return GetClassName(method->input_type()) + " request, ";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700174}
175
Jan Tattermusch4e0f73c2016-04-25 16:11:03 -0700176std::string GetAccessLevel(bool internal_access) {
177 return internal_access ? "internal" : "public";
178}
179
Jan Tattermusch2d924952015-05-06 10:23:17 -0700180std::string GetMethodReturnTypeClient(const MethodDescriptor *method) {
181 switch (GetMethodType(method)) {
182 case METHODTYPE_NO_STREAMING:
Jan Tattermusch5269d162015-07-21 11:56:42 -0700183 return "AsyncUnaryCall<" + GetClassName(method->output_type()) + ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700184 case METHODTYPE_CLIENT_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700185 return "AsyncClientStreamingCall<" + GetClassName(method->input_type())
186 + ", " + GetClassName(method->output_type()) + ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700187 case METHODTYPE_SERVER_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700188 return "AsyncServerStreamingCall<" + GetClassName(method->output_type())
Jan Tattermusch2d924952015-05-06 10:23:17 -0700189 + ">";
190 case METHODTYPE_BIDI_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700191 return "AsyncDuplexStreamingCall<" + GetClassName(method->input_type())
192 + ", " + GetClassName(method->output_type()) + ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700193 }
194 GOOGLE_LOG(FATAL)<< "Can't get here.";
195 return "";
196}
197
198std::string GetMethodRequestParamServer(const MethodDescriptor *method) {
199 switch (GetMethodType(method)) {
200 case METHODTYPE_NO_STREAMING:
201 case METHODTYPE_SERVER_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700202 return GetClassName(method->input_type()) + " request";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700203 case METHODTYPE_CLIENT_STREAMING:
204 case METHODTYPE_BIDI_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700205 return "IAsyncStreamReader<" + GetClassName(method->input_type())
Jan Tattermusch2d924952015-05-06 10:23:17 -0700206 + "> requestStream";
207 }
208 GOOGLE_LOG(FATAL)<< "Can't get here.";
209 return "";
210}
211
212std::string GetMethodReturnTypeServer(const MethodDescriptor *method) {
213 switch (GetMethodType(method)) {
214 case METHODTYPE_NO_STREAMING:
215 case METHODTYPE_CLIENT_STREAMING:
Kirill Katsnelson0ddf46f2016-04-19 18:49:31 -0700216 return "global::System.Threading.Tasks.Task<" + GetClassName(method->output_type()) + ">";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700217 case METHODTYPE_SERVER_STREAMING:
218 case METHODTYPE_BIDI_STREAMING:
Kirill Katsnelson0ddf46f2016-04-19 18:49:31 -0700219 return "global::System.Threading.Tasks.Task";
Jan Tattermusch2d924952015-05-06 10:23:17 -0700220 }
221 GOOGLE_LOG(FATAL)<< "Can't get here.";
222 return "";
223}
224
225std::string GetMethodResponseStreamMaybe(const MethodDescriptor *method) {
226 switch (GetMethodType(method)) {
227 case METHODTYPE_NO_STREAMING:
228 case METHODTYPE_CLIENT_STREAMING:
229 return "";
230 case METHODTYPE_SERVER_STREAMING:
231 case METHODTYPE_BIDI_STREAMING:
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700232 return ", IServerStreamWriter<" + GetClassName(method->output_type())
Jan Tattermusch2d924952015-05-06 10:23:17 -0700233 + "> responseStream";
234 }
235 GOOGLE_LOG(FATAL)<< "Can't get here.";
236 return "";
237}
238
239// Gets vector of all messages used as input or output types.
240std::vector<const Descriptor*> GetUsedMessages(
241 const ServiceDescriptor *service) {
242 std::set<const Descriptor*> descriptor_set;
243 std::vector<const Descriptor*> result; // vector is to maintain stable ordering
244 for (int i = 0; i < service->method_count(); i++) {
245 const MethodDescriptor *method = service->method(i);
246 if (descriptor_set.find(method->input_type()) == descriptor_set.end()) {
247 descriptor_set.insert(method->input_type());
248 result.push_back(method->input_type());
249 }
250 if (descriptor_set.find(method->output_type()) == descriptor_set.end()) {
251 descriptor_set.insert(method->output_type());
252 result.push_back(method->output_type());
253 }
254 }
255 return result;
256}
257
258void GenerateMarshallerFields(Printer* out, const ServiceDescriptor *service) {
259 std::vector<const Descriptor*> used_messages = GetUsedMessages(service);
260 for (size_t i = 0; i < used_messages.size(); i++) {
261 const Descriptor *message = used_messages[i];
262 out->Print(
Jan Tattermuschad75dd12015-08-03 09:43:07 -0700263 "static readonly Marshaller<$type$> $fieldname$ = Marshallers.Create((arg) => global::Google.Protobuf.MessageExtensions.ToByteArray(arg), $type$.Parser.ParseFrom);\n",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700264 "fieldname", GetMarshallerFieldName(message), "type",
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700265 GetClassName(message));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700266 }
267 out->Print("\n");
268}
269
270void GenerateStaticMethodField(Printer* out, const MethodDescriptor *method) {
271 out->Print(
272 "static readonly Method<$request$, $response$> $fieldname$ = new Method<$request$, $response$>(\n",
273 "fieldname", GetMethodFieldName(method), "request",
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700274 GetClassName(method->input_type()), "response",
275 GetClassName(method->output_type()));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700276 out->Indent();
277 out->Indent();
278 out->Print("$methodtype$,\n", "methodtype",
279 GetCSharpMethodType(GetMethodType(method)));
Jan Tattermuscha9ddd022015-08-05 03:04:58 -0700280 out->Print("$servicenamefield$,\n", "servicenamefield",
281 GetServiceNameFieldName());
Jan Tattermusch2d924952015-05-06 10:23:17 -0700282 out->Print("\"$methodname$\",\n", "methodname", method->name());
283 out->Print("$requestmarshaller$,\n", "requestmarshaller",
284 GetMarshallerFieldName(method->input_type()));
285 out->Print("$responsemarshaller$);\n", "responsemarshaller",
286 GetMarshallerFieldName(method->output_type()));
287 out->Print("\n");
288 out->Outdent();
289 out->Outdent();
290}
291
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700292void GenerateServiceDescriptorProperty(Printer* out, const ServiceDescriptor *service) {
yang-gc3ee1d52015-08-28 11:33:52 -0700293 std::ostringstream index;
294 index << service->index();
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700295 out->Print("/// <summary>Service descriptor</summary>\n");
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700296 out->Print("public static global::Google.Protobuf.Reflection.ServiceDescriptor Descriptor\n");
297 out->Print("{\n");
298 out->Print(" get { return $umbrella$.Descriptor.Services[$index$]; }\n",
Jan Tattermuschda717f42016-01-20 13:12:35 -0800299 "umbrella", GetReflectionClassName(service->file()), "index",
yang-gc3ee1d52015-08-28 11:33:52 -0700300 index.str());
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700301 out->Print("}\n");
302 out->Print("\n");
303}
304
Jan Tattermusch2d924952015-05-06 10:23:17 -0700305void GenerateClientInterface(Printer* out, const ServiceDescriptor *service) {
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700306 out->Print("/// <summary>Client for $servicename$</summary>\n",
307 "servicename", GetServiceClassName(service));
Jan Tattermusch5a4e1e32016-03-25 16:51:55 -0700308 out->Print("[System.Obsolete(\"Client side interfaced will be removed "
309 "in the next release. Use client class directly.\")]\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700310 out->Print("public interface $name$\n", "name",
311 GetClientInterfaceName(service));
312 out->Print("{\n");
313 out->Indent();
314 for (int i = 0; i < service->method_count(); i++) {
315 const MethodDescriptor *method = service->method(i);
316 MethodType method_type = GetMethodType(method);
317
318 if (method_type == METHODTYPE_NO_STREAMING) {
319 // unary calls have an extra synchronous stub method
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700320 GenerateDocCommentBody(out, method);
Jan Tattermusch2d924952015-05-06 10:23:17 -0700321 out->Print(
Jan Tattermusch74529562015-07-23 14:04:51 -0700322 "$response$ $methodname$($request$ request, Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken));\n",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700323 "methodname", method->name(), "request",
Jan Tattermusch41f9f332015-05-20 08:52:00 -0700324 GetClassName(method->input_type()), "response",
325 GetClassName(method->output_type()));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700326
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700327 // overload taking CallOptions as a param
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700328 GenerateDocCommentBody(out, method);
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700329 out->Print(
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700330 "$response$ $methodname$($request$ request, CallOptions options);\n",
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700331 "methodname", method->name(), "request",
332 GetClassName(method->input_type()), "response",
333 GetClassName(method->output_type()));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700334 }
335
336 std::string method_name = method->name();
337 if (method_type == METHODTYPE_NO_STREAMING) {
338 method_name += "Async"; // prevent name clash with synchronous method.
339 }
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700340 GenerateDocCommentBody(out, method);
Jan Tattermusch2d924952015-05-06 10:23:17 -0700341 out->Print(
Jan Tattermusch74529562015-07-23 14:04:51 -0700342 "$returntype$ $methodname$($request_maybe$Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken));\n",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700343 "methodname", method_name, "request_maybe",
344 GetMethodRequestParamMaybe(method), "returntype",
345 GetMethodReturnTypeClient(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700346
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700347 // overload taking CallOptions as a param
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700348 GenerateDocCommentBody(out, method);
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700349 out->Print(
Jan Tattermusche5e57ad2015-08-05 14:54:08 -0700350 "$returntype$ $methodname$($request_maybe$CallOptions options);\n",
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700351 "methodname", method_name, "request_maybe",
352 GetMethodRequestParamMaybe(method), "returntype",
353 GetMethodReturnTypeClient(method));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700354 }
355 out->Outdent();
356 out->Print("}\n");
357 out->Print("\n");
358}
359
360void GenerateServerInterface(Printer* out, const ServiceDescriptor *service) {
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700361 out->Print("/// <summary>Interface of server-side implementations of $servicename$</summary>\n",
362 "servicename", GetServiceClassName(service));
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700363 out->Print("[System.Obsolete(\"Service implementations should inherit"
364 " from the generated abstract base class instead.\")]\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700365 out->Print("public interface $name$\n", "name",
366 GetServerInterfaceName(service));
367 out->Print("{\n");
368 out->Indent();
369 for (int i = 0; i < service->method_count(); i++) {
370 const MethodDescriptor *method = service->method(i);
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700371 GenerateDocCommentBody(out, method);
Craig Tillerb256faa2015-07-23 11:28:16 -0700372 out->Print(
373 "$returntype$ $methodname$($request$$response_stream_maybe$, "
374 "ServerCallContext context);\n",
375 "methodname", method->name(), "returntype",
376 GetMethodReturnTypeServer(method), "request",
377 GetMethodRequestParamServer(method), "response_stream_maybe",
378 GetMethodResponseStreamMaybe(method));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700379 }
380 out->Outdent();
381 out->Print("}\n");
382 out->Print("\n");
383}
384
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700385void GenerateServerClass(Printer* out, const ServiceDescriptor *service) {
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700386 out->Print("/// <summary>Base class for server-side implementations of $servicename$</summary>\n",
387 "servicename", GetServiceClassName(service));
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700388 out->Print("public abstract class $name$\n", "name",
389 GetServerClassName(service));
390 out->Print("{\n");
391 out->Indent();
392 for (int i = 0; i < service->method_count(); i++) {
393 const MethodDescriptor *method = service->method(i);
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700394 GenerateDocCommentBody(out, method);
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700395 out->Print(
396 "public virtual $returntype$ $methodname$($request$$response_stream_maybe$, "
397 "ServerCallContext context)\n",
398 "methodname", method->name(), "returntype",
399 GetMethodReturnTypeServer(method), "request",
400 GetMethodRequestParamServer(method), "response_stream_maybe",
401 GetMethodResponseStreamMaybe(method));
402 out->Print("{\n");
403 out->Indent();
404 out->Print("throw new RpcException("
405 "new Status(StatusCode.Unimplemented, \"\"));\n");
406 out->Outdent();
407 out->Print("}\n\n");
408 }
409 out->Outdent();
410 out->Print("}\n");
411 out->Print("\n");
412}
413
Jan Tattermusch2d924952015-05-06 10:23:17 -0700414void GenerateClientStub(Printer* out, const ServiceDescriptor *service) {
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700415 out->Print("/// <summary>Client for $servicename$</summary>\n",
416 "servicename", GetServiceClassName(service));
Jan Tattermuschcc8a4b32016-04-25 13:40:00 -0700417 out->Print("#pragma warning disable 0618\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700418 out->Print(
Jan Tattermusch5a4e1e32016-03-25 16:51:55 -0700419 "public class $name$ : ClientBase<$name$>, $interface$\n",
420 "name", GetClientClassName(service),
421 "interface", GetClientInterfaceName(service));
Jan Tattermuschcc8a4b32016-04-25 13:40:00 -0700422 out->Print("#pragma warning restore 0618\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700423 out->Print("{\n");
424 out->Indent();
425
426 // constructors
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700427 out->Print("public $name$(Channel channel) : base(channel)\n",
428 "name", GetClientClassName(service));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700429 out->Print("{\n");
430 out->Print("}\n");
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700431 out->Print("public $name$(CallInvoker callInvoker) : base(callInvoker)\n",
432 "name", GetClientClassName(service));
433 out->Print("{\n");
Jan Tattermuschefb77842016-03-23 07:47:36 -0700434 out->Print("}\n");
Jan Tattermusch2f0a8372016-03-23 09:16:49 -0700435 out->Print("///<summary>Protected parameterless constructor to allow creation"
Jan Tattermuschefb77842016-03-23 07:47:36 -0700436 " of test doubles.</summary>\n");
437 out->Print("protected $name$() : base()\n",
438 "name", GetClientClassName(service));
439 out->Print("{\n");
Jan Tattermusch2f0a8372016-03-23 09:16:49 -0700440 out->Print("}\n");
441 out->Print("///<summary>Protected constructor to allow creation of configured"
442 " clients.</summary>\n");
443 out->Print("protected $name$(ClientBaseConfiguration configuration)"
444 " : base(configuration)\n",
445 "name", GetClientClassName(service));
446 out->Print("{\n");
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700447 out->Print("}\n\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700448
449 for (int i = 0; i < service->method_count(); i++) {
450 const MethodDescriptor *method = service->method(i);
451 MethodType method_type = GetMethodType(method);
452
453 if (method_type == METHODTYPE_NO_STREAMING) {
454 // unary calls have an extra synchronous stub method
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700455 GenerateDocCommentBody(out, method);
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700456 out->Print("public virtual $response$ $methodname$($request$ request, Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken))\n",
457 "methodname", method->name(), "request",
458 GetClassName(method->input_type()), "response",
459 GetClassName(method->output_type()));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700460 out->Print("{\n");
461 out->Indent();
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700462 out->Print("return $methodname$(request, new CallOptions(headers, deadline, cancellationToken));\n",
463 "methodname", method->name());
464 out->Outdent();
465 out->Print("}\n");
466
467 // overload taking CallOptions as a param
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700468 GenerateDocCommentBody(out, method);
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700469 out->Print("public virtual $response$ $methodname$($request$ request, CallOptions options)\n",
470 "methodname", method->name(), "request",
471 GetClassName(method->input_type()), "response",
472 GetClassName(method->output_type()));
473 out->Print("{\n");
474 out->Indent();
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700475 out->Print("return CallInvoker.BlockingUnaryCall($methodfield$, null, options, request);\n",
Jan Tattermusch641cb1b2015-08-05 03:51:46 -0700476 "methodfield", GetMethodFieldName(method));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700477 out->Outdent();
478 out->Print("}\n");
479 }
480
481 std::string method_name = method->name();
482 if (method_type == METHODTYPE_NO_STREAMING) {
483 method_name += "Async"; // prevent name clash with synchronous method.
484 }
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700485 GenerateDocCommentBody(out, method);
Jan Tattermusch2d924952015-05-06 10:23:17 -0700486 out->Print(
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700487 "public virtual $returntype$ $methodname$($request_maybe$Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken))\n",
488 "methodname", method_name, "request_maybe",
489 GetMethodRequestParamMaybe(method), "returntype",
490 GetMethodReturnTypeClient(method));
491 out->Print("{\n");
492 out->Indent();
493
494 out->Print("return $methodname$($request_maybe$new CallOptions(headers, deadline, cancellationToken));\n",
495 "methodname", method_name,
496 "request_maybe", GetMethodRequestParamMaybe(method, true));
497 out->Outdent();
498 out->Print("}\n");
499
500 // overload taking CallOptions as a param
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700501 GenerateDocCommentBody(out, method);
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700502 out->Print(
503 "public virtual $returntype$ $methodname$($request_maybe$CallOptions options)\n",
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700504 "methodname", method_name, "request_maybe",
505 GetMethodRequestParamMaybe(method), "returntype",
506 GetMethodReturnTypeClient(method));
507 out->Print("{\n");
508 out->Indent();
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700509 switch (GetMethodType(method)) {
510 case METHODTYPE_NO_STREAMING:
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700511 out->Print("return CallInvoker.AsyncUnaryCall($methodfield$, null, options, request);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700512 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700513 break;
514 case METHODTYPE_CLIENT_STREAMING:
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700515 out->Print("return CallInvoker.AsyncClientStreamingCall($methodfield$, null, options);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700516 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700517 break;
518 case METHODTYPE_SERVER_STREAMING:
519 out->Print(
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700520 "return CallInvoker.AsyncServerStreamingCall($methodfield$, null, options, request);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700521 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700522 break;
523 case METHODTYPE_BIDI_STREAMING:
Jan Tattermuschb455bcc2016-03-22 16:08:18 -0700524 out->Print("return CallInvoker.AsyncDuplexStreamingCall($methodfield$, null, options);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700525 "methodfield", GetMethodFieldName(method));
Jan Tattermusch5e10f182015-08-05 00:13:02 -0700526 break;
527 default:
528 GOOGLE_LOG(FATAL)<< "Can't get here.";
529 }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700530 out->Outdent();
531 out->Print("}\n");
532 }
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700533
534 // override NewInstance method
Jan Tattermusch2f0a8372016-03-23 09:16:49 -0700535 out->Print("protected override $name$ NewInstance(ClientBaseConfiguration configuration)\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700536 "name", GetClientClassName(service));
537 out->Print("{\n");
538 out->Indent();
Jan Tattermusch2f0a8372016-03-23 09:16:49 -0700539 out->Print("return new $name$(configuration);\n",
Jan Tattermusch055c2dd2016-03-22 14:43:56 -0700540 "name", GetClientClassName(service));
541 out->Outdent();
542 out->Print("}\n");
543
Jan Tattermusch2d924952015-05-06 10:23:17 -0700544 out->Outdent();
545 out->Print("}\n");
546 out->Print("\n");
547}
548
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700549void GenerateBindServiceMethod(Printer* out, const ServiceDescriptor *service,
550 bool use_server_class) {
Jan Tattermusch2d924952015-05-06 10:23:17 -0700551 out->Print(
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700552 "/// <summary>Creates service definition that can be registered with a server</summary>\n");
Jan Tattermuschcc8a4b32016-04-25 13:40:00 -0700553 out->Print("#pragma warning disable 0618\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700554 out->Print(
555 "public static ServerServiceDefinition BindService($interface$ serviceImpl)\n",
Jan Tattermusch10a002d2016-03-14 15:22:19 -0700556 "interface", use_server_class ? GetServerClassName(service) :
557 GetServerInterfaceName(service));
Jan Tattermuschcc8a4b32016-04-25 13:40:00 -0700558 out->Print("#pragma warning restore 0618\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700559 out->Print("{\n");
560 out->Indent();
561
Jan Tattermusch562cd052016-06-06 08:47:17 -0700562 out->Print("return ServerServiceDefinition.CreateBuilder()\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700563 out->Indent();
564 out->Indent();
565 for (int i = 0; i < service->method_count(); i++) {
566 const MethodDescriptor *method = service->method(i);
567 out->Print(".AddMethod($methodfield$, serviceImpl.$methodname$)",
568 "methodfield", GetMethodFieldName(method), "methodname",
569 method->name());
570 if (i == service->method_count() - 1) {
571 out->Print(".Build();");
572 }
573 out->Print("\n");
574 }
575 out->Outdent();
576 out->Outdent();
577
578 out->Outdent();
579 out->Print("}\n");
580 out->Print("\n");
581}
582
583void GenerateNewStubMethods(Printer* out, const ServiceDescriptor *service) {
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700584 out->Print("/// <summary>Creates a new client for $servicename$</summary>\n",
585 "servicename", GetServiceClassName(service));
Jan Tattermuschb5332812015-07-14 19:29:35 -0700586 out->Print("public static $classname$ NewClient(Channel channel)\n",
587 "classname", GetClientClassName(service));
Jan Tattermusch2d924952015-05-06 10:23:17 -0700588 out->Print("{\n");
589 out->Indent();
590 out->Print("return new $classname$(channel);\n", "classname",
591 GetClientClassName(service));
592 out->Outdent();
593 out->Print("}\n");
594 out->Print("\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700595}
596
Jan Tattermusch44f7c542016-04-25 14:50:27 -0700597void GenerateService(Printer* out, const ServiceDescriptor *service,
Jan Tattermusch4e0f73c2016-04-25 16:11:03 -0700598 bool generate_client, bool generate_server,
599 bool internal_access) {
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700600 GenerateDocCommentBody(out, service);
Jan Tattermusch4e0f73c2016-04-25 16:11:03 -0700601 out->Print("$access_level$ static class $classname$\n", "access_level",
602 GetAccessLevel(internal_access), "classname",
Jan Tattermusch2d924952015-05-06 10:23:17 -0700603 GetServiceClassName(service));
604 out->Print("{\n");
605 out->Indent();
606 out->Print("static readonly string $servicenamefield$ = \"$servicename$\";\n",
607 "servicenamefield", GetServiceNameFieldName(), "servicename",
608 service->full_name());
609 out->Print("\n");
610
611 GenerateMarshallerFields(out, service);
612 for (int i = 0; i < service->method_count(); i++) {
613 GenerateStaticMethodField(out, service->method(i));
614 }
Jan Tattermusche6af5d12015-08-03 10:57:43 -0700615 GenerateServiceDescriptorProperty(out, service);
Jan Tattermusch44f7c542016-04-25 14:50:27 -0700616
617 if (generate_client) {
618 GenerateClientInterface(out, service);
619 }
620 if (generate_server) {
621 GenerateServerInterface(out, service);
622 GenerateServerClass(out, service);
623 }
624 if (generate_client) {
625 GenerateClientStub(out, service);
626 GenerateNewStubMethods(out, service);
627 }
628 if (generate_server) {
629 GenerateBindServiceMethod(out, service, false);
630 GenerateBindServiceMethod(out, service, true);
631 }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700632
633 out->Outdent();
634 out->Print("}\n");
635}
636
637} // anonymous namespace
638
Jan Tattermusch5f8872f2016-04-25 15:57:10 -0700639grpc::string GetServices(const FileDescriptor *file, bool generate_client,
Jan Tattermusch4e0f73c2016-04-25 16:11:03 -0700640 bool generate_server, bool internal_access) {
Jan Tattermusch2d924952015-05-06 10:23:17 -0700641 grpc::string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700642 {
643 // Scope the output stream so it closes and finalizes output to the string.
Jan Tattermusch2d924952015-05-06 10:23:17 -0700644
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700645 StringOutputStream output_stream(&output);
646 Printer out(&output_stream, '$');
647
648 // Don't write out any output if there no services, to avoid empty service
649 // files being generated for proto files that don't declare any.
650 if (file->service_count() == 0) {
651 return output;
652 }
653
654 // Write out a file header.
655 out.Print("// Generated by the protocol buffer compiler. DO NOT EDIT!\n");
656 out.Print("// source: $filename$\n", "filename", file->name());
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700657
658 // use C++ style as there are no file-level XML comments in .NET
murgatroid99210f3c42016-05-20 13:24:59 -0700659 grpc::string leading_comments = GetCsharpComments(file, true);
Jan Tattermusch59c20ed2016-04-28 09:12:13 -0700660 if (!leading_comments.empty()) {
661 out.Print("// Original file comments:\n");
662 out.Print(leading_comments.c_str());
663 }
664
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700665 out.Print("#region Designer generated code\n");
666 out.Print("\n");
667 out.Print("using System;\n");
668 out.Print("using System.Threading;\n");
669 out.Print("using System.Threading.Tasks;\n");
670 out.Print("using Grpc.Core;\n");
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700671 out.Print("\n");
672
673 out.Print("namespace $namespace$ {\n", "namespace", GetFileNamespace(file));
674 out.Indent();
675 for (int i = 0; i < file->service_count(); i++) {
Jan Tattermusch4e0f73c2016-04-25 16:11:03 -0700676 GenerateService(&out, file->service(i), generate_client, generate_server,
677 internal_access);
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700678 }
679 out.Outdent();
680 out.Print("}\n");
681 out.Print("#endregion\n");
Jan Tattermusch2d924952015-05-06 10:23:17 -0700682 }
Jan Tattermusch2d924952015-05-06 10:23:17 -0700683 return output;
684}
685
686} // namespace grpc_csharp_generator