diff options
Diffstat (limited to 'src/compiler')
-rw-r--r-- | src/compiler/config.h | 11 | ||||
-rw-r--r-- | src/compiler/cpp_generator_helpers.h | 7 | ||||
-rw-r--r-- | src/compiler/cpp_plugin.cc | 2 | ||||
-rw-r--r-- | src/compiler/csharp_generator.cc | 119 | ||||
-rw-r--r-- | src/compiler/csharp_generator_helpers.h | 7 | ||||
-rw-r--r-- | src/compiler/generator_helpers.h | 8 | ||||
-rw-r--r-- | src/compiler/node_generator.cc | 103 | ||||
-rw-r--r-- | src/compiler/node_generator.h | 6 | ||||
-rw-r--r-- | src/compiler/node_generator_helpers.h | 7 | ||||
-rw-r--r-- | src/compiler/node_plugin.cc | 8 | ||||
-rw-r--r-- | src/compiler/objective_c_generator.cc | 4 | ||||
-rw-r--r-- | src/compiler/python_generator.cc | 110 | ||||
-rw-r--r-- | src/compiler/ruby_generator.cc | 18 | ||||
-rw-r--r-- | src/compiler/ruby_generator_helpers-inl.h | 8 |
14 files changed, 170 insertions, 248 deletions
diff --git a/src/compiler/config.h b/src/compiler/config.h index d8b95818db..f2abb85186 100644 --- a/src/compiler/config.h +++ b/src/compiler/config.h @@ -34,8 +34,7 @@ #ifndef SRC_COMPILER_CONFIG_H #define SRC_COMPILER_CONFIG_H -#include <grpc++/support/config.h> -#include <grpc++/support/config_protobuf.h> +#include <grpc++/impl/codegen/config_protobuf.h> #ifndef GRPC_CUSTOM_DESCRIPTOR #include <google/protobuf/descriptor.h> @@ -75,7 +74,15 @@ #define GRPC_CUSTOM_PARSEGENERATORPARAMETER ::google::protobuf::compiler::ParseGeneratorParameter #endif +#ifndef GRPC_CUSTOM_STRING +#include <string> +#define GRPC_CUSTOM_STRING std::string +#endif + namespace grpc { + +typedef GRPC_CUSTOM_STRING string; + namespace protobuf { typedef GRPC_CUSTOM_DESCRIPTOR Descriptor; typedef GRPC_CUSTOM_FILEDESCRIPTOR FileDescriptor; diff --git a/src/compiler/cpp_generator_helpers.h b/src/compiler/cpp_generator_helpers.h index be68cbe695..87e278f1b9 100644 --- a/src/compiler/cpp_generator_helpers.h +++ b/src/compiler/cpp_generator_helpers.h @@ -65,6 +65,13 @@ inline grpc::string ClassName(const grpc::protobuf::Descriptor *descriptor, } } +// Get leading or trailing comments in a string. Comment lines start with "// ". +// Leading detached comments are put in in front of leading comments. +template <typename DescriptorType> +inline grpc::string GetCppComments(const DescriptorType *desc, bool leading) { + return grpc_generator::GetPrefixedComments(desc, leading, "//"); +} + } // namespace grpc_cpp_generator #endif // GRPC_INTERNAL_COMPILER_CPP_GENERATOR_HELPERS_H diff --git a/src/compiler/cpp_plugin.cc b/src/compiler/cpp_plugin.cc index 0ec183e474..fc0296cd28 100644 --- a/src/compiler/cpp_plugin.cc +++ b/src/compiler/cpp_plugin.cc @@ -43,7 +43,7 @@ #include "src/compiler/cpp_generator_helpers.h" #include "src/compiler/generator_helpers.h" -using grpc_generator::GetCppComments; +using grpc_cpp_generator::GetCppComments; class ProtoBufMethod : public grpc_cpp_generator::Method { public: diff --git a/src/compiler/csharp_generator.cc b/src/compiler/csharp_generator.cc index 29c359c539..fc8feaf0fc 100644 --- a/src/compiler/csharp_generator.cc +++ b/src/compiler/csharp_generator.cc @@ -52,7 +52,6 @@ using grpc::protobuf::MethodDescriptor; using grpc::protobuf::io::Printer; using grpc::protobuf::io::StringOutputStream; using grpc_generator::MethodType; -using grpc_generator::GetCppComments; using grpc_generator::GetMethodType; using grpc_generator::METHODTYPE_NO_STREAMING; using grpc_generator::METHODTYPE_CLIENT_STREAMING; @@ -120,18 +119,10 @@ std::string GetServiceClassName(const ServiceDescriptor* service) { return service->name(); } -std::string GetClientInterfaceName(const ServiceDescriptor* service) { - return "I" + service->name() + "Client"; -} - std::string GetClientClassName(const ServiceDescriptor* service) { return service->name() + "Client"; } -std::string GetServerInterfaceName(const ServiceDescriptor* service) { - return "I" + service->name(); -} - std::string GetServerClassName(const ServiceDescriptor* service) { return service->name() + "Base"; } @@ -303,86 +294,6 @@ void GenerateServiceDescriptorProperty(Printer* out, const ServiceDescriptor *se out->Print("\n"); } -void GenerateClientInterface(Printer* out, const ServiceDescriptor *service) { - out->Print("/// <summary>Client for $servicename$</summary>\n", - "servicename", GetServiceClassName(service)); - out->Print("[System.Obsolete(\"Client side interfaced will be removed " - "in the next release. Use client class directly.\")]\n"); - out->Print("public interface $name$\n", "name", - GetClientInterfaceName(service)); - out->Print("{\n"); - out->Indent(); - for (int i = 0; i < service->method_count(); i++) { - const MethodDescriptor *method = service->method(i); - MethodType method_type = GetMethodType(method); - - if (method_type == METHODTYPE_NO_STREAMING) { - // unary calls have an extra synchronous stub method - GenerateDocCommentBody(out, method); - out->Print( - "$response$ $methodname$($request$ request, Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken));\n", - "methodname", method->name(), "request", - GetClassName(method->input_type()), "response", - GetClassName(method->output_type())); - - // overload taking CallOptions as a param - GenerateDocCommentBody(out, method); - out->Print( - "$response$ $methodname$($request$ request, CallOptions options);\n", - "methodname", method->name(), "request", - GetClassName(method->input_type()), "response", - GetClassName(method->output_type())); - } - - std::string method_name = method->name(); - if (method_type == METHODTYPE_NO_STREAMING) { - method_name += "Async"; // prevent name clash with synchronous method. - } - GenerateDocCommentBody(out, method); - out->Print( - "$returntype$ $methodname$($request_maybe$Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken));\n", - "methodname", method_name, "request_maybe", - GetMethodRequestParamMaybe(method), "returntype", - GetMethodReturnTypeClient(method)); - - // overload taking CallOptions as a param - GenerateDocCommentBody(out, method); - out->Print( - "$returntype$ $methodname$($request_maybe$CallOptions options);\n", - "methodname", method_name, "request_maybe", - GetMethodRequestParamMaybe(method), "returntype", - GetMethodReturnTypeClient(method)); - } - out->Outdent(); - out->Print("}\n"); - out->Print("\n"); -} - -void GenerateServerInterface(Printer* out, const ServiceDescriptor *service) { - out->Print("/// <summary>Interface of server-side implementations of $servicename$</summary>\n", - "servicename", GetServiceClassName(service)); - out->Print("[System.Obsolete(\"Service implementations should inherit" - " from the generated abstract base class instead.\")]\n"); - out->Print("public interface $name$\n", "name", - GetServerInterfaceName(service)); - out->Print("{\n"); - out->Indent(); - for (int i = 0; i < service->method_count(); i++) { - const MethodDescriptor *method = service->method(i); - GenerateDocCommentBody(out, method); - out->Print( - "$returntype$ $methodname$($request$$response_stream_maybe$, " - "ServerCallContext context);\n", - "methodname", method->name(), "returntype", - GetMethodReturnTypeServer(method), "request", - GetMethodRequestParamServer(method), "response_stream_maybe", - GetMethodResponseStreamMaybe(method)); - } - out->Outdent(); - out->Print("}\n"); - out->Print("\n"); -} - void GenerateServerClass(Printer* out, const ServiceDescriptor *service) { out->Print("/// <summary>Base class for server-side implementations of $servicename$</summary>\n", "servicename", GetServiceClassName(service)); @@ -415,12 +326,9 @@ void GenerateServerClass(Printer* out, const ServiceDescriptor *service) { void GenerateClientStub(Printer* out, const ServiceDescriptor *service) { out->Print("/// <summary>Client for $servicename$</summary>\n", "servicename", GetServiceClassName(service)); - out->Print("#pragma warning disable 0618\n"); out->Print( - "public class $name$ : ClientBase<$name$>, $interface$\n", - "name", GetClientClassName(service), - "interface", GetClientInterfaceName(service)); - out->Print("#pragma warning restore 0618\n"); + "public class $name$ : ClientBase<$name$>\n", + "name", GetClientClassName(service)); out->Print("{\n"); out->Indent(); @@ -547,22 +455,16 @@ void GenerateClientStub(Printer* out, const ServiceDescriptor *service) { out->Print("\n"); } -void GenerateBindServiceMethod(Printer* out, const ServiceDescriptor *service, - bool use_server_class) { +void GenerateBindServiceMethod(Printer* out, const ServiceDescriptor *service) { out->Print( "/// <summary>Creates service definition that can be registered with a server</summary>\n"); - out->Print("#pragma warning disable 0618\n"); out->Print( - "public static ServerServiceDefinition BindService($interface$ serviceImpl)\n", - "interface", use_server_class ? GetServerClassName(service) : - GetServerInterfaceName(service)); - out->Print("#pragma warning restore 0618\n"); + "public static ServerServiceDefinition BindService($implclass$ serviceImpl)\n", + "implclass", GetServerClassName(service)); out->Print("{\n"); out->Indent(); - out->Print( - "return ServerServiceDefinition.CreateBuilder($servicenamefield$)\n", - "servicenamefield", GetServiceNameFieldName()); + out->Print("return ServerServiceDefinition.CreateBuilder()\n"); out->Indent(); out->Indent(); for (int i = 0; i < service->method_count(); i++) { @@ -617,11 +519,7 @@ void GenerateService(Printer* out, const ServiceDescriptor *service, } GenerateServiceDescriptorProperty(out, service); - if (generate_client) { - GenerateClientInterface(out, service); - } if (generate_server) { - GenerateServerInterface(out, service); GenerateServerClass(out, service); } if (generate_client) { @@ -629,8 +527,7 @@ void GenerateService(Printer* out, const ServiceDescriptor *service, GenerateNewStubMethods(out, service); } if (generate_server) { - GenerateBindServiceMethod(out, service, false); - GenerateBindServiceMethod(out, service, true); + GenerateBindServiceMethod(out, service); } out->Outdent(); @@ -659,7 +556,7 @@ grpc::string GetServices(const FileDescriptor *file, bool generate_client, out.Print("// source: $filename$\n", "filename", file->name()); // use C++ style as there are no file-level XML comments in .NET - grpc::string leading_comments = GetCppComments(file, true); + grpc::string leading_comments = GetCsharpComments(file, true); if (!leading_comments.empty()) { out.Print("// Original file comments:\n"); out.Print(leading_comments.c_str()); diff --git a/src/compiler/csharp_generator_helpers.h b/src/compiler/csharp_generator_helpers.h index 5639ea058b..9bdf6fb535 100644 --- a/src/compiler/csharp_generator_helpers.h +++ b/src/compiler/csharp_generator_helpers.h @@ -45,6 +45,13 @@ inline bool ServicesFilename(const grpc::protobuf::FileDescriptor *file, return true; } +// Get leading or trailing comments in a string. Comment lines start with "// ". +// Leading detached comments are put in in front of leading comments. +template <typename DescriptorType> +inline grpc::string GetCsharpComments(const DescriptorType *desc, bool leading) { + return grpc_generator::GetPrefixedComments(desc, leading, "//"); +} + } // namespace grpc_csharp_generator #endif // GRPC_INTERNAL_COMPILER_CSHARP_GENERATOR_HELPERS_H diff --git a/src/compiler/generator_helpers.h b/src/compiler/generator_helpers.h index bd077cf798..53391bc41a 100644 --- a/src/compiler/generator_helpers.h +++ b/src/compiler/generator_helpers.h @@ -265,10 +265,10 @@ inline grpc::string GenerateCommentsWithPrefix( return oss.str(); } -// Get leading or trailing comments in a string. Comment lines start with "// ". -// Leading detached comments are put in in front of leading comments. template <typename DescriptorType> -inline grpc::string GetCppComments(const DescriptorType *desc, bool leading) { +inline grpc::string GetPrefixedComments(const DescriptorType *desc, + bool leading, + const grpc::string &prefix) { std::vector<grpc::string> out; if (leading) { grpc_generator::GetComment( @@ -281,7 +281,7 @@ inline grpc::string GetCppComments(const DescriptorType *desc, bool leading) { grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_TRAILING, &out); } - return GenerateCommentsWithPrefix(out, "//"); + return GenerateCommentsWithPrefix(out, prefix); } } // namespace grpc_generator diff --git a/src/compiler/node_generator.cc b/src/compiler/node_generator.cc index 822622cccf..986b97c26e 100644 --- a/src/compiler/node_generator.cc +++ b/src/compiler/node_generator.cc @@ -181,62 +181,67 @@ void PrintMethod(const MethodDescriptor *method, Printer *out) { // Prints out the service descriptor object void PrintService(const ServiceDescriptor *service, Printer *out) { map<grpc::string, grpc::string> template_vars; + out->Print(GetNodeComments(service, true).c_str()); template_vars["name"] = service->name(); out->Print(template_vars, "var $name$Service = exports.$name$Service = {\n"); out->Indent(); for (int i = 0; i < service->method_count(); i++) { grpc::string method_name = grpc_generator::LowercaseFirstLetter( service->method(i)->name()); + out->Print(GetNodeComments(service->method(i), true).c_str()); out->Print("$method_name$: ", "method_name", method_name); PrintMethod(service->method(i), out); out->Print(",\n"); + out->Print(GetNodeComments(service->method(i), false).c_str()); } out->Outdent(); out->Print("};\n\n"); out->Print(template_vars, "exports.$name$Client = " "grpc.makeGenericClientConstructor($name$Service);\n"); + out->Print(GetNodeComments(service, false).c_str()); } -} - -grpc::string GetImports(const FileDescriptor *file) { - grpc::string output; - { - StringOutputStream output_stream(&output); - Printer out(&output_stream, '$'); - - if (file->service_count() == 0) { - return output; - } - - out.Print("// GENERATED CODE -- DO NOT EDIT!\n\n"); +void PrintImports(const FileDescriptor *file, Printer *out) { + out->Print("var grpc = require('grpc');\n"); + if (file->message_type_count() > 0) { + grpc::string file_path = GetRelativePath(file->name(), + GetJSMessageFilename( + file->name())); + out->Print("var $module_alias$ = require('$file_path$');\n", + "module_alias", ModuleAlias(file->name()), + "file_path", file_path); + } - out.Print("'use strict';\n"); + for (int i = 0; i < file->dependency_count(); i++) { + grpc::string file_path = GetRelativePath( + file->name(), GetJSMessageFilename(file->dependency(i)->name())); + out->Print("var $module_alias$ = require('$file_path$');\n", + "module_alias", ModuleAlias(file->dependency(i)->name()), + "file_path", file_path); + } + out->Print("\n"); +} - out.Print("var grpc = require('grpc');\n"); - if (file->message_type_count() > 0) { - grpc::string file_path = GetRelativePath(file->name(), - GetJSMessageFilename( - file->name())); - out.Print("var $module_alias$ = require('$file_path$');\n", - "module_alias", ModuleAlias(file->name()), - "file_path", file_path); - } +void PrintTransformers(const FileDescriptor *file, Printer *out) { + map<grpc::string, const Descriptor*> messages = GetAllMessages(file); + for (std::map<grpc::string, const Descriptor*>::iterator it = + messages.begin(); + it != messages.end(); it++) { + PrintMessageTransformer(it->second, out); + } + out->Print("\n"); +} - for (int i = 0; i < file->dependency_count(); i++) { - grpc::string file_path = GetRelativePath( - file->name(), GetJSMessageFilename(file->dependency(i)->name())); - out.Print("var $module_alias$ = require('$file_path$');\n", - "module_alias", ModuleAlias(file->dependency(i)->name()), - "file_path", file_path); - } - out.Print("\n"); +void PrintServices(const FileDescriptor *file, Printer *out) { + for (int i = 0; i < file->service_count(); i++) { + PrintService(file->service(i), out); } - return output; } -grpc::string GetTransformers(const FileDescriptor *file) { +} + +grpc::string GenerateFile(const FileDescriptor *file) { grpc::string output; { StringOutputStream output_stream(&output); @@ -245,31 +250,23 @@ grpc::string GetTransformers(const FileDescriptor *file) { if (file->service_count() == 0) { return output; } + out.Print("// GENERATED CODE -- DO NOT EDIT!\n\n"); - map<grpc::string, const Descriptor*> messages = GetAllMessages(file); - for (std::map<grpc::string, const Descriptor*>::iterator it = - messages.begin(); - it != messages.end(); it++) { - PrintMessageTransformer(it->second, &out); + grpc::string leading_comments = GetNodeComments(file, true); + if (!leading_comments.empty()) { + out.Print("// Original file comments:\n"); + out.Print(leading_comments.c_str()); } - out.Print("\n"); - } - return output; -} -grpc::string GetServices(const FileDescriptor *file) { - grpc::string output; - { - StringOutputStream output_stream(&output); - Printer out(&output_stream, '$'); + out.Print("'use strict';\n"); - if (file->service_count() == 0) { - return output; - } + PrintImports(file, &out); - for (int i = 0; i < file->service_count(); i++) { - PrintService(file->service(i), &out); - } + PrintTransformers(file, &out); + + PrintServices(file, &out); + + out.Print(GetNodeComments(file, false).c_str()); } return output; } diff --git a/src/compiler/node_generator.h b/src/compiler/node_generator.h index 249a0d011f..d7765e2d28 100644 --- a/src/compiler/node_generator.h +++ b/src/compiler/node_generator.h @@ -38,11 +38,7 @@ namespace grpc_node_generator { -grpc::string GetImports(const grpc::protobuf::FileDescriptor *file); - -grpc::string GetTransformers(const grpc::protobuf::FileDescriptor *file); - -grpc::string GetServices(const grpc::protobuf::FileDescriptor *file); +grpc::string GenerateFile(const grpc::protobuf::FileDescriptor *file); } // namespace grpc_node_generator diff --git a/src/compiler/node_generator_helpers.h b/src/compiler/node_generator_helpers.h index f41a2bcf59..5862772841 100644 --- a/src/compiler/node_generator_helpers.h +++ b/src/compiler/node_generator_helpers.h @@ -45,6 +45,13 @@ inline grpc::string GetJSServiceFilename(const grpc::string& filename) { return grpc_generator::StripProto(filename) + "_grpc_pb.js"; } +// Get leading or trailing comments in a string. Comment lines start with "// ". +// Leading detached comments are put in in front of leading comments. +template <typename DescriptorType> +inline grpc::string GetNodeComments(const DescriptorType *desc, bool leading) { + return grpc_generator::GetPrefixedComments(desc, leading, "//"); +} + } // namespace grpc_node_generator #endif // GRPC_INTERNAL_COMPILER_NODE_GENERATOR_HELPERS_H diff --git a/src/compiler/node_plugin.cc b/src/compiler/node_plugin.cc index ac5ced3558..39dfa77b8d 100644 --- a/src/compiler/node_plugin.cc +++ b/src/compiler/node_plugin.cc @@ -39,10 +39,8 @@ #include "src/compiler/node_generator.h" #include "src/compiler/node_generator_helpers.h" -using grpc_node_generator::GetImports; +using grpc_node_generator::GenerateFile; using grpc_node_generator::GetJSServiceFilename; -using grpc_node_generator::GetServices; -using grpc_node_generator::GetTransformers; class NodeGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { public: @@ -53,9 +51,7 @@ class NodeGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { const grpc::string ¶meter, grpc::protobuf::compiler::GeneratorContext *context, grpc::string *error) const { - grpc::string code = GetImports(file) + - GetTransformers(file) + - GetServices(file); + grpc::string code = GenerateFile(file); if (code.size() == 0) { return true; } diff --git a/src/compiler/objective_c_generator.cc b/src/compiler/objective_c_generator.cc index 465491e385..71a674174d 100644 --- a/src/compiler/objective_c_generator.cc +++ b/src/compiler/objective_c_generator.cc @@ -94,7 +94,7 @@ void PrintSimpleSignature(Printer *printer, const MethodDescriptor *method, void PrintAdvancedSignature(Printer *printer, const MethodDescriptor *method, map< ::grpc::string, ::grpc::string> vars) { vars["method_name"] = "RPCTo" + vars["method_name"]; - vars["return_type"] = "ProtoRPC *"; + vars["return_type"] = "GRPCProtoCall *"; PrintMethodSignature(printer, method, vars); } @@ -199,7 +199,7 @@ void PrintMethodImplementations(Printer *printer, " marshalling and parsing.\n"); printer.Print(vars, "@interface $service_class$ :" - " ProtoService<$service_class$>\n"); + " GRPCProtoService<$service_class$>\n"); printer.Print( "- (instancetype)initWithHost:(NSString *)host" " NS_DESIGNATED_INITIALIZER;\n"); diff --git a/src/compiler/python_generator.cc b/src/compiler/python_generator.cc index 8e76e6dce6..cd5ddd8832 100644 --- a/src/compiler/python_generator.cc +++ b/src/compiler/python_generator.cc @@ -147,7 +147,8 @@ class IndentScope { // END FORMATTING BOILERPLATE // //////////////////////////////// -// TODO(protobuf team): Export `ModuleName` from protobuf's +// TODO(https://github.com/google/protobuf/issues/888): +// Export `ModuleName` from protobuf's // `src/google/protobuf/compiler/python/python_generator.cc` file. grpc::string ModuleName(const grpc::string& filename) { grpc::string basename = StripProto(filename); @@ -156,8 +157,23 @@ grpc::string ModuleName(const grpc::string& filename) { return basename + "_pb2"; } +// TODO(https://github.com/google/protobuf/issues/888): +// Export `ModuleAlias` from protobuf's +// `src/google/protobuf/compiler/python/python_generator.cc` file. +grpc::string ModuleAlias(const grpc::string& filename) { + grpc::string module_name = ModuleName(filename); + // We can't have dots in the module name, so we replace each with _dot_. + // But that could lead to a collision between a.b and a_dot_b, so we also + // duplicate each underscore. + module_name = StringReplace(module_name, "_", "__"); + module_name = StringReplace(module_name, ".", "_dot_"); + return module_name; +} + + bool GetModuleAndMessagePath(const Descriptor* type, - pair<grpc::string, grpc::string>* out) { + const ServiceDescriptor* service, + grpc::string* out) { const Descriptor* path_elem_type = type; vector<const Descriptor*> message_path; do { @@ -170,7 +186,9 @@ bool GetModuleAndMessagePath(const Descriptor* type, file_name.find_last_of(".proto") == file_name.size() - 1)) { return false; } - grpc::string module = ModuleName(file_name); + grpc::string service_file_name = service->file()->name(); + grpc::string module = service_file_name == file_name ? + "" : ModuleAlias(file_name) + "."; grpc::string message_type; for (auto path_iter = message_path.rbegin(); path_iter != message_path.rend(); ++path_iter) { @@ -178,7 +196,7 @@ bool GetModuleAndMessagePath(const Descriptor* type, } // no pop_back prior to C++11 message_type.resize(message_type.size() - 1); - *out = make_pair(module, message_type); + *out = module + message_type; return true; } @@ -210,7 +228,7 @@ static void PrintAllComments(const DescriptorType* desc, Printer* printer) { bool PrintBetaServicer(const ServiceDescriptor* service, Printer* out) { - out->Print("\n"); + out->Print("\n\n"); out->Print("class Beta$Service$Servicer(object):\n", "Service", service->name()); { @@ -234,7 +252,7 @@ bool PrintBetaServicer(const ServiceDescriptor* service, bool PrintBetaStub(const ServiceDescriptor* service, Printer* out) { - out->Print("\n"); + out->Print("\n\n"); out->Print("class Beta$Service$Stub(object):\n", "Service", service->name()); { IndentScope raii_class_indent(out); @@ -244,7 +262,7 @@ bool PrintBetaStub(const ServiceDescriptor* service, grpc::string arg_name = meth->client_streaming() ? "request_iterator" : "request"; auto methdict = ListToDict({"Method", meth->name(), "ArgName", arg_name}); - out->Print(methdict, "def $Method$(self, $ArgName$, timeout):\n"); + out->Print(methdict, "def $Method$(self, $ArgName$, timeout, metadata=None, with_call=False, protocol_options=None):\n"); { IndentScope raii_method_indent(out); PrintAllComments(meth, out); @@ -260,38 +278,31 @@ bool PrintBetaStub(const ServiceDescriptor* service, bool PrintBetaServerFactory(const grpc::string& package_qualified_service_name, const ServiceDescriptor* service, Printer* out) { - out->Print("\n"); + out->Print("\n\n"); out->Print("def beta_create_$Service$_server(servicer, pool=None, " "pool_size=None, default_timeout=None, maximum_timeout=None):\n", "Service", service->name()); { IndentScope raii_create_server_indent(out); map<grpc::string, grpc::string> method_implementation_constructors; - map<grpc::string, pair<grpc::string, grpc::string>> - input_message_modules_and_classes; - map<grpc::string, pair<grpc::string, grpc::string>> - output_message_modules_and_classes; + map<grpc::string, grpc::string> input_message_modules_and_classes; + map<grpc::string, grpc::string> output_message_modules_and_classes; for (int i = 0; i < service->method_count(); ++i) { const MethodDescriptor* method = service->method(i); const grpc::string method_implementation_constructor = grpc::string(method->client_streaming() ? "stream_" : "unary_") + grpc::string(method->server_streaming() ? "stream_" : "unary_") + "inline"; - pair<grpc::string, grpc::string> input_message_module_and_class; - if (!GetModuleAndMessagePath(method->input_type(), + grpc::string input_message_module_and_class; + if (!GetModuleAndMessagePath(method->input_type(), service, &input_message_module_and_class)) { return false; } - pair<grpc::string, grpc::string> output_message_module_and_class; - if (!GetModuleAndMessagePath(method->output_type(), + grpc::string output_message_module_and_class; + if (!GetModuleAndMessagePath(method->output_type(), service, &output_message_module_and_class)) { return false; } - // Import the modules that define the messages used in RPCs. - out->Print("import $Module$\n", "Module", - input_message_module_and_class.first); - out->Print("import $Module$\n", "Module", - output_message_module_and_class.first); method_implementation_constructors.insert( make_pair(method->name(), method_implementation_constructor)); input_message_modules_and_classes.insert( @@ -307,13 +318,11 @@ bool PrintBetaServerFactory(const grpc::string& package_qualified_service_name, name_and_input_module_class_pair++) { IndentScope raii_indent(out); out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " - "$InputTypeModule$.$InputTypeClass$.FromString,\n", + "$InputTypeModuleAndClass$.FromString,\n", "PackageQualifiedServiceName", package_qualified_service_name, "MethodName", name_and_input_module_class_pair->first, - "InputTypeModule", - name_and_input_module_class_pair->second.first, - "InputTypeClass", - name_and_input_module_class_pair->second.second); + "InputTypeModuleAndClass", + name_and_input_module_class_pair->second); } out->Print("}\n"); out->Print("response_serializers = {\n"); @@ -324,13 +333,11 @@ bool PrintBetaServerFactory(const grpc::string& package_qualified_service_name, name_and_output_module_class_pair++) { IndentScope raii_indent(out); out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " - "$OutputTypeModule$.$OutputTypeClass$.SerializeToString,\n", + "$OutputTypeModuleAndClass$.SerializeToString,\n", "PackageQualifiedServiceName", package_qualified_service_name, "MethodName", name_and_output_module_class_pair->first, - "OutputTypeModule", - name_and_output_module_class_pair->second.first, - "OutputTypeClass", - name_and_output_module_class_pair->second.second); + "OutputTypeModuleAndClass", + name_and_output_module_class_pair->second); } out->Print("}\n"); out->Print("method_implementations = {\n"); @@ -366,37 +373,30 @@ bool PrintBetaStubFactory(const grpc::string& package_qualified_service_name, map<grpc::string, grpc::string> dict = ListToDict({ "Service", service->name(), }); - out->Print("\n"); + out->Print("\n\n"); out->Print(dict, "def beta_create_$Service$_stub(channel, host=None," " metadata_transformer=None, pool=None, pool_size=None):\n"); { IndentScope raii_create_server_indent(out); map<grpc::string, grpc::string> method_cardinalities; - map<grpc::string, pair<grpc::string, grpc::string>> - input_message_modules_and_classes; - map<grpc::string, pair<grpc::string, grpc::string>> - output_message_modules_and_classes; + map<grpc::string, grpc::string> input_message_modules_and_classes; + map<grpc::string, grpc::string> output_message_modules_and_classes; for (int i = 0; i < service->method_count(); ++i) { const MethodDescriptor* method = service->method(i); const grpc::string method_cardinality = grpc::string(method->client_streaming() ? "STREAM" : "UNARY") + "_" + - grpc::string(method->server_streaming() ? "STREAM" : "UNARY"); - pair<grpc::string, grpc::string> input_message_module_and_class; - if (!GetModuleAndMessagePath(method->input_type(), + grpc::string(method->server_streaming() ? "STREAM" : "UNARY"); + grpc::string input_message_module_and_class; + if (!GetModuleAndMessagePath(method->input_type(), service, &input_message_module_and_class)) { return false; } - pair<grpc::string, grpc::string> output_message_module_and_class; - if (!GetModuleAndMessagePath(method->output_type(), + grpc::string output_message_module_and_class; + if (!GetModuleAndMessagePath(method->output_type(), service, &output_message_module_and_class)) { return false; } - // Import the modules that define the messages used in RPCs. - out->Print("import $Module$\n", "Module", - input_message_module_and_class.first); - out->Print("import $Module$\n", "Module", - output_message_module_and_class.first); method_cardinalities.insert( make_pair(method->name(), method_cardinality)); input_message_modules_and_classes.insert( @@ -412,13 +412,11 @@ bool PrintBetaStubFactory(const grpc::string& package_qualified_service_name, name_and_input_module_class_pair++) { IndentScope raii_indent(out); out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " - "$InputTypeModule$.$InputTypeClass$.SerializeToString,\n", + "$InputTypeModuleAndClass$.SerializeToString,\n", "PackageQualifiedServiceName", package_qualified_service_name, "MethodName", name_and_input_module_class_pair->first, - "InputTypeModule", - name_and_input_module_class_pair->second.first, - "InputTypeClass", - name_and_input_module_class_pair->second.second); + "InputTypeModuleAndClass", + name_and_input_module_class_pair->second); } out->Print("}\n"); out->Print("response_deserializers = {\n"); @@ -429,13 +427,11 @@ bool PrintBetaStubFactory(const grpc::string& package_qualified_service_name, name_and_output_module_class_pair++) { IndentScope raii_indent(out); out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " - "$OutputTypeModule$.$OutputTypeClass$.FromString,\n", + "$OutputTypeModuleAndClass$.FromString,\n", "PackageQualifiedServiceName", package_qualified_service_name, "MethodName", name_and_output_module_class_pair->first, - "OutputTypeModule", - name_and_output_module_class_pair->second.first, - "OutputTypeClass", - name_and_output_module_class_pair->second.second); + "OutputTypeModuleAndClass", + name_and_output_module_class_pair->second); } out->Print("}\n"); out->Print("cardinalities = {\n"); @@ -463,8 +459,6 @@ bool PrintBetaStubFactory(const grpc::string& package_qualified_service_name, bool PrintPreamble(const FileDescriptor* file, const GeneratorConfiguration& config, Printer* out) { - out->Print("import abc\n"); - out->Print("import six\n"); out->Print("from $Package$ import implementations as beta_implementations\n", "Package", config.beta_package_root); out->Print("from $Package$ import interfaces as beta_interfaces\n", diff --git a/src/compiler/ruby_generator.cc b/src/compiler/ruby_generator.cc index 936a186beb..1501c3f3e0 100644 --- a/src/compiler/ruby_generator.cc +++ b/src/compiler/ruby_generator.cc @@ -66,7 +66,9 @@ void PrintMethod(const MethodDescriptor *method, const grpc::string &package, std::map<grpc::string, grpc::string> method_vars = ListToDict({"mth.name", method->name(), "input.type", input_type, "output.type", output_type, }); + out->Print(GetRubyComments(method, true).c_str()); out->Print(method_vars, "rpc :$mth.name$, $input.type$, $output.type$\n"); + out->Print(GetRubyComments(method, false).c_str()); } // Prints out the service using the ruby gRPC DSL. @@ -82,12 +84,7 @@ void PrintService(const ServiceDescriptor *service, const grpc::string &package, out->Print(module_vars, "module $module.name$\n"); out->Indent(); - // TODO(temiola): add documentation - grpc::string doc = "TODO: add proto service documentation here"; - std::map<grpc::string, grpc::string> template_vars = - ListToDict({"Documentation", doc, }); - out->Print("\n"); - out->Print(template_vars, "# $Documentation$\n"); + out->Print(GetRubyComments(service, true).c_str()); out->Print("class Service\n"); // Write the indented class body. @@ -113,6 +110,7 @@ void PrintService(const ServiceDescriptor *service, const grpc::string &package, // End the service module out->Outdent(); out->Print("end\n"); + out->Print(GetRubyComments(service, false).c_str()); } } // namespace @@ -138,6 +136,12 @@ grpc::string GetServices(const FileDescriptor *file) { out.Print(header_comment_vars, "# Source: $file.name$ for package '$file.package$'\n"); + grpc::string leading_comments = GetRubyComments(file, true); + if (!leading_comments.empty()) { + out.Print("# Original file comments:\n"); + out.Print(leading_comments.c_str()); + } + out.Print("\n"); out.Print("require 'grpc'\n"); // Write out require statemment to import the separately generated file @@ -164,6 +168,8 @@ grpc::string GetServices(const FileDescriptor *file) { out.Outdent(); out.Print("end\n"); } + + out.Print(GetRubyComments(file, false).c_str()); } return output; } diff --git a/src/compiler/ruby_generator_helpers-inl.h b/src/compiler/ruby_generator_helpers-inl.h index 9da7cab3c7..ff6939ed9f 100644 --- a/src/compiler/ruby_generator_helpers-inl.h +++ b/src/compiler/ruby_generator_helpers-inl.h @@ -35,6 +35,7 @@ #define GRPC_INTERNAL_COMPILER_RUBY_GENERATOR_HELPERS_INL_H #include "src/compiler/config.h" +#include "src/compiler/generator_helpers.h" #include "src/compiler/ruby_generator_string-inl.h" namespace grpc_ruby_generator { @@ -60,6 +61,13 @@ inline grpc::string MessagesRequireName( return Replace(file->name(), ".proto", ""); } +// Get leading or trailing comments in a string. Comment lines start with "# ". +// Leading detached comments are put in in front of leading comments. +template <typename DescriptorType> +inline grpc::string GetRubyComments(const DescriptorType *desc, bool leading) { + return grpc_generator::GetPrefixedComments(desc, leading, "#"); +} + } // namespace grpc_ruby_generator #endif // GRPC_INTERNAL_COMPILER_RUBY_GENERATOR_HELPERS_INL_H |