mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-28 18:00:01 +00:00
flatbuffer force-empty option (#4822)
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
af6c0e6839
commit
51d9641de6
@@ -138,5 +138,8 @@ Additional options:
|
|||||||
|
|
||||||
- `--force-defaults` : Emit default values in binary output from JSON.
|
- `--force-defaults` : Emit default values in binary output from JSON.
|
||||||
|
|
||||||
|
- `--force-empty` : When serializing from object API representation, force
|
||||||
|
strings and vectors to empty rather than null.
|
||||||
|
|
||||||
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.
|
||||||
|
|||||||
@@ -422,6 +422,10 @@ struct IDLOptions {
|
|||||||
// for code generation.
|
// for code generation.
|
||||||
unsigned long lang_to_generate;
|
unsigned long lang_to_generate;
|
||||||
|
|
||||||
|
// If set (default behavior), empty string and vector fields will be set to
|
||||||
|
// nullptr to make the flatbuffer more compact.
|
||||||
|
bool set_empty_to_null;
|
||||||
|
|
||||||
IDLOptions()
|
IDLOptions()
|
||||||
: strict_json(false),
|
: strict_json(false),
|
||||||
skip_js_exports(false),
|
skip_js_exports(false),
|
||||||
@@ -457,7 +461,8 @@ struct IDLOptions {
|
|||||||
force_defaults(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),
|
||||||
|
set_empty_to_null(true) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
// This encapsulates where the parser is in the current source file.
|
// This encapsulates where the parser is in the current source file.
|
||||||
|
|||||||
@@ -123,6 +123,8 @@ std::string FlatCompiler::GetUsageString(const char *program_name) const {
|
|||||||
" --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"
|
" --force-defaults Emit default values in binary output from JSON\n"
|
||||||
|
" --force-empty When serializing from object API representation, "
|
||||||
|
" force strings and vectors to empty rather than null.\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"
|
||||||
@@ -280,6 +282,8 @@ int FlatCompiler::Compile(int argc, const char **argv) {
|
|||||||
opts.root_type = argv[argi];
|
opts.root_type = argv[argi];
|
||||||
} else if (arg == "--force-defaults") {
|
} else if (arg == "--force-defaults") {
|
||||||
opts.force_defaults = true;
|
opts.force_defaults = true;
|
||||||
|
} else if (arg == "--force-empty") {
|
||||||
|
opts.set_empty_to_null = false;
|
||||||
} 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 ||
|
||||||
|
|||||||
@@ -2148,6 +2148,8 @@ class CppGenerator : public BaseGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string GenCreateParam(const FieldDef &field) {
|
std::string GenCreateParam(const FieldDef &field) {
|
||||||
|
const IDLOptions &opts = parser_.opts;
|
||||||
|
|
||||||
std::string value = "_o->";
|
std::string value = "_o->";
|
||||||
if (field.value.type.base_type == BASE_TYPE_UTYPE) {
|
if (field.value.type.base_type == BASE_TYPE_UTYPE) {
|
||||||
value += StripUnionType(Name(field));
|
value += StripUnionType(Name(field));
|
||||||
@@ -2172,8 +2174,13 @@ class CppGenerator : public BaseGenerator {
|
|||||||
code += "_fbb.CreateString(" + value + ")";
|
code += "_fbb.CreateString(" + value + ")";
|
||||||
|
|
||||||
// For optional fields, check to see if there actually is any data
|
// For optional fields, check to see if there actually is any data
|
||||||
// in _o->field before attempting to access it.
|
// in _o->field before attempting to access it. If there isn't,
|
||||||
if (!field.required) { code = value + ".empty() ? 0 : " + code; }
|
// depending on set_empty_to_null either set it to 0 or an empty string.
|
||||||
|
if (!field.required) {
|
||||||
|
auto empty_value =
|
||||||
|
opts.set_empty_to_null ? "0" : "_fbb.CreateSharedString(\"\")";
|
||||||
|
code = value + ".empty() ? " + empty_value + " : " + code;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// Vector fields come in several flavours, of the forms:
|
// Vector fields come in several flavours, of the forms:
|
||||||
@@ -2259,9 +2266,12 @@ class CppGenerator : public BaseGenerator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// For optional fields, check to see if there actually is any data
|
// If set_empty_to_null option is enabled, for optional fields, check to
|
||||||
// in _o->field before attempting to access it.
|
// see if there actually is any data in _o->field before attempting to
|
||||||
if (!field.required) { code = value + ".size() ? " + code + " : 0"; }
|
// access it.
|
||||||
|
if (opts.set_empty_to_null && !field.required) {
|
||||||
|
code = value + ".size() ? " + code + " : 0";
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case BASE_TYPE_UNION: {
|
case BASE_TYPE_UNION: {
|
||||||
|
|||||||
Reference in New Issue
Block a user