mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-09 14:46:26 +00:00
C++/Go: Update gRPC related code to work with gRPC 1.3+ (#4305)
* Don't fail the build on unused parameters gRPC headers have unused parameters so this breaks the test build. * Pull in updated compiler files from gRPC There have been some API breaks in gRPC lately. This commit pulls in the most recent version of the files in this repo that are just copied from gRPC. * Modify the gRPC files so that they can work with Flatbuffers The files taken from gRPC do not work out-of-the-box with Flatbuffers. This commit modifies them so that they work. Hopefully this commit will be able to serve as a guide or maybe even be cherry-picked on top of new versions of those files as newer versions from gRPC are pulled in. * Adjust the rest of Flatbuffers to work with the new gRPC * Change idl_gen_grpc.cpp to work with the new API * Add missing #include in flatbuffers/grpc.h * Run tests/generate_code.sh and check in the results * Don't link with grpc++_unsecure and (secure) grpc. That's just weird * Revert unrelated JS/TS test changes * Simplify compiler/config.h There is no need to import this file from gRPC. In fact, it probably makes sense to not do so, since it seems to be intended to have project specific configuration in it. * Don't emit C++ types in the Go gRPC code generator * Don't emit C++ #includes in generated gRPC code Before this PR, there was a Go-specific additional_includes method in schema_interface.h, which is shared with the gRPC repo. The additional parameter to FlatBufFile in idl_gen_grpc.cpp makes that unnecessary, which means we need less Flatbuffer-specific changes in gRPC.
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
c7bfe06c54
commit
9aeeddf5ac
@@ -46,30 +46,52 @@ 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 std::vector<grpc::string>();
|
||||
}
|
||||
|
||||
std::string name() const { return method_->name; }
|
||||
|
||||
std::string GRPCType(const StructDef &sd) const {
|
||||
return "flatbuffers::BufferRef<" + sd.name + ">";
|
||||
}
|
||||
|
||||
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*/, grpc::string /*generator_file_name*/,
|
||||
bool /*generate_in_pb2_grpc*/, grpc::string /*import_prefix*/) const {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool get_module_and_message_path_output(
|
||||
grpc::string * /*str*/, grpc::string /*generator_file_name*/,
|
||||
bool /*generate_in_pb2_grpc*/, grpc::string /*import_prefix*/) const {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string input_type_name() const {
|
||||
return GRPCType(*method_->request);
|
||||
}
|
||||
|
||||
std::string output_type_name() const {
|
||||
return GRPCType(*method_->response);
|
||||
}
|
||||
|
||||
std::string input_name() const {
|
||||
return (*method_->request).name;
|
||||
}
|
||||
|
||||
std::string output_name() const {
|
||||
return (*method_->response).name;
|
||||
}
|
||||
|
||||
bool NoStreaming() const { return streaming_ == kNone; }
|
||||
bool ClientOnlyStreaming() const { return streaming_ == kClient; }
|
||||
bool ServerOnlyStreaming() const { return streaming_ == kServer; }
|
||||
bool ClientStreaming() const { return streaming_ == kClient; }
|
||||
bool ServerStreaming() const { return streaming_ == kServer; }
|
||||
bool BidiStreaming() const { return streaming_ == kBiDi; }
|
||||
|
||||
private:
|
||||
@@ -81,6 +103,16 @@ class FlatBufService : public grpc_generator::Service {
|
||||
public:
|
||||
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 std::vector<grpc::string>();
|
||||
}
|
||||
|
||||
std::string name() const { return service_->name; }
|
||||
|
||||
int method_count() const {
|
||||
@@ -150,10 +182,26 @@ class FlatBufPrinter : public grpc_generator::Printer {
|
||||
|
||||
class FlatBufFile : public grpc_generator::File {
|
||||
public:
|
||||
FlatBufFile(const Parser &parser, const std::string &file_name)
|
||||
: parser_(parser), file_name_(file_name) {}
|
||||
enum Language {
|
||||
kLanguageGo,
|
||||
kLanguageCpp
|
||||
};
|
||||
|
||||
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_);
|
||||
@@ -171,11 +219,15 @@ class FlatBufFile : public grpc_generator::File {
|
||||
}
|
||||
|
||||
std::string additional_headers() const {
|
||||
return "#include \"flatbuffers/grpc.h\"\n";
|
||||
}
|
||||
|
||||
std::string additional_imports() const {
|
||||
return "import \"github.com/google/flatbuffers/go\"";
|
||||
switch (language_) {
|
||||
case kLanguageCpp: {
|
||||
return "#include \"flatbuffers/grpc.h\"\n";
|
||||
}
|
||||
case kLanguageGo: {
|
||||
return "import \"github.com/google/flatbuffers/go\"";
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
int service_count() const {
|
||||
@@ -195,6 +247,7 @@ class FlatBufFile : public grpc_generator::File {
|
||||
private:
|
||||
const Parser &parser_;
|
||||
const std::string &file_name_;
|
||||
const Language language_;
|
||||
};
|
||||
|
||||
class GoGRPCGenerator : public flatbuffers::BaseGenerator {
|
||||
@@ -205,7 +258,7 @@ class GoGRPCGenerator : public flatbuffers::BaseGenerator {
|
||||
parser_(parser), path_(path), file_name_(file_name) {}
|
||||
|
||||
bool generate() {
|
||||
FlatBufFile file(parser_, file_name_);
|
||||
FlatBufFile file(parser_, file_name_, FlatBufFile::kLanguageGo);
|
||||
grpc_go_generator::Parameters p;
|
||||
p.custom_method_io_type = "flatbuffers.Builder";
|
||||
for (int i = 0; i < file.service_count(); i++) {
|
||||
@@ -252,7 +305,7 @@ bool GenerateCppGRPC(const Parser &parser,
|
||||
// TODO(wvo): make the other parameters in this struct configurable.
|
||||
generator_parameters.use_system_headers = true;
|
||||
|
||||
FlatBufFile fbfile(parser, file_name);
|
||||
FlatBufFile fbfile(parser, file_name, FlatBufFile::kLanguageCpp);
|
||||
|
||||
std::string header_code =
|
||||
grpc_cpp_generator::GetHeaderPrologue(&fbfile, generator_parameters) +
|
||||
|
||||
Reference in New Issue
Block a user