~martijnbraam/bare-py

eb85f7d2c98335f2fe261073d6c8ca8d33c6948f — Martijn Braam 8 months ago 7d8d422
Removed seperate code for top-level unions
3 files changed, 15 insertions(+), 26 deletions(-)

M bare/__init__.py
M bare/__main__.py
M example/schema.py
M bare/__init__.py => bare/__init__.py +8 -22
@@ 290,7 290,7 @@ def pack(instance, member=None):
        return tag + untagged


def unpack(instance, data, union=False, offset=0, primitive=False):
def unpack(instance, data, offset=0, primitive=False):
    """
    Decode a BARE message into an instance of a class with a BARE schema in it.
    :param instance: object to load the data into an to get the schema from


@@ 300,24 300,10 @@ def unpack(instance, data, union=False, offset=0, primitive=False):
    :param primitive: bare message doesn't encode a struct, return the raw decoded data instead
    :return: tuple of the unpacked data, and the number of bytes read from the input
    """
    if union:
        module = sys.modules[instance.__module__]
        tag, offset = _unpack_primitive(BarePrimitive(TypeKind.UINT), data, offset)
        for type in instance._ast.types:
            if type.value == tag:
                break
        else:
            raise ValueError("Cannot find type for tag {}".format(tag))
        result = getattr(module, type.type.name).unpack(data, offset=offset)
        if isinstance(result, tuple):
            return result[0]
        else:
            return result
    else:
        ast = instance._ast
        module = sys.modules[instance.__class__.__module__]
        if primitive:
            value, offset = _unpack_type(ast, instance, module, data, offset)
            instance.value = value
            return instance, offset
        return _unpack_type(ast, instance, module, data, offset)
    ast = instance._ast
    module = sys.modules[instance.__class__.__module__]
    if primitive:
        value, offset = _unpack_type(ast, instance, module, data, offset)
        instance.value = value
        return instance, offset
    return _unpack_type(ast, instance, module, data, offset)

M bare/__main__.py => bare/__main__.py +2 -1
@@ 48,7 48,8 @@ def _gen_type(type):
        result += '\t\treturn bare.pack(cls, member)\n\n'
        result += '\t@classmethod\n'
        result += '\tdef unpack(cls, data, offset=0):\n'
        result += '\t\treturn bare.unpack(cls, data, union=True, offset=offset)\n\n'
        result += '\t\tinstance = cls()\n'
        result += '\t\treturn bare.unpack(instance, data, offset=offset)\n\n'
        result += '\n'
        return result


M example/schema.py => example/schema.py +5 -3
@@ 2,7 2,7 @@ from collections import OrderedDict
from enum import Enum

import bare
from bare.ast import TypeKind, BarePrimitive, StructType, OptionalType, NamedType, ArrayType, MapType, UnionType, \
from bare.bare_ast import TypeKind, BarePrimitive, StructType, OptionalType, NamedType, ArrayType, MapType, UnionType, \
    UnionValue




@@ 133,7 133,8 @@ class Person:

    @classmethod
    def unpack(cls, data, offset=0):
        return bare.unpack(cls, data, union=True, offset=offset)
        instance = cls()
        return bare.unpack(instance, data, offset=offset)


class Address:


@@ 187,4 188,5 @@ class AddressMaybe:

    @classmethod
    def unpack(cls, data, offset=0):
        return bare.unpack(cls, data, union=True, offset=offset)
        instance = cls()
        return bare.unpack(instance, data, offset=offset)