From 482e2d2010628d34e114639aef1205fc00ea3e06 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Tue, 14 Mar 2017 09:25:11 +0100 Subject: [PATCH] prevent name clashes in C# generated code --- src/compiler/csharp_generator.cc | 77 ++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/src/compiler/csharp_generator.cc b/src/compiler/csharp_generator.cc index cc7a7a96aea..ce1e6b94c3f 100644 --- a/src/compiler/csharp_generator.cc +++ b/src/compiler/csharp_generator.cc @@ -203,13 +203,13 @@ std::string GetServerClassName(const ServiceDescriptor *service) { std::string GetCSharpMethodType(MethodType method_type) { switch (method_type) { case METHODTYPE_NO_STREAMING: - return "MethodType.Unary"; + return "grpc::MethodType.Unary"; case METHODTYPE_CLIENT_STREAMING: - return "MethodType.ClientStreaming"; + return "grpc::MethodType.ClientStreaming"; case METHODTYPE_SERVER_STREAMING: - return "MethodType.ServerStreaming"; + return "grpc::MethodType.ServerStreaming"; case METHODTYPE_BIDI_STREAMING: - return "MethodType.DuplexStreaming"; + return "grpc::MethodType.DuplexStreaming"; } GOOGLE_LOG(FATAL) << "Can't get here."; return ""; @@ -243,16 +243,19 @@ std::string GetAccessLevel(bool internal_access) { std::string GetMethodReturnTypeClient(const MethodDescriptor *method) { switch (GetMethodType(method)) { case METHODTYPE_NO_STREAMING: - return "AsyncUnaryCall<" + GetClassName(method->output_type()) + ">"; + return "grpc::AsyncUnaryCall<" + GetClassName(method->output_type()) + + ">"; case METHODTYPE_CLIENT_STREAMING: - return "AsyncClientStreamingCall<" + GetClassName(method->input_type()) + - ", " + GetClassName(method->output_type()) + ">"; + return "grpc::AsyncClientStreamingCall<" + + GetClassName(method->input_type()) + ", " + + GetClassName(method->output_type()) + ">"; case METHODTYPE_SERVER_STREAMING: - return "AsyncServerStreamingCall<" + GetClassName(method->output_type()) + - ">"; + return "grpc::AsyncServerStreamingCall<" + + GetClassName(method->output_type()) + ">"; case METHODTYPE_BIDI_STREAMING: - return "AsyncDuplexStreamingCall<" + GetClassName(method->input_type()) + - ", " + GetClassName(method->output_type()) + ">"; + return "grpc::AsyncDuplexStreamingCall<" + + GetClassName(method->input_type()) + ", " + + GetClassName(method->output_type()) + ">"; } GOOGLE_LOG(FATAL) << "Can't get here."; return ""; @@ -265,7 +268,7 @@ std::string GetMethodRequestParamServer(const MethodDescriptor *method) { return GetClassName(method->input_type()) + " request"; case METHODTYPE_CLIENT_STREAMING: case METHODTYPE_BIDI_STREAMING: - return "IAsyncStreamReader<" + GetClassName(method->input_type()) + + return "grpc::IAsyncStreamReader<" + GetClassName(method->input_type()) + "> requestStream"; } GOOGLE_LOG(FATAL) << "Can't get here."; @@ -293,8 +296,8 @@ std::string GetMethodResponseStreamMaybe(const MethodDescriptor *method) { return ""; case METHODTYPE_SERVER_STREAMING: case METHODTYPE_BIDI_STREAMING: - return ", IServerStreamWriter<" + GetClassName(method->output_type()) + - "> responseStream"; + return ", grpc::IServerStreamWriter<" + + GetClassName(method->output_type()) + "> responseStream"; } GOOGLE_LOG(FATAL) << "Can't get here."; return ""; @@ -325,8 +328,8 @@ void GenerateMarshallerFields(Printer *out, const ServiceDescriptor *service) { for (size_t i = 0; i < used_messages.size(); i++) { const Descriptor *message = used_messages[i]; out->Print( - "static readonly Marshaller<$type$> $fieldname$ = " - "Marshallers.Create((arg) => " + "static readonly grpc::Marshaller<$type$> $fieldname$ = " + "grpc::Marshallers.Create((arg) => " "global::Google.Protobuf.MessageExtensions.ToByteArray(arg), " "$type$.Parser.ParseFrom);\n", "fieldname", GetMarshallerFieldName(message), "type", @@ -337,8 +340,8 @@ void GenerateMarshallerFields(Printer *out, const ServiceDescriptor *service) { void GenerateStaticMethodField(Printer *out, const MethodDescriptor *method) { out->Print( - "static readonly Method<$request$, $response$> $fieldname$ = new " - "Method<$request$, $response$>(\n", + "static readonly grpc::Method<$request$, $response$> $fieldname$ = new " + "grpc::Method<$request$, $response$>(\n", "fieldname", GetMethodFieldName(method), "request", GetClassName(method->input_type()), "response", GetClassName(method->output_type())); @@ -389,7 +392,7 @@ void GenerateServerClass(Printer *out, const ServiceDescriptor *service) { out->Print( "public virtual $returntype$ " "$methodname$($request$$response_stream_maybe$, " - "ServerCallContext context)\n", + "grpc::ServerCallContext context)\n", "methodname", method->name(), "returntype", GetMethodReturnTypeServer(method), "request", GetMethodRequestParamServer(method), "response_stream_maybe", @@ -397,8 +400,8 @@ void GenerateServerClass(Printer *out, const ServiceDescriptor *service) { out->Print("{\n"); out->Indent(); out->Print( - "throw new RpcException(" - "new Status(StatusCode.Unimplemented, \"\"));\n"); + "throw new grpc::RpcException(" + "new grpc::Status(grpc::StatusCode.Unimplemented, \"\"));\n"); out->Outdent(); out->Print("}\n\n"); } @@ -410,7 +413,7 @@ void GenerateServerClass(Printer *out, const ServiceDescriptor *service) { void GenerateClientStub(Printer *out, const ServiceDescriptor *service) { out->Print("/// Client for $servicename$\n", "servicename", 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)); out->Print("{\n"); out->Indent(); @@ -421,7 +424,7 @@ void GenerateClientStub(Printer *out, const ServiceDescriptor *service) { "/// The channel to use to make remote " "calls.\n", "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)); out->Print("{\n"); out->Print("}\n"); @@ -431,8 +434,9 @@ void GenerateClientStub(Printer *out, const ServiceDescriptor *service) { "/// The callInvoker to use to make remote " "calls.\n", "servicename", GetServiceClassName(service)); - out->Print("public $name$(CallInvoker callInvoker) : base(callInvoker)\n", - "name", GetClientClassName(service)); + out->Print( + "public $name$(grpc::CallInvoker callInvoker) : base(callInvoker)\n", + "name", GetClientClassName(service)); out->Print("{\n"); out->Print("}\n"); out->Print( @@ -461,7 +465,8 @@ void GenerateClientStub(Printer *out, const ServiceDescriptor *service) { // unary calls have an extra synchronous stub method GenerateDocCommentClientMethod(out, method, true, false); out->Print( - "public virtual $response$ $methodname$($request$ request, Metadata " + "public virtual $response$ $methodname$($request$ request, " + "grpc::Metadata " "headers = null, DateTime? deadline = null, CancellationToken " "cancellationToken = default(CancellationToken))\n", "methodname", method->name(), "request", @@ -470,7 +475,8 @@ void GenerateClientStub(Printer *out, const ServiceDescriptor *service) { out->Print("{\n"); out->Indent(); out->Print( - "return $methodname$(request, new CallOptions(headers, deadline, " + "return $methodname$(request, new grpc::CallOptions(headers, " + "deadline, " "cancellationToken));\n", "methodname", method->name()); out->Outdent(); @@ -480,7 +486,7 @@ void GenerateClientStub(Printer *out, const ServiceDescriptor *service) { GenerateDocCommentClientMethod(out, method, true, true); out->Print( "public virtual $response$ $methodname$($request$ request, " - "CallOptions options)\n", + "grpc::CallOptions options)\n", "methodname", method->name(), "request", GetClassName(method->input_type()), "response", GetClassName(method->output_type())); @@ -500,7 +506,8 @@ void GenerateClientStub(Printer *out, const ServiceDescriptor *service) { } GenerateDocCommentClientMethod(out, method, false, false); out->Print( - "public virtual $returntype$ $methodname$($request_maybe$Metadata " + "public virtual $returntype$ " + "$methodname$($request_maybe$grpc::Metadata " "headers = null, DateTime? deadline = null, CancellationToken " "cancellationToken = default(CancellationToken))\n", "methodname", method_name, "request_maybe", @@ -510,7 +517,8 @@ void GenerateClientStub(Printer *out, const ServiceDescriptor *service) { out->Indent(); out->Print( - "return $methodname$($request_maybe$new CallOptions(headers, deadline, " + "return $methodname$($request_maybe$new grpc::CallOptions(headers, " + "deadline, " "cancellationToken));\n", "methodname", method_name, "request_maybe", GetMethodRequestParamMaybe(method, true)); @@ -520,7 +528,8 @@ void GenerateClientStub(Printer *out, const ServiceDescriptor *service) { // overload taking CallOptions as a param GenerateDocCommentClientMethod(out, method, false, true); out->Print( - "public virtual $returntype$ $methodname$($request_maybe$CallOptions " + "public virtual $returntype$ " + "$methodname$($request_maybe$grpc::CallOptions " "options)\n", "methodname", method_name, "request_maybe", GetMethodRequestParamMaybe(method), "returntype", @@ -587,13 +596,13 @@ void GenerateBindServiceMethod(Printer *out, const ServiceDescriptor *service) { "/// An object implementing the server-side" " handling logic.\n"); out->Print( - "public static ServerServiceDefinition BindService($implclass$ " + "public static grpc::ServerServiceDefinition BindService($implclass$ " "serviceImpl)\n", "implclass", GetServerClassName(service)); out->Print("{\n"); out->Indent(); - out->Print("return ServerServiceDefinition.CreateBuilder()\n"); + out->Print("return grpc::ServerServiceDefinition.CreateBuilder()\n"); out->Indent(); out->Indent(); 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.Threading;\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("namespace $namespace$ {\n", "namespace", GetFileNamespace(file));