summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Reimer <paulreimer@users.noreply.github.com>2018-06-27 09:12:52 -0700
committerWouter van Oortmerssen <aardappel@gmail.com>2018-06-27 09:12:52 -0700
commit741c63052de40c72b21d704bd685dd311ea7e4a5 (patch)
treed671a0d7a20c49294f05df4ae2ec63b4655d86ba
parente9912e92981d1edd25c91714e1275be6a9f5046b (diff)
downloadflatbuffers-741c63052de40c72b21d704bd685dd311ea7e4a5.tar.gz
flatbuffers-741c63052de40c72b21d704bd685dd311ea7e4a5.tar.bz2
flatbuffers-741c63052de40c72b21d704bd685dd311ea7e4a5.zip
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
-rw-r--r--docs/source/Compiler.md2
-rw-r--r--include/flatbuffers/idl.h5
-rw-r--r--src/flatc.cpp3
3 files changed, 10 insertions, 0 deletions
diff --git a/docs/source/Compiler.md b/docs/source/Compiler.md
index a7302320..3a6adf81 100644
--- a/docs/source/Compiler.md
+++ b/docs/source/Compiler.md
@@ -136,5 +136,7 @@ Additional options:
- `--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
whenever possible.
diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h
index d6001700..88d9553d 100644
--- a/include/flatbuffers/idl.h
+++ b/include/flatbuffers/idl.h
@@ -392,6 +392,7 @@ struct IDLOptions {
bool protobuf_ascii_alike;
bool size_prefixed;
std::string root_type;
+ bool force_defaults;
// Possible options for the more general generator below.
enum Language {
@@ -452,6 +453,7 @@ struct IDLOptions {
reexport_ts_modules(true),
protobuf_ascii_alike(false),
size_prefixed(false),
+ force_defaults(false),
lang(IDLOptions::kJava),
mini_reflect(IDLOptions::kNone),
lang_to_generate(0) {}
@@ -527,6 +529,9 @@ class Parser : public ParserState {
source_(nullptr),
anonymous_counter(0),
recurse_protection_counter(0) {
+ if (opts.force_defaults) {
+ builder_.ForceDefaults(true);
+ }
// Start out with the empty namespace being current.
empty_namespace_ = new Namespace();
namespaces_.push_back(empty_namespace_);
diff --git a/src/flatc.cpp b/src/flatc.cpp
index 0487a451..ef91792d 100644
--- a/src/flatc.cpp
+++ b/src/flatc.cpp
@@ -122,6 +122,7 @@ std::string FlatCompiler::GetUsageString(const char *program_name) const {
" --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"
+ " --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"
"schema). FILEs after the -- must be binary flatbuffer format files.\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) {
} else if (arg == "--root-type") {
if (++argi >= argc) Error("missing type following" + arg, true);
opts.root_type = argv[argi];
+ } else if (arg == "--force-defaults") {
+ opts.force_defaults = true;
} else {
for (size_t i = 0; i < params_.num_generators; ++i) {
if (arg == params_.generators[i].generator_opt_long ||