-
-
Couldn't load subscription status.
- Fork 862
serde_derive: Add alias_all attribute to containers #2946
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
base: master
Are you sure you want to change the base?
Conversation
| #[serde(alias_all = "kebab-case")] | ||
| enum TestEnum { | ||
| VariantOne { field_one: i32 }, | ||
| VariantTwo { field_two: String }, | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this should be applied to variant names instead of field names inside variants? That is, convert to variant-one and variant-two
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your'e right, i made a typo in the test.
It actually works as you described, I pushed a fix to the tests.
This adds a new attribute `#[serde(alias_all = "...")]` to containers, which accepts the same rename rules as `#[serde(rename_all = "...")]`. When applied to a container, the alias will be added to all fields (or variants) similarly to how `#[serde(alias = "...")]` works on a single field/variant. This attribute can be specified multiple times, with the use-case being accepting multiple case styles during de-serialization. Resolves serde-rs#1530 Signed-off-by: Michel Heily <michelheily@gmail.com>
|
Just now realized that there was already an attempt at this in #1972 which was not accepted, missed that when I skimmed through the open issue. I hope @dtolnay will have a change of heart here 🙏 this feature is really desired by many applications that use serde for loading configuration files. I'm for once am tired of getting support tickets where the root cause is that the user used the wrong case style in their config files 😅 Our workaround is very not optimal - we first parse to |
FYI, |
| name: "VariantOne", | ||
| len: 1, | ||
| }, | ||
| Token::Str("field-one"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would expect, that alias_all for enums will influence only variant names. If you need to apply the same rules to struct variants, you should add attribute to struct variant. If I'm not wrong, it is how rename_all attribute work.
Resolves #1530
This adds a new attribute
#[serde(alias_all = "...")]to containers, which accepts the same rename rules as#[serde(rename_all = "...")]. When applied to a container, the alias will be added to all fields (or variants) similarly to how#[serde(alias = "...")]works on a single field/variant.This attribute can be specified multiple times, with the use-case being accepting multiple case styles during de-serialization.
BTW this is my first contribution in this project, so I would appreciate if you take a closer look, and of course help with updating serde.rs documentation if you decide to accept this PR 😄