-
Notifications
You must be signed in to change notification settings - Fork 217
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
env: add a 'translate_key' field to ease dealing with kebab-case #380
Conversation
This allows usage of `kebab-case` attribute in serde, allowing to mapping unambiguously into a config value given a multiple character separator. let environment = Environment::default() .prefix("PREFIX") .translate_key(TranslationType::Kebab) .separator("__");
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 really like that you added tests as well!
Please have a look at my annotations. I will approve CI right now though, so we can catch things early.
/// An environment source collects a dictionary of environment variables values into a hierarchical | ||
/// config Value type. We have to be aware how the config tree is created from the environment | ||
/// dictionary, therefore we are mindful about prefixes for the environment keys, level separators, | ||
/// encoding form (kebab, snake case) etc. |
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.
Could you please split this to a seperate commit?
@@ -77,6 +86,11 @@ pub struct Environment { | |||
source: Option<Map<String, String>>, | |||
} | |||
|
|||
#[derive(Clone, Debug)] | |||
pub enum TranslationType { | |||
Kebab, // Translate '_' to '-' |
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.
Would it make sense to add more here?
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.
We can go even further, depending on convert_case
crate and pass a convert_case::Case
value here instead (eliminating TranslationType
). If doing this, should we add a feature flag with optional dependency?
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.
That sounds good, IMO. Yes, a feature flag would be nice!
if let Some(translate_key) = translate_key { | ||
match translate_key { | ||
TranslationType::Kebab => { | ||
key = key.replace("_", "-"); | ||
}, | ||
} | ||
} |
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.
if let Some(translate_key) = translate_key { | |
match translate_key { | |
TranslationType::Kebab => { | |
key = key.replace("_", "-"); | |
}, | |
} | |
} | |
if let Some(TranslationType::Kebab) = translate_key { | |
key = key.replace("_", "-"); | |
} |
But if we add more types then this is not possible of course. 😆
@matthiasbeyer, all requested done in another PR to fix branch name mixup. Here: #381 |
This allows usage of
kebab-case
attribute in serde, allowing to mapping unambiguously into a config value given a multiple character separator.