[Python] Fixed the issue with nested unions relying on InitFromBuf. (#7576)

* feat: Fixed the issue with nested unions relying on InitFromBuf.
Problem: Issue #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
This commit is contained in:
Joshua Smith
2022-10-26 22:56:52 +01:00
committed by GitHub
parent 5a48b0d7d6
commit 043a24f2e4
30 changed files with 766 additions and 65 deletions

View File

@@ -47,6 +47,11 @@ import MyGame.Example.ArrayTable # refers to generated code
import MyGame.Example.ArrayStruct # refers to generated code
import MyGame.Example.NestedStruct # refers to generated code
import MyGame.Example.TestEnum # refers to generated code
import MyGame.Example.NestedUnion.NestedUnionTest # refers to generated code
import MyGame.Example.NestedUnion.Vec3 # refers to generated code
import MyGame.Example.NestedUnion.Any # refers to generated code
import MyGame.Example.NestedUnion.Test # refers to generated code
import MyGame.Example.NestedUnion.Color # refers to generated code
import monster_test_generated # the one-file version
import optional_scalars
import optional_scalars.ScalarStuff
@@ -2663,6 +2668,65 @@ class TestFixedLengthArrays(unittest.TestCase):
self.assertEqual(table.A().D(1).D(0), -2)
self.assertEqual(table.A().D(1).D(1), 2)
class TestNestedUnionTables(unittest.TestCase):
def test_nested_union_tables(self):
nestUnion = MyGame.Example.NestedUnion.NestedUnionTest.NestedUnionTestT()
nestUnion.name = b"testUnion1"
nestUnion.id = 1
nestUnion.data = MyGame.Example.NestedUnion.Vec3.Vec3T()
nestUnion.dataType = MyGame.Example.NestedUnion.Any.Any.Vec3
nestUnion.data.x = 4.278975356
nestUnion.data.y = 5.32
nestUnion.data.z = -6.464
nestUnion.data.test1 = 0.9
nestUnion.data.test2 = MyGame.Example.NestedUnion.Color.Color.Red
nestUnion.data.test3 = MyGame.Example.NestedUnion.Test.TestT()
nestUnion.data.test3.a = 5
nestUnion.data.test3.b = 2
b = flatbuffers.Builder(0)
b.Finish(nestUnion.Pack(b))
nestUnionDecode = MyGame.Example.NestedUnion.NestedUnionTest.NestedUnionTest.GetRootAs(b.Bytes, b.Head())
nestUnionDecodeT = MyGame.Example.NestedUnion.NestedUnionTest.NestedUnionTestT.InitFromObj(nestUnionDecode)
self.assertEqual(nestUnionDecodeT.name, nestUnion.name)
self.assertEqual(nestUnionDecodeT.id, nestUnion.id)
self.assertEqual(nestUnionDecodeT.dataType, nestUnion.dataType)
self.assertEqual(nestUnionDecodeT.data.x, nestUnion.data.x)
self.assertEqual(nestUnionDecodeT.data.y, nestUnion.data.y)
self.assertEqual(nestUnionDecodeT.data.z, nestUnion.data.z)
self.assertEqual(nestUnionDecodeT.data.test1, nestUnion.data.test1)
self.assertEqual(nestUnionDecodeT.data.test2, nestUnion.data.test2)
self.assertEqual(nestUnionDecodeT.data.test3.a, nestUnion.data.test3.a)
self.assertEqual(nestUnionDecodeT.data.test3.b, nestUnion.data.test3.b)
nestUnionDecodeTFromBuf = MyGame.Example.NestedUnion.NestedUnionTest.NestedUnionTestT.InitFromPackedBuf(b.Bytes, b.Head())
self.assertEqual(nestUnionDecodeTFromBuf.name, nestUnion.name)
self.assertEqual(nestUnionDecodeTFromBuf.id, nestUnion.id)
self.assertEqual(nestUnionDecodeTFromBuf.dataType, nestUnion.dataType)
self.assertEqual(nestUnionDecodeTFromBuf.data.x, nestUnion.data.x)
self.assertEqual(nestUnionDecodeTFromBuf.data.y, nestUnion.data.y)
self.assertEqual(nestUnionDecodeTFromBuf.data.z, nestUnion.data.z)
self.assertEqual(nestUnionDecodeTFromBuf.data.test1, nestUnion.data.test1)
self.assertEqual(nestUnionDecodeTFromBuf.data.test2, nestUnion.data.test2)
self.assertEqual(nestUnionDecodeTFromBuf.data.test3.a, nestUnion.data.test3.a)
self.assertEqual(nestUnionDecodeTFromBuf.data.test3.b, nestUnion.data.test3.b)
nestUnionDecodeTFromBuf2 = MyGame.Example.NestedUnion.NestedUnionTest.NestedUnionTestT.InitFromPackedBuf(b.Output())
self.assertEqual(nestUnionDecodeTFromBuf2.name, nestUnion.name)
self.assertEqual(nestUnionDecodeTFromBuf2.id, nestUnion.id)
self.assertEqual(nestUnionDecodeTFromBuf2.dataType, nestUnion.dataType)
self.assertEqual(nestUnionDecodeTFromBuf2.data.x, nestUnion.data.x)
self.assertEqual(nestUnionDecodeTFromBuf2.data.y, nestUnion.data.y)
self.assertEqual(nestUnionDecodeTFromBuf2.data.z, nestUnion.data.z)
self.assertEqual(nestUnionDecodeTFromBuf2.data.test1, nestUnion.data.test1)
self.assertEqual(nestUnionDecodeTFromBuf2.data.test2, nestUnion.data.test2)
self.assertEqual(nestUnionDecodeTFromBuf2.data.test3.a, nestUnion.data.test3.a)
self.assertEqual(nestUnionDecodeTFromBuf2.data.test3.b, nestUnion.data.test3.b)
def CheckAgainstGoldDataGo():
try:
gen_buf, gen_off = make_monster_from_generated_code()