Commit 65f87035 authored by lu-wang-g's avatar lu-wang-g Committed by Wouter van Oortmerssen

Flatbuffers Python Object API (#5616)

* Flatbuffers Python Object API

Implement the logic to generate the Python object API that can
unpack the data from a buf class into an object class, and pack
the data of an object class to a buf class.

* Fix the build issues

Remove unused parameters and replace auto in the for-loop statement
with std::string to make it compatible with VS2010.

* Fix the build issues.

* Add support for Array type

Added logic to handle Array type in Python Object API. Updated the
generated code accordingly.

* Fix the old style casting from int to char

* Fixed another conversion from int to char

* Fixed the import for typing

Importing typing may cause errors when a machine do not have the
moduel typing installed. This PR fixes the issue by guarding
"import typing" with the "try/except" statement.

* Fix issue of iterating the vector of import list

* Update the generated examples using generate_code.sh

* Fix the import order for typing

The import list was stored in unordered_set, so that each generated
codes may have different import order. Therefore, it failed in the
consistency test where two generated copies need to have exactly the
same apperance.

* Optimize unpack using numpy

Use numpy to unpack vector whenever it is possible to improve unpack
performance.

Also, added codegen command for Python specificly in generate_code.sh,
because --no-includes cannot be turn on for Python.

* Fix the import order

* Update generate_code.bat for windows accordingly

* Replace error message with pass

Avoid printing error message for every Python2 users about typing.
Replace it with pass.
parent 75823cc2
......@@ -94,7 +94,7 @@ Additional options:
statements) use `--no-includes.`
- `--no-includes` : Don't generate include statements for included schemas the
generated file depends on (C++).
generated file depends on (C++ / Python).
- `--gen-mutable` : Generate additional non-const accessors for mutating
FlatBuffers in-place.
......
......@@ -94,7 +94,7 @@ std::string FlatCompiler::GetUsageString(const char *program_name) const {
" If the original behavior is required (no include\n"
" statements) use --no-includes.\n"
" --no-includes Don\'t generate include statements for included\n"
" schemas the generated file depends on (C++).\n"
" schemas the generated file depends on (C++ / Python).\n"
" --gen-mutable Generate accessors that can mutate buffers in-place.\n"
" --gen-onefile Generate single output file for C# and Go.\n"
" --gen-name-strings Generate type name functions for C++.\n"
......
This diff is collapsed.
......@@ -3,6 +3,8 @@
# namespace: Example
import flatbuffers
from flatbuffers.compat import import_numpy
np = import_numpy()
class Ability(object):
__slots__ = ['_tab']
......@@ -21,3 +23,34 @@ def CreateAbility(builder, id, distance):
builder.PrependUint32(distance)
builder.PrependUint32(id)
return builder.Offset()
class AbilityT(object):
# AbilityT
def __init__(self):
self.id = 0 # type: int
self.distance = 0 # type: int
@classmethod
def InitFromBuf(cls, buf, pos):
ability = Ability()
ability.Init(buf, pos)
return cls.InitFromObj(ability)
@classmethod
def InitFromObj(cls, ability):
x = AbilityT()
x._UnPack(ability)
return x
# AbilityT
def _UnPack(self, ability):
if ability is None:
return
self.id = ability.Id()
self.distance = ability.Distance()
# AbilityT
def Pack(self, builder):
return CreateAbility(builder, self.id, self.distance)
......@@ -8,3 +8,18 @@ class Any(object):
TestSimpleTableWithEnum = 2
MyGame_Example2_Monster = 3
def AnyCreator(unionType, table):
from flatbuffers.table import Table
if not isinstance(table, Table):
return None
if unionType == Any().Monster:
import MyGame.Example.Monster
return MyGame.Example.Monster.MonsterT.InitFromBuf(table.Bytes, table.Pos)
if unionType == Any().TestSimpleTableWithEnum:
import MyGame.Example.TestSimpleTableWithEnum
return MyGame.Example.TestSimpleTableWithEnum.TestSimpleTableWithEnumT.InitFromBuf(table.Bytes, table.Pos)
if unionType == Any().MyGame_Example2_Monster:
import MyGame.Example2.Monster
return MyGame.Example2.Monster.MonsterT.InitFromBuf(table.Bytes, table.Pos)
return None
......@@ -8,3 +8,18 @@ class AnyAmbiguousAliases(object):
M2 = 2
M3 = 3
def AnyAmbiguousAliasesCreator(unionType, table):
from flatbuffers.table import Table
if not isinstance(table, Table):
return None
if unionType == AnyAmbiguousAliases().M1:
import MyGame.Example.Monster
return MyGame.Example.Monster.MonsterT.InitFromBuf(table.Bytes, table.Pos)
if unionType == AnyAmbiguousAliases().M2:
import MyGame.Example.Monster
return MyGame.Example.Monster.MonsterT.InitFromBuf(table.Bytes, table.Pos)
if unionType == AnyAmbiguousAliases().M3:
import MyGame.Example.Monster
return MyGame.Example.Monster.MonsterT.InitFromBuf(table.Bytes, table.Pos)
return None
......@@ -8,3 +8,18 @@ class AnyUniqueAliases(object):
TS = 2
M2 = 3
def AnyUniqueAliasesCreator(unionType, table):
from flatbuffers.table import Table
if not isinstance(table, Table):
return None
if unionType == AnyUniqueAliases().M:
import MyGame.Example.Monster
return MyGame.Example.Monster.MonsterT.InitFromBuf(table.Bytes, table.Pos)
if unionType == AnyUniqueAliases().TS:
import MyGame.Example.TestSimpleTableWithEnum
return MyGame.Example.TestSimpleTableWithEnum.TestSimpleTableWithEnumT.InitFromBuf(table.Bytes, table.Pos)
if unionType == AnyUniqueAliases().M2:
import MyGame.Example2.Monster
return MyGame.Example2.Monster.MonsterT.InitFromBuf(table.Bytes, table.Pos)
return None
......@@ -3,6 +3,8 @@
# namespace: Example
import flatbuffers
from flatbuffers.compat import import_numpy
np = import_numpy()
class ArrayStruct(object):
__slots__ = ['_tab']
......@@ -15,6 +17,18 @@ class ArrayStruct(object):
def A(self): return self._tab.Get(flatbuffers.number_types.Float32Flags, self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(0))
# ArrayStruct
def B(self): return [self._tab.Get(flatbuffers.number_types.Int32Flags, self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(4 + i * 4)) for i in range(15)]
# ArrayStruct
def BLength(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
if o != 0:
return self._tab.VectorLen(o)
return 0
# ArrayStruct
def BIsNone(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
return o == 0
# ArrayStruct
def C(self): return self._tab.Get(flatbuffers.number_types.Int8Flags, self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(64))
# ArrayStruct
......@@ -22,10 +36,34 @@ class ArrayStruct(object):
obj.Init(self._tab.Bytes, self._tab.Pos + 72 + i * 32)
return obj
# ArrayStruct
def DLength(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(72))
if o != 0:
return self._tab.VectorLen(o)
return 0
# ArrayStruct
def DIsNone(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(72))
return o == 0
# ArrayStruct
def E(self): return self._tab.Get(flatbuffers.number_types.Int32Flags, self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(136))
# ArrayStruct
def F(self): return [self._tab.Get(flatbuffers.number_types.Int64Flags, self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(144 + i * 8)) for i in range(2)]
# ArrayStruct
def FLength(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(144))
if o != 0:
return self._tab.VectorLen(o)
return 0
# ArrayStruct
def FIsNone(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(144))
return o == 0
def CreateArrayStruct(builder, a, b, c, d_a, d_b, d_c, d_d, e, f):
builder.Prep(8, 160)
......@@ -49,3 +87,62 @@ def CreateArrayStruct(builder, a, b, c, d_a, d_b, d_c, d_d, e, f):
builder.PrependInt32(b[_idx0-1])
builder.PrependFloat32(a)
return builder.Offset()
import MyGame.Example.NestedStruct
try:
from typing import List
except:
pass
class ArrayStructT(object):
# ArrayStructT
def __init__(self):
self.a = 0.0 # type: float
self.b = None # type: List[int]
self.c = 0 # type: int
self.d = None # type: List[MyGame.Example.NestedStruct.NestedStructT]
self.e = 0 # type: int
self.f = None # type: List[int]
@classmethod
def InitFromBuf(cls, buf, pos):
arrayStruct = ArrayStruct()
arrayStruct.Init(buf, pos)
return cls.InitFromObj(arrayStruct)
@classmethod
def InitFromObj(cls, arrayStruct):
x = ArrayStructT()
x._UnPack(arrayStruct)
return x
# ArrayStructT
def _UnPack(self, arrayStruct):
if arrayStruct is None:
return
self.a = arrayStruct.A()
if not arrayStruct.BIsNone():
if np is None:
self.b = []
for i in range(arrayStruct.BLength()):
self.b.append(arrayStruct.B(i))
else:
self.b = arrayStruct.BAsNumpy()
self.c = arrayStruct.C()
if not arrayStruct.DIsNone():
self.d = []
for i in range(arrayStruct.DLength()):
self.d.append(arrayStruct.D(i))
self.e = arrayStruct.E()
if not arrayStruct.FIsNone():
if np is None:
self.f = []
for i in range(arrayStruct.FLength()):
self.f.append(arrayStruct.F(i))
else:
self.f = arrayStruct.FAsNumpy()
# ArrayStructT
def Pack(self, builder):
return CreateArrayStruct(builder, self.a, self.b, self.c, self.d.a, self.d.b, self.d.c, self.d.d, self.e, self.f)
......@@ -3,6 +3,8 @@
# namespace: Example
import flatbuffers
from flatbuffers.compat import import_numpy
np = import_numpy()
class ArrayTable(object):
__slots__ = ['_tab']
......@@ -27,7 +29,7 @@ class ArrayTable(object):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
if o != 0:
x = o + self._tab.Pos
from .ArrayStruct import ArrayStruct
from MyGame.Example.ArrayStruct import ArrayStruct
obj = ArrayStruct()
obj.Init(self._tab.Bytes, x)
return obj
......@@ -36,3 +38,43 @@ class ArrayTable(object):
def ArrayTableStart(builder): builder.StartObject(1)
def ArrayTableAddA(builder, a): builder.PrependStructSlot(0, flatbuffers.number_types.UOffsetTFlags.py_type(a), 0)
def ArrayTableEnd(builder): return builder.EndObject()
import MyGame.Example.ArrayStruct
try:
from typing import Optional
except:
pass
class ArrayTableT(object):
# ArrayTableT
def __init__(self):
self.a = None # type: Optional[MyGame.Example.ArrayStruct.ArrayStructT]
@classmethod
def InitFromBuf(cls, buf, pos):
arrayTable = ArrayTable()
arrayTable.Init(buf, pos)
return cls.InitFromObj(arrayTable)
@classmethod
def InitFromObj(cls, arrayTable):
x = ArrayTableT()
x._UnPack(arrayTable)
return x
# ArrayTableT
def _UnPack(self, arrayTable):
if arrayTable is None:
return
if arrayTable.A() is not None:
self.a = MyGame.Example.ArrayStruct.ArrayStructT.InitFromObj(arrayTable.A())
# ArrayTableT
def Pack(self, builder):
ArrayTableStart(builder)
if self.a is not None:
a = self.a.Pack(builder)
ArrayTableAddA(builder, a)
arrayTable = ArrayTableEnd(builder)
return arrayTable
This diff is collapsed.
......@@ -3,6 +3,8 @@
# namespace: Example
import flatbuffers
from flatbuffers.compat import import_numpy
np = import_numpy()
class NestedStruct(object):
__slots__ = ['_tab']
......@@ -13,12 +15,48 @@ class NestedStruct(object):
# NestedStruct
def A(self): return [self._tab.Get(flatbuffers.number_types.Int32Flags, self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(0 + i * 4)) for i in range(2)]
# NestedStruct
def ALength(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(0))
if o != 0:
return self._tab.VectorLen(o)
return 0
# NestedStruct
def AIsNone(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(0))
return o == 0
# NestedStruct
def B(self): return self._tab.Get(flatbuffers.number_types.Int8Flags, self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(8))
# NestedStruct
def C(self): return [self._tab.Get(flatbuffers.number_types.Int8Flags, self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(9 + i * 1)) for i in range(2)]
# NestedStruct
def CLength(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(9))
if o != 0:
return self._tab.VectorLen(o)
return 0
# NestedStruct
def CIsNone(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(9))
return o == 0
# NestedStruct
def D(self): return [self._tab.Get(flatbuffers.number_types.Int64Flags, self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(16 + i * 8)) for i in range(2)]
# NestedStruct
def DLength(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(16))
if o != 0:
return self._tab.VectorLen(o)
return 0
# NestedStruct
def DIsNone(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(16))
return o == 0
def CreateNestedStruct(builder, a, b, c, d):
builder.Prep(8, 32)
......@@ -31,3 +69,60 @@ def CreateNestedStruct(builder, a, b, c, d):
for _idx0 in range(2 , 0, -1):
builder.PrependInt32(a[_idx0-1])
return builder.Offset()
try:
from typing import List
except:
pass
class NestedStructT(object):
# NestedStructT
def __init__(self):
self.a = None # type: List[int]
self.b = 0 # type: int
self.c = None # type: List[int]
self.d = None # type: List[int]
@classmethod
def InitFromBuf(cls, buf, pos):
nestedStruct = NestedStruct()
nestedStruct.Init(buf, pos)
return cls.InitFromObj(nestedStruct)
@classmethod
def InitFromObj(cls, nestedStruct):
x = NestedStructT()
x._UnPack(nestedStruct)
return x
# NestedStructT
def _UnPack(self, nestedStruct):
if nestedStruct is None:
return
if not nestedStruct.AIsNone():
if np is None:
self.a = []
for i in range(nestedStruct.ALength()):
self.a.append(nestedStruct.A(i))
else:
self.a = nestedStruct.AAsNumpy()
self.b = nestedStruct.B()
if not nestedStruct.CIsNone():
if np is None:
self.c = []
for i in range(nestedStruct.CLength()):
self.c.append(nestedStruct.C(i))
else:
self.c = nestedStruct.CAsNumpy()
if not nestedStruct.DIsNone():
if np is None:
self.d = []
for i in range(nestedStruct.DLength()):
self.d.append(nestedStruct.D(i))
else:
self.d = nestedStruct.DAsNumpy()
# NestedStructT
def Pack(self, builder):
return CreateNestedStruct(builder, self.a, self.b, self.c, self.d)
......@@ -3,6 +3,8 @@
# namespace: Example
import flatbuffers
from flatbuffers.compat import import_numpy
np = import_numpy()
class Referrable(object):
__slots__ = ['_tab']
......@@ -32,3 +34,35 @@ class Referrable(object):
def ReferrableStart(builder): builder.StartObject(1)
def ReferrableAddId(builder, id): builder.PrependUint64Slot(0, id, 0)
def ReferrableEnd(builder): return builder.EndObject()
class ReferrableT(object):
# ReferrableT
def __init__(self):
self.id = 0 # type: int
@classmethod
def InitFromBuf(cls, buf, pos):
referrable = Referrable()
referrable.Init(buf, pos)
return cls.InitFromObj(referrable)
@classmethod
def InitFromObj(cls, referrable):
x = ReferrableT()
x._UnPack(referrable)
return x
# ReferrableT
def _UnPack(self, referrable):
if referrable is None:
return
self.id = referrable.Id()
# ReferrableT
def Pack(self, builder):
ReferrableStart(builder)
ReferrableAddId(builder, self.id)
referrable = ReferrableEnd(builder)
return referrable
......@@ -3,6 +3,8 @@
# namespace: Example
import flatbuffers
from flatbuffers.compat import import_numpy
np = import_numpy()
class Stat(object):
__slots__ = ['_tab']
......@@ -48,3 +50,44 @@ def StatAddId(builder, id): builder.PrependUOffsetTRelativeSlot(0, flatbuffers.n
def StatAddVal(builder, val): builder.PrependInt64Slot(1, val, 0)
def StatAddCount(builder, count): builder.PrependUint16Slot(2, count, 0)
def StatEnd(builder): return builder.EndObject()
class StatT(object):
# StatT
def __init__(self):
self.id = None # type: str
self.val = 0 # type: int
self.count = 0 # type: int
@classmethod
def InitFromBuf(cls, buf, pos):
stat = Stat()
stat.Init(buf, pos)
return cls.InitFromObj(stat)
@classmethod
def InitFromObj(cls, stat):
x = StatT()
x._UnPack(stat)
return x
# StatT
def _UnPack(self, stat):
if stat is None:
return
self.id = stat.Id()
self.val = stat.Val()
self.count = stat.Count()
# StatT
def Pack(self, builder):
if self.id is not None:
id = builder.CreateString(self.id)
StatStart(builder)
if self.id is not None:
StatAddId(builder, id)
StatAddVal(builder, self.val)
StatAddCount(builder, self.count)
stat = StatEnd(builder)
return stat
......@@ -3,6 +3,8 @@
# namespace: Example
import flatbuffers
from flatbuffers.compat import import_numpy
np = import_numpy()
class Test(object):
__slots__ = ['_tab']
......@@ -22,3 +24,34 @@ def CreateTest(builder, a, b):
builder.PrependInt8(b)
builder.PrependInt16(a)
return builder.Offset()
class TestT(object):
# TestT
def __init__(self):
self.a = 0 # type: int
self.b = 0 # type: int
@classmethod
def InitFromBuf(cls, buf, pos):
test = Test()
test.Init(buf, pos)
return cls.InitFromObj(test)
@classmethod
def InitFromObj(cls, test):
x = TestT()
x._UnPack(test)
return x
# TestT
def _UnPack(self, test):
if test is None:
return
self.a = test.A()
self.b = test.B()
# TestT
def Pack(self, builder):
return CreateTest(builder, self.a, self.b)
......@@ -3,6 +3,8 @@
# namespace: Example
import flatbuffers
from flatbuffers.compat import import_numpy
np = import_numpy()
class TestSimpleTableWithEnum(object):
__slots__ = ['_tab']
......@@ -32,3 +34,35 @@ class TestSimpleTableWithEnum(object):
def TestSimpleTableWithEnumStart(builder): builder.StartObject(1)
def TestSimpleTableWithEnumAddColor(builder, color): builder.PrependUint8Slot(0, color, 2)
def TestSimpleTableWithEnumEnd(builder): return builder.EndObject()
class TestSimpleTableWithEnumT(object):
# TestSimpleTableWithEnumT
def __init__(self):
self.color = 2 # type: int
@classmethod
def InitFromBuf(cls, buf, pos):
testSimpleTableWithEnum = TestSimpleTableWithEnum()
testSimpleTableWithEnum.Init(buf, pos)
return cls.InitFromObj(testSimpleTableWithEnum)
@classmethod
def InitFromObj(cls, testSimpleTableWithEnum):
x = TestSimpleTableWithEnumT()
x._UnPack(testSimpleTableWithEnum)
return x
# TestSimpleTableWithEnumT
def _UnPack(self, testSimpleTableWithEnum):
if testSimpleTableWithEnum is None:
return
self.color = testSimpleTableWithEnum.Color()
# TestSimpleTableWithEnumT
def Pack(self, builder):
TestSimpleTableWithEnumStart(builder)
TestSimpleTableWithEnumAddColor(builder, self.color)
testSimpleTableWithEnum = TestSimpleTableWithEnumEnd(builder)
return testSimpleTableWithEnum
......@@ -3,6 +3,8 @@
# namespace: Example
import flatbuffers
from flatbuffers.compat import import_numpy
np = import_numpy()
class TypeAliases(object):
__slots__ = ['_tab']
......@@ -114,6 +116,11 @@ class TypeAliases(object):
return self._tab.VectorLen(o)
return 0
# TypeAliases
def V8IsNone(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(24))
return o == 0
# TypeAliases
def Vf64(self, j):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(26))
......@@ -136,6 +143,11 @@ class TypeAliases(object):
return self._tab.VectorLen(o)
return 0
# TypeAliases
def Vf64IsNone(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(26))
return o == 0
def TypeAliasesStart(builder): builder.StartObject(12)
def TypeAliasesAddI8(builder, i8): builder.PrependInt8Slot(0, i8, 0)
def TypeAliasesAddU8(builder, u8): builder.PrependUint8Slot(1, u8, 0)
......@@ -152,3 +164,96 @@ def TypeAliasesStartV8Vector(builder, numElems): return builder.StartVector(1, n
def TypeAliasesAddVf64(builder, vf64): builder.PrependUOffsetTRelativeSlot(11, flatbuffers.number_types.UOffsetTFlags.py_type(vf64), 0)
def TypeAliasesStartVf64Vector(builder, numElems): return builder.StartVector(8, numElems, 8)
def TypeAliasesEnd(builder): return builder.EndObject()
try:
from typing import List
except:
pass
class TypeAliasesT(object):
# TypeAliasesT
def __init__(self):
self.i8 = 0 # type: int
self.u8 = 0 # type: int
self.i16 = 0 # type: int
self.u16 = 0 # type: int
self.i32 = 0 # type: int
self.u32 = 0 # type: int
self.i64 = 0 # type: int
self.u64 = 0 # type: int
self.f32 = 0.0 # type: float
self.f64 = 0.0 # type: float
self.v8 = None # type: List[int]
self.vf64 = None # type: List[float]
@classmethod
def InitFromBuf(cls, buf, pos):
typeAliases = TypeAliases()
typeAliases.Init(buf, pos)
return cls.InitFromObj(typeAliases)
@classmethod
def InitFromObj(cls, typeAliases):
x = TypeAliasesT()
x._UnPack(typeAliases)
return x
# TypeAliasesT
def _UnPack(self, typeAliases):
if typeAliases is None:
return
self.i8 = typeAliases.I8()
self.u8 = typeAliases.U8()
self.i16 = typeAliases.I16()
self.u16 = typeAliases.U16()
self.i32 = typeAliases.I32()
self.u32 = typeAliases.U32()
self.i64 = typeAliases.I64()
self.u64 = typeAliases.U64()
self.f32 = typeAliases.F32()
self.f64 = typeAliases.F64()
if not typeAliases.V8IsNone():
if np is None:
self.v8 = []
for i in range(typeAliases.V8Length()):
self.v8.append(typeAliases.V8(i))
else:
self.v8 = typeAliases.V8AsNumpy()
if not typeAliases.Vf64IsNone():
if np is None:
self.vf64 = []
for i in range(typeAliases.Vf64Length()):
self.vf64.append(typeAliases.Vf64(i))
else:
self.vf64 = typeAliases.Vf64AsNumpy()
# TypeAliasesT
def Pack(self, builder):
if self.v8 is not None:
TypeAliasesStartV8Vector(builder, len(self.v8))
for i in reversed(range(len(self.v8))):
builder.PrependByte(self.v8[i])
v8 = builder.EndVector(len(self.v8))
if self.vf64 is not None:
TypeAliasesStartVf64Vector(builder, len(self.vf64))
for i in reversed(range(len(self.vf64))):
builder.PrependFloat64(self.vf64[i])
vf64 = builder.EndVector(len(self.vf64))
TypeAliasesStart(builder)
TypeAliasesAddI8(builder, self.i8)
TypeAliasesAddU8(builder, self.u8)
TypeAliasesAddI16(builder, self.i16)
TypeAliasesAddU16(builder, self.u16)
TypeAliasesAddI32(builder, self.i32)
TypeAliasesAddU32(builder, self.u32)
TypeAliasesAddI64(builder, self.i64)
TypeAliasesAddU64(builder, self.u64)
TypeAliasesAddF32(builder, self.f32)
TypeAliasesAddF64(builder, self.f64)
if self.v8 is not None:
TypeAliasesAddV8(builder, v8)
if self.vf64 is not None:
TypeAliasesAddVf64(builder, vf64)
typeAliases = TypeAliasesEnd(builder)
return typeAliases
......@@ -3,6 +3,8 @@
# namespace: Example
import flatbuffers
from flatbuffers.compat import import_numpy
np = import_numpy()
class Vec3(object):
__slots__ = ['_tab']
......@@ -42,3 +44,48 @@ def CreateVec3(builder, x, y, z, test1, test2, test3_a, test3_b):
builder.PrependFloat32(y)
builder.PrependFloat32(x)
return builder.Offset()
import MyGame.Example.Test
try:
from typing import Optional
except:
pass
class Vec3T(object):
# Vec3T
def __init__(self):
self.x = 0.0 # type: float
self.y = 0.0 # type: float
self.z = 0.0 # type: float
self.test1 = 0.0 # type: float
self.test2 = 0 # type: int
self.test3 = None # type: Optional[MyGame.Example.Test.TestT]
@classmethod
def InitFromBuf(cls, buf, pos):
vec3 = Vec3()
vec3.Init(buf, pos)
return cls.InitFromObj(vec3)
@classmethod
def InitFromObj(cls, vec3):
x = Vec3T()
x._UnPack(vec3)
return x
# Vec3T
def _UnPack(self, vec3):
if vec3 is None:
return
self.x = vec3.X()
self.y = vec3.Y()
self.z = vec3.Z()
self.test1 = vec3.Test1()
self.test2 = vec3.Test2()
if vec3.Test3(MyGame.Example.Test.Test()) is not None:
self.test3 = MyGame.Example.Test.TestT.InitFromObj(vec3.Test3(MyGame.Example.Test.Test()))
# Vec3T
def Pack(self, builder):
return CreateVec3(builder, self.x, self.y, self.z, self.test1, self.test2, self.test3.a, self.test3.b)
......@@ -3,6 +3,8 @@
# namespace: Example2
import flatbuffers
from flatbuffers.compat import import_numpy
np = import_numpy()
class Monster(object):
__slots__ = ['_tab']
......@@ -24,3 +26,33 @@ class Monster(object):
def MonsterStart(builder): builder.StartObject(0)
def MonsterEnd(builder): return builder.EndObject()
class MonsterT(object):
# MonsterT
def __init__(self):
pass
@classmethod
def InitFromBuf(cls, buf, pos):
monster = Monster()
monster.Init(buf, pos)
return cls.InitFromObj(monster)
@classmethod
def InitFromObj(cls, monster):
x = MonsterT()
x._UnPack(monster)
return x
# MonsterT
def _UnPack(self, monster):
if monster is None:
return
# MonsterT
def Pack(self, builder):
MonsterStart(builder)
monster = MonsterEnd(builder)
return monster
......@@ -3,6 +3,8 @@
# namespace: MyGame
import flatbuffers
from flatbuffers.compat import import_numpy
np = import_numpy()
class InParentNamespace(object):
__slots__ = ['_tab']
......@@ -24,3 +26,33 @@ class InParentNamespace(object):
def InParentNamespaceStart(builder): builder.StartObject(0)
def InParentNamespaceEnd(builder): return builder.EndObject()
class InParentNamespaceT(object):
# InParentNamespaceT
def __init__(self):
pass
@classmethod
def InitFromBuf(cls, buf, pos):
inParentNamespace = InParentNamespace()
inParentNamespace.Init(buf, pos)
return cls.InitFromObj(inParentNamespace)
@classmethod
def InitFromObj(cls, inParentNamespace):
x = InParentNamespaceT()
x._UnPack(inParentNamespace)
return x
# InParentNamespaceT
def _UnPack(self, inParentNamespace):
if inParentNamespace is None:
return
# InParentNamespaceT
def Pack(self, builder):
InParentNamespaceStart(builder)
inParentNamespace = InParentNamespaceEnd(builder)
return inParentNamespace
......@@ -3,6 +3,8 @@
# namespace: MyGame
import flatbuffers
from flatbuffers.compat import import_numpy
np = import_numpy()
class MonsterExtra(object):
__slots__ = ['_tab']
......@@ -100,6 +102,11 @@ class MonsterExtra(object):
return self._tab.VectorLen(o)
return 0
# MonsterExtra
def DvecIsNone(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(20))
return o == 0
# MonsterExtra
def Fvec(self, j):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(22))
......@@ -122,6 +129,11 @@ class MonsterExtra(object):
return self._tab.VectorLen(o)
return 0
# MonsterExtra
def FvecIsNone(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(22))
return o == 0
def MonsterExtraStart(builder): builder.StartObject(10)
def MonsterExtraAddD0(builder, d0): builder.PrependFloat64Slot(0, d0, float('nan'))
def MonsterExtraAddD1(builder, d1): builder.PrependFloat64Slot(1, d1, float('nan'))
......@@ -136,3 +148,90 @@ def MonsterExtraStartDvecVector(builder, numElems): return builder.StartVector(8
def MonsterExtraAddFvec(builder, fvec): builder.PrependUOffsetTRelativeSlot(9, flatbuffers.number_types.UOffsetTFlags.py_type(fvec), 0)
def MonsterExtraStartFvecVector(builder, numElems): return builder.StartVector(4, numElems, 4)
def MonsterExtraEnd(builder): return builder.EndObject()
try:
from typing import List
except:
pass
class MonsterExtraT(object):
# MonsterExtraT
def __init__(self):
self.d0 = float('nan') # type: float
self.d1 = float('nan') # type: float
self.d2 = float('inf') # type: float
self.d3 = float('-inf') # type: float
self.f0 = float('nan') # type: float
self.f1 = float('nan') # type: float
self.f2 = float('inf') # type: float
self.f3 = float('-inf') # type: float
self.dvec = None # type: List[float]
self.fvec = None # type: List[float]
@classmethod
def InitFromBuf(cls, buf, pos):
monsterExtra = MonsterExtra()
monsterExtra.Init(buf, pos)
return cls.InitFromObj(monsterExtra)
@classmethod
def InitFromObj(cls, monsterExtra):
x = MonsterExtraT()
x._UnPack(monsterExtra)
return x
# MonsterExtraT
def _UnPack(self, monsterExtra):
if monsterExtra is None:
return
self.d0 = monsterExtra.D0()
self.d1 = monsterExtra.D1()
self.d2 = monsterExtra.D2()
self.d3 = monsterExtra.D3()
self.f0 = monsterExtra.F0()
self.f1 = monsterExtra.F1()
self.f2 = monsterExtra.F2()
self.f3 = monsterExtra.F3()
if not monsterExtra.DvecIsNone():
if np is None:
self.dvec = []
for i in range(monsterExtra.DvecLength()):
self.dvec.append(monsterExtra.Dvec(i))
else:
self.dvec = monsterExtra.DvecAsNumpy()
if not monsterExtra.FvecIsNone():
if np is None:
self.fvec = []
for i in range(monsterExtra.FvecLength()):
self.fvec.append(monsterExtra.Fvec(i))
else:
self.fvec = monsterExtra.FvecAsNumpy()
# MonsterExtraT
def Pack(self, builder):
if self.dvec is not None:
MonsterExtraStartDvecVector(builder, len(self.dvec))
for i in reversed(range(len(self.dvec))):
builder.PrependFloat64(self.dvec[i])
dvec = builder.EndVector(len(self.dvec))
if self.fvec is not None:
MonsterExtraStartFvecVector(builder, len(self.fvec))
for i in reversed(range(len(self.fvec))):
builder.PrependFloat32(self.fvec[i])
fvec = builder.EndVector(len(self.fvec))
MonsterExtraStart(builder)
MonsterExtraAddD0(builder, self.d0)
MonsterExtraAddD1(builder, self.d1)
MonsterExtraAddD2(builder, self.d2)
MonsterExtraAddD3(builder, self.d3)
MonsterExtraAddF0(builder, self.f0)
MonsterExtraAddF1(builder, self.f1)
MonsterExtraAddF2(builder, self.f2)
MonsterExtraAddF3(builder, self.f3)
if self.dvec is not None:
MonsterExtraAddDvec(builder, dvec)
if self.fvec is not None:
MonsterExtraAddFvec(builder, fvec)
monsterExtra = MonsterExtraEnd(builder)
return monsterExtra
......@@ -20,7 +20,7 @@ gen_code_path=${test_dir}
runtime_library_dir=${test_dir}/../python
# Emit Python code for the example schema in the test dir:
${test_dir}/../flatc -p -o ${gen_code_path} -I include_test monster_test.fbs
${test_dir}/../flatc -p -o ${gen_code_path} -I include_test monster_test.fbs --gen-object-api
# Syntax: run_tests <interpreter> <benchmark vtable dedupes>
# <benchmark read count> <benchmark build count>
......
......@@ -15,19 +15,23 @@
set buildtype=Release
if "%1"=="-b" set buildtype=%2
..\%buildtype%\flatc.exe --cpp --java --csharp --dart --go --binary --lobster --lua --python --js --ts --php --rust --grpc --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes --cpp-ptr-type flatbuffers::unique_ptr --no-fb-import -I include_test monster_test.fbs monsterdata_test.json || goto FAIL
..\%buildtype%\flatc.exe --cpp --java --csharp --dart --go --binary --lobster --lua --js --ts --php --rust --grpc --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes --cpp-ptr-type flatbuffers::unique_ptr --no-fb-import -I include_test monster_test.fbs monsterdata_test.json || goto FAIL
..\%buildtype%\flatc.exe --python --gen-mutable --reflect-names --gen-object-api --gen-compare --cpp-ptr-type flatbuffers::unique_ptr --no-fb-import -I include_test monster_test.fbs monsterdata_test.json || goto FAIL
..\%buildtype%\flatc.exe --cpp --java --csharp --dart --go --binary --lobster --lua --python --js --ts --php --rust --gen-mutable --reflect-names --no-fb-import --cpp-ptr-type flatbuffers::unique_ptr -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs || goto FAIL
..\%buildtype%\flatc.exe --cpp --java --csharp --js --ts --php --gen-mutable --reflect-names --gen-object-api --gen-compare --cpp-ptr-type flatbuffers::unique_ptr -o union_vector ./union_vector/union_vector.fbs || goto FAIL
..\%buildtype%\flatc.exe --cpp --scoped-enums -o evolution_test ./evolution_test/evolution_v1.fbs ./evolution_test/evolution_v2.fbs|| goto FAIL
..\%buildtype%\flatc.exe -b --schema --bfbs-comments --bfbs-builtins -I include_test monster_test.fbs || goto FAIL
..\%buildtype%\flatc.exe -b --schema --bfbs-comments --bfbs-builtins -I include_test arrays_test.fbs || goto FAIL
..\%buildtype%\flatc.exe --jsonschema --schema -I include_test monster_test.fbs || goto FAIL
..\%buildtype%\flatc.exe --cpp --java --csharp --python --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes --scoped-enums --jsonschema --cpp-ptr-type flatbuffers::unique_ptr arrays_test.fbs || goto FAIL
..\%buildtype%\flatc.exe --cpp --java --csharp --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes --scoped-enums --jsonschema --cpp-ptr-type flatbuffers::unique_ptr arrays_test.fbs || goto FAIL
..\%buildtype%\flatc.exe --python --gen-mutable --reflect-names --gen-object-api --gen-compare --scoped-enums --jsonschema --cpp-ptr-type flatbuffers::unique_ptr arrays_test.fbs || goto FAIL
..\%buildtype%\flatc.exe --cpp --gen-mutable --gen-object-api --reflect-names --cpp-ptr-type flatbuffers::unique_ptr native_type_test.fbs || goto FAIL
IF NOT "%MONSTER_EXTRA%"=="skip" (
@echo Generate MosterExtra
..\%buildtype%\flatc.exe --cpp --java --csharp --python --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes --cpp-ptr-type flatbuffers::unique_ptr monster_extra.fbs monsterdata_extra.json || goto FAIL
..\%buildtype%\flatc.exe --cpp --java --csharp --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes --cpp-ptr-type flatbuffers::unique_ptr monster_extra.fbs monsterdata_extra.json || goto FAIL
..\%buildtype%\flatc.exe --python --gen-mutable --reflect-names --gen-object-api --gen-compare --cpp-ptr-type flatbuffers::unique_ptr monster_extra.fbs monsterdata_extra.json || goto FAIL
) else (
@echo monster_extra.fbs skipped (the strtod function from MSVC2013 or older doesn't support NaN/Inf arguments)
)
......
......@@ -15,7 +15,8 @@
# limitations under the License.
set -e
../flatc --cpp --java --kotlin --csharp --dart --go --binary --lobster --lua --python --js --ts --php --rust --grpc --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes --cpp-ptr-type flatbuffers::unique_ptr --no-fb-import -I include_test monster_test.fbs monsterdata_test.json
../flatc --cpp --java --kotlin --csharp --dart --go --binary --lobster --lua --js --ts --php --rust --grpc --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes --cpp-ptr-type flatbuffers::unique_ptr --no-fb-import -I include_test monster_test.fbs monsterdata_test.json
../flatc --python --gen-mutable --reflect-names --gen-object-api --gen-compare --cpp-ptr-type flatbuffers::unique_ptr --no-fb-import -I include_test monster_test.fbs monsterdata_test.json
../flatc --cpp --java --kotlin --csharp --dart --go --binary --lobster --lua --python --js --ts --php --rust --gen-mutable --reflect-names --no-fb-import --cpp-ptr-type flatbuffers::unique_ptr -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs
../flatc --cpp --java --kotlin --csharp --js --ts --php --gen-mutable --reflect-names --gen-object-api --gen-compare --cpp-ptr-type flatbuffers::unique_ptr -o union_vector ./union_vector/union_vector.fbs
../flatc --cpp --scoped-enums -o evolution_test ./evolution_test/evolution_v*.fbs
......@@ -23,7 +24,8 @@ set -e
../flatc -b --schema --bfbs-comments --bfbs-builtins -I include_test arrays_test.fbs
../flatc --jsonschema --schema -I include_test monster_test.fbs
../flatc --cpp --java --kotlin --csharp --python --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes --cpp-ptr-type flatbuffers::unique_ptr monster_extra.fbs monsterdata_extra.json
../flatc --cpp --java --csharp --python --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes --scoped-enums --jsonschema --cpp-ptr-type flatbuffers::unique_ptr arrays_test.fbs
../flatc --cpp --java --csharp --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes --scoped-enums --jsonschema --cpp-ptr-type flatbuffers::unique_ptr arrays_test.fbs
../flatc --python --gen-mutable --reflect-names --gen-object-api --gen-compare --scoped-enums --jsonschema --cpp-ptr-type flatbuffers::unique_ptr arrays_test.fbs
cd ../samples
../flatc --cpp --lobster --gen-mutable --reflect-names --gen-object-api --gen-compare --cpp-ptr-type flatbuffers::unique_ptr monster.fbs
../flatc -b --schema --bfbs-comments --bfbs-builtins monster.fbs
......
......@@ -3,6 +3,8 @@
# namespace: NamespaceB
import flatbuffers
from flatbuffers.compat import import_numpy
np = import_numpy()
class StructInNestedNS(object):
__slots__ = ['_tab']
......
......@@ -3,6 +3,8 @@
# namespace: NamespaceB
import flatbuffers
from flatbuffers.compat import import_numpy
np = import_numpy()
class TableInNestedNS(object):
__slots__ = ['_tab']
......
......@@ -3,6 +3,8 @@
# namespace: NamespaceA
import flatbuffers
from flatbuffers.compat import import_numpy
np = import_numpy()
class SecondTableInA(object):
__slots__ = ['_tab']
......@@ -23,7 +25,7 @@ class SecondTableInA(object):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
if o != 0:
x = self._tab.Indirect(o + self._tab.Pos)
from .TableInC import TableInC
from NamespaceC.TableInC import TableInC
obj = TableInC()
obj.Init(self._tab.Bytes, x)
return obj
......
......@@ -3,6 +3,8 @@
# namespace: NamespaceA
import flatbuffers
from flatbuffers.compat import import_numpy
np = import_numpy()
class TableInFirstNS(object):
__slots__ = ['_tab']
......@@ -23,7 +25,7 @@ class TableInFirstNS(object):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
if o != 0:
x = self._tab.Indirect(o + self._tab.Pos)
from .TableInNestedNS import TableInNestedNS
from NamespaceA.NamespaceB.TableInNestedNS import TableInNestedNS
obj = TableInNestedNS()
obj.Init(self._tab.Bytes, x)
return obj
......@@ -41,7 +43,7 @@ class TableInFirstNS(object):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(8))
if o != 0:
x = o + self._tab.Pos
from .StructInNestedNS import StructInNestedNS
from NamespaceA.NamespaceB.StructInNestedNS import StructInNestedNS
obj = StructInNestedNS()
obj.Init(self._tab.Bytes, x)
return obj
......
......@@ -3,6 +3,8 @@
# namespace: NamespaceC
import flatbuffers
from flatbuffers.compat import import_numpy
np = import_numpy()
class TableInC(object):
__slots__ = ['_tab']
......@@ -23,7 +25,7 @@ class TableInC(object):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
if o != 0:
x = self._tab.Indirect(o + self._tab.Pos)
from .TableInFirstNS import TableInFirstNS
from NamespaceA.TableInFirstNS import TableInFirstNS
obj = TableInFirstNS()
obj.Init(self._tab.Bytes, x)
return obj
......@@ -34,7 +36,7 @@ class TableInC(object):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(6))
if o != 0:
x = self._tab.Indirect(o + self._tab.Pos)
from .SecondTableInA import SecondTableInA
from NamespaceA.SecondTableInA import SecondTableInA
obj = SecondTableInA()
obj.Init(self._tab.Bytes, x)
return obj
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment