prevent name clashes in C# generated code

pull/10147/head
Jan Tattermusch 8 years ago
parent 20a06a05e8
commit 482e2d2010
  1. 77
      src/compiler/csharp_generator.cc

@ -203,13 +203,13 @@ std::string GetServerClassName(const ServiceDescriptor *service) {
std::string GetCSharpMethodType(MethodType method_type) { std::string GetCSharpMethodType(MethodType method_type) {
switch (method_type) { switch (method_type) {
case METHODTYPE_NO_STREAMING: case METHODTYPE_NO_STREAMING:
return "MethodType.Unary"; return "grpc::MethodType.Unary";
case METHODTYPE_CLIENT_STREAMING: case METHODTYPE_CLIENT_STREAMING:
return "MethodType.ClientStreaming"; return "grpc::MethodType.ClientStreaming";
case METHODTYPE_SERVER_STREAMING: case METHODTYPE_SERVER_STREAMING:
return "MethodType.ServerStreaming"; return "grpc::MethodType.ServerStreaming";
case METHODTYPE_BIDI_STREAMING: case METHODTYPE_BIDI_STREAMING:
return "MethodType.DuplexStreaming"; return "grpc::MethodType.DuplexStreaming";
} }
GOOGLE_LOG(FATAL) << "Can't get here."; GOOGLE_LOG(FATAL) << "Can't get here.";
return ""; return "";
@ -243,16 +243,19 @@ std::string GetAccessLevel(bool internal_access) {
std::string GetMethodReturnTypeClient(const MethodDescriptor *method) { std::string GetMethodReturnTypeClient(const MethodDescriptor *method) {
switch (GetMethodType(method)) { switch (GetMethodType(method)) {
case METHODTYPE_NO_STREAMING: case METHODTYPE_NO_STREAMING:
return "AsyncUnaryCall<" + GetClassName(method->output_type()) + ">"; return "grpc::AsyncUnaryCall<" + GetClassName(method->output_type()) +
">";
case METHODTYPE_CLIENT_STREAMING: case METHODTYPE_CLIENT_STREAMING:
return "AsyncClientStreamingCall<" + GetClassName(method->input_type()) + return "grpc::AsyncClientStreamingCall<" +
", " + GetClassName(method->output_type()) + ">"; GetClassName(method->input_type()) + ", " +
GetClassName(method->output_type()) + ">";
case METHODTYPE_SERVER_STREAMING: case METHODTYPE_SERVER_STREAMING:
return "AsyncServerStreamingCall<" + GetClassName(method->output_type()) + return "grpc::AsyncServerStreamingCall<" +
">"; GetClassName(method->output_type()) + ">";
case METHODTYPE_BIDI_STREAMING: case METHODTYPE_BIDI_STREAMING:
return "AsyncDuplexStreamingCall<" + GetClassName(method->input_type()) + return "grpc::AsyncDuplexStreamingCall<" +
", " + GetClassName(method->output_type()) + ">"; GetClassName(method->input_type()) + ", " +
GetClassName(method->output_type()) + ">";
} }
GOOGLE_LOG(FATAL) << "Can't get here."; GOOGLE_LOG(FATAL) << "Can't get here.";
return ""; return "";
@ -265,7 +268,7 @@ std::string GetMethodRequestParamServer(const MethodDescriptor *method) {
return GetClassName(method->input_type()) + " request"; return GetClassName(method->input_type()) + " request";
case METHODTYPE_CLIENT_STREAMING: case METHODTYPE_CLIENT_STREAMING:
case METHODTYPE_BIDI_STREAMING: case METHODTYPE_BIDI_STREAMING:
return "IAsyncStreamReader<" + GetClassName(method->input_type()) + return "grpc::IAsyncStreamReader<" + GetClassName(method->input_type()) +
"> requestStream"; "> requestStream";
} }
GOOGLE_LOG(FATAL) << "Can't get here."; GOOGLE_LOG(FATAL) << "Can't get here.";
@ -293,8 +296,8 @@ std::string GetMethodResponseStreamMaybe(const MethodDescriptor *method) {
return ""; return "";
case METHODTYPE_SERVER_STREAMING: case METHODTYPE_SERVER_STREAMING:
case METHODTYPE_BIDI_STREAMING: case METHODTYPE_BIDI_STREAMING:
return ", IServerStreamWriter<" + GetClassName(method->output_type()) + return ", grpc::IServerStreamWriter<" +
"> responseStream"; GetClassName(method->output_type()) + "> responseStream";
} }
GOOGLE_LOG(FATAL) << "Can't get here."; GOOGLE_LOG(FATAL) << "Can't get here.";
return ""; return "";
@ -325,8 +328,8 @@ void GenerateMarshallerFields(Printer *out, const ServiceDescriptor *service) {
for (size_t i = 0; i < used_messages.size(); i++) { for (size_t i = 0; i < used_messages.size(); i++) {
const Descriptor *message = used_messages[i]; const Descriptor *message = used_messages[i];
out->Print( out->Print(
"static readonly Marshaller<$type$> $fieldname$ = " "static readonly grpc::Marshaller<$type$> $fieldname$ = "
"Marshallers.Create((arg) => " "grpc::Marshallers.Create((arg) => "
"global::Google.Protobuf.MessageExtensions.ToByteArray(arg), " "global::Google.Protobuf.MessageExtensions.ToByteArray(arg), "
"$type$.Parser.ParseFrom);\n", "$type$.Parser.ParseFrom);\n",
"fieldname", GetMarshallerFieldName(message), "type", "fieldname", GetMarshallerFieldName(message), "type",
@ -337,8 +340,8 @@ void GenerateMarshallerFields(Printer *out, const ServiceDescriptor *service) {
void GenerateStaticMethodField(Printer *out, const MethodDescriptor *method) { void GenerateStaticMethodField(Printer *out, const MethodDescriptor *method) {
out->Print( out->Print(
"static readonly Method<$request$, $response$> $fieldname$ = new " "static readonly grpc::Method<$request$, $response$> $fieldname$ = new "
"Method<$request$, $response$>(\n", "grpc::Method<$request$, $response$>(\n",
"fieldname", GetMethodFieldName(method), "request", "fieldname", GetMethodFieldName(method), "request",
GetClassName(method->input_type()), "response", GetClassName(method->input_type()), "response",
GetClassName(method->output_type())); GetClassName(method->output_type()));
@ -389,7 +392,7 @@ void GenerateServerClass(Printer *out, const ServiceDescriptor *service) {
out->Print( out->Print(
"public virtual $returntype$ " "public virtual $returntype$ "
"$methodname$($request$$response_stream_maybe$, " "$methodname$($request$$response_stream_maybe$, "
"ServerCallContext context)\n", "grpc::ServerCallContext context)\n",
"methodname", method->name(), "returntype", "methodname", method->name(), "returntype",
GetMethodReturnTypeServer(method), "request", GetMethodReturnTypeServer(method), "request",
GetMethodRequestParamServer(method), "response_stream_maybe", GetMethodRequestParamServer(method), "response_stream_maybe",
@ -397,8 +400,8 @@ void GenerateServerClass(Printer *out, const ServiceDescriptor *service) {
out->Print("{\n"); out->Print("{\n");
out->Indent(); out->Indent();
out->Print( out->Print(
"throw new RpcException(" "throw new grpc::RpcException("
"new Status(StatusCode.Unimplemented, \"\"));\n"); "new grpc::Status(grpc::StatusCode.Unimplemented, \"\"));\n");
out->Outdent(); out->Outdent();
out->Print("}\n\n"); out->Print("}\n\n");
} }
@ -410,7 +413,7 @@ void GenerateServerClass(Printer *out, const ServiceDescriptor *service) {
void GenerateClientStub(Printer *out, const ServiceDescriptor *service) { void GenerateClientStub(Printer *out, const ServiceDescriptor *service) {
out->Print("/// <summary>Client for $servicename$</summary>\n", "servicename", out->Print("/// <summary>Client for $servicename$</summary>\n", "servicename",
GetServiceClassName(service)); GetServiceClassName(service));
out->Print("public partial class $name$ : ClientBase<$name$>\n", "name", out->Print("public partial class $name$ : grpc::ClientBase<$name$>\n", "name",
GetClientClassName(service)); GetClientClassName(service));
out->Print("{\n"); out->Print("{\n");
out->Indent(); out->Indent();
@ -421,7 +424,7 @@ void GenerateClientStub(Printer *out, const ServiceDescriptor *service) {
"/// <param name=\"channel\">The channel to use to make remote " "/// <param name=\"channel\">The channel to use to make remote "
"calls.</param>\n", "calls.</param>\n",
"servicename", GetServiceClassName(service)); "servicename", GetServiceClassName(service));
out->Print("public $name$(Channel channel) : base(channel)\n", "name", out->Print("public $name$(grpc::Channel channel) : base(channel)\n", "name",
GetClientClassName(service)); GetClientClassName(service));
out->Print("{\n"); out->Print("{\n");
out->Print("}\n"); out->Print("}\n");
@ -431,8 +434,9 @@ void GenerateClientStub(Printer *out, const ServiceDescriptor *service) {
"/// <param name=\"callInvoker\">The callInvoker to use to make remote " "/// <param name=\"callInvoker\">The callInvoker to use to make remote "
"calls.</param>\n", "calls.</param>\n",
"servicename", GetServiceClassName(service)); "servicename", GetServiceClassName(service));
out->Print("public $name$(CallInvoker callInvoker) : base(callInvoker)\n", out->Print(
"name", GetClientClassName(service)); "public $name$(grpc::CallInvoker callInvoker) : base(callInvoker)\n",
"name", GetClientClassName(service));
out->Print("{\n"); out->Print("{\n");
out->Print("}\n"); out->Print("}\n");
out->Print( out->Print(
@ -461,7 +465,8 @@ void GenerateClientStub(Printer *out, const ServiceDescriptor *service) {
// unary calls have an extra synchronous stub method // unary calls have an extra synchronous stub method
GenerateDocCommentClientMethod(out, method, true, false); GenerateDocCommentClientMethod(out, method, true, false);
out->Print( out->Print(
"public virtual $response$ $methodname$($request$ request, Metadata " "public virtual $response$ $methodname$($request$ request, "
"grpc::Metadata "
"headers = null, DateTime? deadline = null, CancellationToken " "headers = null, DateTime? deadline = null, CancellationToken "
"cancellationToken = default(CancellationToken))\n", "cancellationToken = default(CancellationToken))\n",
"methodname", method->name(), "request", "methodname", method->name(), "request",
@ -470,7 +475,8 @@ void GenerateClientStub(Printer *out, const ServiceDescriptor *service) {
out->Print("{\n"); out->Print("{\n");
out->Indent(); out->Indent();
out->Print( out->Print(
"return $methodname$(request, new CallOptions(headers, deadline, " "return $methodname$(request, new grpc::CallOptions(headers, "
"deadline, "
"cancellationToken));\n", "cancellationToken));\n",
"methodname", method->name()); "methodname", method->name());
out->Outdent(); out->Outdent();
@ -480,7 +486,7 @@ void GenerateClientStub(Printer *out, const ServiceDescriptor *service) {
GenerateDocCommentClientMethod(out, method, true, true); GenerateDocCommentClientMethod(out, method, true, true);
out->Print( out->Print(
"public virtual $response$ $methodname$($request$ request, " "public virtual $response$ $methodname$($request$ request, "
"CallOptions options)\n", "grpc::CallOptions options)\n",
"methodname", method->name(), "request", "methodname", method->name(), "request",
GetClassName(method->input_type()), "response", GetClassName(method->input_type()), "response",
GetClassName(method->output_type())); GetClassName(method->output_type()));
@ -500,7 +506,8 @@ void GenerateClientStub(Printer *out, const ServiceDescriptor *service) {
} }
GenerateDocCommentClientMethod(out, method, false, false); GenerateDocCommentClientMethod(out, method, false, false);
out->Print( out->Print(
"public virtual $returntype$ $methodname$($request_maybe$Metadata " "public virtual $returntype$ "
"$methodname$($request_maybe$grpc::Metadata "
"headers = null, DateTime? deadline = null, CancellationToken " "headers = null, DateTime? deadline = null, CancellationToken "
"cancellationToken = default(CancellationToken))\n", "cancellationToken = default(CancellationToken))\n",
"methodname", method_name, "request_maybe", "methodname", method_name, "request_maybe",
@ -510,7 +517,8 @@ void GenerateClientStub(Printer *out, const ServiceDescriptor *service) {
out->Indent(); out->Indent();
out->Print( out->Print(
"return $methodname$($request_maybe$new CallOptions(headers, deadline, " "return $methodname$($request_maybe$new grpc::CallOptions(headers, "
"deadline, "
"cancellationToken));\n", "cancellationToken));\n",
"methodname", method_name, "request_maybe", "methodname", method_name, "request_maybe",
GetMethodRequestParamMaybe(method, true)); GetMethodRequestParamMaybe(method, true));
@ -520,7 +528,8 @@ void GenerateClientStub(Printer *out, const ServiceDescriptor *service) {
// overload taking CallOptions as a param // overload taking CallOptions as a param
GenerateDocCommentClientMethod(out, method, false, true); GenerateDocCommentClientMethod(out, method, false, true);
out->Print( out->Print(
"public virtual $returntype$ $methodname$($request_maybe$CallOptions " "public virtual $returntype$ "
"$methodname$($request_maybe$grpc::CallOptions "
"options)\n", "options)\n",
"methodname", method_name, "request_maybe", "methodname", method_name, "request_maybe",
GetMethodRequestParamMaybe(method), "returntype", GetMethodRequestParamMaybe(method), "returntype",
@ -587,13 +596,13 @@ void GenerateBindServiceMethod(Printer *out, const ServiceDescriptor *service) {
"/// <param name=\"serviceImpl\">An object implementing the server-side" "/// <param name=\"serviceImpl\">An object implementing the server-side"
" handling logic.</param>\n"); " handling logic.</param>\n");
out->Print( out->Print(
"public static ServerServiceDefinition BindService($implclass$ " "public static grpc::ServerServiceDefinition BindService($implclass$ "
"serviceImpl)\n", "serviceImpl)\n",
"implclass", GetServerClassName(service)); "implclass", GetServerClassName(service));
out->Print("{\n"); out->Print("{\n");
out->Indent(); out->Indent();
out->Print("return ServerServiceDefinition.CreateBuilder()\n"); out->Print("return grpc::ServerServiceDefinition.CreateBuilder()\n");
out->Indent(); out->Indent();
out->Indent(); out->Indent();
for (int i = 0; i < service->method_count(); i++) { for (int i = 0; i < service->method_count(); i++) {
@ -681,7 +690,7 @@ grpc::string GetServices(const FileDescriptor *file, bool generate_client,
out.Print("using System;\n"); out.Print("using System;\n");
out.Print("using System.Threading;\n"); out.Print("using System.Threading;\n");
out.Print("using System.Threading.Tasks;\n"); out.Print("using System.Threading.Tasks;\n");
out.Print("using Grpc.Core;\n"); out.Print("using grpc = global::Grpc.Core;\n");
out.Print("\n"); out.Print("\n");
out.Print("namespace $namespace$ {\n", "namespace", GetFileNamespace(file)); out.Print("namespace $namespace$ {\n", "namespace", GetFileNamespace(file));

Loading…
Cancel
Save