Add binary schema reflection (#7932)

* Add binary schema reflection

* remove not-used parameter

* move logic from object API to base API

* forward declare

* remove duplicate code gen that was stompping on the edits

* reduce to just typedef generation

* fixed bazel rules to not stomp

* more bazel fixes to support additional generated files
This commit is contained in:
Derek Bailey
2023-05-04 16:12:45 -07:00
committed by GitHub
parent 67084b9921
commit 01a7bc3c58
6 changed files with 83 additions and 11 deletions

View File

@@ -25,6 +25,7 @@
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/minireflect.h"
#include "flatbuffers/reflection_generated.h"
#include "flatbuffers/registry.h"
#include "flatbuffers/util.h"
#include "fuzz_test.h"
@@ -912,7 +913,7 @@ void NativeTypeTest() {
// Guard against -Wunused-function on platforms without file tests.
#ifndef FLATBUFFERS_NO_FILE_TESTS
// VS10 does not support typed enums, exclude from tests
#if !defined(_MSC_VER) || _MSC_VER >= 1700
# if !defined(_MSC_VER) || _MSC_VER >= 1700
void FixedLengthArrayJsonTest(const std::string &tests_data_path, bool binary) {
// load FlatBuffer schema (.fbs) and JSON from disk
std::string schemafile;
@@ -1031,7 +1032,7 @@ void FixedLengthArraySpanTest(const std::string &tests_data_path) {
std::equal(const_d_c.begin(), const_d_c.end(), mutable_d_c.begin()));
}
// test little endian array of int32
# if FLATBUFFERS_LITTLEENDIAN
# if FLATBUFFERS_LITTLEENDIAN
{
flatbuffers::span<const int32_t, 2> const_d_a =
flatbuffers::make_span(*const_nested.a());
@@ -1046,12 +1047,12 @@ void FixedLengthArraySpanTest(const std::string &tests_data_path) {
TEST_ASSERT(
std::equal(const_d_a.begin(), const_d_a.end(), mutable_d_a.begin()));
}
# endif
# endif
}
#else
# else
void FixedLengthArrayJsonTest(bool /*binary*/) {}
void FixedLengthArraySpanTest() {}
#endif
# endif
void TestEmbeddedBinarySchema(const std::string &tests_data_path) {
// load JSON from disk
@@ -1101,6 +1102,37 @@ void TestEmbeddedBinarySchema(const std::string &tests_data_path) {
}
#endif
template<typename T> void EmbeddedSchemaAccessByType() {
// Get the binary schema from the Type itself.
// Verify the schema is OK.
flatbuffers::Verifier verifierEmbeddedSchema(
T::TableType::BinarySchema::data(), T::TableType::BinarySchema::size());
TEST_EQ(reflection::VerifySchemaBuffer(verifierEmbeddedSchema), true);
// Reflect it.
auto schema = reflection::GetSchema(T::TableType::BinarySchema::data());
// This should equal the expected root table.
TEST_EQ_STR(schema->root_table()->name()->c_str(), "MyGame.Example.Monster");
}
void EmbeddedSchemaAccess() {
// Get the binary schema for the monster.
// Verify the schema is OK.
flatbuffers::Verifier verifierEmbeddedSchema(Monster::BinarySchema::data(),
Monster::BinarySchema::size());
TEST_EQ(reflection::VerifySchemaBuffer(verifierEmbeddedSchema), true);
// Reflect it.
auto schema = reflection::GetSchema(Monster::BinarySchema::data());
// This should equal the expected root table.
TEST_EQ_STR(schema->root_table()->name()->c_str(), "MyGame.Example.Monster");
// Repeat above, but do so through a template parameter:
EmbeddedSchemaAccessByType<StatT>();
}
void NestedVerifierTest() {
// Create a nested monster.
flatbuffers::FlatBufferBuilder nested_builder;
@@ -1458,9 +1490,7 @@ void NativeInlineTableVectorTest() {
TestNativeInlineTableT unpacked;
root->UnPackTo(&unpacked);
for (int i = 0; i < 10; ++i) {
TEST_ASSERT(unpacked.t[i] == test.t[i]);
}
for (int i = 0; i < 10; ++i) { TEST_ASSERT(unpacked.t[i] == test.t[i]); }
TEST_ASSERT(unpacked.t == test.t);
}
@@ -1620,6 +1650,7 @@ int FlatBufferTests(const std::string &tests_data_path) {
StructKeyInStructTest();
NestedStructKeyInStructTest();
FixedSizedStructArrayKeyInStructTest();
EmbeddedSchemaAccess();
return 0;
}
} // namespace