# 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
