[BREAKING CHANGE] Python: handle bool table fields properly. (#4736)

* Python: handle bool table fields properly.

* Small refactor.

* Use snake_case instead of camelCase. Use auto.
This commit is contained in:
Tin Tvrtković 2018-05-14 22:30:10 +02:00 committed by Wouter van Oortmerssen
parent f11ffedb2b
commit a9640bd9e1
2 changed files with 10 additions and 5 deletions

View File

@ -141,9 +141,14 @@ static void GetScalarFieldOfTable(const StructDef &struct_def,
code += MakeCamel(field.name); code += MakeCamel(field.name);
code += "(self):"; code += "(self):";
code += OffsetPrefix(field); code += OffsetPrefix(field);
code += Indent + Indent + Indent + "return " + getter; getter += "o + self._tab.Pos)";
code += "o + self._tab.Pos)\n"; auto is_bool = field.value.type.base_type == BASE_TYPE_BOOL;
code += Indent + Indent + "return " + field.value.constant + "\n\n"; if (is_bool) {
getter = "bool(" + getter + ")";
}
code += Indent + Indent + Indent + "return " + getter + "\n";
auto defaultValue = (is_bool ? "False" : field.value.constant);
code += Indent + Indent + "return " + defaultValue + "\n\n";
} }
// Get a struct by initializing an existing struct. // Get a struct by initializing an existing struct.

View File

@ -201,8 +201,8 @@ class Monster(object):
def Testbool(self): def Testbool(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(34)) o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(34))
if o != 0: if o != 0:
return self._tab.Get(flatbuffers.number_types.BoolFlags, o + self._tab.Pos) return bool(self._tab.Get(flatbuffers.number_types.BoolFlags, o + self._tab.Pos))
return 0 return False
# Monster # Monster
def Testhashs32Fnv1(self): def Testhashs32Fnv1(self):