mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-18 01:36:31 +00:00
Added .proto parsing and convertion to .fbs.
Bug: 15777858 Change-Id: Iabef9b8c8044e593bb89510feebdee00d2f1840b Tested: on Linux and Windows.
This commit is contained in:
@@ -139,7 +139,8 @@ func (rcv *Monster) TestarrayofstringLength() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
/// an example documentation comment: this will end up in the generated code multiline too
|
||||
/// an example documentation comment: this will end up in the generated code
|
||||
/// multiline too
|
||||
func (rcv *Monster) Testarrayoftables(obj *Monster, j int) bool {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(26))
|
||||
if o != 0 {
|
||||
|
||||
@@ -44,7 +44,7 @@ inline const char **EnumNamesAny() {
|
||||
|
||||
inline const char *EnumNameAny(Any e) { return EnumNamesAny()[e]; }
|
||||
|
||||
inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, uint8_t type);
|
||||
inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, Any type);
|
||||
|
||||
MANUALLY_ALIGNED_STRUCT(2) Test {
|
||||
private:
|
||||
@@ -228,7 +228,7 @@ inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder
|
||||
return builder_.Finish();
|
||||
}
|
||||
|
||||
inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, uint8_t type) {
|
||||
inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, Any type) {
|
||||
switch (type) {
|
||||
case Any_NONE: return true;
|
||||
case Any_Monster: return verifier.VerifyTable(reinterpret_cast<const Monster *>(union_obj));
|
||||
|
||||
32
tests/prototest/test.golden
Normal file
32
tests/prototest/test.golden
Normal file
@@ -0,0 +1,32 @@
|
||||
// Generated from test.proto
|
||||
|
||||
namespace proto.test;
|
||||
|
||||
enum ProtoEnum : short {
|
||||
FOO = 1,
|
||||
BAR = 5,
|
||||
}
|
||||
|
||||
table OtherMessage {
|
||||
a:double;
|
||||
b:float = 3.14149;
|
||||
}
|
||||
|
||||
table ProtoMessage {
|
||||
c:int = 16;
|
||||
d:long;
|
||||
p:uint;
|
||||
e:ulong;
|
||||
f:int = -1;
|
||||
g:long;
|
||||
h:uint;
|
||||
q:ulong;
|
||||
i:int;
|
||||
j:long;
|
||||
k:bool;
|
||||
l:string (required);
|
||||
m:string;
|
||||
n:OtherMessage;
|
||||
o:[string];
|
||||
}
|
||||
|
||||
34
tests/prototest/test.proto
Normal file
34
tests/prototest/test.proto
Normal file
@@ -0,0 +1,34 @@
|
||||
// Sample .proto file that we can translate to the corresponding .fbs.
|
||||
|
||||
package proto.test;
|
||||
|
||||
option some_option = is_ignored;
|
||||
import "some_other_schema.proto";
|
||||
|
||||
enum ProtoEnum {
|
||||
FOO = 1;
|
||||
BAR = 5;
|
||||
}
|
||||
|
||||
message OtherMessage {
|
||||
optional double a = 26;
|
||||
optional float b = 32 [default = 3.14149];
|
||||
}
|
||||
|
||||
message ProtoMessage {
|
||||
optional int32 c = 12 [default = 16];
|
||||
optional int64 d = 1 [default = 0];
|
||||
optional uint32 p = 1;
|
||||
optional uint64 e = 2;
|
||||
optional sint32 f = 3 [default = -1];
|
||||
optional sint64 g = 4;
|
||||
optional fixed32 h = 5;
|
||||
optional fixed64 q = 6;
|
||||
optional sfixed32 i = 7;
|
||||
optional sfixed64 j = 8;
|
||||
optional bool k = 9;
|
||||
required string l = 10;
|
||||
optional bytes m = 11;
|
||||
optional OtherMessage n = 12;
|
||||
repeated string o = 14;
|
||||
}
|
||||
@@ -182,7 +182,6 @@ void AccessFlatBufferTest(const std::string &flatbuf) {
|
||||
// example of parsing text straight into a buffer, and generating
|
||||
// text back from it:
|
||||
void ParseAndGenerateTextTest() {
|
||||
|
||||
// load FlatBuffer schema (.fbs) and JSON from disk
|
||||
std::string schemafile;
|
||||
std::string jsonfile;
|
||||
@@ -216,6 +215,34 @@ void ParseAndGenerateTextTest() {
|
||||
}
|
||||
}
|
||||
|
||||
// Parse a .proto schema, output as .fbs
|
||||
void ParseProtoTest() {
|
||||
// load the .proto and the golden file from disk
|
||||
std::string protofile;
|
||||
std::string goldenfile;
|
||||
TEST_EQ(flatbuffers::LoadFile(
|
||||
"tests/prototest/test.proto", false, &protofile), true);
|
||||
TEST_EQ(flatbuffers::LoadFile(
|
||||
"tests/prototest/test.golden", false, &goldenfile), true);
|
||||
|
||||
// Parse proto.
|
||||
flatbuffers::Parser parser(true);
|
||||
TEST_EQ(parser.Parse(protofile.c_str(), nullptr), true);
|
||||
|
||||
// Generate fbs.
|
||||
flatbuffers::GeneratorOptions opts;
|
||||
auto fbs = flatbuffers::GenerateFBS(parser, "test", opts);
|
||||
|
||||
// Ensure generated file is parsable.
|
||||
flatbuffers::Parser parser2;
|
||||
TEST_EQ(parser2.Parse(fbs.c_str(), nullptr), true);
|
||||
|
||||
if (fbs != goldenfile) {
|
||||
printf("%s----------------\n%s", fbs.c_str(), goldenfile.c_str());
|
||||
TEST_NOTNULL(NULL);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T> void CompareTableFieldValue(flatbuffers::Table *table,
|
||||
flatbuffers::voffset_t voffset,
|
||||
T val) {
|
||||
@@ -544,6 +571,7 @@ int main(int /*argc*/, const char * /*argv*/[]) {
|
||||
|
||||
#ifndef __ANDROID__ // requires file access
|
||||
ParseAndGenerateTextTest();
|
||||
ParseProtoTest();
|
||||
#endif
|
||||
|
||||
FuzzTest1();
|
||||
|
||||
Reference in New Issue
Block a user