Use new `msgspec.structs` api for `.typecast()`
parent
994564f923
commit
00a51c0288
|
@ -22,11 +22,15 @@ import builtins
|
||||||
# import sys
|
# import sys
|
||||||
from pprint import pformat
|
from pprint import pformat
|
||||||
|
|
||||||
import msgspec
|
from msgspec import (
|
||||||
|
msgpack,
|
||||||
|
Struct,
|
||||||
|
structs,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class Struct(
|
class Struct(
|
||||||
msgspec.Struct,
|
Struct,
|
||||||
|
|
||||||
# https://jcristharif.com/msgspec/structs.html#tagged-unions
|
# https://jcristharif.com/msgspec/structs.html#tagged-unions
|
||||||
# tag='pikerstruct',
|
# tag='pikerstruct',
|
||||||
|
@ -37,10 +41,14 @@ class Struct(
|
||||||
|
|
||||||
'''
|
'''
|
||||||
def to_dict(self) -> dict:
|
def to_dict(self) -> dict:
|
||||||
return {
|
'''
|
||||||
f: getattr(self, f)
|
Like it sounds.. direct delegation to:
|
||||||
for f in self.__struct_fields__
|
https://jcristharif.com/msgspec/api.html#msgspec.structs.asdict
|
||||||
}
|
|
||||||
|
TODO: probably just drop this method since it's now a built-int method?
|
||||||
|
|
||||||
|
'''
|
||||||
|
return structs.asdict(self)
|
||||||
|
|
||||||
def pformat(self) -> str:
|
def pformat(self) -> str:
|
||||||
return f'Struct({pformat(self.to_dict())})'
|
return f'Struct({pformat(self.to_dict())})'
|
||||||
|
@ -49,7 +57,7 @@ class Struct(
|
||||||
self,
|
self,
|
||||||
update: dict | None = None,
|
update: dict | None = None,
|
||||||
|
|
||||||
) -> msgspec.Struct:
|
) -> Struct:
|
||||||
'''
|
'''
|
||||||
Validate-typecast all self defined fields, return a copy of
|
Validate-typecast all self defined fields, return a copy of
|
||||||
us with all such fields.
|
us with all such fields.
|
||||||
|
@ -66,15 +74,15 @@ class Struct(
|
||||||
# NOTE: roundtrip serialize to validate
|
# NOTE: roundtrip serialize to validate
|
||||||
# - enode to msgpack binary format,
|
# - enode to msgpack binary format,
|
||||||
# - decode that back to a struct.
|
# - decode that back to a struct.
|
||||||
return msgspec.msgpack.Decoder(
|
return msgpack.Decoder(type=type(self)).decode(
|
||||||
type=type(self)
|
msgpack.Encoder().encode(self)
|
||||||
).decode(
|
|
||||||
msgspec.msgpack.Encoder().encode(self)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def typecast(
|
def typecast(
|
||||||
self,
|
self,
|
||||||
# fields: list[str] | None = None,
|
|
||||||
|
# TODO: allow only casting a named subset?
|
||||||
|
# fields: set[str] | None = None,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
'''
|
'''
|
||||||
|
@ -85,15 +93,11 @@ class Struct(
|
||||||
``.copy()`` above in such cases.
|
``.copy()`` above in such cases.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
annots: dict = self.__annotations__
|
# https://jcristharif.com/msgspec/api.html#msgspec.structs.fields
|
||||||
for fname, ftype in annots.items():
|
fi: structs.FieldInfo
|
||||||
if isinstance(ftype, str):
|
for fi in structs.fields(self):
|
||||||
print(f'{self} has `str` annotations!?\n{annots}\n')
|
|
||||||
ftype = getattr(builtins, ftype)
|
|
||||||
|
|
||||||
attr = getattr(self, fname)
|
|
||||||
setattr(
|
setattr(
|
||||||
self,
|
self,
|
||||||
fname,
|
fi.name,
|
||||||
ftype(attr),
|
fi.type(getattr(self, fi.name)),
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue