Merge pull request #6284 from jtattermusch/csharp_optional_client_codegen

Add new cmdline options to C# codegen.
pull/6329/head
Nicolas Noble 9 years ago
commit b143ca0f36
  1. 10
      src/compiler/config.h
  2. 40
      src/compiler/csharp_generator.cc
  3. 4
      src/compiler/csharp_generator.h
  4. 24
      src/compiler/csharp_plugin.cc
  5. 12
      src/csharp/Grpc.Examples/MathGrpc.cs
  6. 12
      src/csharp/Grpc.HealthCheck/HealthGrpc.cs
  7. 12
      src/csharp/Grpc.IntegrationTesting/MetricsGrpc.cs
  8. 24
      src/csharp/Grpc.IntegrationTesting/ServicesGrpc.cs
  9. 36
      src/csharp/Grpc.IntegrationTesting/TestGrpc.cs

@ -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;

@ -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");

@ -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

@ -48,7 +48,29 @@ class CSharpGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
const grpc::string &parameter,
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
}

@ -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

@ -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

@ -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

@ -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

@ -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

Loading…
Cancel
Save