aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/grpc/src/compiler/php_generator.cc
diff options
context:
space:
mode:
authorheretic <heretic@yandex-team.ru>2022-03-25 12:34:53 +0300
committerheretic <heretic@yandex-team.ru>2022-03-25 12:34:53 +0300
commita41f3739eed6fceb6f62056a7620d220958a47e7 (patch)
tree278103258b510cb4a96761ea79d6ccd397ca05a0 /contrib/libs/grpc/src/compiler/php_generator.cc
parent73d3613a82e5c217fcbe0ab8bbf8120c1ed1af55 (diff)
downloadydb-a41f3739eed6fceb6f62056a7620d220958a47e7.tar.gz
Update grpc to 1.43.2 DTCC-864
ref:50a492c335cda70f458797cf945e49fe739c2715
Diffstat (limited to 'contrib/libs/grpc/src/compiler/php_generator.cc')
-rw-r--r--contrib/libs/grpc/src/compiler/php_generator.cc184
1 files changed, 164 insertions, 20 deletions
diff --git a/contrib/libs/grpc/src/compiler/php_generator.cc b/contrib/libs/grpc/src/compiler/php_generator.cc
index b35d3f2a02..a0c1ef9be1 100644
--- a/contrib/libs/grpc/src/compiler/php_generator.cc
+++ b/contrib/libs/grpc/src/compiler/php_generator.cc
@@ -19,6 +19,7 @@
#include <map>
#include <google/protobuf/compiler/php/php_generator.h>
+
#include "src/compiler/config.h"
#include "src/compiler/generator_helpers.h"
#include "src/compiler/php_generator_helpers.h"
@@ -131,34 +132,177 @@ void PrintMethod(const MethodDescriptor* method, Printer* out) {
out->Print("}\n\n");
}
-// Prints out the service descriptor object
-void PrintService(const ServiceDescriptor* service,
- const TString& class_suffix, Printer* out) {
+void PrintServerMethod(const MethodDescriptor* method, Printer* out) {
map<TString, TString> vars;
+ const Descriptor* input_type = method->input_type();
+ const Descriptor* output_type = method->output_type();
+ vars["service_name"] = method->service()->full_name();
+ vars["method_name"] = method->name();
+ vars["input_type_id"] =
+ MessageIdentifierName(GeneratedClassName(input_type), input_type->file());
+ vars["output_type_id"] = MessageIdentifierName(
+ GeneratedClassName(output_type), output_type->file());
+
out->Print("/**\n");
- out->Print(GetPHPComments(service, " *").c_str());
- out->Print(" */\n");
- vars["name"] = GetPHPServiceClassname(service, class_suffix);
- out->Print(vars, "class $name$ extends \\Grpc\\BaseStub {\n\n");
+ out->Print(GetPHPComments(method, " *").c_str());
+
+ const char* method_template;
+ if (method->client_streaming() && method->server_streaming()) {
+ method_template =
+ " * @param \\Grpc\\ServerCallReader $$reader read client request data "
+ "of \\$input_type_id$\n"
+ " * @param \\Grpc\\ServerCallWriter $$writer write response data of "
+ "\\$output_type_id$\n"
+ " * @param \\Grpc\\ServerContext $$context server request context\n"
+ " * @return void\n"
+ " */\n"
+ "public function $method_name$(\n"
+ " \\Grpc\\ServerCallReader $$reader,\n"
+ " \\Grpc\\ServerCallWriter $$writer,\n"
+ " \\Grpc\\ServerContext $$context\n"
+ "): void {\n"
+ " $$context->setStatus(\\Grpc\\Status::unimplemented());\n"
+ " $$writer->finish();\n"
+ "}\n\n";
+ } else if (method->client_streaming()) {
+ method_template =
+ " * @param \\Grpc\\ServerCallReader $$reader read client request data "
+ "of \\$input_type_id$\n"
+ " * @param \\Grpc\\ServerContext $$context server request context\n"
+ " * @return \\$output_type_id$ for response data, null if if error "
+ "occured\n"
+ " * initial metadata (if any) and status (if not ok) should be set "
+ "to $$context\n"
+ " */\n"
+ "public function $method_name$(\n"
+ " \\Grpc\\ServerCallReader $$reader,\n"
+ " \\Grpc\\ServerContext $$context\n"
+ "): ?\\$output_type_id$ {\n"
+ " $$context->setStatus(\\Grpc\\Status::unimplemented());\n"
+ " return null;\n"
+ "}\n\n";
+ } else if (method->server_streaming()) {
+ method_template =
+ " * @param \\$input_type_id$ $$request client request\n"
+ " * @param \\Grpc\\ServerCallWriter $$writer write response data of "
+ "\\$output_type_id$\n"
+ " * @param \\Grpc\\ServerContext $$context server request context\n"
+ " * @return void\n"
+ " */\n"
+ "public function $method_name$(\n"
+ " \\$input_type_id$ $$request,\n"
+ " \\Grpc\\ServerCallWriter $$writer,\n"
+ " \\Grpc\\ServerContext $$context\n"
+ "): void {\n"
+ " $$context->setStatus(\\Grpc\\Status::unimplemented());\n"
+ " $$writer->finish();\n"
+ "}\n\n";
+ } else {
+ method_template =
+ " * @param \\$input_type_id$ $$request client request\n"
+ " * @param \\Grpc\\ServerContext $$context server request context\n"
+ " * @return \\$output_type_id$ for response data, null if if error "
+ "occured\n"
+ " * initial metadata (if any) and status (if not ok) should be set "
+ "to $$context\n"
+ " */\n"
+ "public function $method_name$(\n"
+ " \\$input_type_id$ $$request,\n"
+ " \\Grpc\\ServerContext $$context\n"
+ "): ?\\$output_type_id$ {\n"
+ " $$context->setStatus(\\Grpc\\Status::unimplemented());\n"
+ " return null;\n"
+ "}\n\n";
+ }
+ out->Print(vars, method_template);
+}
+
+void PrintServerMethodDescriptors(const ServiceDescriptor* service,
+ Printer* out) {
+ map<TString, TString> vars;
+ vars["service_name"] = service->full_name();
+
+ out->Print(
+ "/**\n"
+ " * Get the method descriptors of the service for server registration\n"
+ " *\n"
+ " * @return array of \\Grpc\\MethodDescriptor for the service methods\n"
+ " */\n"
+ "public final function getMethodDescriptors(): array\n{\n");
out->Indent();
out->Indent();
- out->Print(
- "/**\n * @param string $$hostname hostname\n"
- " * @param array $$opts channel options\n"
- " * @param \\Grpc\\Channel $$channel (optional) re-use channel "
- "object\n */\n"
- "public function __construct($$hostname, $$opts, "
- "$$channel = null) {\n");
+ out->Print("return [\n");
out->Indent();
out->Indent();
- out->Print("parent::__construct($$hostname, $$opts, $$channel);\n");
+ for (int i = 0; i < service->method_count(); i++) {
+ auto method = service->method(i);
+ auto input_type = method->input_type();
+ vars["method_name"] = method->name();
+ vars["input_type_id"] = MessageIdentifierName(
+ GeneratedClassName(input_type), input_type->file());
+ if (method->client_streaming() && method->server_streaming()) {
+ vars["call_type"] = "BIDI_STREAMING_CALL";
+ } else if (method->client_streaming()) {
+ vars["call_type"] = "CLIENT_STREAMING_CALL";
+ } else if (method->server_streaming()) {
+ vars["call_type"] = "SERVER_STREAMING_CALL";
+ } else {
+ vars["call_type"] = "UNARY_CALL";
+ }
+ out->Print(
+ vars,
+ "'/$service_name$/$method_name$' => new \\Grpc\\MethodDescriptor(\n"
+ " $$this,\n"
+ " '$method_name$',\n"
+ " '\\$input_type_id$',\n"
+ " \\Grpc\\MethodDescriptor::$call_type$\n"
+ "),\n");
+ }
+ out->Outdent();
+ out->Outdent();
+ out->Print("];\n");
out->Outdent();
out->Outdent();
out->Print("}\n\n");
+}
+
+// Prints out the service descriptor object
+void PrintService(const ServiceDescriptor* service,
+ const TString& class_suffix, bool is_server,
+ Printer* out) {
+ map<TString, TString> vars;
+ out->Print("/**\n");
+ out->Print(GetPHPComments(service, " *").c_str());
+ out->Print(" */\n");
+ vars["name"] = GetPHPServiceClassname(service, class_suffix, is_server);
+ vars["extends"] = is_server ? "" : "extends \\Grpc\\BaseStub ";
+ out->Print(vars, "class $name$ $extends${\n\n");
+ out->Indent();
+ out->Indent();
+ if (!is_server) {
+ out->Print(
+ "/**\n * @param string $$hostname hostname\n"
+ " * @param array $$opts channel options\n"
+ " * @param \\Grpc\\Channel $$channel (optional) re-use channel object\n"
+ " */\n"
+ "public function __construct($$hostname, $$opts, "
+ "$$channel = null) {\n");
+ out->Indent();
+ out->Indent();
+ out->Print("parent::__construct($$hostname, $$opts, $$channel);\n");
+ out->Outdent();
+ out->Outdent();
+ out->Print("}\n\n");
+ }
for (int i = 0; i < service->method_count(); i++) {
- TString method_name =
- grpc_generator::LowercaseFirstLetter(service->method(i)->name());
- PrintMethod(service->method(i), out);
+ if (is_server) {
+ PrintServerMethod(service->method(i), out);
+ } else {
+ PrintMethod(service->method(i), out);
+ }
+ }
+ if (is_server) {
+ PrintServerMethodDescriptors(service, out);
}
out->Outdent();
out->Outdent();
@@ -168,7 +312,7 @@ void PrintService(const ServiceDescriptor* service,
TString GenerateFile(const FileDescriptor* file,
const ServiceDescriptor* service,
- const TString& class_suffix) {
+ const TString& class_suffix, bool is_server) {
TString output;
{
StringOutputStream output_stream(&output);
@@ -188,7 +332,7 @@ TString GenerateFile(const FileDescriptor* file,
vars["package"] = php_namespace;
out.Print(vars, "namespace $package$;\n\n");
- PrintService(service, class_suffix, &out);
+ PrintService(service, class_suffix, is_server, &out);
}
return output;
}