(Optionally) add an additional suffix namespace to generated fbs files. (#5698)

This change allows for the generation of fbs files (from proto) that
don't contain name collisions with the protobuf generated C++ code,
allowing both the proto and fbs message types to be linked into the same binary.
This commit is contained in:
Michael Beardsworth
2020-01-06 10:00:59 -08:00
committed by Wouter van Oortmerssen
parent 35daaf83d3
commit 21b7061963
7 changed files with 190 additions and 0 deletions

View File

@@ -134,6 +134,8 @@ std::string FlatCompiler::GetUsageString(const char *program_name) const {
" This may crash flatc given a mismatched schema.\n"
" --size-prefixed Input binaries are size prefixed buffers.\n"
" --proto Input is a .proto, translate to .fbs.\n"
" --proto-namespace-suffix Add this namespace to any flatbuffers generated\n"
" SUFFIX from protobufs.\n"
" --oneof-union Translate .proto oneofs to flatbuffer unions.\n"
" --grpc Generate GRPC interfaces for the specified languages.\n"
" --schema Serialize schemas instead of JSON (use with -b).\n"
@@ -299,6 +301,9 @@ int FlatCompiler::Compile(int argc, const char **argv) {
binary_files_from = filenames.size();
} else if (arg == "--proto") {
opts.proto_mode = true;
} else if (arg == "--proto-namespace-suffix") {
if (++argi >= argc) Error("missing namespace suffix" + arg, true);
opts.proto_namespace_suffix = argv[argi];
} else if (arg == "--oneof-union") {
opts.proto_oneof_union = true;
} else if (arg == "--schema") {

View File

@@ -63,6 +63,13 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name) {
for (size_t i = 0; i < ns.from_table; i++) {
ns.components[ns.components.size() - 1 - i] += "_";
}
if (parser.opts.proto_mode && !parser.opts.proto_namespace_suffix.empty()) {
// Since we know that all these namespaces come from a .proto, and all are
// being converted, we can simply apply this suffix to all of them.
ns.components.insert(ns.components.end() - ns.from_table,
parser.opts.proto_namespace_suffix);
}
}
std::string schema;