[flac] Add FlexBuffers option for generating data (#5519)

Alongside --binary and --json, an additional
switch (--flexbuffers) can be passed to convert
data using FlexBuffers
This commit is contained in:
Paulo Pinheiro
2019-09-27 00:06:11 +02:00
committed by Wouter van Oortmerssen
parent a92039687a
commit cfb4ecf6f0
6 changed files with 52 additions and 4 deletions

View File

@@ -149,6 +149,8 @@ std::string FlatCompiler::GetUsageString(const char *program_name) const {
" --force-defaults Emit default values in binary output from JSON\n"
" --force-empty When serializing from object API representation,\n"
" force strings and vectors to empty rather than null.\n"
" --flexbuffers Used with \"binary\" and \"json\" options, it generates\n"
" data using schema-less FlexBuffers.\n"
"FILEs may be schemas (must end in .fbs), binary schemas (must end in .bfbs),\n"
"or JSON files (conforming to preceding schema). FILEs after the -- must be\n"
"binary flatbuffer format files.\n"
@@ -324,6 +326,8 @@ int FlatCompiler::Compile(int argc, const char **argv) {
opts.set_empty_to_null = false;
} else if (arg == "--java-primitive-has-method") {
opts.java_primitive_has_method = true;
} else if (arg == "--flexbuffers") {
opts.use_flexbuffers = true;
} else {
for (size_t i = 0; i < params_.num_generators; ++i) {
if (arg == params_.generators[i].generator_opt_long ||
@@ -407,7 +411,8 @@ int FlatCompiler::Compile(int argc, const char **argv) {
}
} else {
// Check if file contains 0 bytes.
if (!is_binary_schema && contents.length() != strlen(contents.c_str())) {
if (!opts.use_flexbuffers && !is_binary_schema &&
contents.length() != strlen(contents.c_str())) {
Error("input file appears to be binary: " + filename, true);
}
if (is_schema) {
@@ -418,9 +423,20 @@ int FlatCompiler::Compile(int argc, const char **argv) {
}
if (is_binary_schema) {
LoadBinarySchema(*parser.get(), filename, contents);
}
if (opts.use_flexbuffers) {
if (opts.lang_to_generate == IDLOptions::kJson) {
parser->flex_root_ = flexbuffers::GetRoot(
reinterpret_cast<const uint8_t *>(contents.c_str()),
contents.size());
} else {
parser->flex_builder_.Clear();
ParseFile(*parser.get(), filename, contents, include_directories);
}
} else {
ParseFile(*parser.get(), filename, contents, include_directories);
if (!is_schema && !parser->builder_.GetSize()) {
if (!opts.use_flexbuffers && !is_schema &&
!parser->builder_.GetSize()) {
// If a file doesn't end in .fbs, it must be json/binary. Ensure we
// didn't just parse a schema with a different extension.
Error("input file is neither json nor a .fbs (schema) file: " +