Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 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> |
yang-g | c3ee1d5 | 2015-08-28 11:33:52 -0700 | [diff] [blame] | 36 | #include <sstream> |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 37 | #include <vector> |
| 38 | |
Jan Tattermusch | 741e64c | 2015-08-03 08:08:56 -0700 | [diff] [blame] | 39 | #include "src/compiler/csharp_generator.h" |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 40 | #include "src/compiler/config.h" |
Jan Tattermusch | b5897bf | 2015-05-07 15:45:37 -0700 | [diff] [blame] | 41 | #include "src/compiler/csharp_generator_helpers.h" |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 42 | #include "src/compiler/csharp_generator.h" |
| 43 | |
Jan Tattermusch | 741e64c | 2015-08-03 08:08:56 -0700 | [diff] [blame] | 44 | |
| 45 | using google::protobuf::compiler::csharp::GetFileNamespace; |
| 46 | using google::protobuf::compiler::csharp::GetClassName; |
Jan Tattermusch | da717f4 | 2016-01-20 13:12:35 -0800 | [diff] [blame] | 47 | using google::protobuf::compiler::csharp::GetReflectionClassName; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 48 | using grpc::protobuf::FileDescriptor; |
| 49 | using grpc::protobuf::Descriptor; |
| 50 | using grpc::protobuf::ServiceDescriptor; |
| 51 | using grpc::protobuf::MethodDescriptor; |
| 52 | using grpc::protobuf::io::Printer; |
| 53 | using grpc::protobuf::io::StringOutputStream; |
Jan Tattermusch | b5897bf | 2015-05-07 15:45:37 -0700 | [diff] [blame] | 54 | using grpc_generator::MethodType; |
| 55 | using grpc_generator::GetMethodType; |
| 56 | using grpc_generator::METHODTYPE_NO_STREAMING; |
| 57 | using grpc_generator::METHODTYPE_CLIENT_STREAMING; |
| 58 | using grpc_generator::METHODTYPE_SERVER_STREAMING; |
| 59 | using grpc_generator::METHODTYPE_BIDI_STREAMING; |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 60 | using grpc_generator::StringReplace; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 61 | using std::map; |
| 62 | using std::vector; |
| 63 | |
Jan Tattermusch | 741e64c | 2015-08-03 08:08:56 -0700 | [diff] [blame] | 64 | |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 65 | namespace grpc_csharp_generator { |
| 66 | namespace { |
| 67 | |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 68 | // 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. |
| 73 | void 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, "&", "&", true); |
| 82 | comments = grpc_generator::StringReplace(comments, "<", "<", 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 | |
| 109 | template <typename DescriptorType> |
| 110 | void 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 Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 118 | std::string GetServiceClassName(const ServiceDescriptor* service) { |
| 119 | return service->name(); |
| 120 | } |
| 121 | |
| 122 | std::string GetClientInterfaceName(const ServiceDescriptor* service) { |
| 123 | return "I" + service->name() + "Client"; |
| 124 | } |
| 125 | |
| 126 | std::string GetClientClassName(const ServiceDescriptor* service) { |
| 127 | return service->name() + "Client"; |
| 128 | } |
| 129 | |
| 130 | std::string GetServerInterfaceName(const ServiceDescriptor* service) { |
| 131 | return "I" + service->name(); |
| 132 | } |
| 133 | |
Jan Tattermusch | 10a002d | 2016-03-14 15:22:19 -0700 | [diff] [blame] | 134 | std::string GetServerClassName(const ServiceDescriptor* service) { |
| 135 | return service->name() + "Base"; |
| 136 | } |
| 137 | |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 138 | std::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 | |
| 153 | std::string GetServiceNameFieldName() { |
| 154 | return "__ServiceName"; |
| 155 | } |
| 156 | |
| 157 | std::string GetMarshallerFieldName(const Descriptor *message) { |
| 158 | return "__Marshaller_" + message->name(); |
| 159 | } |
| 160 | |
| 161 | std::string GetMethodFieldName(const MethodDescriptor *method) { |
| 162 | return "__Method_" + method->name(); |
| 163 | } |
| 164 | |
Jan Tattermusch | 8cd4ae9 | 2016-03-21 18:46:32 -0700 | [diff] [blame] | 165 | std::string GetMethodRequestParamMaybe(const MethodDescriptor *method, |
| 166 | bool invocation_param=false) { |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 167 | if (method->client_streaming()) { |
| 168 | return ""; |
| 169 | } |
Jan Tattermusch | 8cd4ae9 | 2016-03-21 18:46:32 -0700 | [diff] [blame] | 170 | if (invocation_param) { |
| 171 | return "request, "; |
| 172 | } |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 173 | return GetClassName(method->input_type()) + " request, "; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Jan Tattermusch | 4e0f73c | 2016-04-25 16:11:03 -0700 | [diff] [blame] | 176 | std::string GetAccessLevel(bool internal_access) { |
| 177 | return internal_access ? "internal" : "public"; |
| 178 | } |
| 179 | |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 180 | std::string GetMethodReturnTypeClient(const MethodDescriptor *method) { |
| 181 | switch (GetMethodType(method)) { |
| 182 | case METHODTYPE_NO_STREAMING: |
Jan Tattermusch | 5269d16 | 2015-07-21 11:56:42 -0700 | [diff] [blame] | 183 | return "AsyncUnaryCall<" + GetClassName(method->output_type()) + ">"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 184 | case METHODTYPE_CLIENT_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 185 | return "AsyncClientStreamingCall<" + GetClassName(method->input_type()) |
| 186 | + ", " + GetClassName(method->output_type()) + ">"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 187 | case METHODTYPE_SERVER_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 188 | return "AsyncServerStreamingCall<" + GetClassName(method->output_type()) |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 189 | + ">"; |
| 190 | case METHODTYPE_BIDI_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 191 | return "AsyncDuplexStreamingCall<" + GetClassName(method->input_type()) |
| 192 | + ", " + GetClassName(method->output_type()) + ">"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 193 | } |
| 194 | GOOGLE_LOG(FATAL)<< "Can't get here."; |
| 195 | return ""; |
| 196 | } |
| 197 | |
| 198 | std::string GetMethodRequestParamServer(const MethodDescriptor *method) { |
| 199 | switch (GetMethodType(method)) { |
| 200 | case METHODTYPE_NO_STREAMING: |
| 201 | case METHODTYPE_SERVER_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 202 | return GetClassName(method->input_type()) + " request"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 203 | case METHODTYPE_CLIENT_STREAMING: |
| 204 | case METHODTYPE_BIDI_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 205 | return "IAsyncStreamReader<" + GetClassName(method->input_type()) |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 206 | + "> requestStream"; |
| 207 | } |
| 208 | GOOGLE_LOG(FATAL)<< "Can't get here."; |
| 209 | return ""; |
| 210 | } |
| 211 | |
| 212 | std::string GetMethodReturnTypeServer(const MethodDescriptor *method) { |
| 213 | switch (GetMethodType(method)) { |
| 214 | case METHODTYPE_NO_STREAMING: |
| 215 | case METHODTYPE_CLIENT_STREAMING: |
Kirill Katsnelson | 0ddf46f | 2016-04-19 18:49:31 -0700 | [diff] [blame] | 216 | return "global::System.Threading.Tasks.Task<" + GetClassName(method->output_type()) + ">"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 217 | case METHODTYPE_SERVER_STREAMING: |
| 218 | case METHODTYPE_BIDI_STREAMING: |
Kirill Katsnelson | 0ddf46f | 2016-04-19 18:49:31 -0700 | [diff] [blame] | 219 | return "global::System.Threading.Tasks.Task"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 220 | } |
| 221 | GOOGLE_LOG(FATAL)<< "Can't get here."; |
| 222 | return ""; |
| 223 | } |
| 224 | |
| 225 | std::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 Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 232 | return ", IServerStreamWriter<" + GetClassName(method->output_type()) |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 233 | + "> 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. |
| 240 | std::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 | |
| 258 | void 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 Tattermusch | ad75dd1 | 2015-08-03 09:43:07 -0700 | [diff] [blame] | 263 | "static readonly Marshaller<$type$> $fieldname$ = Marshallers.Create((arg) => global::Google.Protobuf.MessageExtensions.ToByteArray(arg), $type$.Parser.ParseFrom);\n", |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 264 | "fieldname", GetMarshallerFieldName(message), "type", |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 265 | GetClassName(message)); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 266 | } |
| 267 | out->Print("\n"); |
| 268 | } |
| 269 | |
| 270 | void 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 Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 274 | GetClassName(method->input_type()), "response", |
| 275 | GetClassName(method->output_type())); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 276 | out->Indent(); |
| 277 | out->Indent(); |
| 278 | out->Print("$methodtype$,\n", "methodtype", |
| 279 | GetCSharpMethodType(GetMethodType(method))); |
Jan Tattermusch | a9ddd02 | 2015-08-05 03:04:58 -0700 | [diff] [blame] | 280 | out->Print("$servicenamefield$,\n", "servicenamefield", |
| 281 | GetServiceNameFieldName()); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 282 | 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 Tattermusch | e6af5d1 | 2015-08-03 10:57:43 -0700 | [diff] [blame] | 292 | void GenerateServiceDescriptorProperty(Printer* out, const ServiceDescriptor *service) { |
yang-g | c3ee1d5 | 2015-08-28 11:33:52 -0700 | [diff] [blame] | 293 | std::ostringstream index; |
| 294 | index << service->index(); |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 295 | out->Print("/// <summary>Service descriptor</summary>\n"); |
Jan Tattermusch | e6af5d1 | 2015-08-03 10:57:43 -0700 | [diff] [blame] | 296 | 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 Tattermusch | da717f4 | 2016-01-20 13:12:35 -0800 | [diff] [blame] | 299 | "umbrella", GetReflectionClassName(service->file()), "index", |
yang-g | c3ee1d5 | 2015-08-28 11:33:52 -0700 | [diff] [blame] | 300 | index.str()); |
Jan Tattermusch | e6af5d1 | 2015-08-03 10:57:43 -0700 | [diff] [blame] | 301 | out->Print("}\n"); |
| 302 | out->Print("\n"); |
| 303 | } |
| 304 | |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 305 | void GenerateClientInterface(Printer* out, const ServiceDescriptor *service) { |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 306 | out->Print("/// <summary>Client for $servicename$</summary>\n", |
| 307 | "servicename", GetServiceClassName(service)); |
Jan Tattermusch | 5a4e1e3 | 2016-03-25 16:51:55 -0700 | [diff] [blame] | 308 | out->Print("[System.Obsolete(\"Client side interfaced will be removed " |
| 309 | "in the next release. Use client class directly.\")]\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 310 | 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 Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 320 | GenerateDocCommentBody(out, method); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 321 | out->Print( |
Jan Tattermusch | 7452956 | 2015-07-23 14:04:51 -0700 | [diff] [blame] | 322 | "$response$ $methodname$($request$ request, Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken));\n", |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 323 | "methodname", method->name(), "request", |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 324 | GetClassName(method->input_type()), "response", |
| 325 | GetClassName(method->output_type())); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 326 | |
Jan Tattermusch | e5e57ad | 2015-08-05 14:54:08 -0700 | [diff] [blame] | 327 | // overload taking CallOptions as a param |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 328 | GenerateDocCommentBody(out, method); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 329 | out->Print( |
Jan Tattermusch | e5e57ad | 2015-08-05 14:54:08 -0700 | [diff] [blame] | 330 | "$response$ $methodname$($request$ request, CallOptions options);\n", |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 331 | "methodname", method->name(), "request", |
| 332 | GetClassName(method->input_type()), "response", |
| 333 | GetClassName(method->output_type())); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 334 | } |
| 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 Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 340 | GenerateDocCommentBody(out, method); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 341 | out->Print( |
Jan Tattermusch | 7452956 | 2015-07-23 14:04:51 -0700 | [diff] [blame] | 342 | "$returntype$ $methodname$($request_maybe$Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken));\n", |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 343 | "methodname", method_name, "request_maybe", |
| 344 | GetMethodRequestParamMaybe(method), "returntype", |
| 345 | GetMethodReturnTypeClient(method)); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 346 | |
Jan Tattermusch | e5e57ad | 2015-08-05 14:54:08 -0700 | [diff] [blame] | 347 | // overload taking CallOptions as a param |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 348 | GenerateDocCommentBody(out, method); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 349 | out->Print( |
Jan Tattermusch | e5e57ad | 2015-08-05 14:54:08 -0700 | [diff] [blame] | 350 | "$returntype$ $methodname$($request_maybe$CallOptions options);\n", |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 351 | "methodname", method_name, "request_maybe", |
| 352 | GetMethodRequestParamMaybe(method), "returntype", |
| 353 | GetMethodReturnTypeClient(method)); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 354 | } |
| 355 | out->Outdent(); |
| 356 | out->Print("}\n"); |
| 357 | out->Print("\n"); |
| 358 | } |
| 359 | |
| 360 | void GenerateServerInterface(Printer* out, const ServiceDescriptor *service) { |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 361 | out->Print("/// <summary>Interface of server-side implementations of $servicename$</summary>\n", |
| 362 | "servicename", GetServiceClassName(service)); |
Jan Tattermusch | 10a002d | 2016-03-14 15:22:19 -0700 | [diff] [blame] | 363 | out->Print("[System.Obsolete(\"Service implementations should inherit" |
| 364 | " from the generated abstract base class instead.\")]\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 365 | 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 Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 371 | GenerateDocCommentBody(out, method); |
Craig Tiller | b256faa | 2015-07-23 11:28:16 -0700 | [diff] [blame] | 372 | 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 Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 379 | } |
| 380 | out->Outdent(); |
| 381 | out->Print("}\n"); |
| 382 | out->Print("\n"); |
| 383 | } |
| 384 | |
Jan Tattermusch | 10a002d | 2016-03-14 15:22:19 -0700 | [diff] [blame] | 385 | void GenerateServerClass(Printer* out, const ServiceDescriptor *service) { |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 386 | out->Print("/// <summary>Base class for server-side implementations of $servicename$</summary>\n", |
| 387 | "servicename", GetServiceClassName(service)); |
Jan Tattermusch | 10a002d | 2016-03-14 15:22:19 -0700 | [diff] [blame] | 388 | 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 Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 394 | GenerateDocCommentBody(out, method); |
Jan Tattermusch | 10a002d | 2016-03-14 15:22:19 -0700 | [diff] [blame] | 395 | 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 Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 414 | void GenerateClientStub(Printer* out, const ServiceDescriptor *service) { |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 415 | out->Print("/// <summary>Client for $servicename$</summary>\n", |
| 416 | "servicename", GetServiceClassName(service)); |
Jan Tattermusch | cc8a4b3 | 2016-04-25 13:40:00 -0700 | [diff] [blame] | 417 | out->Print("#pragma warning disable 0618\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 418 | out->Print( |
Jan Tattermusch | 5a4e1e3 | 2016-03-25 16:51:55 -0700 | [diff] [blame] | 419 | "public class $name$ : ClientBase<$name$>, $interface$\n", |
| 420 | "name", GetClientClassName(service), |
| 421 | "interface", GetClientInterfaceName(service)); |
Jan Tattermusch | cc8a4b3 | 2016-04-25 13:40:00 -0700 | [diff] [blame] | 422 | out->Print("#pragma warning restore 0618\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 423 | out->Print("{\n"); |
| 424 | out->Indent(); |
| 425 | |
| 426 | // constructors |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 427 | out->Print("public $name$(Channel channel) : base(channel)\n", |
| 428 | "name", GetClientClassName(service)); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 429 | out->Print("{\n"); |
| 430 | out->Print("}\n"); |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 431 | out->Print("public $name$(CallInvoker callInvoker) : base(callInvoker)\n", |
| 432 | "name", GetClientClassName(service)); |
| 433 | out->Print("{\n"); |
Jan Tattermusch | efb7784 | 2016-03-23 07:47:36 -0700 | [diff] [blame] | 434 | out->Print("}\n"); |
Jan Tattermusch | 2f0a837 | 2016-03-23 09:16:49 -0700 | [diff] [blame] | 435 | out->Print("///<summary>Protected parameterless constructor to allow creation" |
Jan Tattermusch | efb7784 | 2016-03-23 07:47:36 -0700 | [diff] [blame] | 436 | " of test doubles.</summary>\n"); |
| 437 | out->Print("protected $name$() : base()\n", |
| 438 | "name", GetClientClassName(service)); |
| 439 | out->Print("{\n"); |
Jan Tattermusch | 2f0a837 | 2016-03-23 09:16:49 -0700 | [diff] [blame] | 440 | 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 Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 447 | out->Print("}\n\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 448 | |
| 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 Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 455 | GenerateDocCommentBody(out, method); |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 456 | 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 Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 460 | out->Print("{\n"); |
| 461 | out->Indent(); |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 462 | 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 Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 468 | GenerateDocCommentBody(out, method); |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 469 | 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 Tattermusch | b455bcc | 2016-03-22 16:08:18 -0700 | [diff] [blame] | 475 | out->Print("return CallInvoker.BlockingUnaryCall($methodfield$, null, options, request);\n", |
Jan Tattermusch | 641cb1b | 2015-08-05 03:51:46 -0700 | [diff] [blame] | 476 | "methodfield", GetMethodFieldName(method)); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 477 | 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 Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 485 | GenerateDocCommentBody(out, method); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 486 | out->Print( |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 487 | "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 Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 501 | GenerateDocCommentBody(out, method); |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 502 | out->Print( |
| 503 | "public virtual $returntype$ $methodname$($request_maybe$CallOptions options)\n", |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 504 | "methodname", method_name, "request_maybe", |
| 505 | GetMethodRequestParamMaybe(method), "returntype", |
| 506 | GetMethodReturnTypeClient(method)); |
| 507 | out->Print("{\n"); |
| 508 | out->Indent(); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 509 | switch (GetMethodType(method)) { |
| 510 | case METHODTYPE_NO_STREAMING: |
Jan Tattermusch | b455bcc | 2016-03-22 16:08:18 -0700 | [diff] [blame] | 511 | out->Print("return CallInvoker.AsyncUnaryCall($methodfield$, null, options, request);\n", |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 512 | "methodfield", GetMethodFieldName(method)); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 513 | break; |
| 514 | case METHODTYPE_CLIENT_STREAMING: |
Jan Tattermusch | b455bcc | 2016-03-22 16:08:18 -0700 | [diff] [blame] | 515 | out->Print("return CallInvoker.AsyncClientStreamingCall($methodfield$, null, options);\n", |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 516 | "methodfield", GetMethodFieldName(method)); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 517 | break; |
| 518 | case METHODTYPE_SERVER_STREAMING: |
| 519 | out->Print( |
Jan Tattermusch | b455bcc | 2016-03-22 16:08:18 -0700 | [diff] [blame] | 520 | "return CallInvoker.AsyncServerStreamingCall($methodfield$, null, options, request);\n", |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 521 | "methodfield", GetMethodFieldName(method)); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 522 | break; |
| 523 | case METHODTYPE_BIDI_STREAMING: |
Jan Tattermusch | b455bcc | 2016-03-22 16:08:18 -0700 | [diff] [blame] | 524 | out->Print("return CallInvoker.AsyncDuplexStreamingCall($methodfield$, null, options);\n", |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 525 | "methodfield", GetMethodFieldName(method)); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 526 | break; |
| 527 | default: |
| 528 | GOOGLE_LOG(FATAL)<< "Can't get here."; |
| 529 | } |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 530 | out->Outdent(); |
| 531 | out->Print("}\n"); |
| 532 | } |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 533 | |
| 534 | // override NewInstance method |
Jan Tattermusch | 2f0a837 | 2016-03-23 09:16:49 -0700 | [diff] [blame] | 535 | out->Print("protected override $name$ NewInstance(ClientBaseConfiguration configuration)\n", |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 536 | "name", GetClientClassName(service)); |
| 537 | out->Print("{\n"); |
| 538 | out->Indent(); |
Jan Tattermusch | 2f0a837 | 2016-03-23 09:16:49 -0700 | [diff] [blame] | 539 | out->Print("return new $name$(configuration);\n", |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 540 | "name", GetClientClassName(service)); |
| 541 | out->Outdent(); |
| 542 | out->Print("}\n"); |
| 543 | |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 544 | out->Outdent(); |
| 545 | out->Print("}\n"); |
| 546 | out->Print("\n"); |
| 547 | } |
| 548 | |
Jan Tattermusch | 10a002d | 2016-03-14 15:22:19 -0700 | [diff] [blame] | 549 | void GenerateBindServiceMethod(Printer* out, const ServiceDescriptor *service, |
| 550 | bool use_server_class) { |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 551 | out->Print( |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 552 | "/// <summary>Creates service definition that can be registered with a server</summary>\n"); |
Jan Tattermusch | cc8a4b3 | 2016-04-25 13:40:00 -0700 | [diff] [blame] | 553 | out->Print("#pragma warning disable 0618\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 554 | out->Print( |
| 555 | "public static ServerServiceDefinition BindService($interface$ serviceImpl)\n", |
Jan Tattermusch | 10a002d | 2016-03-14 15:22:19 -0700 | [diff] [blame] | 556 | "interface", use_server_class ? GetServerClassName(service) : |
| 557 | GetServerInterfaceName(service)); |
Jan Tattermusch | cc8a4b3 | 2016-04-25 13:40:00 -0700 | [diff] [blame] | 558 | out->Print("#pragma warning restore 0618\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 559 | out->Print("{\n"); |
| 560 | out->Indent(); |
| 561 | |
Jan Tattermusch | 562cd05 | 2016-06-06 08:47:17 -0700 | [diff] [blame^] | 562 | out->Print("return ServerServiceDefinition.CreateBuilder()\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 563 | 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 | |
| 583 | void GenerateNewStubMethods(Printer* out, const ServiceDescriptor *service) { |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 584 | out->Print("/// <summary>Creates a new client for $servicename$</summary>\n", |
| 585 | "servicename", GetServiceClassName(service)); |
Jan Tattermusch | b533281 | 2015-07-14 19:29:35 -0700 | [diff] [blame] | 586 | out->Print("public static $classname$ NewClient(Channel channel)\n", |
| 587 | "classname", GetClientClassName(service)); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 588 | 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 Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Jan Tattermusch | 44f7c54 | 2016-04-25 14:50:27 -0700 | [diff] [blame] | 597 | void GenerateService(Printer* out, const ServiceDescriptor *service, |
Jan Tattermusch | 4e0f73c | 2016-04-25 16:11:03 -0700 | [diff] [blame] | 598 | bool generate_client, bool generate_server, |
| 599 | bool internal_access) { |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 600 | GenerateDocCommentBody(out, service); |
Jan Tattermusch | 4e0f73c | 2016-04-25 16:11:03 -0700 | [diff] [blame] | 601 | out->Print("$access_level$ static class $classname$\n", "access_level", |
| 602 | GetAccessLevel(internal_access), "classname", |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 603 | 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 Tattermusch | e6af5d1 | 2015-08-03 10:57:43 -0700 | [diff] [blame] | 615 | GenerateServiceDescriptorProperty(out, service); |
Jan Tattermusch | 44f7c54 | 2016-04-25 14:50:27 -0700 | [diff] [blame] | 616 | |
| 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 Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 632 | |
| 633 | out->Outdent(); |
| 634 | out->Print("}\n"); |
| 635 | } |
| 636 | |
| 637 | } // anonymous namespace |
| 638 | |
Jan Tattermusch | 5f8872f | 2016-04-25 15:57:10 -0700 | [diff] [blame] | 639 | grpc::string GetServices(const FileDescriptor *file, bool generate_client, |
Jan Tattermusch | 4e0f73c | 2016-04-25 16:11:03 -0700 | [diff] [blame] | 640 | bool generate_server, bool internal_access) { |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 641 | grpc::string output; |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 642 | { |
| 643 | // Scope the output stream so it closes and finalizes output to the string. |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 644 | |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 645 | 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 Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 657 | |
| 658 | // use C++ style as there are no file-level XML comments in .NET |
murgatroid99 | 210f3c4 | 2016-05-20 13:24:59 -0700 | [diff] [blame] | 659 | grpc::string leading_comments = GetCsharpComments(file, true); |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 660 | if (!leading_comments.empty()) { |
| 661 | out.Print("// Original file comments:\n"); |
| 662 | out.Print(leading_comments.c_str()); |
| 663 | } |
| 664 | |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 665 | 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 Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 671 | 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 Tattermusch | 4e0f73c | 2016-04-25 16:11:03 -0700 | [diff] [blame] | 676 | GenerateService(&out, file->service(i), generate_client, generate_server, |
| 677 | internal_access); |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 678 | } |
| 679 | out.Outdent(); |
| 680 | out.Print("}\n"); |
| 681 | out.Print("#endregion\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 682 | } |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 683 | return output; |
| 684 | } |
| 685 | |
| 686 | } // namespace grpc_csharp_generator |