Generate code to encode and decode nested flatbuffers in Python. (#6354)

* Generate code to encode and decode nested flatbuffers in Python.

* Delete accidental trailing whitespace.

* Fully delete trailing whitespace.
This commit is contained in:
Richard A Hofer
2021-01-04 15:18:19 -05:00
committed by GitHub
parent 57f68e2896
commit 24dd85fd28
3 changed files with 127 additions and 0 deletions

View File

@@ -1874,6 +1874,35 @@ class TestAllCodePathsOfExampleSchema(unittest.TestCase):
lambda: mon2.TestnestedflatbufferAsNumpy(),
NumpyRequiredForThisFeature)
def test_nested_monster_testnestedflatbuffer(self):
b = flatbuffers.Builder(0)
# build another monster to nest inside testnestedflatbuffer
nestedB = flatbuffers.Builder(0)
nameStr = nestedB.CreateString("Nested Monster")
MyGame.Example.Monster.MonsterStart(nestedB)
MyGame.Example.Monster.MonsterAddHp(nestedB, 30)
MyGame.Example.Monster.MonsterAddName(nestedB, nameStr)
nestedMon = MyGame.Example.Monster.MonsterEnd(nestedB)
nestedB.Finish(nestedMon)
# write the nested FB bytes
sub_buf = MyGame.Example.Monster.MonsterMakeTestnestedflatbufferVectorFromBytes(
b, nestedB.Output())
# make the parent monster and include the bytes of the nested monster
MyGame.Example.Monster.MonsterStart(b)
MyGame.Example.Monster.MonsterAddTestnestedflatbuffer(b, sub_buf)
mon = MyGame.Example.Monster.MonsterEnd(b)
b.Finish(mon)
# inspect the resulting data:
mon2 = MyGame.Example.Monster.Monster.GetRootAsMonster(b.Bytes,
b.Head())
nestedMon2 = mon2.TestnestedflatbufferNestedRoot()
self.assertEqual(b"Nested Monster", nestedMon2.Name())
self.assertEqual(30, nestedMon2.Hp())
def test_nondefault_monster_testempty(self):
b = flatbuffers.Builder(0)