Merge pull request #4025 from paszea/reflection_fqn
serialize fully qualified struct & enum name in schema binary
diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp
index 21cbed3..dc3604d 100644
--- a/src/idl_parser.cpp
+++ b/src/idl_parser.cpp
@@ -1982,7 +1982,8 @@
// Schema serialization functionality:
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) {
@@ -2028,8 +2029,9 @@
(*it)->Serialize(builder,
static_cast<uint16_t>(it - fields.vec.begin()), parser));
}
+ auto qualified_name = defined_namespace->GetFullyQualifiedName(name);
return reflection::CreateObject(*builder,
- builder->CreateString(name),
+ builder->CreateString(qualified_name),
builder->CreateVectorOfSortedTables(
&field_offsets),
fixed,
@@ -2066,8 +2068,9 @@
for (auto it = vals.vec.begin(); it != vals.vec.end(); ++it) {
enumval_offsets.push_back((*it)->Serialize(builder));
}
+ auto qualified_name = defined_namespace->GetFullyQualifiedName(name);
return reflection::CreateEnum(*builder,
- builder->CreateString(name),
+ builder->CreateString(qualified_name),
builder->CreateVector(enumval_offsets),
is_union,
underlying_type.Serialize(builder),
diff --git a/tests/monster_test.bfbs b/tests/monster_test.bfbs
index e131ac8..0369f89 100644
--- a/tests/monster_test.bfbs
+++ b/tests/monster_test.bfbs
Binary files differ
diff --git a/tests/test.cpp b/tests/test.cpp
index 45eb1fe..2cc04fa 100644
--- a/tests/test.cpp
+++ b/tests/test.cpp
@@ -433,7 +433,7 @@
// Make sure the schema is what we expect it to be.
auto &schema = *reflection::GetSchema(bfbsfile.c_str());
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 hp_field_ptr = fields->LookupByKey("hp");
TEST_NOTNULL(hp_field_ptr);
@@ -446,6 +446,14 @@
TEST_NOTNULL(friendly_field_ptr->attributes());
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.
auto &root = *flatbuffers::GetAnyRoot(flatbuf);
auto hp = flatbuffers::GetFieldI<uint16_t>(root, hp_field);