Skip to content
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

Multiple aliases for field #176

Open
CralixRaev opened this issue Nov 4, 2023 · 6 comments
Open

Multiple aliases for field #176

CralixRaev opened this issue Nov 4, 2023 · 6 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@CralixRaev
Copy link

Is your feature request related to a problem? Please describe.
Sometimes you need to add multiple aliases for one field (e.g. one endpoint in poorly-designed api returns id with name "id", and another one returns it as "product_id")

Describe the solution you'd like
Add an feature to deserialize one field by multiple different aliases

Describe alternatives you've considered
Now i just made two different fields in my dataclass, and it's very confusing

@Fatal1ty
Copy link
Owner

Hi @CralixRaev

This would be a useful feature. I think reusing existing alias field option and aliases config option is a good way to go.

The field option would accept Union[str, Sequence[str]] instead of current str. The config option would accept Dict[str, Union[str, Sequence[str]]] instead of current Dict[str, str]

Would you like to work on it? It will be pretty straightforward after upcoming merge of the related PR:

@Fatal1ty Fatal1ty added the enhancement New feature or request label Nov 14, 2023
@Fatal1ty Fatal1ty added the good first issue Good for newcomers label Nov 27, 2023
@real-or-random
Copy link

real-or-random commented Mar 13, 2024

A related idea is to have the alias(es) depend on the dialect. Depending on the scenario, this may be a somewhat cleaner solution to the above example problem, e.g., if you want to deserialize in of the two ways. It would also help in scenarios where the key used may depend on natural language, e.g., in a scenario where a config file may be given in multiple languages (e.g., it has a key "date" and a German alias "Datum").

@Fatal1ty Do you think this would be a reasonable feature to have?

edit: A less elegant thing would be to handle such things via __post_deserialize__ or __pre_deserialize__ hooks, but unfortunately, they don't take a context. (Is there a specific reason to have the context only for serialization?)

@Fatal1ty
Copy link
Owner

@real-or-random In short, I like your idea.

I also thought about configuring aliases in dialects but in a more complex scenario. Right now, it's not possible to use the aliases config option for nested structures, which is why I haven't mirrored it in dialects yet. If there is only one dialect, it would be useful to have aliases for all the fields including those nested in dataclasses or other structures. I imagine something like foo.bar[*].date for a key date in a dictionary-like structure (dataclasses, TypedDicts) within a sequence bar of a dictionary-like structure in the key foo. Or it could be [*].date for a key date of any dictionary-like structure within a sequence when the dialect is being used for codecs that work not only with top level dataclasses.

If you'd like to contribute, we could start with the simple scenario and add support for a path syntax later.

@Fatal1ty
Copy link
Owner

Fatal1ty commented Mar 13, 2024

edit: A less elegant thing would be to handle such things via __post_deserialize__ or __pre_deserialize__ hooks, but unfortunately, they don't take a context. (Is there a specific reason to have the context only for serialization?)

Actually, no specific reason here. It was requested just for serialization.

@real-or-random
Copy link

If you'd like to contribute, we could start with the simple scenario and add support for a path syntax later.

Thanks for the swift reply! I might need this in the future, but it's further down the road. I'm still in the process of figuring out whether this lib is the right for me, see my other issue. ;)

@Fatal1ty
Copy link
Owner

Thanks for the swift reply! I might need this in the future, but it's further down the road. I'm still in the process of figuring out whether this lib is the right for me, see my other issue. ;)

Thank you for your feedback and illustrative examples with German words. This will help me to prioritize what needs to be done first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants