Skip to content

Commit

Permalink
add flatbuffers reflection support
Browse files Browse the repository at this point in the history
  • Loading branch information
iceboy233 committed Jan 4, 2020
1 parent 5e1ef1e commit bb40f4c
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/.vscode/
/bazel-*
19 changes: 19 additions & 0 deletions util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ cc_test(
],
)

cc_library(
name = "flatbuffers-reflection",
srcs = ["flatbuffers-reflection.cc"],
hdrs = ["flatbuffers-reflection.h"],
deps = [
"@com_github_google_flatbuffers//:flatbuffers",
"@com_google_absl//absl/types:span",
],
)

cc_test(
name = "flatbuffers-reflection_test",
srcs = ["flatbuffers-reflection_test.cc"],
deps = [
":flatbuffers-reflection",
"@com_google_googletest//:gtest_main",
],
)

cc_library(
name = "utf",
srcs = ["utf.cc"],
Expand Down
2 changes: 1 addition & 1 deletion util/fibonacci-sequence_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace util {
namespace {

TEST(FibonacciSequenceTest, Values) {
TEST(FibonacciSequenceTest, values) {
FibonacciSequence<int> fib;
EXPECT_EQ(fib.a(), 1);
fib.next();
Expand Down
34 changes: 34 additions & 0 deletions util/flatbuffers-reflection.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "util/flatbuffers-reflection.h"

#include "flatbuffers/reflection.h"

namespace util {

bool pack(
flatbuffers::Parser &parser,
const char *in,
std::vector<uint8_t> &out) {
if (!parser.Parse(in)) {
return false;
}
out.assign(
parser.builder_.GetBufferPointer(),
parser.builder_.GetBufferPointer() + parser.builder_.GetSize());
return true;
}

bool verify_and_unpack(
flatbuffers::Parser &parser,
absl::Span<const uint8_t> in,
std::string &out) {
parser.Serialize();
const reflection::Schema *schema = reflection::GetSchema(
parser.builder_.GetBufferPointer());
if (!flatbuffers::Verify(
*schema, *schema->root_table(), in.data(), in.size())) {
return false;
}
return flatbuffers::GenerateText(parser, in.data(), &out);
}

} // namespace util
23 changes: 23 additions & 0 deletions util/flatbuffers-reflection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef _UTIL_FLATBUFFERS_REFLECTION_H
#define _UTIL_FLATBUFFERS_REFLECTION_H

#include <cstdint>
#include <vector>
#include "absl/types/span.h"
#include "flatbuffers/idl.h"

namespace util {

bool pack(
flatbuffers::Parser &parser,
const char *in,
std::vector<uint8_t> &out);

bool verify_and_unpack(
flatbuffers::Parser &parser,
absl::Span<const uint8_t> in,
std::string &out);

} // namespace util

#endif // _UTIL_FLATBUFFERS_REFLECTION_H
21 changes: 21 additions & 0 deletions util/flatbuffers-reflection_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "util/flatbuffers-reflection.h"

#include <gtest/gtest.h>
#include "flatbuffers/idl.h"

namespace util {
namespace {

TEST(FlatbuffersReflectionDynamicTest, pack_verify_unpack) {
flatbuffers::Parser parser;
ASSERT_TRUE(parser.Parse("table TestTable { dummy: int32; }"));
ASSERT_TRUE(parser.SetRootType("TestTable"));
std::vector<uint8_t> buffer;
ASSERT_TRUE(pack(parser, "{dummy: 123}", buffer));
std::string json;
ASSERT_TRUE(verify_and_unpack(parser, buffer, json));
EXPECT_EQ(json, "{\n dummy: 123\n}\n");
}

} // namespace
} // namespace util

0 comments on commit bb40f4c

Please sign in to comment.