Python: Add forceDefaults opt to python Builder (#5564)

* Add forceDefaults opt to python Builder

* Add test functions for force_default option for python builder

* Simplify

* Add force default test for UOffsetTFlags
This commit is contained in:
Bharat Tak
2019-10-18 00:25:05 +02:00
committed by Wouter van Oortmerssen
parent 8bfafc76de
commit e9d29c21a7
2 changed files with 48 additions and 4 deletions

View File

@@ -103,7 +103,7 @@ class Builder(object):
## @cond FLATBUFFERS_INTENRAL
__slots__ = ("Bytes", "current_vtable", "head", "minalign", "objectEnd",
"vtables", "nested", "finished")
"vtables", "nested", "forceDefaults", "finished")
"""Maximum buffer size constant, in bytes.
@@ -131,10 +131,10 @@ class Builder(object):
self.objectEnd = None
self.vtables = {}
self.nested = False
self.forceDefaults = False
## @endcond
self.finished = False
def Output(self):
"""Return the portion of the buffer that has been used for writing data.
@@ -552,7 +552,7 @@ class Builder(object):
def PrependSlot(self, flags, o, x, d):
N.enforce_number(x, flags)
N.enforce_number(d, flags)
if x != d:
if x != d or self.forceDefaults:
self.Prepend(flags, x)
self.Slot(o)
@@ -589,7 +589,7 @@ class Builder(object):
be set to zero and no other data will be written.
"""
if x != d:
if x != d or self.forceDefaults:
self.PrependUOffsetTRelative(x)
self.Slot(o)
@@ -691,6 +691,15 @@ class Builder(object):
"""
self.Prepend(N.Float64Flags, x)
def ForceDefaults(self, forceDefaults):
"""
In order to save space, fields that are set to their default value
don't get serialized into the buffer. Forcing defaults provides a
way to manually disable this optimization. When set to `True`, will
always serialize default values.
"""
self.forceDefaults = forceDefaults
##############################################################
## @cond FLATBUFFERS_INTERNAL