bool java_primitive_has_method;
std::vector<std::string> cpp_includes;
std::string cpp_std;
+ std::string proto_namespace_suffix;
// Possible options for the more general generator below.
enum Language {
" 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"
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") {
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;
":prototest/test.golden",
":prototest/test.proto",
":prototest/test_include.golden",
+ ":prototest/test_suffix.golden",
":prototest/test_union.golden",
":prototest/test_union_include.golden",
+ ":prototest/test_union_suffix.golden",
":unicode_test.json",
":union_vector/union_vector.fbs",
":union_vector/union_vector.json",
--- /dev/null
+// Generated from test.proto
+
+namespace proto.test.test_namespace_suffix;
+
+/// Enum doc comment.
+enum ProtoEnum : int {
+ NUL = 0,
+ FOO = 1,
+ /// Enum 2nd value doc comment misaligned.
+ BAR = 5,
+}
+
+table ImportedMessage {
+ a:int;
+}
+
+/// 2nd table doc comment with
+/// many lines.
+table ProtoMessage {
+ c:int = 16;
+ d:long;
+ p:uint;
+ e:ulong;
+ /// doc comment for f.
+ f:int = -1;
+ g:long;
+ h:uint;
+ q:ulong;
+ i:int;
+ j:long;
+ /// doc comment for k.
+ k:bool;
+ /// doc comment for l on 2
+ /// lines
+ l:string (required);
+ m:[ubyte];
+ n:proto.test.test_namespace_suffix.ProtoMessage_.OtherMessage;
+ o:[string];
+ z:proto.test.test_namespace_suffix.ImportedMessage;
+ /// doc comment for r.
+ r:proto.test.test_namespace_suffix.ProtoMessage_.Anonymous0;
+}
+
+namespace proto.test.test_namespace_suffix.ProtoMessage_;
+
+table OtherMessage {
+ a:double;
+ /// doc comment for b.
+ b:float = 3.14149;
+}
+
+table Anonymous0 {
+ /// doc comment for s.
+ s:proto.test.test_namespace_suffix.ImportedMessage;
+ /// doc comment for t on 2
+ /// lines.
+ t:proto.test.test_namespace_suffix.ProtoMessage_.OtherMessage;
+}
+
--- /dev/null
+// Generated from test.proto
+
+namespace proto.test.test_namespace_suffix;
+
+/// Enum doc comment.
+enum ProtoEnum : int {
+ NUL = 0,
+ FOO = 1,
+ /// Enum 2nd value doc comment misaligned.
+ BAR = 5,
+}
+
+namespace proto.test.test_namespace_suffix.ProtoMessage_;
+
+union RUnion {
+ /// doc comment for s.
+ proto.test.test_namespace_suffix.ImportedMessage,
+ /// doc comment for t on 2
+ /// lines.
+ proto.test.test_namespace_suffix.ProtoMessage_.OtherMessage,
+}
+
+namespace proto.test.test_namespace_suffix;
+
+table ImportedMessage {
+ a:int;
+}
+
+/// 2nd table doc comment with
+/// many lines.
+table ProtoMessage {
+ c:int = 16;
+ d:long;
+ p:uint;
+ e:ulong;
+ /// doc comment for f.
+ f:int = -1;
+ g:long;
+ h:uint;
+ q:ulong;
+ i:int;
+ j:long;
+ /// doc comment for k.
+ k:bool;
+ /// doc comment for l on 2
+ /// lines
+ l:string (required);
+ m:[ubyte];
+ n:proto.test.test_namespace_suffix.ProtoMessage_.OtherMessage;
+ o:[string];
+ z:proto.test.test_namespace_suffix.ImportedMessage;
+ /// doc comment for r.
+ r:proto.test.test_namespace_suffix.ProtoMessage_.RUnion;
+}
+
+namespace proto.test.test_namespace_suffix.ProtoMessage_;
+
+table OtherMessage {
+ a:double;
+ /// doc comment for b.
+ b:float = 3.14149;
+}
+
}
// Parse a .proto schema, output as .fbs
+void ParseProtoTestWithSuffix() {
+ // load the .proto and the golden file from disk
+ std::string protofile;
+ std::string goldenfile;
+ std::string goldenunionfile;
+ TEST_EQ(
+ flatbuffers::LoadFile((test_data_path + "prototest/test.proto").c_str(),
+ false, &protofile),
+ true);
+ TEST_EQ(
+ flatbuffers::LoadFile((test_data_path + "prototest/test_suffix.golden").c_str(),
+ false, &goldenfile),
+ true);
+ TEST_EQ(flatbuffers::LoadFile(
+ (test_data_path + "prototest/test_union_suffix.golden").c_str(), false,
+ &goldenunionfile),
+ true);
+
+ flatbuffers::IDLOptions opts;
+ opts.include_dependence_headers = false;
+ opts.proto_mode = true;
+ opts.proto_namespace_suffix = "test_namespace_suffix";
+
+ // Parse proto.
+ flatbuffers::Parser parser(opts);
+ auto protopath = test_data_path + "prototest/";
+ const char *include_directories[] = { protopath.c_str(), nullptr };
+ TEST_EQ(parser.Parse(protofile.c_str(), include_directories), true);
+
+ // Generate fbs.
+ auto fbs = flatbuffers::GenerateFBS(parser, "test");
+
+ // Ensure generated file is parsable.
+ flatbuffers::Parser parser2;
+ TEST_EQ(parser2.Parse(fbs.c_str(), nullptr), true);
+ TEST_EQ_STR(fbs.c_str(), goldenfile.c_str());
+
+ // Parse proto with --oneof-union option.
+ opts.proto_oneof_union = true;
+ flatbuffers::Parser parser3(opts);
+ TEST_EQ(parser3.Parse(protofile.c_str(), include_directories), true);
+
+ // Generate fbs.
+ auto fbs_union = flatbuffers::GenerateFBS(parser3, "test");
+
+ // Ensure generated file is parsable.
+ flatbuffers::Parser parser4;
+ TEST_EQ(parser4.Parse(fbs_union.c_str(), nullptr), true);
+ TEST_EQ_STR(fbs_union.c_str(), goldenunionfile.c_str());
+}
+
+// Parse a .proto schema, output as .fbs
void ParseProtoTestWithIncludes() {
// load the .proto and the golden file from disk
std::string protofile;
FixedLengthArrayJsonTest(true);
ReflectionTest(flatbuf.data(), flatbuf.size());
ParseProtoTest();
+ ParseProtoTestWithSuffix();
ParseProtoTestWithIncludes();
EvolutionTest();
UnionVectorTest();