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

Add optional snake_case conversion of member names #56

Merged
merged 4 commits into from
Apr 21, 2022
Merged

Add optional snake_case conversion of member names #56

merged 4 commits into from
Apr 21, 2022

Conversation

clux
Copy link
Member

@clux clux commented Apr 21, 2022

Using heck as it's the most popular libary out there (plus it's maintained by boats).

It now avoids awkward behaviour of ToSnakeCase not being a true inverse of #[serde(rename_all = "camelCase")] by adding noisy #[serde(rename = "originalName")] on fields that apply. It will be noisy, but only on fields with LONG names.

I think it's generally OK. Perhaps even good enough for -z to be the default, but will wait a bit to see.

Produces code looking like:

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ServerPodSelector {
    #[serde(rename = "matchExpressions")]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub match_expressions: Option<Vec<ServerPodSelectorMatchExpressions>>,
    #[serde(rename = "matchLabels")]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub match_labels: Option<BTreeMap<String, serde_json::Value>>,
}

Using heck as it's the most popular libary out there (plus it's
maintained by boats).

This CAN lead to some subtle behaviour due to how serde's rename_all is
not a true inverse of ToSnakeCase, but I believe this can be rectified
for those cases with some extra override flags (awkward tho it may be).

fixes #22

Signed-off-by: clux <sszynrae@gmail.com>
Signed-off-by: clux <sszynrae@gmail.com>
- allow tests to override inner attribute being emitted due to a bug
- cleanup docs to make help more informative

Signed-off-by: clux <sszynrae@gmail.com>
Signed-off-by: clux <sszynrae@gmail.com>
@clux clux merged commit 2317396 into main Apr 21, 2022
@clux clux deleted the camelcase branch April 21, 2022 12:10
@clux clux linked an issue Apr 21, 2022 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

struct members should be snake_case
1 participant