mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-18 23:28:56 +00:00
(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:
committed by
Wouter van Oortmerssen
parent
35daaf83d3
commit
21b7061963
@@ -45,8 +45,10 @@ cc_test(
|
||||
":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",
|
||||
|
||||
59
tests/prototest/test_suffix.golden
Normal file
59
tests/prototest/test_suffix.golden
Normal file
@@ -0,0 +1,59 @@
|
||||
// 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;
|
||||
}
|
||||
|
||||
63
tests/prototest/test_union_suffix.golden
Normal file
63
tests/prototest/test_union_suffix.golden
Normal file
@@ -0,0 +1,63 @@
|
||||
// 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;
|
||||
}
|
||||
|
||||
@@ -1127,6 +1127,58 @@ void ParseProtoTest() {
|
||||
TEST_EQ_STR(fbs_union.c_str(), goldenunionfile.c_str());
|
||||
}
|
||||
|
||||
// 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
|
||||
@@ -3261,6 +3313,7 @@ int FlatBufferTests() {
|
||||
FixedLengthArrayJsonTest(true);
|
||||
ReflectionTest(flatbuf.data(), flatbuf.size());
|
||||
ParseProtoTest();
|
||||
ParseProtoTestWithSuffix();
|
||||
ParseProtoTestWithIncludes();
|
||||
EvolutionTest();
|
||||
UnionVectorTest();
|
||||
|
||||
Reference in New Issue
Block a user