changed "swapped" to "reverse" in TStruct and TBitStruct

This commit is contained in:
Tim Rid 2021-03-30 19:54:23 +02:00
parent b336087493
commit 94b7f7f606
2 changed files with 9 additions and 9 deletions

View file

@ -93,7 +93,7 @@ class _TStruct(Adapter[t.Any, t.Any, ContainerType, BuildTypes]):
def __init__(
self,
container_type: t.Type[ContainerType],
swapped: bool = False,
reverse: bool = False,
add_offsets: bool = False,
) -> None:
if not issubclass(container_type, TContainerMixin):
@ -107,12 +107,12 @@ class _TStruct(Adapter[t.Any, t.Any, ContainerType, BuildTypes]):
"'{}' has to be a 'dataclasses.dataclass'".format(repr(container_type))
)
self.container_type = container_type
self.swapped = swapped
self.reverse = reverse
self.add_offsets = add_offsets
# get all fields from the dataclass
fields = dataclasses.fields(self.container_type)
if self.swapped:
if self.reverse:
fields = tuple(reversed(fields))
# extract the construct formats from the struct_type
@ -192,7 +192,7 @@ class TStruct(_TStruct[ContainerType, ContainerType]):
def __new__(
cls,
container_type: t.Type[ContainerType],
swapped: bool = False,
reverse: bool = False,
add_offsets: bool = False,
) -> "TStruct[ContainerType]":
...
@ -213,7 +213,7 @@ class TBitStruct(_TStruct[ContainerType, ContainerType]):
def __new__(
cls,
container_type: t.Type[ContainerType],
swapped: bool = False,
reverse: bool = False,
add_offsets: bool = False,
) -> "TBitStruct[ContainerType]":
...

View file

@ -99,21 +99,21 @@ def test_tstruct() -> None:
assert c.b.subcon is cs.Int8ub
def test_tstruct_swapped() -> None:
def test_tstruct_reverse() -> None:
@dataclasses.dataclass
class TestContainer(cst.TContainerMixin):
a: int = cst.sfield(cs.Int16ub)
b: int = cst.sfield(cs.Int8ub)
common(
cst.TStruct(TestContainer, swapped=True),
cst.TStruct(TestContainer, reverse=True),
b"\x02\x00\x01",
TestContainer(a=1, b=2),
3,
)
normal = cst.TStruct(TestContainer)
swapped = cst.TStruct(TestContainer, swapped=True)
assert str(normal.parse(b"\x00\x01\x02")) == str(swapped.parse(b"\x02\x00\x01"))
reverse = cst.TStruct(TestContainer, reverse=True)
assert str(normal.parse(b"\x00\x01\x02")) == str(reverse.parse(b"\x02\x00\x01"))
def test_tstruct_add_offsets() -> None: