From 25e3d26e8cb361692101555cdb199933079cd90f Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Fri, 26 Apr 2019 07:37:21 -0400 Subject: [PATCH] C# lite client codegen --- src/compiler/csharp_generator.cc | 116 ++++++++++++++++++------------- src/compiler/csharp_generator.h | 4 +- src/compiler/csharp_plugin.cc | 7 +- 3 files changed, 77 insertions(+), 50 deletions(-) diff --git a/src/compiler/csharp_generator.cc b/src/compiler/csharp_generator.cc index 778e5c39284..10737c5e8db 100644 --- a/src/compiler/csharp_generator.cc +++ b/src/compiler/csharp_generator.cc @@ -176,8 +176,9 @@ std::string GetServiceClassName(const ServiceDescriptor* service) { return service->name(); } -std::string GetClientClassName(const ServiceDescriptor* service) { - return service->name() + "Client"; +std::string GetClientClassName(const ServiceDescriptor* service, + bool lite_client) { + return service->name() + (lite_client ? "LiteClient" : "Client"); } std::string GetServerClassName(const ServiceDescriptor* service) { @@ -414,24 +415,34 @@ void GenerateServerClass(Printer* out, const ServiceDescriptor* service) { out->Print("\n"); } -void GenerateClientStub(Printer* out, const ServiceDescriptor* service) { - out->Print("/// Client for $servicename$\n", "servicename", - GetServiceClassName(service)); - out->Print("public partial class $name$ : grpc::ClientBase<$name$>\n", "name", - GetClientClassName(service)); +void GenerateClientStub(Printer* out, const ServiceDescriptor* service, + bool lite_client) { + if (!lite_client) { + out->Print("/// Client for $servicename$\n", + "servicename", GetServiceClassName(service)); + out->Print("public partial class $name$ : grpc::ClientBase<$name$>\n", + "name", GetClientClassName(service, lite_client)); + } else { + out->Print("/// Lite client for $servicename$\n", + "servicename", GetServiceClassName(service)); + out->Print("public partial class $name$ : grpc::LiteClientBase\n", "name", + GetClientClassName(service, lite_client)); + } out->Print("{\n"); out->Indent(); // constructors - out->Print( - "/// Creates a new client for $servicename$\n" - "/// The channel to use to make remote " - "calls.\n", - "servicename", GetServiceClassName(service)); - out->Print("public $name$(grpc::Channel channel) : base(channel)\n", "name", - GetClientClassName(service)); - out->Print("{\n"); - out->Print("}\n"); + if (!lite_client) { + out->Print( + "/// Creates a new client for $servicename$\n" + "/// The channel to use to make remote " + "calls.\n", + "servicename", GetServiceClassName(service)); + out->Print("public $name$(grpc::Channel channel) : base(channel)\n", "name", + GetClientClassName(service, lite_client)); + out->Print("{\n"); + out->Print("}\n"); + } out->Print( "/// Creates a new client for $servicename$ that uses a custom " "CallInvoker.\n" @@ -440,26 +451,30 @@ void GenerateClientStub(Printer* out, const ServiceDescriptor* service) { "servicename", GetServiceClassName(service)); out->Print( "public $name$(grpc::CallInvoker callInvoker) : base(callInvoker)\n", - "name", GetClientClassName(service)); + "name", GetClientClassName(service, lite_client)); out->Print("{\n"); out->Print("}\n"); out->Print( "/// Protected parameterless constructor to allow creation" " of test doubles.\n"); out->Print("protected $name$() : base()\n", "name", - GetClientClassName(service)); + GetClientClassName(service, lite_client)); out->Print("{\n"); out->Print("}\n"); - out->Print( - "/// Protected constructor to allow creation of configured " - "clients.\n" - "/// The client configuration.\n"); - out->Print( - "protected $name$(ClientBaseConfiguration configuration)" - " : base(configuration)\n", - "name", GetClientClassName(service)); - out->Print("{\n"); - out->Print("}\n\n"); + if (!lite_client) { + out->Print( + "/// Protected constructor to allow creation of configured " + "clients.\n" + "/// The client " + "configuration.\n"); + out->Print( + "protected $name$(ClientBaseConfiguration configuration)" + " : base(configuration)\n", + "name", GetClientClassName(service, lite_client)); + out->Print("{\n"); + out->Print("}\n"); + } + out->Print("\n"); for (int i = 0; i < service->method_count(); i++) { const MethodDescriptor* method = service->method(i); @@ -577,19 +592,21 @@ void GenerateClientStub(Printer* out, const ServiceDescriptor* service) { } // override NewInstance method - out->Print( - "/// Creates a new instance of client from given " - "ClientBaseConfiguration.\n"); - out->Print( - "protected override $name$ NewInstance(ClientBaseConfiguration " - "configuration)\n", - "name", GetClientClassName(service)); - out->Print("{\n"); - out->Indent(); - out->Print("return new $name$(configuration);\n", "name", - GetClientClassName(service)); - out->Outdent(); - out->Print("}\n"); + if (!lite_client) { + out->Print( + "/// Creates a new instance of client from given " + "ClientBaseConfiguration.\n"); + out->Print( + "protected override $name$ NewInstance(ClientBaseConfiguration " + "configuration)\n", + "name", GetClientClassName(service, lite_client)); + out->Print("{\n"); + out->Indent(); + out->Print("return new $name$(configuration);\n", "name", + GetClientClassName(service, lite_client)); + out->Outdent(); + out->Print("}\n"); + } out->Outdent(); out->Print("}\n"); @@ -670,8 +687,8 @@ void GenerateBindServiceWithBinderMethod(Printer* out, } void GenerateService(Printer* out, const ServiceDescriptor* service, - bool generate_client, bool generate_server, - bool internal_access) { + bool generate_client, bool generate_lite_client, + bool generate_server, bool internal_access) { GenerateDocCommentBody(out, service); out->Print("$access_level$ static partial class $classname$\n", "access_level", GetAccessLevel(internal_access), "classname", @@ -693,8 +710,12 @@ void GenerateService(Printer* out, const ServiceDescriptor* service, GenerateServerClass(out, service); } if (generate_client) { - GenerateClientStub(out, service); + GenerateClientStub(out, service, false); } + if (generate_lite_client) { + GenerateClientStub(out, service, true); + } + if (generate_server) { GenerateBindServiceMethod(out, service); GenerateBindServiceWithBinderMethod(out, service); @@ -707,7 +728,8 @@ void GenerateService(Printer* out, const ServiceDescriptor* service, } // anonymous namespace grpc::string GetServices(const FileDescriptor* file, bool generate_client, - bool generate_server, bool internal_access) { + bool generate_lite_client, bool generate_server, + bool internal_access) { grpc::string output; { // Scope the output stream so it closes and finalizes output to the string. @@ -748,8 +770,8 @@ grpc::string GetServices(const FileDescriptor* file, bool generate_client, out.Indent(); } for (int i = 0; i < file->service_count(); i++) { - GenerateService(&out, file->service(i), generate_client, generate_server, - internal_access); + GenerateService(&out, file->service(i), generate_client, + generate_lite_client, generate_server, internal_access); } if (file_namespace != "") { out.Outdent(); diff --git a/src/compiler/csharp_generator.h b/src/compiler/csharp_generator.h index fd36e11851b..842d136494c 100644 --- a/src/compiler/csharp_generator.h +++ b/src/compiler/csharp_generator.h @@ -26,8 +26,8 @@ namespace grpc_csharp_generator { grpc::string GetServices(const grpc::protobuf::FileDescriptor* file, - bool generate_client, bool generate_server, - bool internal_access); + bool generate_client, bool generate_lite_client, + bool generate_server, bool internal_access); } // namespace grpc_csharp_generator diff --git a/src/compiler/csharp_plugin.cc b/src/compiler/csharp_plugin.cc index 5f13aa6749e..a495f876792 100644 --- a/src/compiler/csharp_plugin.cc +++ b/src/compiler/csharp_plugin.cc @@ -38,10 +38,14 @@ class CSharpGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { bool generate_client = true; bool generate_server = true; + bool generate_lite_client = true; bool internal_access = false; for (size_t i = 0; i < options.size(); i++) { if (options[i].first == "no_client") { generate_client = false; + } else if (options[i].first == "no_lite_client") { + // TODO: better option + generate_lite_client = false; } else if (options[i].first == "no_server") { generate_server = false; } else if (options[i].first == "internal_access") { @@ -53,7 +57,8 @@ class CSharpGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { } grpc::string code = grpc_csharp_generator::GetServices( - file, generate_client, generate_server, internal_access); + file, generate_client, generate_lite_client, generate_server, + internal_access); if (code.size() == 0) { return true; // don't generate a file if there are no services }