union: fix dumping semantics
This commit is contained in:
parent
bfb2a17b71
commit
dcdb37b0f4
|
@ -110,7 +110,16 @@ class StructType(G[T], Type[T]):
|
|||
pos = stream.tell()
|
||||
|
||||
with self.enter():
|
||||
for name, type in self.fields.items():
|
||||
if self.union:
|
||||
if value._sx_lastset_:
|
||||
fields = [value._sx_lastset_]
|
||||
else:
|
||||
fields = next(self.fields)
|
||||
else:
|
||||
fields = list(self.fields)
|
||||
|
||||
for name in fields:
|
||||
type = self.fields[name]
|
||||
with context.enter(name, type):
|
||||
if self.union:
|
||||
stream.seek(pos, os.SEEK_SET)
|
||||
|
@ -328,4 +337,13 @@ class Struct:
|
|||
|
||||
|
||||
class Union(Struct, union=True, inject=False):
|
||||
pass
|
||||
_sx_lastset_ = ''
|
||||
|
||||
def __init__(self, **kwargs) -> None:
|
||||
super().__init__(**kwargs)
|
||||
if kwargs:
|
||||
super().__setattr__('_sx_lastset_', list(kwargs)[-1])
|
||||
|
||||
def __setattr__(self, name: str, value: Any) -> None:
|
||||
super().__setattr__(name, value)
|
||||
super().__setattr__('_sx_lastset_', name)
|
||||
|
|
Loading…
Reference in New Issue