Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
F
flatbuffers
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
flatbuffers
Commits
51d9641d
Commit
51d9641d
authored
Jul 16, 2018
by
shassani
Committed by
Wouter van Oortmerssen
Jul 16, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
flatbuffer force-empty option (#4822)
parent
af6c0e68
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
28 additions
and
6 deletions
+28
-6
Compiler.md
docs/source/Compiler.md
+3
-0
idl.h
include/flatbuffers/idl.h
+6
-1
flatc.cpp
src/flatc.cpp
+4
-0
idl_gen_cpp.cpp
src/idl_gen_cpp.cpp
+15
-5
No files found.
docs/source/Compiler.md
View file @
51d9641d
...
@@ -138,5 +138,8 @@ Additional options:
...
@@ -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.
include/flatbuffers/idl.h
View file @
51d9641d
...
@@ -422,6 +422,10 @@ struct IDLOptions {
...
@@ -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 {
...
@@ -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.
...
...
src/flatc.cpp
View file @
51d9641d
...
@@ -123,6 +123,8 @@ std::string FlatCompiler::GetUsageString(const char *program_name) const {
...
@@ -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) {
...
@@ -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
||
...
...
src/idl_gen_cpp.cpp
View file @
51d9641d
...
@@ -2148,6 +2148,8 @@ class CppGenerator : public BaseGenerator {
...
@@ -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 {
...
@@ -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 {
...
@@ -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
:
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment