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
This commit is contained in:
Kapil Sharma 2018-06-27 11:57:40 -05:00 committed by Wouter van Oortmerssen
parent 741c63052d
commit ea06768ad1
3 changed files with 38 additions and 15 deletions

View File

@ -110,7 +110,7 @@ void GenerateServerMethod(const grpc_generator::Method *method, grpc_generator::
vars["Method"] = exportName(method->name());
vars["Request"] = method->get_input_type_name();
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";
if (method->NoStreaming()) {
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::
vars["Method"] = exportName(method->name());
vars["Request"] = (vars["CustomMethodIO"] == "") ? method->get_input_type_name() : vars["CustomMethodIO"];
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()) {
printer->Print(vars, "out := new($Response$)\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
//Service Descriptor
printer->Print(vars, "var $ServiceDesc$ = $grpc$.ServiceDesc{\n");
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, "Methods: []$grpc$.MethodDesc{\n");
printer->Indent();
@ -433,6 +433,7 @@ grpc::string GenerateServiceSource(grpc_generator::File *file,
auto printer = p.get();
std::map<grpc::string, grpc::string> vars;
vars["Package"] = parameters->package_name;
vars["ServicePrefix"] = parameters->service_prefix;
vars["grpc"] = "grpc";
vars["context"] = "context";
GenerateImports(file, printer, vars);

View File

@ -49,6 +49,9 @@ struct Parameters {
//Package name for the service
grpc::string package_name;
//Prefix for RPC Calls
grpc::string service_prefix;
};
// Return the source of the generated service file.

View File

@ -28,14 +28,16 @@
#if defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable : 4512) // C4512: 'class' : assignment operator could
// not be generated
// not be generated
#endif
namespace flatbuffers {
class FlatBufMethod : public grpc_generator::Method {
public:
enum Streaming { kNone, kClient, kServer, kBiDi };
enum Streaming {
kNone, kClient, kServer, kBiDi
};
FlatBufMethod(const RPCCall *method) : method_(method) {
streaming_ = kNone;
@ -48,7 +50,9 @@ class FlatBufMethod : public grpc_generator::Method {
}
grpc::string GetLeadingComments(const grpc::string) const { return ""; }
grpc::string GetTrailingComments(const grpc::string) const { return ""; }
std::vector<grpc::string> GetAllComments() const {
return method_->doc_comment;
}
@ -60,6 +64,7 @@ class FlatBufMethod : public grpc_generator::Method {
}
std::string get_input_type_name() const { return (*method_->request).name; }
std::string get_output_type_name() const { return (*method_->response).name; }
bool get_module_and_message_path_input(grpc::string * /*str*/,
@ -80,8 +85,11 @@ class FlatBufMethod : public grpc_generator::Method {
std::string output_type_name() const { return GRPCType(*method_->response); }
bool NoStreaming() const { return streaming_ == kNone; }
bool ClientStreaming() const { return streaming_ == kClient; }
bool ServerStreaming() const { return streaming_ == kServer; }
bool BidiStreaming() const { return streaming_ == kBiDi; }
private:
@ -94,7 +102,9 @@ class FlatBufService : public grpc_generator::Service {
FlatBufService(const ServiceDef *service) : service_(service) {}
grpc::string GetLeadingComments(const grpc::string) const { return ""; }
grpc::string GetTrailingComments(const grpc::string) const { return ""; }
std::vector<grpc::string> GetAllComments() const {
return service_->doc_comment;
}
@ -158,9 +168,10 @@ class FlatBufPrinter : public grpc_generator::Printer {
}
void Indent() { indent_++; }
void Outdent() {
indent_--;
FLATBUFFERS_ASSERT(indent_ >= 0);
FLATBUFFERS_ASSERT(indent_ >= 0);
}
private:
@ -171,25 +182,32 @@ class FlatBufPrinter : public grpc_generator::Printer {
class FlatBufFile : public grpc_generator::File {
public:
enum Language { kLanguageGo, kLanguageCpp, kLanguageJava };
enum Language {
kLanguageGo, kLanguageCpp, kLanguageJava
};
FlatBufFile(const Parser &parser, const std::string &file_name,
Language language)
: parser_(parser), file_name_(file_name), language_(language) {}
FlatBufFile &operator=(const FlatBufFile &);
grpc::string GetLeadingComments(const grpc::string) const { return ""; }
grpc::string GetTrailingComments(const grpc::string) const { return ""; }
std::vector<grpc::string> GetAllComments() const {
return std::vector<grpc::string>();
}
std::string filename() const { return file_name_; }
std::string filename_without_ext() const {
return StripExtension(file_name_);
}
std::string message_header_ext() const { return "_generated.h"; }
std::string service_header_ext() const { return ".grpc.fb.h"; }
std::string package() const {
@ -252,6 +270,7 @@ class GoGRPCGenerator : public flatbuffers::BaseGenerator {
auto service = file.service(i);
const Definition *def = parser_.services_.vec[i];
p.package_name = LastNamespacePart(*(def->defined_namespace));
p.service_prefix = def->defined_namespace->GetFullyQualifiedName(""); // file.package();
std::string output =
grpc_go_generator::GenerateServiceSource(&file, service.get(), &p);
std::string filename =
@ -294,20 +313,20 @@ bool GenerateCppGRPC(const Parser &parser, const std::string &path,
std::string header_code =
grpc_cpp_generator::GetHeaderPrologue(&fbfile, generator_parameters) +
grpc_cpp_generator::GetHeaderIncludes(&fbfile, generator_parameters) +
grpc_cpp_generator::GetHeaderServices(&fbfile, generator_parameters) +
grpc_cpp_generator::GetHeaderEpilogue(&fbfile, generator_parameters);
grpc_cpp_generator::GetHeaderIncludes(&fbfile, generator_parameters) +
grpc_cpp_generator::GetHeaderServices(&fbfile, generator_parameters) +
grpc_cpp_generator::GetHeaderEpilogue(&fbfile, generator_parameters);
std::string source_code =
grpc_cpp_generator::GetSourcePrologue(&fbfile, generator_parameters) +
grpc_cpp_generator::GetSourceIncludes(&fbfile, generator_parameters) +
grpc_cpp_generator::GetSourceServices(&fbfile, generator_parameters) +
grpc_cpp_generator::GetSourceEpilogue(&fbfile, generator_parameters);
grpc_cpp_generator::GetSourceIncludes(&fbfile, generator_parameters) +
grpc_cpp_generator::GetSourceServices(&fbfile, generator_parameters) +
grpc_cpp_generator::GetSourceEpilogue(&fbfile, generator_parameters);
return flatbuffers::SaveFile((path + file_name + ".grpc.fb.h").c_str(),
header_code, false) &&
flatbuffers::SaveFile((path + file_name + ".grpc.fb.cc").c_str(),
source_code, false);
flatbuffers::SaveFile((path + file_name + ".grpc.fb.cc").c_str(),
source_code, false);
}
class JavaGRPCGenerator : public flatbuffers::BaseGenerator {