forked from google/flatbuffers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Python] Fixed the issue with nested unions relying on InitFromBuf. (g…
…oogle#7576) * feat: Fixed the issue with nested unions relying on InitFromBuf. Problem: Issue google#7569 Nested Unions were broken with the introduction of parsing buffers with an initial encoding offset. Fix: Revert the InitFromBuf method to the previous version and introduction of InitFromPackedBuf that allows users to read types from packed buffers applying the offset automatically. Test: Added in TestNestedUnionTables to test the encoding and decoding ability using a nested table with a union field. * fix: Uncommented generate code command
- Loading branch information
1 parent
7758c89
commit b7f34d1
Showing
30 changed files
with
766 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# automatically generated by the FlatBuffers compiler, do not modify | ||
|
||
# namespace: NestedUnion | ||
|
||
class Any(object): | ||
NONE = 0 | ||
Vec3 = 1 | ||
TestSimpleTableWithEnum = 2 | ||
|
||
def AnyCreator(unionType, table): | ||
from flatbuffers.table import Table | ||
if not isinstance(table, Table): | ||
return None | ||
if unionType == Any().Vec3: | ||
import MyGame.Example.NestedUnion.Vec3 | ||
return MyGame.Example.NestedUnion.Vec3.Vec3T.InitFromBuf(table.Bytes, table.Pos) | ||
if unionType == Any().TestSimpleTableWithEnum: | ||
import MyGame.Example.NestedUnion.TestSimpleTableWithEnum | ||
return MyGame.Example.NestedUnion.TestSimpleTableWithEnum.TestSimpleTableWithEnumT.InitFromBuf(table.Bytes, table.Pos) | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# automatically generated by the FlatBuffers compiler, do not modify | ||
|
||
# namespace: NestedUnion | ||
|
||
# Composite components of Monster color. | ||
class Color(object): | ||
Red = 1 | ||
# \brief color Green | ||
# Green is bit_flag with value (1u << 1) | ||
Green = 2 | ||
# \brief color Blue (1u << 3) | ||
Blue = 8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# automatically generated by the FlatBuffers compiler, do not modify | ||
|
||
# namespace: NestedUnion | ||
|
||
import flatbuffers | ||
from flatbuffers.compat import import_numpy | ||
np = import_numpy() | ||
|
||
class NestedUnionTest(object): | ||
__slots__ = ['_tab'] | ||
|
||
@classmethod | ||
def GetRootAs(cls, buf, offset=0): | ||
n = flatbuffers.encode.Get(flatbuffers.packer.uoffset, buf, offset) | ||
x = NestedUnionTest() | ||
x.Init(buf, n + offset) | ||
return x | ||
|
||
@classmethod | ||
def GetRootAsNestedUnionTest(cls, buf, offset=0): | ||
"""This method is deprecated. Please switch to GetRootAs.""" | ||
return cls.GetRootAs(buf, offset) | ||
# NestedUnionTest | ||
def Init(self, buf, pos): | ||
self._tab = flatbuffers.table.Table(buf, pos) | ||
|
||
# NestedUnionTest | ||
def Name(self): | ||
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4)) | ||
if o != 0: | ||
return self._tab.String(o + self._tab.Pos) | ||
return None | ||
|
||
# NestedUnionTest | ||
def DataType(self): | ||
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(6)) | ||
if o != 0: | ||
return self._tab.Get(flatbuffers.number_types.Uint8Flags, o + self._tab.Pos) | ||
return 0 | ||
|
||
# NestedUnionTest | ||
def Data(self): | ||
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(8)) | ||
if o != 0: | ||
from flatbuffers.table import Table | ||
obj = Table(bytearray(), 0) | ||
self._tab.Union(obj, o) | ||
return obj | ||
return None | ||
|
||
# NestedUnionTest | ||
def Id(self): | ||
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(10)) | ||
if o != 0: | ||
return self._tab.Get(flatbuffers.number_types.Int16Flags, o + self._tab.Pos) | ||
return 0 | ||
|
||
def NestedUnionTestStart(builder): builder.StartObject(4) | ||
def Start(builder): | ||
return NestedUnionTestStart(builder) | ||
def NestedUnionTestAddName(builder, name): builder.PrependUOffsetTRelativeSlot(0, flatbuffers.number_types.UOffsetTFlags.py_type(name), 0) | ||
def AddName(builder, name): | ||
return NestedUnionTestAddName(builder, name) | ||
def NestedUnionTestAddDataType(builder, dataType): builder.PrependUint8Slot(1, dataType, 0) | ||
def AddDataType(builder, dataType): | ||
return NestedUnionTestAddDataType(builder, dataType) | ||
def NestedUnionTestAddData(builder, data): builder.PrependUOffsetTRelativeSlot(2, flatbuffers.number_types.UOffsetTFlags.py_type(data), 0) | ||
def AddData(builder, data): | ||
return NestedUnionTestAddData(builder, data) | ||
def NestedUnionTestAddId(builder, id): builder.PrependInt16Slot(3, id, 0) | ||
def AddId(builder, id): | ||
return NestedUnionTestAddId(builder, id) | ||
def NestedUnionTestEnd(builder): return builder.EndObject() | ||
def End(builder): | ||
return NestedUnionTestEnd(builder) | ||
import MyGame.Example.NestedUnion.Any | ||
import MyGame.Example.NestedUnion.TestSimpleTableWithEnum | ||
import MyGame.Example.NestedUnion.Vec3 | ||
try: | ||
from typing import Union | ||
except: | ||
pass | ||
|
||
class NestedUnionTestT(object): | ||
|
||
# NestedUnionTestT | ||
def __init__(self): | ||
self.name = None # type: str | ||
self.dataType = 0 # type: int | ||
self.data = None # type: Union[None, MyGame.Example.NestedUnion.Vec3.Vec3T, MyGame.Example.NestedUnion.TestSimpleTableWithEnum.TestSimpleTableWithEnumT] | ||
self.id = 0 # type: int | ||
|
||
@classmethod | ||
def InitFromBuf(cls, buf, pos): | ||
nestedUnionTest = NestedUnionTest() | ||
nestedUnionTest.Init(buf, pos) | ||
return cls.InitFromObj(nestedUnionTest) | ||
|
||
@classmethod | ||
def InitFromPackedBuf(cls, buf, pos=0): | ||
n = flatbuffers.encode.Get(flatbuffers.packer.uoffset, buf, pos) | ||
return cls.InitFromBuf(buf, pos+n) | ||
|
||
@classmethod | ||
def InitFromObj(cls, nestedUnionTest): | ||
x = NestedUnionTestT() | ||
x._UnPack(nestedUnionTest) | ||
return x | ||
|
||
# NestedUnionTestT | ||
def _UnPack(self, nestedUnionTest): | ||
if nestedUnionTest is None: | ||
return | ||
self.name = nestedUnionTest.Name() | ||
self.dataType = nestedUnionTest.DataType() | ||
self.data = MyGame.Example.NestedUnion.Any.AnyCreator(self.dataType, nestedUnionTest.Data()) | ||
self.id = nestedUnionTest.Id() | ||
|
||
# NestedUnionTestT | ||
def Pack(self, builder): | ||
if self.name is not None: | ||
name = builder.CreateString(self.name) | ||
if self.data is not None: | ||
data = self.data.Pack(builder) | ||
NestedUnionTestStart(builder) | ||
if self.name is not None: | ||
NestedUnionTestAddName(builder, name) | ||
NestedUnionTestAddDataType(builder, self.dataType) | ||
if self.data is not None: | ||
NestedUnionTestAddData(builder, data) | ||
NestedUnionTestAddId(builder, self.id) | ||
nestedUnionTest = NestedUnionTestEnd(builder) | ||
return nestedUnionTest |
Oops, something went wrong.