Protobufs: Added '--oneof-union' option. (#4647)

* Added '--oneof-union' option.

Used with the .proto -> .fbs converter, will translate protobuff oneofs to flatbuffer unions.
Updated proto test to check both methods of converting oneofs.

* Added '--oneof-union' option.

Used with the .proto -> .fbs converter, will translate protobuff oneofs to flatbuffer unions.
Updated proto test to check both methods of converting oneofs.

* FlatBuffers: Moved MakeCamel() into idl_parser.cpp

Removes library dependency on Java/C# generator code.
This commit is contained in:
Nik Hemmings
2018-03-05 16:45:25 +00:00
committed by Wouter van Oortmerssen
parent 77b458bee5
commit fb94af8899
10 changed files with 211 additions and 63 deletions

View File

@@ -36,6 +36,8 @@ table ProtoMessage {
n:proto.test.ProtoMessage_.OtherMessage;
o:[string];
z:proto.test.ImportedMessage;
/// doc comment for r.
r:proto.test.ProtoMessage_.Anonymous0;
}
namespace proto.test.ProtoMessage_;
@@ -46,3 +48,11 @@ table OtherMessage {
b:float = 3.14149;
}
table Anonymous0 {
/// doc comment for s.
s:proto.test.ImportedMessage;
/// doc comment for t on 2
/// lines.
t:proto.test.ProtoMessage_.OtherMessage;
}

View File

@@ -42,4 +42,12 @@ message ProtoMessage {
optional OtherMessage n = 12;
repeated string o = 14;
optional ImportedMessage z = 16;
/// doc comment for r.
oneof r {
/// doc comment for s.
ImportedMessage s = 17;
/// doc comment for t on 2
/// lines.
OtherMessage t = 18;
}
}

View File

@@ -0,0 +1,62 @@
// Generated from test.proto
namespace proto.test;
/// Enum doc comment.
enum ProtoEnum : int {
FOO = 1,
/// Enum 2nd value doc comment misaligned.
BAR = 5,
}
namespace proto.test.ProtoMessage_;
union RUnion {
/// doc comment for s.
proto.test.ImportedMessage,
/// doc comment for t on 2
/// lines.
proto.test.ProtoMessage_.OtherMessage,
}
namespace proto.test;
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.ProtoMessage_.OtherMessage;
o:[string];
z:proto.test.ImportedMessage;
/// doc comment for r.
r:proto.test.ProtoMessage_.RUnion;
}
namespace proto.test.ProtoMessage_;
table OtherMessage {
a:double;
/// doc comment for b.
b:float = 3.14149;
}

View File

@@ -863,6 +863,7 @@ void ParseProtoTest() {
// 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),
@@ -871,6 +872,11 @@ void ParseProtoTest() {
flatbuffers::LoadFile((test_data_path + "prototest/test.golden").c_str(),
false, &goldenfile),
true);
TEST_EQ(
flatbuffers::LoadFile((test_data_path +
"prototest/test_union.golden").c_str(),
false, &goldenunionfile),
true);
flatbuffers::IDLOptions opts;
opts.include_dependence_headers = false;
@@ -889,6 +895,19 @@ void ParseProtoTest() {
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());
}
template<typename T>