diff options
author | Liu Liu <i@liuliu.me> | 2020-07-31 00:07:45 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-31 10:07:45 +0300 |
commit | a0fb30575c2425512ebc2757910d106e15114b58 (patch) | |
tree | 3ad5f378c2a01e3656c1eff0c1d42571c03a08dc /grpc | |
parent | 77c18c1d6980bb4f7119eb1b75753de95611a315 (diff) | |
download | flatbuffers-a0fb30575c2425512ebc2757910d106e15114b58.tar.gz flatbuffers-a0fb30575c2425512ebc2757910d106e15114b58.tar.bz2 flatbuffers-a0fb30575c2425512ebc2757910d106e15114b58.zip |
[Swift] Append namespace for Swift Grpc implementation (#6049)
* [Swift] Append namespace for Swift Grpc implementation
* Separate ServiceName from ServiceQualifiedName.
Diffstat (limited to 'grpc')
-rw-r--r-- | grpc/src/compiler/schema_interface.h | 3 | ||||
-rw-r--r-- | grpc/src/compiler/swift_generator.cc | 40 |
2 files changed, 27 insertions, 16 deletions
diff --git a/grpc/src/compiler/schema_interface.h b/grpc/src/compiler/schema_interface.h index 9611642e..444b864d 100644 --- a/grpc/src/compiler/schema_interface.h +++ b/grpc/src/compiler/schema_interface.h @@ -62,7 +62,9 @@ struct Method : public CommentHolder { grpc::string *str, grpc::string generator_file_name, bool generate_in_pb2_grpc, grpc::string import_prefix) const = 0; + virtual std::vector<grpc::string> get_input_namespace_parts() const = 0; virtual grpc::string get_input_type_name() const = 0; + virtual std::vector<grpc::string> get_output_namespace_parts() const = 0; virtual grpc::string get_output_type_name() const = 0; virtual grpc::string get_fb_builder() const = 0; @@ -77,6 +79,7 @@ struct Method : public CommentHolder { struct Service : public CommentHolder { virtual ~Service() {} + virtual std::vector<grpc::string> namespace_parts() const = 0; virtual grpc::string name() const = 0; virtual int method_count() const = 0; diff --git a/grpc/src/compiler/swift_generator.cc b/grpc/src/compiler/swift_generator.cc index a6016afb..87332011 100644 --- a/grpc/src/compiler/swift_generator.cc +++ b/grpc/src/compiler/swift_generator.cc @@ -29,8 +29,15 @@ namespace grpc_swift_generator { -grpc::string GenerateMessage(const grpc::string &name) { - return "Message<" + name + ">"; +std::string WrapInNameSpace(const std::vector<std::string> &components, const grpc::string &name) { + std::string qualified_name; + for (auto it = components.begin(); it != components.end(); ++it) + qualified_name += *it + "_"; + return qualified_name + name; +} + +grpc::string GenerateMessage(const std::vector<std::string> &components, const grpc::string &name) { + return "Message<" + WrapInNameSpace(components, name) + ">"; } // MARK: - Client @@ -85,12 +92,12 @@ void GenerateClientProtocol(const grpc_generator::Service *service, grpc_generator::Printer *printer, std::map<grpc::string, grpc::string> *dictonary) { auto vars = *dictonary; - printer->Print(vars, "$ACCESS$ protocol $ServiceName$Service {\n"); + printer->Print(vars, "$ACCESS$ protocol $ServiceQualifiedName$Service {\n"); vars["GenAccess"] = ""; for (auto it = 0; it < service->method_count(); it++) { auto method = service->method(it); - vars["Input"] = GenerateMessage(method->get_input_type_name()); - vars["Output"] = GenerateMessage(method->get_output_type_name()); + vars["Input"] = GenerateMessage(method->get_input_namespace_parts(), method->get_input_type_name()); + vars["Output"] = GenerateMessage(method->get_output_namespace_parts(), method->get_output_type_name()); vars["MethodName"] = method->name(); vars["isNil"] = ""; printer->Print("\t"); @@ -106,8 +113,8 @@ void GenerateClientClass(const grpc_generator::Service *service, std::map<grpc::string, grpc::string> *dictonary) { auto vars = *dictonary; printer->Print(vars, - "$ACCESS$ final class $ServiceName$ServiceClient: GRPCClient, " - "$ServiceName$Service {\n"); + "$ACCESS$ final class $ServiceQualifiedName$ServiceClient: GRPCClient, " + "$ServiceQualifiedName$Service {\n"); printer->Print(vars, "\t$ACCESS$ let channel: GRPCChannel\n"); printer->Print(vars, "\t$ACCESS$ var defaultCallOptions: CallOptions\n"); printer->Print("\n"); @@ -121,8 +128,8 @@ void GenerateClientClass(const grpc_generator::Service *service, vars["GenAccess"] = "public"; for (auto it = 0; it < service->method_count(); it++) { auto method = service->method(it); - vars["Input"] = GenerateMessage(method->get_input_type_name()); - vars["Output"] = GenerateMessage(method->get_output_type_name()); + vars["Input"] = GenerateMessage(method->get_input_namespace_parts(), method->get_input_type_name()); + vars["Output"] = GenerateMessage(method->get_output_namespace_parts(), method->get_output_type_name()); vars["MethodName"] = method->name(); vars["isNil"] = " = nil"; printer->Print("\n\t"); @@ -208,11 +215,11 @@ void GenerateServerProtocol(const grpc_generator::Service *service, std::map<grpc::string, grpc::string> *dictonary) { auto vars = *dictonary; printer->Print( - vars, "$ACCESS$ protocol $ServiceName$Provider: CallHandlerProvider {\n"); + vars, "$ACCESS$ protocol $ServiceQualifiedName$Provider: CallHandlerProvider {\n"); for (auto it = 0; it < service->method_count(); it++) { auto method = service->method(it); - vars["Input"] = GenerateMessage(method->get_input_type_name()); - vars["Output"] = GenerateMessage(method->get_output_type_name()); + vars["Input"] = GenerateMessage(method->get_input_namespace_parts(), method->get_input_type_name()); + vars["Output"] = GenerateMessage(method->get_output_namespace_parts(), method->get_output_type_name()); vars["MethodName"] = method->name(); printer->Print("\t"); auto func = GenerateServerFuncName(method.get()); @@ -221,7 +228,7 @@ void GenerateServerProtocol(const grpc_generator::Service *service, } printer->Print("}\n\n"); - printer->Print(vars, "$ACCESS$ extension $ServiceName$Provider {\n"); + printer->Print(vars, "$ACCESS$ extension $ServiceQualifiedName$Provider {\n"); printer->Print(vars, "\tvar serviceName: String { return " "\"$PATH$$ServiceName$\" }\n"); @@ -231,8 +238,8 @@ void GenerateServerProtocol(const grpc_generator::Service *service, printer->Print("\t\tswitch methodName {\n"); for (auto it = 0; it < service->method_count(); it++) { auto method = service->method(it); - vars["Input"] = GenerateMessage(method->get_input_type_name()); - vars["Output"] = GenerateMessage(method->get_output_type_name()); + vars["Input"] = GenerateMessage(method->get_input_namespace_parts(), method->get_input_type_name()); + vars["Output"] = GenerateMessage(method->get_output_namespace_parts(), method->get_output_type_name()); vars["MethodName"] = method->name(); auto body = GenerateServerExtensionBody(method.get()); printer->Print(vars, body.c_str()); @@ -250,11 +257,12 @@ grpc::string Generate(grpc_generator::File *file, std::map<grpc::string, grpc::string> vars; vars["PATH"] = file->package(); if (!file->package().empty()) { vars["PATH"].append("."); } + vars["ServiceQualifiedName"] = WrapInNameSpace(service->namespace_parts(), service->name()); vars["ServiceName"] = service->name(); vars["ACCESS"] = "public"; auto printer = file->CreatePrinter(&output); printer->Print(vars, - "/// Usage: instantiate $ServiceName$ServiceClient, then call " + "/// Usage: instantiate $ServiceQualifiedName$ServiceClient, then call " "methods of this protocol to make API calls.\n"); GenerateClientProtocol(service, &*printer, &vars); GenerateClientClass(service, &*printer, &vars); |