diff --git a/src/compiler/protobuf_plugin.h b/src/compiler/protobuf_plugin.h index f0d6b62e513..dcff6654a5a 100644 --- a/src/compiler/protobuf_plugin.h +++ b/src/compiler/protobuf_plugin.h @@ -66,6 +66,14 @@ class ProtoBufMethod : public grpc_generator::Method { 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(); } diff --git a/src/compiler/python_generator.cc b/src/compiler/python_generator.cc index 94f23e7fde6..f9a135c629c 100644 --- a/src/compiler/python_generator.cc +++ b/src/compiler/python_generator.cc @@ -52,18 +52,10 @@ #include "src/compiler/python_private_generator.h" #include "src/compiler/python_generator_helpers.h" -using grpc_generator::StringReplace; -using grpc_generator::StripProto; -using grpc::protobuf::Descriptor; using grpc::protobuf::FileDescriptor; -using grpc::protobuf::MethodDescriptor; -using grpc::protobuf::ServiceDescriptor; using grpc::protobuf::compiler::GeneratorContext; using grpc::protobuf::io::CodedOutputStream; -using grpc::protobuf::io::Printer; -using grpc::protobuf::io::StringOutputStream; using grpc::protobuf::io::ZeroCopyOutputStream; -using std::initializer_list; using std::make_pair; using std::map; using std::pair; @@ -78,7 +70,6 @@ grpc::string generator_file_name; namespace { -typedef vector DescriptorVector; typedef map StringMap; typedef vector StringVector; typedef tuple StringPair; @@ -145,14 +136,14 @@ bool PrivateGenerator::PrintBetaServicer(const grpc_generator::Service* service, for (int i = 0; i < service->method_count(); ++i) { auto method = service->method(i); grpc::string arg_name = - method.get()->ClientStreaming() ? "request_iterator" : "request"; + method->python_ClientStreaming() ? "request_iterator" : "request"; StringMap method_dict; - method_dict["Method"] = method.get()->name(); + method_dict["Method"] = method->name(); method_dict["ArgName"] = arg_name; out->Print(method_dict, "def $Method$(self, $ArgName$, context):\n"); { IndentScope raii_method_indent(out); - StringVector method_comments = method.get()->GetAllComments(); + StringVector method_comments = method->GetAllComments(); PrintAllComments(method_comments, out); out->Print("context.code(beta_interfaces.StatusCode.UNIMPLEMENTED)\n"); } @@ -181,20 +172,20 @@ bool PrivateGenerator::PrintBetaStub(const grpc_generator::Service* service, for (int i = 0; i < service->method_count(); ++i) { auto method = service->method(i); grpc::string arg_name = - method.get()->ClientStreaming() ? "request_iterator" : "request"; + method->python_ClientStreaming() ? "request_iterator" : "request"; StringMap method_dict; - method_dict["Method"] = method.get()->name(); + method_dict["Method"] = method->name(); method_dict["ArgName"] = arg_name; out->Print(method_dict, "def $Method$(self, $ArgName$, timeout, metadata=None, " "with_call=False, protocol_options=None):\n"); { IndentScope raii_method_indent(out); - StringVector method_comments = method.get()->GetAllComments(); + StringVector method_comments = method->GetAllComments(); PrintAllComments(method_comments, out); out->Print("raise NotImplementedError()\n"); } - if (!method.get()->ServerStreaming()) { + if (!method->python_ServerStreaming()) { out->Print(method_dict, "$Method$.future = None\n"); } } @@ -225,25 +216,25 @@ 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.get()->ClientStreaming() ? "stream_" : "unary_") + - grpc::string(method.get()->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()->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()->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( - make_pair(method.get()->name(), method_implementation_constructor)); + make_pair(method->name(), method_implementation_constructor)); input_message_modules_and_classes.insert( - make_pair(method.get()->name(), input_message_module_and_class)); + make_pair(method->name(), input_message_module_and_class)); output_message_modules_and_classes.insert( - make_pair(method.get()->name(), output_message_module_and_class)); + make_pair(method->name(), output_message_module_and_class)); } StringMap method_dict; method_dict["PackageQualifiedServiceName"] = package_qualified_service_name; @@ -329,24 +320,24 @@ 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.get()->ClientStreaming() ? "STREAM" : "UNARY") + "_" + - grpc::string(method.get()->ServerStreaming() ? "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()->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()->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( - make_pair(method.get()->name(), method_cardinality)); + make_pair(method->name(), method_cardinality)); input_message_modules_and_classes.insert( - make_pair(method.get()->name(), input_message_module_and_class)); + make_pair(method->name(), input_message_module_and_class)); output_message_modules_and_classes.insert( - make_pair(method.get()->name(), output_message_module_and_class)); + make_pair(method->name(), output_message_module_and_class)); } StringMap method_dict; method_dict["PackageQualifiedServiceName"] = package_qualified_service_name; @@ -430,20 +421,20 @@ 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.get()->ClientStreaming() ? "stream" : "unary") + - "_" + grpc::string(method.get()->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()->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()->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.get()->name(); + method_dict["Method"] = method->name(); method_dict["MultiCallableConstructor"] = multi_callable_constructor; out->Print(method_dict, "self.$Method$ = channel.$MultiCallableConstructor$(\n"); { @@ -478,15 +469,15 @@ bool PrivateGenerator::PrintServicer(const grpc_generator::Service* service, for (int i = 0; i < service->method_count(); ++i) { auto method = service->method(i); grpc::string arg_name = - method.get()->ClientStreaming() ? "request_iterator" : "request"; + method->python_ClientStreaming() ? "request_iterator" : "request"; StringMap method_dict; - method_dict["Method"] = method.get()->name(); + method_dict["Method"] = method->name(); method_dict["ArgName"] = arg_name; out->Print("\n"); out->Print(method_dict, "def $Method$(self, $ArgName$, context):\n"); { IndentScope raii_method_indent(out); - StringVector method_comments = method.get()->GetAllComments(); + StringVector method_comments = method->GetAllComments(); PrintAllComments(method_comments, out); out->Print("context.set_code(grpc.StatusCode.UNIMPLEMENTED)\n"); out->Print("context.set_details('Method not implemented!')\n"); @@ -513,22 +504,22 @@ 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.get()->ClientStreaming() ? "stream" : "unary") + + grpc::string(method->python_ClientStreaming() ? "stream" : "unary") + "_" + - grpc::string(method.get()->ServerStreaming() ? "stream" : "unary") + + grpc::string(method->python_ServerStreaming() ? "stream" : "unary") + "_rpc_method_handler"; grpc::string request_module_and_class; - if (!method.get()->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()->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.get()->name(); + method_dict["Method"] = method->name(); method_dict["MethodHandlerConstructor"] = method_handler_constructor; method_dict["RequestModuleAndClass"] = request_module_and_class; method_dict["ResponseModuleAndClass"] = response_module_and_class; diff --git a/src/compiler/python_generator_helpers.h b/src/compiler/python_generator_helpers.h index 1a5dde9d6b3..5b8b024386d 100644 --- a/src/compiler/python_generator_helpers.h +++ b/src/compiler/python_generator_helpers.h @@ -44,11 +44,10 @@ #include "src/compiler/python_private_generator.h" #include "src/compiler/generator_helpers.h" +using std::vector; using grpc_generator::StringReplace; using grpc_generator::StripProto; using grpc::protobuf::Descriptor; -using std::vector; - using grpc::protobuf::FileDescriptor; using grpc::protobuf::MethodDescriptor; using grpc::protobuf::ServiceDescriptor; @@ -135,7 +134,7 @@ StringVector get_all_comments(const DescriptorType* descriptor) { } // namespace -} +} // namespace grpc_python_generator #endif // GRPC_INTERNAL_COMPILER_PYTHON_GENERATOR_HELPERS_H \ No newline at end of file diff --git a/src/compiler/schema_interface.h b/src/compiler/schema_interface.h index 77349487d57..94920026ce1 100644 --- a/src/compiler/schema_interface.h +++ b/src/compiler/schema_interface.h @@ -81,7 +81,9 @@ namespace grpc_generator { 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; };