From 66214243082a1ba6156da8c3f562595ab175f035 Mon Sep 17 00:00:00 2001 From: Paul Reimer Date: Mon, 7 May 2018 16:07:52 -0700 Subject: [PATCH] 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 --- docs/source/Compiler.md | 2 ++ include/flatbuffers/idl.h | 1 + src/flatc.cpp | 11 +++++++++++ src/idl_parser.cpp | 9 ++++++--- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/docs/source/Compiler.md b/docs/source/Compiler.md index aaa274c3d..1c0b50e6c 100644 --- a/docs/source/Compiler.md +++ b/docs/source/Compiler.md @@ -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. diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index 4e0b08d92..c261b74a8 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -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 { diff --git a/src/flatc.cpp b/src/flatc.cpp index a2f3a7dcd..c7e1b0919 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -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 diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 3102cd61f..b98defb8d 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -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();