Add --root-type option to flatc [C++, parser, JSON] (#4728)

* Add --root-type option to flatc

To select or override a root_type

* Add help text from flatc --root-type to Compiler.md doc
This commit is contained in:
Paul Reimer 2018-05-07 16:07:52 -07:00 committed by Wouter van Oortmerssen
parent d215852f52
commit 6621424308
4 changed files with 20 additions and 3 deletions

View File

@ -128,5 +128,7 @@ Additional options:
- `--reflect-types` : Add minimal type reflection to code generation.
- `--reflect-names` : Add minimal type/name reflection.
- `--root-type T` : Select or override the default root_type.
NOTE: short-form options for generators are deprecated, use the long form
whenever possible.

View File

@ -390,6 +390,7 @@ struct IDLOptions {
bool reexport_ts_modules;
bool protobuf_ascii_alike;
bool size_prefixed;
std::string root_type;
// Possible options for the more general generator below.
enum Language {

View File

@ -120,6 +120,7 @@ std::string FlatCompiler::GetUsageString(const char *program_name) const {
" --no-ts-reexport Don't re-export imported dependencies for TypeScript.\n"
" --reflect-types Add minimal type reflection to code generation.\n"
" --reflect-names Add minimal type/name reflection.\n"
" --root-type T Select or override the default root_type\n"
"FILEs may be schemas (must end in .fbs), or JSON files (conforming to preceding\n"
"schema). FILEs after the -- must be binary flatbuffer format files.\n"
"Output files are named using the base file name of the input,\n"
@ -268,6 +269,9 @@ int FlatCompiler::Compile(int argc, const char **argv) {
opts.mini_reflect = IDLOptions::kTypes;
} else if (arg == "--reflect-names") {
opts.mini_reflect = IDLOptions::kTypesAndNames;
} else if (arg == "--root-type") {
if (++argi >= argc) Error("missing type following" + arg, true);
opts.root_type = argv[argi];
} else {
for (size_t i = 0; i < params_.num_generators; ++i) {
if (arg == params_.generators[i].generator_opt_long ||
@ -407,6 +411,13 @@ int FlatCompiler::Compile(int argc, const char **argv) {
}
}
if (!opts.root_type.empty()) {
if (!parser->SetRootType(opts.root_type.c_str()))
Error("unknown root type: " + opts.root_type);
else if (parser->root_struct_def_->fixed)
Error("root type must be a table");
}
if (opts.proto_mode) GenerateFBS(*parser.get(), output_path, filebase);
// We do not want to generate code for the definitions in this file

View File

@ -2432,9 +2432,12 @@ CheckedError Parser::DoParse(const char *source, const char **include_paths,
auto root_type = attribute_;
EXPECT(kTokenIdentifier);
ECHECK(ParseNamespacing(&root_type, nullptr));
if (!SetRootType(root_type.c_str()))
return Error("unknown root type: " + root_type);
if (root_struct_def_->fixed) return Error("root type must be a table");
if (opts.root_type.empty()) {
if (!SetRootType(root_type.c_str()))
return Error("unknown root type: " + root_type);
if (root_struct_def_->fixed)
return Error("root type must be a table");
}
EXPECT(';');
} else if (IsIdent("file_identifier")) {
NEXT();