Commit 741c6305 authored by Paul Reimer's avatar Paul Reimer Committed by Wouter van Oortmerssen

Add --force-defaults option to flatc [C++, parser] (#4729)

* Add --force-defaults option to flatc

To emit default values for fields which are not present or which are set
to the default value.

* flatc option --force-defaults should have a default value (false) and take action on the builder_ within the Parser constructor

* Add help text from flatc --force-defaults to Compiler.md doc

* Clarified docs for flatc --force-defaults, and imply that this behaviour is not normally needed.

* Updated docs and flatc help text for --force-defaults option
parent e9912e92
...@@ -136,5 +136,7 @@ Additional options: ...@@ -136,5 +136,7 @@ Additional options:
- `--root-type T` : Select or override the default root_type. - `--root-type T` : Select or override the default root_type.
- `--force-defaults` : Emit default values in binary output from JSON.
NOTE: short-form options for generators are deprecated, use the long form NOTE: short-form options for generators are deprecated, use the long form
whenever possible. whenever possible.
...@@ -392,6 +392,7 @@ struct IDLOptions { ...@@ -392,6 +392,7 @@ struct IDLOptions {
bool protobuf_ascii_alike; bool protobuf_ascii_alike;
bool size_prefixed; bool size_prefixed;
std::string root_type; std::string root_type;
bool force_defaults;
// Possible options for the more general generator below. // Possible options for the more general generator below.
enum Language { enum Language {
...@@ -452,6 +453,7 @@ struct IDLOptions { ...@@ -452,6 +453,7 @@ struct IDLOptions {
reexport_ts_modules(true), reexport_ts_modules(true),
protobuf_ascii_alike(false), protobuf_ascii_alike(false),
size_prefixed(false), size_prefixed(false),
force_defaults(false),
lang(IDLOptions::kJava), lang(IDLOptions::kJava),
mini_reflect(IDLOptions::kNone), mini_reflect(IDLOptions::kNone),
lang_to_generate(0) {} lang_to_generate(0) {}
...@@ -527,6 +529,9 @@ class Parser : public ParserState { ...@@ -527,6 +529,9 @@ class Parser : public ParserState {
source_(nullptr), source_(nullptr),
anonymous_counter(0), anonymous_counter(0),
recurse_protection_counter(0) { recurse_protection_counter(0) {
if (opts.force_defaults) {
builder_.ForceDefaults(true);
}
// Start out with the empty namespace being current. // Start out with the empty namespace being current.
empty_namespace_ = new Namespace(); empty_namespace_ = new Namespace();
namespaces_.push_back(empty_namespace_); namespaces_.push_back(empty_namespace_);
......
...@@ -122,6 +122,7 @@ std::string FlatCompiler::GetUsageString(const char *program_name) const { ...@@ -122,6 +122,7 @@ std::string FlatCompiler::GetUsageString(const char *program_name) const {
" --reflect-types Add minimal type reflection to code generation.\n" " --reflect-types Add minimal type reflection to code generation.\n"
" --reflect-names Add minimal type/name reflection.\n" " --reflect-names Add minimal type/name reflection.\n"
" --root-type T Select or override the default root_type\n" " --root-type T Select or override the default root_type\n"
" --force-defaults Emit default values in binary output from JSON\n"
"FILEs may be schemas (must end in .fbs), or JSON files (conforming to preceding\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" "schema). FILEs after the -- must be binary flatbuffer format files.\n"
"Output files are named using the base file name of the input,\n" "Output files are named using the base file name of the input,\n"
...@@ -277,6 +278,8 @@ int FlatCompiler::Compile(int argc, const char **argv) { ...@@ -277,6 +278,8 @@ int FlatCompiler::Compile(int argc, const char **argv) {
} else if (arg == "--root-type") { } else if (arg == "--root-type") {
if (++argi >= argc) Error("missing type following" + arg, true); if (++argi >= argc) Error("missing type following" + arg, true);
opts.root_type = argv[argi]; opts.root_type = argv[argi];
} else if (arg == "--force-defaults") {
opts.force_defaults = true;
} else { } else {
for (size_t i = 0; i < params_.num_generators; ++i) { for (size_t i = 0; i < params_.num_generators; ++i) {
if (arg == params_.generators[i].generator_opt_long || if (arg == params_.generators[i].generator_opt_long ||
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment