diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/compiler/config.h | 10 | ||||
-rw-r--r-- | src/compiler/csharp_generator.cc | 40 | ||||
-rw-r--r-- | src/compiler/csharp_generator.h | 4 | ||||
-rw-r--r-- | src/compiler/csharp_plugin.cc | 24 | ||||
-rw-r--r-- | src/csharp/Grpc.Examples/MathGrpc.cs | 12 | ||||
-rw-r--r-- | src/csharp/Grpc.HealthCheck/HealthGrpc.cs | 12 | ||||
-rw-r--r-- | src/csharp/Grpc.IntegrationTesting/MetricsGrpc.cs | 12 | ||||
-rw-r--r-- | src/csharp/Grpc.IntegrationTesting/ServicesGrpc.cs | 24 | ||||
-rw-r--r-- | src/csharp/Grpc.IntegrationTesting/TestGrpc.cs | 36 |
9 files changed, 113 insertions, 61 deletions
diff --git a/src/compiler/config.h b/src/compiler/config.h index a534b119d2..d8b95818db 100644 --- a/src/compiler/config.h +++ b/src/compiler/config.h @@ -70,6 +70,11 @@ #define GRPC_CUSTOM_PLUGINMAIN ::google::protobuf::compiler::PluginMain #endif +#ifndef GRPC_CUSTOM_PARSEGENERATORPARAMETER +#include <google/protobuf/compiler/code_generator.h> +#define GRPC_CUSTOM_PARSEGENERATORPARAMETER ::google::protobuf::compiler::ParseGeneratorParameter +#endif + namespace grpc { namespace protobuf { typedef GRPC_CUSTOM_DESCRIPTOR Descriptor; @@ -85,6 +90,11 @@ static inline int PluginMain(int argc, char* argv[], const CodeGenerator* generator) { return GRPC_CUSTOM_PLUGINMAIN(argc, argv, generator); } +static inline void ParseGeneratorParameter(const string& parameter, + std::vector<std::pair<string, string> >* options) { + GRPC_CUSTOM_PARSEGENERATORPARAMETER(parameter, options); +} + } // namespace compiler namespace io { typedef GRPC_CUSTOM_PRINTER Printer; diff --git a/src/compiler/csharp_generator.cc b/src/compiler/csharp_generator.cc index 4def6c5e31..0d1404341d 100644 --- a/src/compiler/csharp_generator.cc +++ b/src/compiler/csharp_generator.cc @@ -123,6 +123,10 @@ std::string GetMethodRequestParamMaybe(const MethodDescriptor *method, return GetClassName(method->input_type()) + " request, "; } +std::string GetAccessLevel(bool internal_access) { + return internal_access ? "internal" : "public"; +} + std::string GetMethodReturnTypeClient(const MethodDescriptor *method) { switch (GetMethodType(method)) { case METHODTYPE_NO_STREAMING: @@ -527,8 +531,11 @@ void GenerateNewStubMethods(Printer* out, const ServiceDescriptor *service) { out->Print("\n"); } -void GenerateService(Printer* out, const ServiceDescriptor *service) { - out->Print("public static class $classname$\n", "classname", +void GenerateService(Printer* out, const ServiceDescriptor *service, + bool generate_client, bool generate_server, + bool internal_access) { + out->Print("$access_level$ static class $classname$\n", "access_level", + GetAccessLevel(internal_access), "classname", GetServiceClassName(service)); out->Print("{\n"); out->Indent(); @@ -542,13 +549,22 @@ void GenerateService(Printer* out, const ServiceDescriptor *service) { GenerateStaticMethodField(out, service->method(i)); } GenerateServiceDescriptorProperty(out, service); - GenerateClientInterface(out, service); - GenerateServerInterface(out, service); - GenerateServerClass(out, service); - GenerateClientStub(out, service); - GenerateBindServiceMethod(out, service, false); - GenerateBindServiceMethod(out, service, true); - GenerateNewStubMethods(out, service); + + if (generate_client) { + GenerateClientInterface(out, service); + } + if (generate_server) { + GenerateServerInterface(out, service); + GenerateServerClass(out, service); + } + if (generate_client) { + GenerateClientStub(out, service); + GenerateNewStubMethods(out, service); + } + if (generate_server) { + GenerateBindServiceMethod(out, service, false); + GenerateBindServiceMethod(out, service, true); + } out->Outdent(); out->Print("}\n"); @@ -556,7 +572,8 @@ void GenerateService(Printer* out, const ServiceDescriptor *service) { } // anonymous namespace -grpc::string GetServices(const FileDescriptor *file) { +grpc::string GetServices(const FileDescriptor *file, bool generate_client, + bool generate_server, bool internal_access) { grpc::string output; { // Scope the output stream so it closes and finalizes output to the string. @@ -584,7 +601,8 @@ grpc::string GetServices(const FileDescriptor *file) { out.Print("namespace $namespace$ {\n", "namespace", GetFileNamespace(file)); out.Indent(); for (int i = 0; i < file->service_count(); i++) { - GenerateService(&out, file->service(i)); + GenerateService(&out, file->service(i), generate_client, generate_server, + internal_access); } out.Outdent(); out.Print("}\n"); diff --git a/src/compiler/csharp_generator.h b/src/compiler/csharp_generator.h index 90eb7e2984..f0585af4fd 100644 --- a/src/compiler/csharp_generator.h +++ b/src/compiler/csharp_generator.h @@ -40,7 +40,9 @@ namespace grpc_csharp_generator { -grpc::string GetServices(const grpc::protobuf::FileDescriptor *file); +grpc::string GetServices(const grpc::protobuf::FileDescriptor *file, + bool generate_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 8b9395f9e2..5350e73f10 100644 --- a/src/compiler/csharp_plugin.cc +++ b/src/compiler/csharp_plugin.cc @@ -48,7 +48,29 @@ class CSharpGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { const grpc::string ¶meter, grpc::protobuf::compiler::GeneratorContext *context, grpc::string *error) const { - grpc::string code = grpc_csharp_generator::GetServices(file); + std::vector<std::pair<grpc::string, grpc::string> > options; + grpc::protobuf::compiler::ParseGeneratorParameter(parameter, &options); + + bool generate_client = true; + bool generate_server = 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_server") { + generate_server = false; + } else if (options[i].first == "internal_access") { + internal_access = true; + } else { + *error = "Unknown generator option: " + options[i].first; + return false; + } + } + + grpc::string code = grpc_csharp_generator::GetServices(file, + generate_client, + generate_server, + internal_access); if (code.size() == 0) { return true; // don't generate a file if there are no services } diff --git a/src/csharp/Grpc.Examples/MathGrpc.cs b/src/csharp/Grpc.Examples/MathGrpc.cs index 1a6482df90..edbce913b8 100644 --- a/src/csharp/Grpc.Examples/MathGrpc.cs +++ b/src/csharp/Grpc.Examples/MathGrpc.cs @@ -168,6 +168,12 @@ namespace Math { } } + // creates a new client + public static MathClient NewClient(Channel channel) + { + return new MathClient(channel); + } + // creates service definition that can be registered with a server #pragma warning disable 0618 public static ServerServiceDefinition BindService(IMath serviceImpl) @@ -192,12 +198,6 @@ namespace Math { .AddMethod(__Method_Sum, serviceImpl.Sum).Build(); } - // creates a new client - public static MathClient NewClient(Channel channel) - { - return new MathClient(channel); - } - } } #endregion diff --git a/src/csharp/Grpc.HealthCheck/HealthGrpc.cs b/src/csharp/Grpc.HealthCheck/HealthGrpc.cs index e7f779753d..e2cdabf011 100644 --- a/src/csharp/Grpc.HealthCheck/HealthGrpc.cs +++ b/src/csharp/Grpc.HealthCheck/HealthGrpc.cs @@ -97,6 +97,12 @@ namespace Grpc.Health.V1 { } } + // creates a new client + public static HealthClient NewClient(Channel channel) + { + return new HealthClient(channel); + } + // creates service definition that can be registered with a server #pragma warning disable 0618 public static ServerServiceDefinition BindService(IHealth serviceImpl) @@ -115,12 +121,6 @@ namespace Grpc.Health.V1 { .AddMethod(__Method_Check, serviceImpl.Check).Build(); } - // creates a new client - public static HealthClient NewClient(Channel channel) - { - return new HealthClient(channel); - } - } } #endregion diff --git a/src/csharp/Grpc.IntegrationTesting/MetricsGrpc.cs b/src/csharp/Grpc.IntegrationTesting/MetricsGrpc.cs index 11c1572c19..0f701a837f 100644 --- a/src/csharp/Grpc.IntegrationTesting/MetricsGrpc.cs +++ b/src/csharp/Grpc.IntegrationTesting/MetricsGrpc.cs @@ -121,6 +121,12 @@ namespace Grpc.Testing { } } + // creates a new client + public static MetricsServiceClient NewClient(Channel channel) + { + return new MetricsServiceClient(channel); + } + // creates service definition that can be registered with a server #pragma warning disable 0618 public static ServerServiceDefinition BindService(IMetricsService serviceImpl) @@ -141,12 +147,6 @@ namespace Grpc.Testing { .AddMethod(__Method_GetGauge, serviceImpl.GetGauge).Build(); } - // creates a new client - public static MetricsServiceClient NewClient(Channel channel) - { - return new MetricsServiceClient(channel); - } - } } #endregion diff --git a/src/csharp/Grpc.IntegrationTesting/ServicesGrpc.cs b/src/csharp/Grpc.IntegrationTesting/ServicesGrpc.cs index 18cf0672e3..3f07a7aeb6 100644 --- a/src/csharp/Grpc.IntegrationTesting/ServicesGrpc.cs +++ b/src/csharp/Grpc.IntegrationTesting/ServicesGrpc.cs @@ -120,6 +120,12 @@ namespace Grpc.Testing { } } + // creates a new client + public static BenchmarkServiceClient NewClient(Channel channel) + { + return new BenchmarkServiceClient(channel); + } + // creates service definition that can be registered with a server #pragma warning disable 0618 public static ServerServiceDefinition BindService(IBenchmarkService serviceImpl) @@ -140,12 +146,6 @@ namespace Grpc.Testing { .AddMethod(__Method_StreamingCall, serviceImpl.StreamingCall).Build(); } - // creates a new client - public static BenchmarkServiceClient NewClient(Channel channel) - { - return new BenchmarkServiceClient(channel); - } - } public static class WorkerService { @@ -320,6 +320,12 @@ namespace Grpc.Testing { } } + // creates a new client + public static WorkerServiceClient NewClient(Channel channel) + { + return new WorkerServiceClient(channel); + } + // creates service definition that can be registered with a server #pragma warning disable 0618 public static ServerServiceDefinition BindService(IWorkerService serviceImpl) @@ -344,12 +350,6 @@ namespace Grpc.Testing { .AddMethod(__Method_QuitWorker, serviceImpl.QuitWorker).Build(); } - // creates a new client - public static WorkerServiceClient NewClient(Channel channel) - { - return new WorkerServiceClient(channel); - } - } } #endregion diff --git a/src/csharp/Grpc.IntegrationTesting/TestGrpc.cs b/src/csharp/Grpc.IntegrationTesting/TestGrpc.cs index 3b915f6df1..4efd35f81f 100644 --- a/src/csharp/Grpc.IntegrationTesting/TestGrpc.cs +++ b/src/csharp/Grpc.IntegrationTesting/TestGrpc.cs @@ -227,6 +227,12 @@ namespace Grpc.Testing { } } + // creates a new client + public static TestServiceClient NewClient(Channel channel) + { + return new TestServiceClient(channel); + } + // creates service definition that can be registered with a server #pragma warning disable 0618 public static ServerServiceDefinition BindService(ITestService serviceImpl) @@ -255,12 +261,6 @@ namespace Grpc.Testing { .AddMethod(__Method_HalfDuplexCall, serviceImpl.HalfDuplexCall).Build(); } - // creates a new client - public static TestServiceClient NewClient(Channel channel) - { - return new TestServiceClient(channel); - } - } public static class UnimplementedService { @@ -350,6 +350,12 @@ namespace Grpc.Testing { } } + // creates a new client + public static UnimplementedServiceClient NewClient(Channel channel) + { + return new UnimplementedServiceClient(channel); + } + // creates service definition that can be registered with a server #pragma warning disable 0618 public static ServerServiceDefinition BindService(IUnimplementedService serviceImpl) @@ -368,12 +374,6 @@ namespace Grpc.Testing { .AddMethod(__Method_UnimplementedCall, serviceImpl.UnimplementedCall).Build(); } - // creates a new client - public static UnimplementedServiceClient NewClient(Channel channel) - { - return new UnimplementedServiceClient(channel); - } - } public static class ReconnectService { @@ -498,6 +498,12 @@ namespace Grpc.Testing { } } + // creates a new client + public static ReconnectServiceClient NewClient(Channel channel) + { + return new ReconnectServiceClient(channel); + } + // creates service definition that can be registered with a server #pragma warning disable 0618 public static ServerServiceDefinition BindService(IReconnectService serviceImpl) @@ -518,12 +524,6 @@ namespace Grpc.Testing { .AddMethod(__Method_Stop, serviceImpl.Stop).Build(); } - // creates a new client - public static ReconnectServiceClient NewClient(Channel channel) - { - return new ReconnectServiceClient(channel); - } - } } #endregion |