forked from goodboy/tractor
1
0
Fork 0

Add `from_dict_msg(user_pretty: bool)` flag

Allows for optionally (and dynamically) constructing the "expected"
`MsgType` from a `dict` into a `pretty_struct.Struct`, mostly for
logging usage.
runtime_to_msgspec
Tyler Goodlet 2024-04-14 16:29:21 -04:00
parent df548257ad
commit 2d22713806
1 changed files with 15 additions and 1 deletions

View File

@ -451,7 +451,8 @@ def from_dict_msg(
dict_msg: dict,
msgT: MsgType|None = None,
tag_field: str = 'msg_type'
tag_field: str = 'msg_type',
use_pretty: bool = False,
) -> MsgType:
'''
@ -468,6 +469,19 @@ def from_dict_msg(
# XXX ensure tag field is removed
msgT_name: str = dict_msg.pop(msg_type_tag_field)
msgT: MsgType = _msg_table[msgT_name]
if use_pretty:
msgT = defstruct(
name=msgT_name,
fields=[
(key, fi.type)
for fi, key, _
in pretty_struct.iter_fields(msgT)
],
bases=(
pretty_struct.Struct,
msgT,
),
)
return msgT(**dict_msg)
# TODO: should be make a msg version of `ContextCancelled?`