# 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
