-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hot to get field alias name from Struct? #672
Comments
@Olegt0rr, I think from msgspec.inspect import type_info
# to access "user_id" field's metadata
field_meta = type_info(HelpIssue).fields[0]
assert field_meta.name == "user_id"
assert field_meta.encode_name == "userId" References: https://jcristharif.com/msgspec/inspect.html |
@uwinx, thanks! I've also found this way: for name, encode_name in zip(
struct.__struct_fields__,
struct.__struct_encode_fields__,
):
... |
In [3]: class Example(msgspec.Struct):
...: user_id: int = msgspec.field(name="userId")
...:
In [4]: msgspec.structs.fields(Example)
Out[4]: (FieldInfo(name='user_id', encode_name='userId', type=<class 'int'>, default=NODEFAULT, default_factory=NODEFAULT),) |
Question
name
from fielduser_id
The text was updated successfully, but these errors were encountered: