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 | |
| 39 | #include "src/compiler/config.h" |
| 40 | #include "src/compiler/csharp_generator.h" |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 41 | #include "src/compiler/csharp_generator.h" |
| 42 | #include "src/compiler/csharp_generator_helpers.h" |
Jan Tattermusch | 741e64c | 2015-08-03 08:08:56 -0700 | [diff] [blame] | 43 | |
| 44 | using google::protobuf::compiler::csharp::GetFileNamespace; |
| 45 | using google::protobuf::compiler::csharp::GetClassName; |
Jan Tattermusch | da717f4 | 2016-01-20 13:12:35 -0800 | [diff] [blame] | 46 | using google::protobuf::compiler::csharp::GetReflectionClassName; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 47 | using grpc::protobuf::FileDescriptor; |
| 48 | using grpc::protobuf::Descriptor; |
| 49 | using grpc::protobuf::ServiceDescriptor; |
| 50 | using grpc::protobuf::MethodDescriptor; |
| 51 | using grpc::protobuf::io::Printer; |
| 52 | using grpc::protobuf::io::StringOutputStream; |
Jan Tattermusch | b5897bf | 2015-05-07 15:45:37 -0700 | [diff] [blame] | 53 | using grpc_generator::MethodType; |
| 54 | using grpc_generator::GetMethodType; |
| 55 | using grpc_generator::METHODTYPE_NO_STREAMING; |
| 56 | using grpc_generator::METHODTYPE_CLIENT_STREAMING; |
| 57 | using grpc_generator::METHODTYPE_SERVER_STREAMING; |
| 58 | using grpc_generator::METHODTYPE_BIDI_STREAMING; |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 59 | using grpc_generator::StringReplace; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 60 | using std::map; |
| 61 | using std::vector; |
| 62 | |
| 63 | namespace grpc_csharp_generator { |
| 64 | namespace { |
| 65 | |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 66 | // This function is a massaged version of |
| 67 | // https://github.com/google/protobuf/blob/master/src/google/protobuf/compiler/csharp/csharp_doc_comment.cc |
| 68 | // Currently, we cannot easily reuse the functionality as |
| 69 | // google/protobuf/compiler/csharp/csharp_doc_comment.h is not a public header. |
| 70 | // TODO(jtattermusch): reuse the functionality from google/protobuf. |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 71 | void GenerateDocCommentBodyImpl(grpc::protobuf::io::Printer *printer, |
| 72 | grpc::protobuf::SourceLocation location) { |
| 73 | grpc::string comments = location.leading_comments.empty() |
| 74 | ? location.trailing_comments |
| 75 | : location.leading_comments; |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 76 | if (comments.empty()) { |
| 77 | return; |
| 78 | } |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 79 | // XML escaping... no need for apostrophes etc as the whole text is going to |
| 80 | // be a child |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 81 | // node of a summary element, not part of an attribute. |
| 82 | comments = grpc_generator::StringReplace(comments, "&", "&", true); |
| 83 | comments = grpc_generator::StringReplace(comments, "<", "<", true); |
| 84 | |
| 85 | std::vector<grpc::string> lines; |
| 86 | grpc_generator::Split(comments, '\n', &lines); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 87 | // TODO: We really should work out which part to put in the summary and which |
| 88 | // to put in the remarks... |
| 89 | // but that needs to be part of a bigger effort to understand the markdown |
| 90 | // better anyway. |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 91 | printer->Print("/// <summary>\n"); |
| 92 | bool last_was_empty = false; |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 93 | // We squash multiple blank lines down to one, and remove any trailing blank |
| 94 | // lines. We need |
| 95 | // to preserve the blank lines themselves, as this is relevant in the |
| 96 | // markdown. |
| 97 | // Note that we can't remove leading or trailing whitespace as *that's* |
| 98 | // relevant in markdown too. |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 99 | // (We don't skip "just whitespace" lines, either.) |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 100 | for (std::vector<grpc::string>::iterator it = lines.begin(); |
| 101 | it != lines.end(); ++it) { |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 102 | grpc::string line = *it; |
| 103 | if (line.empty()) { |
| 104 | last_was_empty = true; |
| 105 | } else { |
| 106 | if (last_was_empty) { |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 107 | printer->Print("///\n"); |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 108 | } |
| 109 | last_was_empty = false; |
| 110 | printer->Print("/// $line$\n", "line", *it); |
| 111 | } |
| 112 | } |
| 113 | printer->Print("/// </summary>\n"); |
| 114 | } |
| 115 | |
| 116 | template <typename DescriptorType> |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 117 | void GenerateDocCommentBody(grpc::protobuf::io::Printer *printer, |
| 118 | const DescriptorType *descriptor) { |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 119 | grpc::protobuf::SourceLocation location; |
| 120 | if (descriptor->GetSourceLocation(&location)) { |
| 121 | GenerateDocCommentBodyImpl(printer, location); |
| 122 | } |
| 123 | } |
| 124 | |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 125 | std::string GetServiceClassName(const ServiceDescriptor *service) { |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 126 | return service->name(); |
| 127 | } |
| 128 | |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 129 | std::string GetClientClassName(const ServiceDescriptor *service) { |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 130 | return service->name() + "Client"; |
| 131 | } |
| 132 | |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 133 | std::string GetServerClassName(const ServiceDescriptor *service) { |
Jan Tattermusch | 10a002d | 2016-03-14 15:22:19 -0700 | [diff] [blame] | 134 | return service->name() + "Base"; |
| 135 | } |
| 136 | |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 137 | std::string GetCSharpMethodType(MethodType method_type) { |
| 138 | switch (method_type) { |
| 139 | case METHODTYPE_NO_STREAMING: |
| 140 | return "MethodType.Unary"; |
| 141 | case METHODTYPE_CLIENT_STREAMING: |
| 142 | return "MethodType.ClientStreaming"; |
| 143 | case METHODTYPE_SERVER_STREAMING: |
| 144 | return "MethodType.ServerStreaming"; |
| 145 | case METHODTYPE_BIDI_STREAMING: |
| 146 | return "MethodType.DuplexStreaming"; |
| 147 | } |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 148 | GOOGLE_LOG(FATAL) << "Can't get here."; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 149 | return ""; |
| 150 | } |
| 151 | |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 152 | std::string GetServiceNameFieldName() { return "__ServiceName"; } |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 153 | |
| 154 | std::string GetMarshallerFieldName(const Descriptor *message) { |
| 155 | return "__Marshaller_" + message->name(); |
| 156 | } |
| 157 | |
| 158 | std::string GetMethodFieldName(const MethodDescriptor *method) { |
| 159 | return "__Method_" + method->name(); |
| 160 | } |
| 161 | |
Jan Tattermusch | 8cd4ae9 | 2016-03-21 18:46:32 -0700 | [diff] [blame] | 162 | std::string GetMethodRequestParamMaybe(const MethodDescriptor *method, |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 163 | bool invocation_param = false) { |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 164 | if (method->client_streaming()) { |
| 165 | return ""; |
| 166 | } |
Jan Tattermusch | 8cd4ae9 | 2016-03-21 18:46:32 -0700 | [diff] [blame] | 167 | if (invocation_param) { |
| 168 | return "request, "; |
| 169 | } |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 170 | return GetClassName(method->input_type()) + " request, "; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Jan Tattermusch | 4e0f73c | 2016-04-25 16:11:03 -0700 | [diff] [blame] | 173 | std::string GetAccessLevel(bool internal_access) { |
| 174 | return internal_access ? "internal" : "public"; |
| 175 | } |
| 176 | |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 177 | std::string GetMethodReturnTypeClient(const MethodDescriptor *method) { |
| 178 | switch (GetMethodType(method)) { |
| 179 | case METHODTYPE_NO_STREAMING: |
Jan Tattermusch | 5269d16 | 2015-07-21 11:56:42 -0700 | [diff] [blame] | 180 | return "AsyncUnaryCall<" + GetClassName(method->output_type()) + ">"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 181 | case METHODTYPE_CLIENT_STREAMING: |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 182 | return "AsyncClientStreamingCall<" + GetClassName(method->input_type()) + |
| 183 | ", " + GetClassName(method->output_type()) + ">"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 184 | case METHODTYPE_SERVER_STREAMING: |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 185 | return "AsyncServerStreamingCall<" + GetClassName(method->output_type()) + |
| 186 | ">"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 187 | case METHODTYPE_BIDI_STREAMING: |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 188 | return "AsyncDuplexStreamingCall<" + GetClassName(method->input_type()) + |
| 189 | ", " + GetClassName(method->output_type()) + ">"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 190 | } |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 191 | GOOGLE_LOG(FATAL) << "Can't get here."; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 192 | return ""; |
| 193 | } |
| 194 | |
| 195 | std::string GetMethodRequestParamServer(const MethodDescriptor *method) { |
| 196 | switch (GetMethodType(method)) { |
| 197 | case METHODTYPE_NO_STREAMING: |
| 198 | case METHODTYPE_SERVER_STREAMING: |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 199 | return GetClassName(method->input_type()) + " request"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 200 | case METHODTYPE_CLIENT_STREAMING: |
| 201 | case METHODTYPE_BIDI_STREAMING: |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 202 | return "IAsyncStreamReader<" + GetClassName(method->input_type()) + |
| 203 | "> requestStream"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 204 | } |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 205 | GOOGLE_LOG(FATAL) << "Can't get here."; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 206 | return ""; |
| 207 | } |
| 208 | |
| 209 | std::string GetMethodReturnTypeServer(const MethodDescriptor *method) { |
| 210 | switch (GetMethodType(method)) { |
| 211 | case METHODTYPE_NO_STREAMING: |
| 212 | case METHODTYPE_CLIENT_STREAMING: |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 213 | return "global::System.Threading.Tasks.Task<" + |
| 214 | GetClassName(method->output_type()) + ">"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 215 | case METHODTYPE_SERVER_STREAMING: |
| 216 | case METHODTYPE_BIDI_STREAMING: |
Kirill Katsnelson | 0ddf46f | 2016-04-19 18:49:31 -0700 | [diff] [blame] | 217 | return "global::System.Threading.Tasks.Task"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 218 | } |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 219 | GOOGLE_LOG(FATAL) << "Can't get here."; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 220 | return ""; |
| 221 | } |
| 222 | |
| 223 | std::string GetMethodResponseStreamMaybe(const MethodDescriptor *method) { |
| 224 | switch (GetMethodType(method)) { |
| 225 | case METHODTYPE_NO_STREAMING: |
| 226 | case METHODTYPE_CLIENT_STREAMING: |
| 227 | return ""; |
| 228 | case METHODTYPE_SERVER_STREAMING: |
| 229 | case METHODTYPE_BIDI_STREAMING: |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 230 | return ", IServerStreamWriter<" + GetClassName(method->output_type()) + |
| 231 | "> responseStream"; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 232 | } |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 233 | GOOGLE_LOG(FATAL) << "Can't get here."; |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 234 | return ""; |
| 235 | } |
| 236 | |
| 237 | // Gets vector of all messages used as input or output types. |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 238 | std::vector<const Descriptor *> GetUsedMessages( |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 239 | const ServiceDescriptor *service) { |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 240 | std::set<const Descriptor *> descriptor_set; |
| 241 | std::vector<const Descriptor *> |
| 242 | result; // vector is to maintain stable ordering |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 243 | for (int i = 0; i < service->method_count(); i++) { |
| 244 | const MethodDescriptor *method = service->method(i); |
| 245 | if (descriptor_set.find(method->input_type()) == descriptor_set.end()) { |
| 246 | descriptor_set.insert(method->input_type()); |
| 247 | result.push_back(method->input_type()); |
| 248 | } |
| 249 | if (descriptor_set.find(method->output_type()) == descriptor_set.end()) { |
| 250 | descriptor_set.insert(method->output_type()); |
| 251 | result.push_back(method->output_type()); |
| 252 | } |
| 253 | } |
| 254 | return result; |
| 255 | } |
| 256 | |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 257 | void GenerateMarshallerFields(Printer *out, const ServiceDescriptor *service) { |
| 258 | std::vector<const Descriptor *> used_messages = GetUsedMessages(service); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 259 | for (size_t i = 0; i < used_messages.size(); i++) { |
| 260 | const Descriptor *message = used_messages[i]; |
| 261 | out->Print( |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 262 | "static readonly Marshaller<$type$> $fieldname$ = " |
| 263 | "Marshallers.Create((arg) => " |
| 264 | "global::Google.Protobuf.MessageExtensions.ToByteArray(arg), " |
| 265 | "$type$.Parser.ParseFrom);\n", |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 266 | "fieldname", GetMarshallerFieldName(message), "type", |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 267 | GetClassName(message)); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 268 | } |
| 269 | out->Print("\n"); |
| 270 | } |
| 271 | |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 272 | void GenerateStaticMethodField(Printer *out, const MethodDescriptor *method) { |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 273 | out->Print( |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 274 | "static readonly Method<$request$, $response$> $fieldname$ = new " |
| 275 | "Method<$request$, $response$>(\n", |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 276 | "fieldname", GetMethodFieldName(method), "request", |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 277 | GetClassName(method->input_type()), "response", |
| 278 | GetClassName(method->output_type())); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 279 | out->Indent(); |
| 280 | out->Indent(); |
| 281 | out->Print("$methodtype$,\n", "methodtype", |
| 282 | GetCSharpMethodType(GetMethodType(method))); |
Jan Tattermusch | a9ddd02 | 2015-08-05 03:04:58 -0700 | [diff] [blame] | 283 | out->Print("$servicenamefield$,\n", "servicenamefield", |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 284 | GetServiceNameFieldName()); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 285 | out->Print("\"$methodname$\",\n", "methodname", method->name()); |
| 286 | out->Print("$requestmarshaller$,\n", "requestmarshaller", |
| 287 | GetMarshallerFieldName(method->input_type())); |
| 288 | out->Print("$responsemarshaller$);\n", "responsemarshaller", |
| 289 | GetMarshallerFieldName(method->output_type())); |
| 290 | out->Print("\n"); |
| 291 | out->Outdent(); |
| 292 | out->Outdent(); |
| 293 | } |
| 294 | |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 295 | void GenerateServiceDescriptorProperty(Printer *out, |
| 296 | const ServiceDescriptor *service) { |
yang-g | c3ee1d5 | 2015-08-28 11:33:52 -0700 | [diff] [blame] | 297 | std::ostringstream index; |
| 298 | index << service->index(); |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 299 | out->Print("/// <summary>Service descriptor</summary>\n"); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 300 | out->Print( |
| 301 | "public static global::Google.Protobuf.Reflection.ServiceDescriptor " |
| 302 | "Descriptor\n"); |
Jan Tattermusch | e6af5d1 | 2015-08-03 10:57:43 -0700 | [diff] [blame] | 303 | out->Print("{\n"); |
| 304 | out->Print(" get { return $umbrella$.Descriptor.Services[$index$]; }\n", |
Jan Tattermusch | da717f4 | 2016-01-20 13:12:35 -0800 | [diff] [blame] | 305 | "umbrella", GetReflectionClassName(service->file()), "index", |
yang-g | c3ee1d5 | 2015-08-28 11:33:52 -0700 | [diff] [blame] | 306 | index.str()); |
Jan Tattermusch | e6af5d1 | 2015-08-03 10:57:43 -0700 | [diff] [blame] | 307 | out->Print("}\n"); |
| 308 | out->Print("\n"); |
| 309 | } |
| 310 | |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 311 | void GenerateServerClass(Printer *out, const ServiceDescriptor *service) { |
| 312 | out->Print( |
| 313 | "/// <summary>Base class for server-side implementations of " |
| 314 | "$servicename$</summary>\n", |
| 315 | "servicename", GetServiceClassName(service)); |
Jan Tattermusch | 10a002d | 2016-03-14 15:22:19 -0700 | [diff] [blame] | 316 | out->Print("public abstract class $name$\n", "name", |
| 317 | GetServerClassName(service)); |
| 318 | out->Print("{\n"); |
| 319 | out->Indent(); |
| 320 | for (int i = 0; i < service->method_count(); i++) { |
| 321 | const MethodDescriptor *method = service->method(i); |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 322 | GenerateDocCommentBody(out, method); |
Jan Tattermusch | 10a002d | 2016-03-14 15:22:19 -0700 | [diff] [blame] | 323 | out->Print( |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 324 | "public virtual $returntype$ " |
| 325 | "$methodname$($request$$response_stream_maybe$, " |
Jan Tattermusch | 10a002d | 2016-03-14 15:22:19 -0700 | [diff] [blame] | 326 | "ServerCallContext context)\n", |
| 327 | "methodname", method->name(), "returntype", |
| 328 | GetMethodReturnTypeServer(method), "request", |
| 329 | GetMethodRequestParamServer(method), "response_stream_maybe", |
| 330 | GetMethodResponseStreamMaybe(method)); |
| 331 | out->Print("{\n"); |
| 332 | out->Indent(); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 333 | out->Print( |
| 334 | "throw new RpcException(" |
| 335 | "new Status(StatusCode.Unimplemented, \"\"));\n"); |
Jan Tattermusch | 10a002d | 2016-03-14 15:22:19 -0700 | [diff] [blame] | 336 | out->Outdent(); |
| 337 | out->Print("}\n\n"); |
| 338 | } |
| 339 | out->Outdent(); |
| 340 | out->Print("}\n"); |
| 341 | out->Print("\n"); |
| 342 | } |
| 343 | |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 344 | void GenerateClientStub(Printer *out, const ServiceDescriptor *service) { |
| 345 | out->Print("/// <summary>Client for $servicename$</summary>\n", "servicename", |
| 346 | GetServiceClassName(service)); |
| 347 | out->Print("public class $name$ : ClientBase<$name$>\n", "name", |
| 348 | GetClientClassName(service)); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 349 | out->Print("{\n"); |
| 350 | out->Indent(); |
| 351 | |
| 352 | // constructors |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 353 | out->Print( |
| 354 | "/// <summary>Creates a new client for $servicename$</summary>\n" |
| 355 | "/// <param name=\"channel\">The channel to use to make remote " |
| 356 | "calls.</param>\n", |
| 357 | "servicename", GetServiceClassName(service)); |
| 358 | out->Print("public $name$(Channel channel) : base(channel)\n", "name", |
| 359 | GetClientClassName(service)); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 360 | out->Print("{\n"); |
| 361 | out->Print("}\n"); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 362 | out->Print( |
| 363 | "/// <summary>Creates a new client for $servicename$ that uses a custom " |
| 364 | "<c>CallInvoker</c>.</summary>\n" |
| 365 | "/// <param name=\"callInvoker\">The callInvoker to use to make remote " |
| 366 | "calls.</param>\n", |
| 367 | "servicename", GetServiceClassName(service)); |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 368 | out->Print("public $name$(CallInvoker callInvoker) : base(callInvoker)\n", |
| 369 | "name", GetClientClassName(service)); |
| 370 | out->Print("{\n"); |
Jan Tattermusch | efb7784 | 2016-03-23 07:47:36 -0700 | [diff] [blame] | 371 | out->Print("}\n"); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 372 | out->Print( |
| 373 | "/// <summary>Protected parameterless constructor to allow creation" |
| 374 | " of test doubles.</summary>\n"); |
| 375 | out->Print("protected $name$() : base()\n", "name", |
| 376 | GetClientClassName(service)); |
Jan Tattermusch | efb7784 | 2016-03-23 07:47:36 -0700 | [diff] [blame] | 377 | out->Print("{\n"); |
Jan Tattermusch | 2f0a837 | 2016-03-23 09:16:49 -0700 | [diff] [blame] | 378 | out->Print("}\n"); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 379 | out->Print( |
| 380 | "/// <summary>Protected constructor to allow creation of configured " |
| 381 | "clients.</summary>\n" |
| 382 | "/// <param name=\"configuration\">The client configuration.</param>\n"); |
| 383 | out->Print( |
| 384 | "protected $name$(ClientBaseConfiguration configuration)" |
| 385 | " : base(configuration)\n", |
| 386 | "name", GetClientClassName(service)); |
Jan Tattermusch | 2f0a837 | 2016-03-23 09:16:49 -0700 | [diff] [blame] | 387 | out->Print("{\n"); |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 388 | out->Print("}\n\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 389 | |
| 390 | for (int i = 0; i < service->method_count(); i++) { |
| 391 | const MethodDescriptor *method = service->method(i); |
| 392 | MethodType method_type = GetMethodType(method); |
| 393 | |
| 394 | if (method_type == METHODTYPE_NO_STREAMING) { |
| 395 | // unary calls have an extra synchronous stub method |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 396 | GenerateDocCommentBody(out, method); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 397 | out->Print( |
| 398 | "public virtual $response$ $methodname$($request$ request, Metadata " |
| 399 | "headers = null, DateTime? deadline = null, CancellationToken " |
| 400 | "cancellationToken = default(CancellationToken))\n", |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 401 | "methodname", method->name(), "request", |
| 402 | GetClassName(method->input_type()), "response", |
| 403 | GetClassName(method->output_type())); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 404 | out->Print("{\n"); |
| 405 | out->Indent(); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 406 | out->Print( |
| 407 | "return $methodname$(request, new CallOptions(headers, deadline, " |
| 408 | "cancellationToken));\n", |
| 409 | "methodname", method->name()); |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 410 | out->Outdent(); |
| 411 | out->Print("}\n"); |
| 412 | |
| 413 | // overload taking CallOptions as a param |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 414 | GenerateDocCommentBody(out, method); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 415 | out->Print( |
| 416 | "public virtual $response$ $methodname$($request$ request, " |
| 417 | "CallOptions options)\n", |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 418 | "methodname", method->name(), "request", |
| 419 | GetClassName(method->input_type()), "response", |
| 420 | GetClassName(method->output_type())); |
| 421 | out->Print("{\n"); |
| 422 | out->Indent(); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 423 | out->Print( |
| 424 | "return CallInvoker.BlockingUnaryCall($methodfield$, null, options, " |
| 425 | "request);\n", |
| 426 | "methodfield", GetMethodFieldName(method)); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 427 | out->Outdent(); |
| 428 | out->Print("}\n"); |
| 429 | } |
| 430 | |
| 431 | std::string method_name = method->name(); |
| 432 | if (method_type == METHODTYPE_NO_STREAMING) { |
| 433 | method_name += "Async"; // prevent name clash with synchronous method. |
| 434 | } |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 435 | GenerateDocCommentBody(out, method); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 436 | out->Print( |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 437 | "public virtual $returntype$ $methodname$($request_maybe$Metadata " |
| 438 | "headers = null, DateTime? deadline = null, CancellationToken " |
| 439 | "cancellationToken = default(CancellationToken))\n", |
| 440 | "methodname", method_name, "request_maybe", |
| 441 | GetMethodRequestParamMaybe(method), "returntype", |
| 442 | GetMethodReturnTypeClient(method)); |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 443 | out->Print("{\n"); |
| 444 | out->Indent(); |
| 445 | |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 446 | out->Print( |
| 447 | "return $methodname$($request_maybe$new CallOptions(headers, deadline, " |
| 448 | "cancellationToken));\n", |
| 449 | "methodname", method_name, "request_maybe", |
| 450 | GetMethodRequestParamMaybe(method, true)); |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 451 | out->Outdent(); |
| 452 | out->Print("}\n"); |
| 453 | |
| 454 | // overload taking CallOptions as a param |
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( |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 457 | "public virtual $returntype$ $methodname$($request_maybe$CallOptions " |
| 458 | "options)\n", |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 459 | "methodname", method_name, "request_maybe", |
| 460 | GetMethodRequestParamMaybe(method), "returntype", |
| 461 | GetMethodReturnTypeClient(method)); |
| 462 | out->Print("{\n"); |
| 463 | out->Indent(); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 464 | switch (GetMethodType(method)) { |
| 465 | case METHODTYPE_NO_STREAMING: |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 466 | out->Print( |
| 467 | "return CallInvoker.AsyncUnaryCall($methodfield$, null, options, " |
| 468 | "request);\n", |
| 469 | "methodfield", GetMethodFieldName(method)); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 470 | break; |
| 471 | case METHODTYPE_CLIENT_STREAMING: |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 472 | out->Print( |
| 473 | "return CallInvoker.AsyncClientStreamingCall($methodfield$, null, " |
| 474 | "options);\n", |
| 475 | "methodfield", GetMethodFieldName(method)); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 476 | break; |
| 477 | case METHODTYPE_SERVER_STREAMING: |
| 478 | out->Print( |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 479 | "return CallInvoker.AsyncServerStreamingCall($methodfield$, null, " |
| 480 | "options, request);\n", |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 481 | "methodfield", GetMethodFieldName(method)); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 482 | break; |
| 483 | case METHODTYPE_BIDI_STREAMING: |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 484 | out->Print( |
| 485 | "return CallInvoker.AsyncDuplexStreamingCall($methodfield$, null, " |
| 486 | "options);\n", |
| 487 | "methodfield", GetMethodFieldName(method)); |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 488 | break; |
| 489 | default: |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 490 | GOOGLE_LOG(FATAL) << "Can't get here."; |
Jan Tattermusch | 5e10f18 | 2015-08-05 00:13:02 -0700 | [diff] [blame] | 491 | } |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 492 | out->Outdent(); |
| 493 | out->Print("}\n"); |
| 494 | } |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 495 | |
| 496 | // override NewInstance method |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 497 | out->Print( |
| 498 | "protected override $name$ NewInstance(ClientBaseConfiguration " |
| 499 | "configuration)\n", |
| 500 | "name", GetClientClassName(service)); |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 501 | out->Print("{\n"); |
| 502 | out->Indent(); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 503 | out->Print("return new $name$(configuration);\n", "name", |
| 504 | GetClientClassName(service)); |
Jan Tattermusch | 055c2dd | 2016-03-22 14:43:56 -0700 | [diff] [blame] | 505 | out->Outdent(); |
| 506 | out->Print("}\n"); |
| 507 | |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 508 | out->Outdent(); |
| 509 | out->Print("}\n"); |
| 510 | out->Print("\n"); |
| 511 | } |
| 512 | |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 513 | void GenerateBindServiceMethod(Printer *out, const ServiceDescriptor *service) { |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 514 | out->Print( |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 515 | "/// <summary>Creates service definition that can be registered with a " |
| 516 | "server</summary>\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 517 | out->Print( |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 518 | "public static ServerServiceDefinition BindService($implclass$ " |
| 519 | "serviceImpl)\n", |
Jan Tattermusch | fcc8d97 | 2016-06-06 09:44:02 -0700 | [diff] [blame] | 520 | "implclass", GetServerClassName(service)); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 521 | out->Print("{\n"); |
| 522 | out->Indent(); |
| 523 | |
Jan Tattermusch | 562cd05 | 2016-06-06 08:47:17 -0700 | [diff] [blame] | 524 | out->Print("return ServerServiceDefinition.CreateBuilder()\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 525 | out->Indent(); |
| 526 | out->Indent(); |
| 527 | for (int i = 0; i < service->method_count(); i++) { |
| 528 | const MethodDescriptor *method = service->method(i); |
| 529 | out->Print(".AddMethod($methodfield$, serviceImpl.$methodname$)", |
| 530 | "methodfield", GetMethodFieldName(method), "methodname", |
| 531 | method->name()); |
| 532 | if (i == service->method_count() - 1) { |
| 533 | out->Print(".Build();"); |
| 534 | } |
| 535 | out->Print("\n"); |
| 536 | } |
| 537 | out->Outdent(); |
| 538 | out->Outdent(); |
| 539 | |
| 540 | out->Outdent(); |
| 541 | out->Print("}\n"); |
| 542 | out->Print("\n"); |
| 543 | } |
| 544 | |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame^] | 545 | void GenerateService(Printer *out, const ServiceDescriptor *service, |
Jan Tattermusch | 4e0f73c | 2016-04-25 16:11:03 -0700 | [diff] [blame] | 546 | bool generate_client, bool generate_server, |
| 547 | bool internal_access) { |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 548 | GenerateDocCommentBody(out, service); |
Jan Tattermusch | 4e0f73c | 2016-04-25 16:11:03 -0700 | [diff] [blame] | 549 | out->Print("$access_level$ static class $classname$\n", "access_level", |
| 550 | GetAccessLevel(internal_access), "classname", |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 551 | GetServiceClassName(service)); |
| 552 | out->Print("{\n"); |
| 553 | out->Indent(); |
| 554 | out->Print("static readonly string $servicenamefield$ = \"$servicename$\";\n", |
| 555 | "servicenamefield", GetServiceNameFieldName(), "servicename", |
| 556 | service->full_name()); |
| 557 | out->Print("\n"); |
| 558 | |
| 559 | GenerateMarshallerFields(out, service); |
| 560 | for (int i = 0; i < service->method_count(); i++) { |
| 561 | GenerateStaticMethodField(out, service->method(i)); |
| 562 | } |
Jan Tattermusch | e6af5d1 | 2015-08-03 10:57:43 -0700 | [diff] [blame] | 563 | GenerateServiceDescriptorProperty(out, service); |
Jan Tattermusch | 44f7c54 | 2016-04-25 14:50:27 -0700 | [diff] [blame] | 564 | |
Jan Tattermusch | 44f7c54 | 2016-04-25 14:50:27 -0700 | [diff] [blame] | 565 | if (generate_server) { |
Jan Tattermusch | 44f7c54 | 2016-04-25 14:50:27 -0700 | [diff] [blame] | 566 | GenerateServerClass(out, service); |
| 567 | } |
| 568 | if (generate_client) { |
| 569 | GenerateClientStub(out, service); |
Jan Tattermusch | 44f7c54 | 2016-04-25 14:50:27 -0700 | [diff] [blame] | 570 | } |
| 571 | if (generate_server) { |
Jan Tattermusch | fcc8d97 | 2016-06-06 09:44:02 -0700 | [diff] [blame] | 572 | GenerateBindServiceMethod(out, service); |
Jan Tattermusch | 44f7c54 | 2016-04-25 14:50:27 -0700 | [diff] [blame] | 573 | } |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 574 | |
| 575 | out->Outdent(); |
| 576 | out->Print("}\n"); |
| 577 | } |
| 578 | |
| 579 | } // anonymous namespace |
| 580 | |
Jan Tattermusch | 5f8872f | 2016-04-25 15:57:10 -0700 | [diff] [blame] | 581 | grpc::string GetServices(const FileDescriptor *file, bool generate_client, |
Jan Tattermusch | 4e0f73c | 2016-04-25 16:11:03 -0700 | [diff] [blame] | 582 | bool generate_server, bool internal_access) { |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 583 | grpc::string output; |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 584 | { |
| 585 | // 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] | 586 | |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 587 | StringOutputStream output_stream(&output); |
| 588 | Printer out(&output_stream, '$'); |
| 589 | |
| 590 | // Don't write out any output if there no services, to avoid empty service |
| 591 | // files being generated for proto files that don't declare any. |
| 592 | if (file->service_count() == 0) { |
| 593 | return output; |
| 594 | } |
| 595 | |
| 596 | // Write out a file header. |
| 597 | out.Print("// Generated by the protocol buffer compiler. DO NOT EDIT!\n"); |
| 598 | out.Print("// source: $filename$\n", "filename", file->name()); |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 599 | |
| 600 | // use C++ style as there are no file-level XML comments in .NET |
murgatroid99 | 210f3c4 | 2016-05-20 13:24:59 -0700 | [diff] [blame] | 601 | grpc::string leading_comments = GetCsharpComments(file, true); |
Jan Tattermusch | 59c20ed | 2016-04-28 09:12:13 -0700 | [diff] [blame] | 602 | if (!leading_comments.empty()) { |
| 603 | out.Print("// Original file comments:\n"); |
| 604 | out.Print(leading_comments.c_str()); |
| 605 | } |
| 606 | |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 607 | out.Print("#region Designer generated code\n"); |
| 608 | out.Print("\n"); |
| 609 | out.Print("using System;\n"); |
| 610 | out.Print("using System.Threading;\n"); |
| 611 | out.Print("using System.Threading.Tasks;\n"); |
| 612 | out.Print("using Grpc.Core;\n"); |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 613 | out.Print("\n"); |
| 614 | |
| 615 | out.Print("namespace $namespace$ {\n", "namespace", GetFileNamespace(file)); |
| 616 | out.Indent(); |
| 617 | for (int i = 0; i < file->service_count(); i++) { |
Jan Tattermusch | 4e0f73c | 2016-04-25 16:11:03 -0700 | [diff] [blame] | 618 | GenerateService(&out, file->service(i), generate_client, generate_server, |
| 619 | internal_access); |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 620 | } |
| 621 | out.Outdent(); |
| 622 | out.Print("}\n"); |
| 623 | out.Print("#endregion\n"); |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 624 | } |
Jan Tattermusch | 2d92495 | 2015-05-06 10:23:17 -0700 | [diff] [blame] | 625 | return output; |
| 626 | } |
| 627 | |
| 628 | } // namespace grpc_csharp_generator |