diff options
author | Harsh Vardhan <harshvd95@gmail.com> | 2017-02-25 20:54:52 +0530 |
---|---|---|
committer | Harsh Vardhan <harshvd95@gmail.com> | 2017-03-28 21:01:51 +0530 |
commit | 4b0ba1a6908399a64e198f93b095b4bdd39d7b67 (patch) | |
tree | 1a46775e0be17705c6c74f1d28cbea8a052c2302 /src/compiler | |
parent | 0f12cd9247784beccc2eb4b36aff416c49617972 (diff) |
clang-format
Diffstat (limited to 'src/compiler')
-rw-r--r-- | src/compiler/protobuf_plugin.h | 337 | ||||
-rw-r--r-- | src/compiler/python_generator.cc | 198 | ||||
-rw-r--r-- | src/compiler/python_generator_helpers.h | 13 | ||||
-rw-r--r-- | src/compiler/python_private_generator.h | 23 | ||||
-rw-r--r-- | src/compiler/schema_interface.h | 146 |
5 files changed, 384 insertions, 333 deletions
diff --git a/src/compiler/protobuf_plugin.h b/src/compiler/protobuf_plugin.h index dcff6654a5..0f9f647f0e 100644 --- a/src/compiler/protobuf_plugin.h +++ b/src/compiler/protobuf_plugin.h @@ -1,191 +1,214 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ #ifndef GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H #define GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H #include "src/compiler/config.h" -#include "src/compiler/schema_interface.h" -#include "src/compiler/python_private_generator.h" #include "src/compiler/cpp_generator_helpers.h" #include "src/compiler/python_generator_helpers.h" +#include "src/compiler/python_private_generator.h" +#include "src/compiler/schema_interface.h" #include <vector> // Get leading or trailing comments in a string. template <typename DescriptorType> -inline grpc::string GetCommentsHelper(const DescriptorType *desc, bool leading, - const grpc::string &prefix) { +inline grpc::string GetCommentsHelper(const DescriptorType *desc, bool leading, + const grpc::string &prefix) { return grpc_generator::GetPrefixedComments(desc, leading, prefix); } class ProtoBufMethod : public grpc_generator::Method { - public: - ProtoBufMethod(const grpc::protobuf::MethodDescriptor *method) + public: + ProtoBufMethod(const grpc::protobuf::MethodDescriptor *method) : method_(method) {} - grpc::string name() const { return method_->name(); } - - grpc::string input_type_name() const { - return grpc_cpp_generator::ClassName(method_->input_type(), true); - } - grpc::string output_type_name() const { - return grpc_cpp_generator::ClassName(method_->output_type(), true); - } - - grpc::string get_input_type_name() const { - return method_->input_type()->file()->name(); - } - grpc::string get_output_type_name() const { - return method_->output_type()->file()->name(); - } - - bool get_module_and_message_path_input(grpc::string *str, - grpc::string generator_file_name, - bool generate_in_pb2_grpc) const { - return grpc_python_generator::GetModuleAndMessagePath(method_->input_type(), - str, generator_file_name, - generate_in_pb2_grpc); - } - - bool get_module_and_message_path_output(grpc::string *str, - grpc::string generator_file_name, - bool generate_in_pb2_grpc) const { - return grpc_python_generator::GetModuleAndMessagePath(method_->output_type(), - str, generator_file_name, - generate_in_pb2_grpc); - } - - bool NoStreaming() const { - return !method_->client_streaming() && !method_->server_streaming(); - } - - bool ClientStreaming() const { - return method_->client_streaming() && !method_->server_streaming(); - } - - bool ServerStreaming() const { - return !method_->client_streaming() && method_->server_streaming(); - } - - bool python_ClientStreaming() const { - return method_->client_streaming(); - } - - bool python_ServerStreaming() const { - return method_->server_streaming(); - } - - bool BidiStreaming() const { - return method_->client_streaming() && method_->server_streaming(); - } - - grpc::string GetLeadingComments(const grpc::string prefix) const { - return GetCommentsHelper(method_, true, prefix); - } - - grpc::string GetTrailingComments(const grpc::string prefix) const { - return GetCommentsHelper(method_, false, prefix); - } - - vector<grpc::string> GetAllComments() const { - return grpc_python_generator::get_all_comments(method_); - } - - private: - const grpc::protobuf::MethodDescriptor *method_; - }; + grpc::string name() const { return method_->name(); } + + grpc::string input_type_name() const { + return grpc_cpp_generator::ClassName(method_->input_type(), true); + } + grpc::string output_type_name() const { + return grpc_cpp_generator::ClassName(method_->output_type(), true); + } + + grpc::string get_input_type_name() const { + return method_->input_type()->file()->name(); + } + grpc::string get_output_type_name() const { + return method_->output_type()->file()->name(); + } + + bool get_module_and_message_path_input(grpc::string *str, + grpc::string generator_file_name, + bool generate_in_pb2_grpc) const { + return grpc_python_generator::GetModuleAndMessagePath( + method_->input_type(), str, generator_file_name, generate_in_pb2_grpc); + } + + bool get_module_and_message_path_output(grpc::string *str, + grpc::string generator_file_name, + bool generate_in_pb2_grpc) const { + return grpc_python_generator::GetModuleAndMessagePath( + method_->output_type(), str, generator_file_name, generate_in_pb2_grpc); + } + + bool NoStreaming() const { + return !method_->client_streaming() && !method_->server_streaming(); + } + + bool ClientStreaming() const { + return method_->client_streaming() && !method_->server_streaming(); + } + + bool ServerStreaming() const { + return !method_->client_streaming() && method_->server_streaming(); + } + + bool python_ClientStreaming() const { return method_->client_streaming(); } + + bool python_ServerStreaming() const { return method_->server_streaming(); } + + bool BidiStreaming() const { + return method_->client_streaming() && method_->server_streaming(); + } + + grpc::string GetLeadingComments(const grpc::string prefix) const { + return GetCommentsHelper(method_, true, prefix); + } + + grpc::string GetTrailingComments(const grpc::string prefix) const { + return GetCommentsHelper(method_, false, prefix); + } + + vector<grpc::string> GetAllComments() const { + return grpc_python_generator::get_all_comments(method_); + } + + private: + const grpc::protobuf::MethodDescriptor *method_; +}; class ProtoBufService : public grpc_generator::Service { - public: - ProtoBufService(const grpc::protobuf::ServiceDescriptor *service) - : service_(service) {} + public: + ProtoBufService(const grpc::protobuf::ServiceDescriptor *service) + : service_(service) {} - grpc::string name() const { return service_->name(); } + grpc::string name() const { return service_->name(); } - int method_count() const { return service_->method_count(); }; - std::unique_ptr<const grpc_generator::Method> method(int i) const { - return std::unique_ptr<const grpc_generator::Method>( - new ProtoBufMethod(service_->method(i))); - }; + int method_count() const { return service_->method_count(); }; + std::unique_ptr<const grpc_generator::Method> method(int i) const { + return std::unique_ptr<const grpc_generator::Method>( + new ProtoBufMethod(service_->method(i))); + }; - grpc::string GetLeadingComments(const grpc::string prefix) const { - return GetCommentsHelper(service_, true, prefix); - } + grpc::string GetLeadingComments(const grpc::string prefix) const { + return GetCommentsHelper(service_, true, prefix); + } - grpc::string GetTrailingComments(const grpc::string prefix) const { - return GetCommentsHelper(service_, false, prefix); - } + grpc::string GetTrailingComments(const grpc::string prefix) const { + return GetCommentsHelper(service_, false, prefix); + } - vector<grpc::string> GetAllComments() const { - return grpc_python_generator::get_all_comments(service_); - } + vector<grpc::string> GetAllComments() const { + return grpc_python_generator::get_all_comments(service_); + } - private: - const grpc::protobuf::ServiceDescriptor *service_; + private: + const grpc::protobuf::ServiceDescriptor *service_; }; class ProtoBufPrinter : public grpc_generator::Printer { - public: - ProtoBufPrinter(grpc::string *str) - : output_stream_(str), printer_(&output_stream_, '$') {} - - void Print(const std::map<grpc::string, grpc::string> &vars, - const char *string_template) { - printer_.Print(vars, string_template); - } - - void Print(const char *string) { printer_.Print(string); } - void Indent() { printer_.Indent(); } - void Outdent() { printer_.Outdent(); } - - private: - grpc::protobuf::io::StringOutputStream output_stream_; - grpc::protobuf::io::Printer printer_; - }; + public: + ProtoBufPrinter(grpc::string *str) + : output_stream_(str), printer_(&output_stream_, '$') {} + + void Print(const std::map<grpc::string, grpc::string> &vars, + const char *string_template) { + printer_.Print(vars, string_template); + } + + void Print(const char *string) { printer_.Print(string); } + void Indent() { printer_.Indent(); } + void Outdent() { printer_.Outdent(); } + + private: + grpc::protobuf::io::StringOutputStream output_stream_; + grpc::protobuf::io::Printer printer_; +}; class ProtoBufFile : public grpc_generator::File { - public: - ProtoBufFile(const grpc::protobuf::FileDescriptor *file) : file_(file) {} - - grpc::string filename() const { return file_->name(); } - grpc::string filename_without_ext() const { - return grpc_generator::StripProto(filename()); - } - - grpc::string package() const { return file_->package(); } - std::vector<grpc::string> package_parts() const { - return grpc_generator::tokenize(package(), "."); - } - - grpc::string additional_headers() const { return ""; } - - int service_count() const { return file_->service_count(); }; - std::unique_ptr<const grpc_generator::Service> service(int i) const { - return std::unique_ptr<const grpc_generator::Service>( - new ProtoBufService(file_->service(i))); - } - - std::unique_ptr<grpc_generator::Printer> CreatePrinter( - grpc::string *str) const { - return std::unique_ptr<grpc_generator::Printer>( - new ProtoBufPrinter(str)); - } - - grpc::string GetLeadingComments(const grpc::string prefix) const { - return GetCommentsHelper(file_, true, prefix); - } - - grpc::string GetTrailingComments(const grpc::string prefix) const { - return GetCommentsHelper(file_, false, prefix); - } - - vector<grpc::string> GetAllComments() const { - return grpc_python_generator::get_all_comments(file_); - } - - private: - const grpc::protobuf::FileDescriptor *file_; + public: + ProtoBufFile(const grpc::protobuf::FileDescriptor *file) : file_(file) {} + + grpc::string filename() const { return file_->name(); } + grpc::string filename_without_ext() const { + return grpc_generator::StripProto(filename()); + } + + grpc::string package() const { return file_->package(); } + std::vector<grpc::string> package_parts() const { + return grpc_generator::tokenize(package(), "."); + } + + grpc::string additional_headers() const { return ""; } + + int service_count() const { return file_->service_count(); }; + std::unique_ptr<const grpc_generator::Service> service(int i) const { + return std::unique_ptr<const grpc_generator::Service>( + new ProtoBufService(file_->service(i))); + } + + std::unique_ptr<grpc_generator::Printer> CreatePrinter( + grpc::string *str) const { + return std::unique_ptr<grpc_generator::Printer>(new ProtoBufPrinter(str)); + } + + grpc::string GetLeadingComments(const grpc::string prefix) const { + return GetCommentsHelper(file_, true, prefix); + } + + grpc::string GetTrailingComments(const grpc::string prefix) const { + return GetCommentsHelper(file_, false, prefix); + } + + vector<grpc::string> GetAllComments() const { + return grpc_python_generator::get_all_comments(file_); + } + + private: + const grpc::protobuf::FileDescriptor *file_; }; - - #endif // GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H
\ No newline at end of file diff --git a/src/compiler/python_generator.cc b/src/compiler/python_generator.cc index f9a135c629..523fe801d1 100644 --- a/src/compiler/python_generator.cc +++ b/src/compiler/python_generator.cc @@ -46,11 +46,11 @@ #include <vector> #include "src/compiler/config.h" -#include "src/compiler/schema_interface.h" #include "src/compiler/generator_helpers.h" #include "src/compiler/protobuf_plugin.h" -#include "src/compiler/python_private_generator.h" #include "src/compiler/python_generator_helpers.h" +#include "src/compiler/python_private_generator.h" +#include "src/compiler/schema_interface.h" using grpc::protobuf::FileDescriptor; using grpc::protobuf::compiler::GeneratorContext; @@ -100,20 +100,19 @@ PrivateGenerator::PrivateGenerator(const GeneratorConfiguration& config, void PrivateGenerator::PrintAllComments(StringVector comments, grpc_generator::Printer* out) { - - if (comments.empty()) { - return; - } - out->Print("\"\"\""); - for (StringVector::iterator it = comments.begin(); it != comments.end(); - ++it) { - size_t start_pos = it->find_first_not_of(' '); - if (start_pos != grpc::string::npos) { - out->Print(it->c_str() + start_pos); - } - out->Print("\n"); + if (comments.empty()) { + return; + } + out->Print("\"\"\""); + for (StringVector::iterator it = comments.begin(); it != comments.end(); + ++it) { + size_t start_pos = it->find_first_not_of(' '); + if (start_pos != grpc::string::npos) { + out->Print(it->c_str() + start_pos); } - out->Print("\"\"\"\n"); + out->Print("\n"); + } + out->Print("\"\"\"\n"); } bool PrivateGenerator::PrintBetaServicer(const grpc_generator::Service* service, @@ -153,7 +152,7 @@ bool PrivateGenerator::PrintBetaServicer(const grpc_generator::Service* service, } bool PrivateGenerator::PrintBetaStub(const grpc_generator::Service* service, - grpc_generator::Printer* out) { + grpc_generator::Printer* out) { StringMap service_dict; service_dict["Service"] = service->name(); out->Print("\n\n"); @@ -200,8 +199,8 @@ bool PrivateGenerator::PrintBetaServerFactory( service_dict["Service"] = service->name(); out->Print("\n\n"); out->Print(service_dict, - "def beta_create_$Service$_server(servicer, pool=None, " - "pool_size=None, default_timeout=None, maximum_timeout=None):\n"); + "def beta_create_$Service$_server(servicer, pool=None, " + "pool_size=None, default_timeout=None, maximum_timeout=None):\n"); { IndentScope raii_create_server_indent(out); out->Print( @@ -216,17 +215,21 @@ bool PrivateGenerator::PrintBetaServerFactory( for (int i = 0; i < service->method_count(); ++i) { auto method = service->method(i); const grpc::string method_implementation_constructor = - grpc::string(method->python_ClientStreaming() ? "stream_" : "unary_") + - grpc::string(method->python_ServerStreaming() ? "stream_" : "unary_") + + grpc::string(method->python_ClientStreaming() ? "stream_" + : "unary_") + + grpc::string(method->python_ServerStreaming() ? "stream_" + : "unary_") + "inline"; grpc::string input_message_module_and_class; - if (!method->get_module_and_message_path_input(&input_message_module_and_class, - generator_file_name, generate_in_pb2_grpc)) { + if (!method->get_module_and_message_path_input( + &input_message_module_and_class, generator_file_name, + generate_in_pb2_grpc)) { return false; } grpc::string output_message_module_and_class; - if (!method->get_module_and_message_path_output(&output_message_module_and_class, - generator_file_name, generate_in_pb2_grpc)) { + if (!method->get_module_and_message_path_output( + &output_message_module_and_class, generator_file_name, + generate_in_pb2_grpc)) { return false; } method_implementation_constructors.insert( @@ -245,11 +248,12 @@ bool PrivateGenerator::PrintBetaServerFactory( input_message_modules_and_classes.end(); name_and_input_module_class_pair++) { method_dict["MethodName"] = name_and_input_module_class_pair->first; - method_dict["InputTypeModuleAndClass"] = name_and_input_module_class_pair->second; + method_dict["InputTypeModuleAndClass"] = + name_and_input_module_class_pair->second; IndentScope raii_indent(out); out->Print(method_dict, - "(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " - "$InputTypeModuleAndClass$.FromString,\n"); + "(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " + "$InputTypeModuleAndClass$.FromString,\n"); } out->Print("}\n"); out->Print("response_serializers = {\n"); @@ -259,11 +263,12 @@ bool PrivateGenerator::PrintBetaServerFactory( output_message_modules_and_classes.end(); name_and_output_module_class_pair++) { method_dict["MethodName"] = name_and_output_module_class_pair->first; - method_dict["OutputTypeModuleAndClass"] = name_and_output_module_class_pair->second; + method_dict["OutputTypeModuleAndClass"] = + name_and_output_module_class_pair->second; IndentScope raii_indent(out); out->Print(method_dict, - "(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " - "$OutputTypeModuleAndClass$.SerializeToString,\n"); + "(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " + "$OutputTypeModuleAndClass$.SerializeToString,\n"); } out->Print("}\n"); out->Print("method_implementations = {\n"); @@ -278,8 +283,8 @@ bool PrivateGenerator::PrintBetaServerFactory( const grpc::string method_name = name_and_implementation_constructor->first; out->Print(method_dict, - "(\'$PackageQualifiedServiceName$\', \'$Method$\'): " - "face_utilities.$Constructor$(servicer.$Method$),\n"); + "(\'$PackageQualifiedServiceName$\', \'$Method$\'): " + "face_utilities.$Constructor$(servicer.$Method$),\n"); } out->Print("}\n"); out->Print( @@ -298,8 +303,7 @@ bool PrivateGenerator::PrintBetaServerFactory( bool PrivateGenerator::PrintBetaStubFactory( const grpc::string& package_qualified_service_name, - const grpc_generator::Service* service, - grpc_generator::Printer* out) { + const grpc_generator::Service* service, grpc_generator::Printer* out) { StringMap dict; dict["Service"] = service->name(); out->Print("\n\n"); @@ -320,16 +324,19 @@ bool PrivateGenerator::PrintBetaStubFactory( for (int i = 0; i < service->method_count(); ++i) { auto method = service->method(i); const grpc::string method_cardinality = - grpc::string(method->python_ClientStreaming() ? "STREAM" : "UNARY") + "_" + + grpc::string(method->python_ClientStreaming() ? "STREAM" : "UNARY") + + "_" + grpc::string(method->python_ServerStreaming() ? "STREAM" : "UNARY"); grpc::string input_message_module_and_class; - if (!method->get_module_and_message_path_input(&input_message_module_and_class, - generator_file_name, generate_in_pb2_grpc)) { + if (!method->get_module_and_message_path_input( + &input_message_module_and_class, generator_file_name, + generate_in_pb2_grpc)) { return false; } grpc::string output_message_module_and_class; - if (!method->get_module_and_message_path_output(&output_message_module_and_class, - generator_file_name, generate_in_pb2_grpc)) { + if (!method->get_module_and_message_path_output( + &output_message_module_and_class, generator_file_name, + generate_in_pb2_grpc)) { return false; } method_cardinalities.insert( @@ -348,11 +355,12 @@ bool PrivateGenerator::PrintBetaStubFactory( input_message_modules_and_classes.end(); name_and_input_module_class_pair++) { method_dict["MethodName"] = name_and_input_module_class_pair->first; - method_dict["InputTypeModuleAndClass"] = name_and_input_module_class_pair->second; + method_dict["InputTypeModuleAndClass"] = + name_and_input_module_class_pair->second; IndentScope raii_indent(out); out->Print(method_dict, - "(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " - "$InputTypeModuleAndClass$.SerializeToString,\n"); + "(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " + "$InputTypeModuleAndClass$.SerializeToString,\n"); } out->Print("}\n"); out->Print("response_deserializers = {\n"); @@ -362,11 +370,12 @@ bool PrivateGenerator::PrintBetaStubFactory( output_message_modules_and_classes.end(); name_and_output_module_class_pair++) { method_dict["MethodName"] = name_and_output_module_class_pair->first; - method_dict["OutputTypeModuleAndClass"] = name_and_output_module_class_pair->second; + method_dict["OutputTypeModuleAndClass"] = + name_and_output_module_class_pair->second; IndentScope raii_indent(out); out->Print(method_dict, - "(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " - "$OutputTypeModuleAndClass$.FromString,\n"); + "(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " + "$OutputTypeModuleAndClass$.FromString,\n"); } out->Print("}\n"); out->Print("cardinalities = {\n"); @@ -377,7 +386,8 @@ bool PrivateGenerator::PrintBetaStubFactory( method_dict["Method"] = name_and_cardinality->first; method_dict["Cardinality"] = name_and_cardinality->second; IndentScope raii_descriptions_indent(out); - out->Print(method_dict, "\'$Method$\': cardinality.Cardinality.$Cardinality$,\n"); + out->Print(method_dict, + "\'$Method$\': cardinality.Cardinality.$Cardinality$,\n"); } out->Print("}\n"); out->Print( @@ -387,17 +397,16 @@ bool PrivateGenerator::PrintBetaStubFactory( "response_deserializers=response_deserializers, " "thread_pool=pool, thread_pool_size=pool_size)\n"); out->Print(method_dict, - "return beta_implementations.dynamic_stub(channel, " - "\'$PackageQualifiedServiceName$\', " - "cardinalities, options=stub_options)\n"); + "return beta_implementations.dynamic_stub(channel, " + "\'$PackageQualifiedServiceName$\', " + "cardinalities, options=stub_options)\n"); } return true; } bool PrivateGenerator::PrintStub( const grpc::string& package_qualified_service_name, - const grpc_generator::Service* service, - grpc_generator::Printer* out) { + const grpc_generator::Service* service, grpc_generator::Printer* out) { StringMap dict; dict["Service"] = service->name(); out->Print("\n\n"); @@ -421,32 +430,40 @@ bool PrivateGenerator::PrintStub( for (int i = 0; i < service->method_count(); ++i) { auto method = service->method(i); grpc::string multi_callable_constructor = - grpc::string(method->python_ClientStreaming() ? "stream" : "unary") + - "_" + grpc::string(method->python_ServerStreaming() ? "stream" : "unary"); + grpc::string(method->python_ClientStreaming() ? "stream" + : "unary") + + "_" + + grpc::string(method->python_ServerStreaming() ? "stream" : "unary"); grpc::string request_module_and_class; - if (!method->get_module_and_message_path_input(&request_module_and_class, - generator_file_name, generate_in_pb2_grpc)) { + if (!method->get_module_and_message_path_input( + &request_module_and_class, generator_file_name, + generate_in_pb2_grpc)) { return false; } grpc::string response_module_and_class; - if (!method->get_module_and_message_path_output(&response_module_and_class, - generator_file_name, generate_in_pb2_grpc)) { + if (!method->get_module_and_message_path_output( + &response_module_and_class, generator_file_name, + generate_in_pb2_grpc)) { return false; } StringMap method_dict; method_dict["Method"] = method->name(); method_dict["MultiCallableConstructor"] = multi_callable_constructor; - out->Print(method_dict, "self.$Method$ = channel.$MultiCallableConstructor$(\n"); + out->Print(method_dict, + "self.$Method$ = channel.$MultiCallableConstructor$(\n"); { - method_dict["PackageQualifiedService"] = package_qualified_service_name; + method_dict["PackageQualifiedService"] = + package_qualified_service_name; method_dict["RequestModuleAndClass"] = request_module_and_class; method_dict["ResponseModuleAndClass"] = response_module_and_class; IndentScope raii_first_attribute_indent(out); IndentScope raii_second_attribute_indent(out); out->Print(method_dict, "'/$PackageQualifiedService$/$Method$',\n"); out->Print(method_dict, - "request_serializer=$RequestModuleAndClass$.SerializeToString,\n"); - out->Print(method_dict, + "request_serializer=$RequestModuleAndClass$." + "SerializeToString,\n"); + out->Print( + method_dict, "response_deserializer=$ResponseModuleAndClass$.FromString,\n"); out->Print(")\n"); } @@ -457,7 +474,7 @@ bool PrivateGenerator::PrintStub( } bool PrivateGenerator::PrintServicer(const grpc_generator::Service* service, - grpc_generator::Printer* out) { + grpc_generator::Printer* out) { StringMap service_dict; service_dict["Service"] = service->name(); out->Print("\n\n"); @@ -494,7 +511,8 @@ bool PrivateGenerator::PrintAddServicerToServer( StringMap service_dict; service_dict["Service"] = service->name(); out->Print("\n\n"); - out->Print(service_dict, "def add_$Service$Servicer_to_server(servicer, server):\n"); + out->Print(service_dict, + "def add_$Service$Servicer_to_server(servicer, server):\n"); { IndentScope raii_class_indent(out); out->Print("rpc_method_handlers = {\n"); @@ -504,18 +522,21 @@ bool PrivateGenerator::PrintAddServicerToServer( for (int i = 0; i < service->method_count(); ++i) { auto method = service->method(i); grpc::string method_handler_constructor = - grpc::string(method->python_ClientStreaming() ? "stream" : "unary") + - "_" + - grpc::string(method->python_ServerStreaming() ? "stream" : "unary") + + grpc::string(method->python_ClientStreaming() ? "stream" + : "unary") + + "_" + grpc::string(method->python_ServerStreaming() ? "stream" + : "unary") + "_rpc_method_handler"; grpc::string request_module_and_class; - if (!method->get_module_and_message_path_input(&request_module_and_class, - generator_file_name, generate_in_pb2_grpc)) { + if (!method->get_module_and_message_path_input( + &request_module_and_class, generator_file_name, + generate_in_pb2_grpc)) { return false; } grpc::string response_module_and_class; - if (!method->get_module_and_message_path_output(&response_module_and_class, - generator_file_name, generate_in_pb2_grpc)) { + if (!method->get_module_and_message_path_output( + &response_module_and_class, generator_file_name, + generate_in_pb2_grpc)) { return false; } StringMap method_dict; @@ -523,14 +544,17 @@ bool PrivateGenerator::PrintAddServicerToServer( method_dict["MethodHandlerConstructor"] = method_handler_constructor; method_dict["RequestModuleAndClass"] = request_module_and_class; method_dict["ResponseModuleAndClass"] = response_module_and_class; - out->Print(method_dict, "'$Method$': grpc.$MethodHandlerConstructor$(\n"); + out->Print(method_dict, + "'$Method$': grpc.$MethodHandlerConstructor$(\n"); { IndentScope raii_call_first_indent(out); IndentScope raii_call_second_indent(out); out->Print(method_dict, "servicer.$Method$,\n"); - out->Print(method_dict, + out->Print( + method_dict, "request_deserializer=$RequestModuleAndClass$.FromString,\n"); - out->Print(method_dict, + out->Print( + method_dict, "response_serializer=$ResponseModuleAndClass$.SerializeToString," "\n"); } @@ -544,17 +568,19 @@ bool PrivateGenerator::PrintAddServicerToServer( { IndentScope raii_call_first_indent(out); IndentScope raii_call_second_indent(out); - out->Print(method_dict, "'$PackageQualifiedServiceName$', rpc_method_handlers)\n"); + out->Print(method_dict, + "'$PackageQualifiedServiceName$', rpc_method_handlers)\n"); } out->Print("server.add_generic_rpc_handlers((generic_handler,))\n"); } return true; } -bool PrivateGenerator::PrintBetaPreamble(grpc_generator::Printer *out) { +bool PrivateGenerator::PrintBetaPreamble(grpc_generator::Printer* out) { StringMap var; var["Package"] = config.beta_package_root; - out->Print(var, "from $Package$ import implementations as beta_implementations\n"); + out->Print(var, + "from $Package$ import implementations as beta_implementations\n"); out->Print(var, "from $Package$ import interfaces as beta_interfaces\n"); out->Print("from grpc.framework.common import cardinality\n"); out->Print( @@ -578,12 +604,14 @@ bool PrivateGenerator::PrintPreamble(grpc_generator::Printer* out) { grpc::string input_type_file_name = method->get_input_type_name(); grpc::string input_module_name = ModuleName(input_type_file_name); grpc::string input_module_alias = ModuleAlias(input_type_file_name); - imports_set.insert(std::make_tuple(input_module_name, input_module_alias)); + imports_set.insert( + std::make_tuple(input_module_name, input_module_alias)); grpc::string output_type_file_name = method->get_output_type_name(); grpc::string output_module_name = ModuleName(output_type_file_name); grpc::string output_module_alias = ModuleAlias(output_type_file_name); - imports_set.insert(std::make_tuple(output_module_name, output_module_alias)); + imports_set.insert( + std::make_tuple(output_module_name, output_module_alias)); } } @@ -607,7 +635,8 @@ bool PrivateGenerator::PrintGAServices(grpc_generator::Printer* out) { grpc::string package_qualified_service_name = package + service->name(); if (!(PrintStub(package_qualified_service_name, service.get(), out) && PrintServicer(service.get(), out) && - PrintAddServicerToServer(package_qualified_service_name, service.get(), out))) { + PrintAddServicerToServer(package_qualified_service_name, + service.get(), out))) { return false; } } @@ -622,15 +651,18 @@ bool PrivateGenerator::PrintBetaServices(grpc_generator::Printer* out) { for (int i = 0; i < file->service_count(); ++i) { auto service = file->service(i); grpc::string package_qualified_service_name = package + service->name(); - if (!(PrintBetaServicer(service.get(), out) && PrintBetaStub(service.get(), out) && - PrintBetaServerFactory(package_qualified_service_name, service.get(), out) && - PrintBetaStubFactory(package_qualified_service_name, service.get(), out))) { + if (!(PrintBetaServicer(service.get(), out) && + PrintBetaStub(service.get(), out) && + PrintBetaServerFactory(package_qualified_service_name, service.get(), + out) && + PrintBetaStubFactory(package_qualified_service_name, service.get(), + out))) { return false; } } return true; } - + pair<bool, grpc::string> PrivateGenerator::GetGrpcServices() { grpc::string output; { diff --git a/src/compiler/python_generator_helpers.h b/src/compiler/python_generator_helpers.h index 5b8b024386..c862346619 100644 --- a/src/compiler/python_generator_helpers.h +++ b/src/compiler/python_generator_helpers.h @@ -40,9 +40,9 @@ #include <vector> #include "src/compiler/config.h" +#include "src/compiler/generator_helpers.h" #include "src/compiler/python_generator.h" #include "src/compiler/python_private_generator.h" -#include "src/compiler/generator_helpers.h" using std::vector; using grpc_generator::StringReplace; @@ -87,8 +87,8 @@ grpc::string ModuleAlias(const grpc::string& filename) { return module_name; } -bool GetModuleAndMessagePath(const Descriptor* type, - grpc::string* out, grpc::string generator_file_name, +bool GetModuleAndMessagePath(const Descriptor* type, grpc::string* out, + grpc::string generator_file_name, bool generate_in_pb2_grpc) { const Descriptor* path_elem_type = type; DescriptorVector message_path; @@ -132,9 +132,8 @@ StringVector get_all_comments(const DescriptorType* descriptor) { return comments; } -} // namespace - -} // namespace grpc_python_generator +} // namespace +} // namespace grpc_python_generator -#endif // GRPC_INTERNAL_COMPILER_PYTHON_GENERATOR_HELPERS_H
\ No newline at end of file +#endif // GRPC_INTERNAL_COMPILER_PYTHON_GENERATOR_HELPERS_H
\ No newline at end of file diff --git a/src/compiler/python_private_generator.h b/src/compiler/python_private_generator.h index 72c3a8d0e8..d20ff51b20 100644 --- a/src/compiler/python_private_generator.h +++ b/src/compiler/python_private_generator.h @@ -37,8 +37,8 @@ #include <iostream> #include <vector> -#include "src/compiler/schema_interface.h" #include "src/compiler/python_generator.h" +#include "src/compiler/schema_interface.h" namespace grpc_python_generator { @@ -61,28 +61,26 @@ struct PrivateGenerator { private: bool PrintPreamble(grpc_generator::Printer* out); - bool PrintBetaPreamble(grpc_generator::Printer *out); + bool PrintBetaPreamble(grpc_generator::Printer* out); bool PrintGAServices(grpc_generator::Printer* out); bool PrintBetaServices(grpc_generator::Printer* out); bool PrintAddServicerToServer( const grpc::string& package_qualified_service_name, - const grpc_generator::Service* service, - grpc_generator::Printer* out); + const grpc_generator::Service* service, grpc_generator::Printer* out); bool PrintServicer(const grpc_generator::Service* service, - grpc_generator::Printer* out); + grpc_generator::Printer* out); bool PrintStub(const grpc::string& package_qualified_service_name, const grpc_generator::Service* service, grpc_generator::Printer* out); bool PrintBetaServicer(const grpc_generator::Service* service, - grpc_generator::Printer* out); + grpc_generator::Printer* out); bool PrintBetaServerFactory( const grpc::string& package_qualified_service_name, - const grpc_generator::Service* service, - grpc_generator::Printer* out); + const grpc_generator::Service* service, grpc_generator::Printer* out); bool PrintBetaStub(const grpc_generator::Service* service, - grpc_generator::Printer* out); + grpc_generator::Printer* out); bool PrintBetaStubFactory(const grpc::string& package_qualified_service_name, const grpc_generator::Service* service, grpc_generator::Printer* out); @@ -92,11 +90,10 @@ struct PrivateGenerator { // wrapping will not be changed. void PrintAllComments(std::vector<grpc::string> comments, grpc_generator::Printer* out); - }; -} // namespace +} // namespace -} // namespace grpc_python_generator +} // namespace grpc_python_generator -#endif // GRPC_INTERNAL_COMPILER_PYTHON_PRIVATE_GENERATOR_H +#endif // GRPC_INTERNAL_COMPILER_PYTHON_PRIVATE_GENERATOR_H diff --git a/src/compiler/schema_interface.h b/src/compiler/schema_interface.h index 94920026ce..88de1ecdf7 100644 --- a/src/compiler/schema_interface.h +++ b/src/compiler/schema_interface.h @@ -46,83 +46,83 @@ namespace grpc { - typedef GRPC_CUSTOM_STRING string; +typedef GRPC_CUSTOM_STRING string; } // namespace grpc namespace grpc_generator { - // A common interface for objects having comments in the source. - // Return formatted comments to be inserted in generated code. - struct CommentHolder { - virtual ~CommentHolder() {} - virtual grpc::string GetLeadingComments(const grpc::string prefix) const = 0; - virtual grpc::string GetTrailingComments(const grpc::string prefix) const = 0; - virtual std::vector<grpc::string> GetAllComments() const = 0; - }; - - // An abstract interface representing a method. - struct Method : public CommentHolder { - virtual ~Method() {} - - virtual grpc::string name() const = 0; - - virtual grpc::string input_type_name() const = 0; - virtual grpc::string output_type_name() const = 0; - - virtual bool get_module_and_message_path_input(grpc::string *str, - grpc::string generator_file_name, - bool generate_in_pb2_grpc) const = 0; - virtual bool get_module_and_message_path_output(grpc::string *str, - grpc::string generator_file_name, - bool generate_in_pb2_grpc) const = 0; - - virtual grpc::string get_input_type_name() const = 0; - virtual grpc::string get_output_type_name() const = 0; - virtual bool NoStreaming() const = 0; - virtual bool ClientStreaming() const = 0; - virtual bool python_ClientStreaming() const = 0; - virtual bool ServerStreaming() const = 0; - virtual bool python_ServerStreaming() const = 0; - virtual bool BidiStreaming() const = 0; - }; - - // An abstract interface representing a service. - struct Service : public CommentHolder { - virtual ~Service() {} - - virtual grpc::string name() const = 0; - - virtual int method_count() const = 0; - virtual std::unique_ptr<const Method> method(int i) const = 0; - }; - - struct Printer { - virtual ~Printer() {} - - virtual void Print(const std::map<grpc::string, grpc::string> &vars, - const char *template_string) = 0; - virtual void Print(const char *string) = 0; - virtual void Indent() = 0; - virtual void Outdent() = 0; - }; - - // An interface that allows the source generated to be output using various - // libraries/idls/serializers. - struct File : public CommentHolder { - virtual ~File() {} - - virtual grpc::string filename() const = 0; - virtual grpc::string filename_without_ext() const = 0; - virtual grpc::string package() const = 0; - virtual std::vector<grpc::string> package_parts() const = 0; - virtual grpc::string additional_headers() const = 0; - - virtual int service_count() const = 0; - virtual std::unique_ptr<const Service> service(int i) const = 0; - - virtual std::unique_ptr<Printer> CreatePrinter(grpc::string *str) const = 0; - }; -} // namespace grpc_generator +// A common interface for objects having comments in the source. +// Return formatted comments to be inserted in generated code. +struct CommentHolder { + virtual ~CommentHolder() {} + virtual grpc::string GetLeadingComments(const grpc::string prefix) const = 0; + virtual grpc::string GetTrailingComments(const grpc::string prefix) const = 0; + virtual std::vector<grpc::string> GetAllComments() const = 0; +}; + +// An abstract interface representing a method. +struct Method : public CommentHolder { + virtual ~Method() {} + + virtual grpc::string name() const = 0; + + virtual grpc::string input_type_name() const = 0; + virtual grpc::string output_type_name() const = 0; + + virtual bool get_module_and_message_path_input( + grpc::string *str, grpc::string generator_file_name, + bool generate_in_pb2_grpc) const = 0; + virtual bool get_module_and_message_path_output( + grpc::string *str, grpc::string generator_file_name, + bool generate_in_pb2_grpc) const = 0; + + virtual grpc::string get_input_type_name() const = 0; + virtual grpc::string get_output_type_name() const = 0; + virtual bool NoStreaming() const = 0; + virtual bool ClientStreaming() const = 0; + virtual bool python_ClientStreaming() const = 0; + virtual bool ServerStreaming() const = 0; + virtual bool python_ServerStreaming() const = 0; + virtual bool BidiStreaming() const = 0; +}; + +// An abstract interface representing a service. +struct Service : public CommentHolder { + virtual ~Service() {} + + virtual grpc::string name() const = 0; + + virtual int method_count() const = 0; + virtual std::unique_ptr<const Method> method(int i) const = 0; +}; + +struct Printer { + virtual ~Printer() {} + + virtual void Print(const std::map<grpc::string, grpc::string> &vars, + const char *template_string) = 0; + virtual void Print(const char *string) = 0; + virtual void Indent() = 0; + virtual void Outdent() = 0; +}; + +// An interface that allows the source generated to be output using various +// libraries/idls/serializers. +struct File : public CommentHolder { + virtual ~File() {} + + virtual grpc::string filename() const = 0; + virtual grpc::string filename_without_ext() const = 0; + virtual grpc::string package() const = 0; + virtual std::vector<grpc::string> package_parts() const = 0; + virtual grpc::string additional_headers() const = 0; + + virtual int service_count() const = 0; + virtual std::unique_ptr<const Service> service(int i) const = 0; + + virtual std::unique_ptr<Printer> CreatePrinter(grpc::string *str) const = 0; +}; +} // namespace grpc_generator #endif // GRPC_INTERNAL_COMPILER_SCHEMA_INTERFACE_H |