Commit ea06768a authored by Kapil Sharma's avatar Kapil Sharma Committed by Wouter van Oortmerssen

Go GRPC generator Updates - Fixes #4787 (#4797)

* Fix for #4787

- Updated the grpc generator for go to use full namespace for service
rpc method names

* Formatting Fix

- Set to Google Style Formatting
parent 741c6305
...@@ -110,7 +110,7 @@ void GenerateServerMethod(const grpc_generator::Method *method, grpc_generator:: ...@@ -110,7 +110,7 @@ void GenerateServerMethod(const grpc_generator::Method *method, grpc_generator::
vars["Method"] = exportName(method->name()); vars["Method"] = exportName(method->name());
vars["Request"] = method->get_input_type_name(); vars["Request"] = method->get_input_type_name();
vars["Response"] = (vars["CustomMethodIO"] == "") ? method->get_output_type_name() : vars["CustomMethodIO"]; vars["Response"] = (vars["CustomMethodIO"] == "") ? method->get_output_type_name() : vars["CustomMethodIO"];
vars["FullMethodName"] = "/" + vars["Package"] + "." + vars["Service"] + "/" + vars["Method"]; vars["FullMethodName"] = "/" + vars["ServicePrefix"] + "." + vars["Service"] + "/" + vars["Method"];
vars["Handler"] = "_" + vars["Service"] + "_" + vars["Method"] + "_Handler"; vars["Handler"] = "_" + vars["Service"] + "_" + vars["Method"] + "_Handler";
if (method->NoStreaming()) { if (method->NoStreaming()) {
printer->Print(vars, "func $Handler$(srv interface{}, ctx $context$.Context,\n\tdec func(interface{}) error, interceptor $grpc$.UnaryServerInterceptor) (interface{}, error) {\n"); printer->Print(vars, "func $Handler$(srv interface{}, ctx $context$.Context,\n\tdec func(interface{}) error, interceptor $grpc$.UnaryServerInterceptor) (interface{}, error) {\n");
...@@ -223,7 +223,7 @@ void GenerateClientMethod(const grpc_generator::Method *method, grpc_generator:: ...@@ -223,7 +223,7 @@ void GenerateClientMethod(const grpc_generator::Method *method, grpc_generator::
vars["Method"] = exportName(method->name()); vars["Method"] = exportName(method->name());
vars["Request"] = (vars["CustomMethodIO"] == "") ? method->get_input_type_name() : vars["CustomMethodIO"]; vars["Request"] = (vars["CustomMethodIO"] == "") ? method->get_input_type_name() : vars["CustomMethodIO"];
vars["Response"] = method->get_output_type_name(); vars["Response"] = method->get_output_type_name();
vars["FullMethodName"] = "/" + vars["Package"] + "." + vars["Service"] + "/" + vars["Method"]; vars["FullMethodName"] = "/" + vars["ServicePrefix"] + "." + vars["Service"] + "/" + vars["Method"];
if (method->NoStreaming()) { if (method->NoStreaming()) {
printer->Print(vars, "out := new($Response$)\n"); printer->Print(vars, "out := new($Response$)\n");
printer->Print(vars, "err := $grpc$.Invoke(ctx, \"$FullMethodName$\", in, out, c.cc, opts...)\n"); printer->Print(vars, "err := $grpc$.Invoke(ctx, \"$FullMethodName$\", in, out, c.cc, opts...)\n");
...@@ -374,7 +374,7 @@ void GenerateService(const grpc_generator::Service *service, grpc_generator::Pri ...@@ -374,7 +374,7 @@ void GenerateService(const grpc_generator::Service *service, grpc_generator::Pri
//Service Descriptor //Service Descriptor
printer->Print(vars, "var $ServiceDesc$ = $grpc$.ServiceDesc{\n"); printer->Print(vars, "var $ServiceDesc$ = $grpc$.ServiceDesc{\n");
printer->Indent(); printer->Indent();
printer->Print(vars, "ServiceName: \"$Package$.$Service$\",\n"); printer->Print(vars, "ServiceName: \"$ServicePrefix$.$Service$\",\n");
printer->Print(vars, "HandlerType: (*$Service$Server)(nil),\n"); printer->Print(vars, "HandlerType: (*$Service$Server)(nil),\n");
printer->Print(vars, "Methods: []$grpc$.MethodDesc{\n"); printer->Print(vars, "Methods: []$grpc$.MethodDesc{\n");
printer->Indent(); printer->Indent();
...@@ -433,6 +433,7 @@ grpc::string GenerateServiceSource(grpc_generator::File *file, ...@@ -433,6 +433,7 @@ grpc::string GenerateServiceSource(grpc_generator::File *file,
auto printer = p.get(); auto printer = p.get();
std::map<grpc::string, grpc::string> vars; std::map<grpc::string, grpc::string> vars;
vars["Package"] = parameters->package_name; vars["Package"] = parameters->package_name;
vars["ServicePrefix"] = parameters->service_prefix;
vars["grpc"] = "grpc"; vars["grpc"] = "grpc";
vars["context"] = "context"; vars["context"] = "context";
GenerateImports(file, printer, vars); GenerateImports(file, printer, vars);
......
...@@ -49,6 +49,9 @@ struct Parameters { ...@@ -49,6 +49,9 @@ struct Parameters {
//Package name for the service //Package name for the service
grpc::string package_name; grpc::string package_name;
//Prefix for RPC Calls
grpc::string service_prefix;
}; };
// Return the source of the generated service file. // Return the source of the generated service file.
......
...@@ -28,14 +28,16 @@ ...@@ -28,14 +28,16 @@
#if defined(_MSC_VER) #if defined(_MSC_VER)
# pragma warning(push) # pragma warning(push)
# pragma warning(disable : 4512) // C4512: 'class' : assignment operator could # pragma warning(disable : 4512) // C4512: 'class' : assignment operator could
// not be generated // not be generated
#endif #endif
namespace flatbuffers { namespace flatbuffers {
class FlatBufMethod : public grpc_generator::Method { class FlatBufMethod : public grpc_generator::Method {
public: public:
enum Streaming { kNone, kClient, kServer, kBiDi }; enum Streaming {
kNone, kClient, kServer, kBiDi
};
FlatBufMethod(const RPCCall *method) : method_(method) { FlatBufMethod(const RPCCall *method) : method_(method) {
streaming_ = kNone; streaming_ = kNone;
...@@ -48,7 +50,9 @@ class FlatBufMethod : public grpc_generator::Method { ...@@ -48,7 +50,9 @@ class FlatBufMethod : public grpc_generator::Method {
} }
grpc::string GetLeadingComments(const grpc::string) const { return ""; } grpc::string GetLeadingComments(const grpc::string) const { return ""; }
grpc::string GetTrailingComments(const grpc::string) const { return ""; } grpc::string GetTrailingComments(const grpc::string) const { return ""; }
std::vector<grpc::string> GetAllComments() const { std::vector<grpc::string> GetAllComments() const {
return method_->doc_comment; return method_->doc_comment;
} }
...@@ -60,6 +64,7 @@ class FlatBufMethod : public grpc_generator::Method { ...@@ -60,6 +64,7 @@ class FlatBufMethod : public grpc_generator::Method {
} }
std::string get_input_type_name() const { return (*method_->request).name; } std::string get_input_type_name() const { return (*method_->request).name; }
std::string get_output_type_name() const { return (*method_->response).name; } std::string get_output_type_name() const { return (*method_->response).name; }
bool get_module_and_message_path_input(grpc::string * /*str*/, bool get_module_and_message_path_input(grpc::string * /*str*/,
...@@ -80,8 +85,11 @@ class FlatBufMethod : public grpc_generator::Method { ...@@ -80,8 +85,11 @@ class FlatBufMethod : public grpc_generator::Method {
std::string output_type_name() const { return GRPCType(*method_->response); } std::string output_type_name() const { return GRPCType(*method_->response); }
bool NoStreaming() const { return streaming_ == kNone; } bool NoStreaming() const { return streaming_ == kNone; }
bool ClientStreaming() const { return streaming_ == kClient; } bool ClientStreaming() const { return streaming_ == kClient; }
bool ServerStreaming() const { return streaming_ == kServer; } bool ServerStreaming() const { return streaming_ == kServer; }
bool BidiStreaming() const { return streaming_ == kBiDi; } bool BidiStreaming() const { return streaming_ == kBiDi; }
private: private:
...@@ -94,7 +102,9 @@ class FlatBufService : public grpc_generator::Service { ...@@ -94,7 +102,9 @@ class FlatBufService : public grpc_generator::Service {
FlatBufService(const ServiceDef *service) : service_(service) {} FlatBufService(const ServiceDef *service) : service_(service) {}
grpc::string GetLeadingComments(const grpc::string) const { return ""; } grpc::string GetLeadingComments(const grpc::string) const { return ""; }
grpc::string GetTrailingComments(const grpc::string) const { return ""; } grpc::string GetTrailingComments(const grpc::string) const { return ""; }
std::vector<grpc::string> GetAllComments() const { std::vector<grpc::string> GetAllComments() const {
return service_->doc_comment; return service_->doc_comment;
} }
...@@ -158,6 +168,7 @@ class FlatBufPrinter : public grpc_generator::Printer { ...@@ -158,6 +168,7 @@ class FlatBufPrinter : public grpc_generator::Printer {
} }
void Indent() { indent_++; } void Indent() { indent_++; }
void Outdent() { void Outdent() {
indent_--; indent_--;
FLATBUFFERS_ASSERT(indent_ >= 0); FLATBUFFERS_ASSERT(indent_ >= 0);
...@@ -171,25 +182,32 @@ class FlatBufPrinter : public grpc_generator::Printer { ...@@ -171,25 +182,32 @@ class FlatBufPrinter : public grpc_generator::Printer {
class FlatBufFile : public grpc_generator::File { class FlatBufFile : public grpc_generator::File {
public: public:
enum Language { kLanguageGo, kLanguageCpp, kLanguageJava }; enum Language {
kLanguageGo, kLanguageCpp, kLanguageJava
};
FlatBufFile(const Parser &parser, const std::string &file_name, FlatBufFile(const Parser &parser, const std::string &file_name,
Language language) Language language)
: parser_(parser), file_name_(file_name), language_(language) {} : parser_(parser), file_name_(file_name), language_(language) {}
FlatBufFile &operator=(const FlatBufFile &); FlatBufFile &operator=(const FlatBufFile &);
grpc::string GetLeadingComments(const grpc::string) const { return ""; } grpc::string GetLeadingComments(const grpc::string) const { return ""; }
grpc::string GetTrailingComments(const grpc::string) const { return ""; } grpc::string GetTrailingComments(const grpc::string) const { return ""; }
std::vector<grpc::string> GetAllComments() const { std::vector<grpc::string> GetAllComments() const {
return std::vector<grpc::string>(); return std::vector<grpc::string>();
} }
std::string filename() const { return file_name_; } std::string filename() const { return file_name_; }
std::string filename_without_ext() const { std::string filename_without_ext() const {
return StripExtension(file_name_); return StripExtension(file_name_);
} }
std::string message_header_ext() const { return "_generated.h"; } std::string message_header_ext() const { return "_generated.h"; }
std::string service_header_ext() const { return ".grpc.fb.h"; } std::string service_header_ext() const { return ".grpc.fb.h"; }
std::string package() const { std::string package() const {
...@@ -252,6 +270,7 @@ class GoGRPCGenerator : public flatbuffers::BaseGenerator { ...@@ -252,6 +270,7 @@ class GoGRPCGenerator : public flatbuffers::BaseGenerator {
auto service = file.service(i); auto service = file.service(i);
const Definition *def = parser_.services_.vec[i]; const Definition *def = parser_.services_.vec[i];
p.package_name = LastNamespacePart(*(def->defined_namespace)); p.package_name = LastNamespacePart(*(def->defined_namespace));
p.service_prefix = def->defined_namespace->GetFullyQualifiedName(""); // file.package();
std::string output = std::string output =
grpc_go_generator::GenerateServiceSource(&file, service.get(), &p); grpc_go_generator::GenerateServiceSource(&file, service.get(), &p);
std::string filename = std::string filename =
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment