data.types.Struct.to_dict(): discard non-member struct by default

account_tests
Tyler Goodlet 2023-07-12 12:33:30 -04:00
parent c30d8ac9ba
commit 7f4884a6d9
1 changed files with 19 additions and 3 deletions

View File

@ -72,15 +72,31 @@ class Struct(
A "human friendlier" (aka repl buddy) struct subtype. A "human friendlier" (aka repl buddy) struct subtype.
''' '''
def to_dict(self) -> dict: def to_dict(
self,
include_non_members: bool = False,
) -> dict:
''' '''
Like it sounds.. direct delegation to: Like it sounds.. direct delegation to:
https://jcristharif.com/msgspec/api.html#msgspec.structs.asdict https://jcristharif.com/msgspec/api.html#msgspec.structs.asdict
TODO: probably just drop this method since it's now a built-int method? BUT, by default we pop all non-member (aka not defined as
struct fields) fields by default.
''' '''
return structs.asdict(self) asdict: dict = structs.asdict(self)
if include_non_members:
return asdict
# only return a dict of the struct members
# which were provided as input, NOT anything
# added as `@properties`!
sin_props: dict = {}
for fi in structs.fields(self):
key: str = fi.name
sin_props[key] = asdict[key]
return sin_props
def pformat(self) -> str: def pformat(self) -> str:
return f'Struct({pformat(self.to_dict())})' return f'Struct({pformat(self.to_dict())})'