mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-17 01:26:45 +00:00
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:
@@ -1,6 +1,6 @@
|
||||
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")
|
||||
load("@rules_cc//cc:defs.bzl", "cc_test")
|
||||
load("//:build_defs.bzl", "flatbuffer_cc_library")
|
||||
load("//:build_defs.bzl", "DEFAULT_FLATC_ARGS", "flatbuffer_cc_library")
|
||||
|
||||
package(default_visibility = ["//visibility:private"])
|
||||
|
||||
@@ -160,6 +160,7 @@ cc_library(
|
||||
],
|
||||
hdrs = [
|
||||
"monster_test.grpc.fb.h",
|
||||
"monster_test_bfbs_generated.h",
|
||||
"monster_test_generated.h",
|
||||
],
|
||||
includes = ["."],
|
||||
@@ -182,6 +183,13 @@ flatbuffer_cc_library(
|
||||
flatbuffer_cc_library(
|
||||
name = "monster_test_cc_fbs",
|
||||
srcs = ["monster_test.fbs"],
|
||||
outs = ["monster_test_bfbs_generated.h"],
|
||||
flatc_args = DEFAULT_FLATC_ARGS + [
|
||||
"--bfbs-comments",
|
||||
"--bfbs-builtins",
|
||||
"--bfbs-gen-embed",
|
||||
"--bfbs-filenames tests",
|
||||
],
|
||||
include_paths = ["tests/include_test"],
|
||||
visibility = ["//grpc/tests:__subpackages__"],
|
||||
deps = [":include_test_fbs"],
|
||||
|
||||
@@ -15,6 +15,9 @@ static_assert(FLATBUFFERS_VERSION_MAJOR == 23 &&
|
||||
FLATBUFFERS_VERSION_REVISION == 3,
|
||||
"Non-compatible flatbuffers version included");
|
||||
|
||||
// For access to the binary schema that produced this file.
|
||||
#include "monster_test_bfbs_generated.h"
|
||||
|
||||
namespace MyGame {
|
||||
|
||||
struct InParentNamespace;
|
||||
@@ -946,6 +949,7 @@ struct InParentNamespaceT : public ::flatbuffers::NativeTable {
|
||||
struct InParentNamespace FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
|
||||
typedef InParentNamespaceT NativeTableType;
|
||||
typedef InParentNamespaceBuilder Builder;
|
||||
typedef MyGame::Example::MonsterBinarySchema BinarySchema;
|
||||
static const ::flatbuffers::TypeTable *MiniReflectTypeTable() {
|
||||
return InParentNamespaceTypeTable();
|
||||
}
|
||||
@@ -990,6 +994,7 @@ struct MonsterT : public ::flatbuffers::NativeTable {
|
||||
struct Monster FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
|
||||
typedef MonsterT NativeTableType;
|
||||
typedef MonsterBuilder Builder;
|
||||
typedef MyGame::Example::MonsterBinarySchema BinarySchema;
|
||||
static const ::flatbuffers::TypeTable *MiniReflectTypeTable() {
|
||||
return MonsterTypeTable();
|
||||
}
|
||||
@@ -1037,6 +1042,7 @@ struct TestSimpleTableWithEnumT : public ::flatbuffers::NativeTable {
|
||||
struct TestSimpleTableWithEnum FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
|
||||
typedef TestSimpleTableWithEnumT NativeTableType;
|
||||
typedef TestSimpleTableWithEnumBuilder Builder;
|
||||
typedef MyGame::Example::MonsterBinarySchema BinarySchema;
|
||||
static const ::flatbuffers::TypeTable *MiniReflectTypeTable() {
|
||||
return TestSimpleTableWithEnumTypeTable();
|
||||
}
|
||||
@@ -1097,6 +1103,7 @@ struct StatT : public ::flatbuffers::NativeTable {
|
||||
struct Stat FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
|
||||
typedef StatT NativeTableType;
|
||||
typedef StatBuilder Builder;
|
||||
typedef MyGame::Example::MonsterBinarySchema BinarySchema;
|
||||
static const ::flatbuffers::TypeTable *MiniReflectTypeTable() {
|
||||
return StatTypeTable();
|
||||
}
|
||||
@@ -1201,6 +1208,7 @@ struct ReferrableT : public ::flatbuffers::NativeTable {
|
||||
struct Referrable FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
|
||||
typedef ReferrableT NativeTableType;
|
||||
typedef ReferrableBuilder Builder;
|
||||
typedef MyGame::Example::MonsterBinarySchema BinarySchema;
|
||||
static const ::flatbuffers::TypeTable *MiniReflectTypeTable() {
|
||||
return ReferrableTypeTable();
|
||||
}
|
||||
@@ -1327,6 +1335,7 @@ struct MonsterT : public ::flatbuffers::NativeTable {
|
||||
struct Monster FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
|
||||
typedef MonsterT NativeTableType;
|
||||
typedef MonsterBuilder Builder;
|
||||
typedef MyGame::Example::MonsterBinarySchema BinarySchema;
|
||||
static const ::flatbuffers::TypeTable *MiniReflectTypeTable() {
|
||||
return MonsterTypeTable();
|
||||
}
|
||||
@@ -2429,6 +2438,7 @@ struct TypeAliasesT : public ::flatbuffers::NativeTable {
|
||||
struct TypeAliases FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table {
|
||||
typedef TypeAliasesT NativeTableType;
|
||||
typedef TypeAliasesBuilder Builder;
|
||||
typedef MyGame::Example::MonsterBinarySchema BinarySchema;
|
||||
static const ::flatbuffers::TypeTable *MiniReflectTypeTable() {
|
||||
return TypeAliasesTypeTable();
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user