mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-01 19:13:57 +00:00
serialize fully qualified struct & enum name in schema binary
This commit is contained in:
@@ -1977,7 +1977,8 @@ std::set<std::string> Parser::GetIncludedFilesRecursive(
|
|||||||
// Schema serialization functionality:
|
// Schema serialization functionality:
|
||||||
|
|
||||||
template<typename T> bool compareName(const T* a, const T* b) {
|
template<typename T> bool compareName(const T* a, const T* b) {
|
||||||
return a->name < b->name;
|
return a->defined_namespace->GetFullyQualifiedName(a->name)
|
||||||
|
< b->defined_namespace->GetFullyQualifiedName(b->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T> void AssignIndices(const std::vector<T *> &defvec) {
|
template<typename T> void AssignIndices(const std::vector<T *> &defvec) {
|
||||||
@@ -2023,8 +2024,9 @@ Offset<reflection::Object> StructDef::Serialize(FlatBufferBuilder *builder,
|
|||||||
(*it)->Serialize(builder,
|
(*it)->Serialize(builder,
|
||||||
static_cast<uint16_t>(it - fields.vec.begin()), parser));
|
static_cast<uint16_t>(it - fields.vec.begin()), parser));
|
||||||
}
|
}
|
||||||
|
auto qualified_name = defined_namespace->GetFullyQualifiedName(name);
|
||||||
return reflection::CreateObject(*builder,
|
return reflection::CreateObject(*builder,
|
||||||
builder->CreateString(name),
|
builder->CreateString(qualified_name),
|
||||||
builder->CreateVectorOfSortedTables(
|
builder->CreateVectorOfSortedTables(
|
||||||
&field_offsets),
|
&field_offsets),
|
||||||
fixed,
|
fixed,
|
||||||
@@ -2061,8 +2063,9 @@ Offset<reflection::Enum> EnumDef::Serialize(FlatBufferBuilder *builder,
|
|||||||
for (auto it = vals.vec.begin(); it != vals.vec.end(); ++it) {
|
for (auto it = vals.vec.begin(); it != vals.vec.end(); ++it) {
|
||||||
enumval_offsets.push_back((*it)->Serialize(builder));
|
enumval_offsets.push_back((*it)->Serialize(builder));
|
||||||
}
|
}
|
||||||
|
auto qualified_name = defined_namespace->GetFullyQualifiedName(name);
|
||||||
return reflection::CreateEnum(*builder,
|
return reflection::CreateEnum(*builder,
|
||||||
builder->CreateString(name),
|
builder->CreateString(qualified_name),
|
||||||
builder->CreateVector(enumval_offsets),
|
builder->CreateVector(enumval_offsets),
|
||||||
is_union,
|
is_union,
|
||||||
underlying_type.Serialize(builder),
|
underlying_type.Serialize(builder),
|
||||||
|
|||||||
Binary file not shown.
@@ -433,7 +433,7 @@ void ReflectionTest(uint8_t *flatbuf, size_t length) {
|
|||||||
// Make sure the schema is what we expect it to be.
|
// Make sure the schema is what we expect it to be.
|
||||||
auto &schema = *reflection::GetSchema(bfbsfile.c_str());
|
auto &schema = *reflection::GetSchema(bfbsfile.c_str());
|
||||||
auto root_table = schema.root_table();
|
auto root_table = schema.root_table();
|
||||||
TEST_EQ_STR(root_table->name()->c_str(), "Monster");
|
TEST_EQ_STR(root_table->name()->c_str(), "MyGame.Example.Monster");
|
||||||
auto fields = root_table->fields();
|
auto fields = root_table->fields();
|
||||||
auto hp_field_ptr = fields->LookupByKey("hp");
|
auto hp_field_ptr = fields->LookupByKey("hp");
|
||||||
TEST_NOTNULL(hp_field_ptr);
|
TEST_NOTNULL(hp_field_ptr);
|
||||||
@@ -446,6 +446,14 @@ void ReflectionTest(uint8_t *flatbuf, size_t length) {
|
|||||||
TEST_NOTNULL(friendly_field_ptr->attributes());
|
TEST_NOTNULL(friendly_field_ptr->attributes());
|
||||||
TEST_NOTNULL(friendly_field_ptr->attributes()->LookupByKey("priority"));
|
TEST_NOTNULL(friendly_field_ptr->attributes()->LookupByKey("priority"));
|
||||||
|
|
||||||
|
// Make sure the table index is what we expect it to be.
|
||||||
|
auto pos_field_ptr = fields->LookupByKey("pos");
|
||||||
|
TEST_NOTNULL(pos_field_ptr);
|
||||||
|
TEST_EQ(pos_field_ptr->type()->base_type(), reflection::Obj);
|
||||||
|
auto pos_table_ptr = schema.objects()->Get(pos_field_ptr->type()->index());
|
||||||
|
TEST_NOTNULL(pos_table_ptr);
|
||||||
|
TEST_EQ_STR(pos_table_ptr->name()->c_str(), "MyGame.Example.Vec3");
|
||||||
|
|
||||||
// Now use it to dynamically access a buffer.
|
// Now use it to dynamically access a buffer.
|
||||||
auto &root = *flatbuffers::GetAnyRoot(flatbuf);
|
auto &root = *flatbuffers::GetAnyRoot(flatbuf);
|
||||||
auto hp = flatbuffers::GetFieldI<uint16_t>(root, hp_field);
|
auto hp = flatbuffers::GetFieldI<uint16_t>(root, hp_field);
|
||||||
|
|||||||
Reference in New Issue
Block a user