-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/.vscode/ | ||
/bazel-* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |