mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-01 05:51:39 +00:00
Fix inconsistent Python union creator function naming (#8981)
This commit is contained in:
93
tests/union_name_test/Bar.py
Normal file
93
tests/union_name_test/Bar.py
Normal file
@@ -0,0 +1,93 @@
|
||||
# automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
# namespace: union_name_test
|
||||
|
||||
import flatbuffers
|
||||
from flatbuffers.compat import import_numpy
|
||||
np = import_numpy()
|
||||
|
||||
class Bar(object):
|
||||
__slots__ = ['_tab']
|
||||
|
||||
@classmethod
|
||||
def GetRootAs(cls, buf, offset=0):
|
||||
n = flatbuffers.encode.Get(flatbuffers.packer.uoffset, buf, offset)
|
||||
x = Bar()
|
||||
x.Init(buf, n + offset)
|
||||
return x
|
||||
|
||||
@classmethod
|
||||
def GetRootAsBar(cls, buf, offset=0):
|
||||
"""This method is deprecated. Please switch to GetRootAs."""
|
||||
return cls.GetRootAs(buf, offset)
|
||||
# Bar
|
||||
def Init(self, buf, pos):
|
||||
self._tab = flatbuffers.table.Table(buf, pos)
|
||||
|
||||
# Bar
|
||||
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
|
||||
|
||||
def BarStart(builder):
|
||||
builder.StartObject(1)
|
||||
|
||||
def Start(builder):
|
||||
BarStart(builder)
|
||||
|
||||
def BarAddName(builder, name):
|
||||
builder.PrependUOffsetTRelativeSlot(0, flatbuffers.number_types.UOffsetTFlags.py_type(name), 0)
|
||||
|
||||
def AddName(builder, name):
|
||||
BarAddName(builder, name)
|
||||
|
||||
def BarEnd(builder):
|
||||
return builder.EndObject()
|
||||
|
||||
def End(builder):
|
||||
return BarEnd(builder)
|
||||
|
||||
|
||||
class BarT(object):
|
||||
|
||||
# BarT
|
||||
def __init__(
|
||||
self,
|
||||
name = None,
|
||||
):
|
||||
self.name = name # type: Optional[str]
|
||||
|
||||
@classmethod
|
||||
def InitFromBuf(cls, buf, pos):
|
||||
bar = Bar()
|
||||
bar.Init(buf, pos)
|
||||
return cls.InitFromObj(bar)
|
||||
|
||||
@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, bar):
|
||||
x = BarT()
|
||||
x._UnPack(bar)
|
||||
return x
|
||||
|
||||
# BarT
|
||||
def _UnPack(self, bar):
|
||||
if bar is None:
|
||||
return
|
||||
self.name = bar.Name()
|
||||
|
||||
# BarT
|
||||
def Pack(self, builder):
|
||||
if self.name is not None:
|
||||
name = builder.CreateString(self.name)
|
||||
BarStart(builder)
|
||||
if self.name is not None:
|
||||
BarAddName(builder, name)
|
||||
bar = BarEnd(builder)
|
||||
return bar
|
||||
120
tests/union_name_test/Container.py
Normal file
120
tests/union_name_test/Container.py
Normal file
@@ -0,0 +1,120 @@
|
||||
# automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
# namespace: union_name_test
|
||||
|
||||
import flatbuffers
|
||||
from flatbuffers.compat import import_numpy
|
||||
np = import_numpy()
|
||||
|
||||
class Container(object):
|
||||
__slots__ = ['_tab']
|
||||
|
||||
@classmethod
|
||||
def GetRootAs(cls, buf, offset=0):
|
||||
n = flatbuffers.encode.Get(flatbuffers.packer.uoffset, buf, offset)
|
||||
x = Container()
|
||||
x.Init(buf, n + offset)
|
||||
return x
|
||||
|
||||
@classmethod
|
||||
def GetRootAsContainer(cls, buf, offset=0):
|
||||
"""This method is deprecated. Please switch to GetRootAs."""
|
||||
return cls.GetRootAs(buf, offset)
|
||||
# Container
|
||||
def Init(self, buf, pos):
|
||||
self._tab = flatbuffers.table.Table(buf, pos)
|
||||
|
||||
# Container
|
||||
def UType(self):
|
||||
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
|
||||
if o != 0:
|
||||
return self._tab.Get(flatbuffers.number_types.Uint8Flags, o + self._tab.Pos)
|
||||
return 0
|
||||
|
||||
# Container
|
||||
def U(self):
|
||||
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(6))
|
||||
if o != 0:
|
||||
from flatbuffers.table import Table
|
||||
obj = Table(bytearray(), 0)
|
||||
self._tab.Union(obj, o)
|
||||
return obj
|
||||
return None
|
||||
|
||||
def ContainerStart(builder):
|
||||
builder.StartObject(2)
|
||||
|
||||
def Start(builder):
|
||||
ContainerStart(builder)
|
||||
|
||||
def ContainerAddUType(builder, uType):
|
||||
builder.PrependUint8Slot(0, uType, 0)
|
||||
|
||||
def AddUType(builder, uType):
|
||||
ContainerAddUType(builder, uType)
|
||||
|
||||
def ContainerAddU(builder, u):
|
||||
builder.PrependUOffsetTRelativeSlot(1, flatbuffers.number_types.UOffsetTFlags.py_type(u), 0)
|
||||
|
||||
def AddU(builder, u):
|
||||
ContainerAddU(builder, u)
|
||||
|
||||
def ContainerEnd(builder):
|
||||
return builder.EndObject()
|
||||
|
||||
def End(builder):
|
||||
return ContainerEnd(builder)
|
||||
|
||||
import union_name_test.Bar
|
||||
import union_name_test.Foo
|
||||
import union_name_test.my_test_union
|
||||
try:
|
||||
from typing import Union
|
||||
except:
|
||||
pass
|
||||
|
||||
class ContainerT(object):
|
||||
|
||||
# ContainerT
|
||||
def __init__(
|
||||
self,
|
||||
uType = 0,
|
||||
u = None,
|
||||
):
|
||||
self.uType = uType # type: int
|
||||
self.u = u # type: Union[None, 'union_name_test.Foo.FooT', 'union_name_test.Bar.BarT']
|
||||
|
||||
@classmethod
|
||||
def InitFromBuf(cls, buf, pos):
|
||||
container = Container()
|
||||
container.Init(buf, pos)
|
||||
return cls.InitFromObj(container)
|
||||
|
||||
@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, container):
|
||||
x = ContainerT()
|
||||
x._UnPack(container)
|
||||
return x
|
||||
|
||||
# ContainerT
|
||||
def _UnPack(self, container):
|
||||
if container is None:
|
||||
return
|
||||
self.uType = container.UType()
|
||||
self.u = union_name_test.my_test_union.MyTestUnionCreator(self.uType, container.U())
|
||||
|
||||
# ContainerT
|
||||
def Pack(self, builder):
|
||||
if self.u is not None:
|
||||
u = self.u.Pack(builder)
|
||||
ContainerStart(builder)
|
||||
ContainerAddUType(builder, self.uType)
|
||||
if self.u is not None:
|
||||
ContainerAddU(builder, u)
|
||||
container = ContainerEnd(builder)
|
||||
return container
|
||||
90
tests/union_name_test/Foo.py
Normal file
90
tests/union_name_test/Foo.py
Normal file
@@ -0,0 +1,90 @@
|
||||
# automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
# namespace: union_name_test
|
||||
|
||||
import flatbuffers
|
||||
from flatbuffers.compat import import_numpy
|
||||
np = import_numpy()
|
||||
|
||||
class Foo(object):
|
||||
__slots__ = ['_tab']
|
||||
|
||||
@classmethod
|
||||
def GetRootAs(cls, buf, offset=0):
|
||||
n = flatbuffers.encode.Get(flatbuffers.packer.uoffset, buf, offset)
|
||||
x = Foo()
|
||||
x.Init(buf, n + offset)
|
||||
return x
|
||||
|
||||
@classmethod
|
||||
def GetRootAsFoo(cls, buf, offset=0):
|
||||
"""This method is deprecated. Please switch to GetRootAs."""
|
||||
return cls.GetRootAs(buf, offset)
|
||||
# Foo
|
||||
def Init(self, buf, pos):
|
||||
self._tab = flatbuffers.table.Table(buf, pos)
|
||||
|
||||
# Foo
|
||||
def Val(self):
|
||||
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
|
||||
if o != 0:
|
||||
return self._tab.Get(flatbuffers.number_types.Int32Flags, o + self._tab.Pos)
|
||||
return 0
|
||||
|
||||
def FooStart(builder):
|
||||
builder.StartObject(1)
|
||||
|
||||
def Start(builder):
|
||||
FooStart(builder)
|
||||
|
||||
def FooAddVal(builder, val):
|
||||
builder.PrependInt32Slot(0, val, 0)
|
||||
|
||||
def AddVal(builder, val):
|
||||
FooAddVal(builder, val)
|
||||
|
||||
def FooEnd(builder):
|
||||
return builder.EndObject()
|
||||
|
||||
def End(builder):
|
||||
return FooEnd(builder)
|
||||
|
||||
|
||||
class FooT(object):
|
||||
|
||||
# FooT
|
||||
def __init__(
|
||||
self,
|
||||
val = 0,
|
||||
):
|
||||
self.val = val # type: int
|
||||
|
||||
@classmethod
|
||||
def InitFromBuf(cls, buf, pos):
|
||||
foo = Foo()
|
||||
foo.Init(buf, pos)
|
||||
return cls.InitFromObj(foo)
|
||||
|
||||
@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, foo):
|
||||
x = FooT()
|
||||
x._UnPack(foo)
|
||||
return x
|
||||
|
||||
# FooT
|
||||
def _UnPack(self, foo):
|
||||
if foo is None:
|
||||
return
|
||||
self.val = foo.Val()
|
||||
|
||||
# FooT
|
||||
def Pack(self, builder):
|
||||
FooStart(builder)
|
||||
FooAddVal(builder, self.val)
|
||||
foo = FooEnd(builder)
|
||||
return foo
|
||||
0
tests/union_name_test/__init__.py
Normal file
0
tests/union_name_test/__init__.py
Normal file
20
tests/union_name_test/my_test_union.py
Normal file
20
tests/union_name_test/my_test_union.py
Normal file
@@ -0,0 +1,20 @@
|
||||
# automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
# namespace: union_name_test
|
||||
|
||||
class my_test_union(object):
|
||||
NONE = 0
|
||||
Foo = 1
|
||||
Bar = 2
|
||||
|
||||
def MyTestUnionCreator(unionType, table):
|
||||
from flatbuffers.table import Table
|
||||
if not isinstance(table, Table):
|
||||
return None
|
||||
if unionType == my_test_union.Foo:
|
||||
import union_name_test.Foo
|
||||
return union_name_test.Foo.FooT.InitFromBuf(table.Bytes, table.Pos)
|
||||
if unionType == my_test_union.Bar:
|
||||
import union_name_test.Bar
|
||||
return union_name_test.Bar.BarT.InitFromBuf(table.Bytes, table.Pos)
|
||||
return None
|
||||
Reference in New Issue
Block a user