diff options
author | Dan Aloni <dan@kernelim.com> | 2022-09-28 18:33:46 +0300 |
---|---|---|
committer | Dan Aloni <dan@kernelim.com> | 2022-09-29 16:07:35 +0300 |
commit | f8e577f299370ed4b9a0b5a352f42eb899f86c01 (patch) | |
tree | 1a50b5cf37063991cf9e2a860231f9c1b022e24a /tests/env.rs | |
parent | e3167a16910fdf65aa903e7689e84011de6563e3 (diff) |
env: add a 'convert_case' field to ease dealing with kebab-case
This allows usage of `kebab-case` attribute in serde, mapping
unambiguously into a config value given a multiple character separator.
This also add the `convert-case` feature.
For example:
let environment = Environment::default()
.prefix("PREFIX")
.translate_key(Case::Kebab)
.separator("__");
Diffstat (limited to 'tests/env.rs')
-rw-r--r-- | tests/env.rs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/tests/env.rs b/tests/env.rs index 3a24bde..a144d08 100644 --- a/tests/env.rs +++ b/tests/env.rs @@ -464,6 +464,61 @@ fn test_parse_string_and_list() { } #[test] +fn test_parse_nested_kebab() { + use config::Case; + + #[derive(Deserialize, Debug)] + #[serde(rename_all = "kebab-case")] + struct TestConfig { + single: String, + plain: SimpleInner, + value_with_multipart_name: String, + inner_config: ComplexInner, + } + + #[derive(Deserialize, Debug)] + #[serde(rename_all = "kebab-case")] + struct SimpleInner { + val: String, + } + + #[derive(Deserialize, Debug)] + #[serde(rename_all = "kebab-case")] + struct ComplexInner { + another_multipart_name: String, + } + + temp_env::with_vars( + vec![ + ("PREFIX__SINGLE", Some("test")), + ("PREFIX__PLAIN__VAL", Some("simple")), + ("PREFIX__VALUE_WITH_MULTIPART_NAME", Some("value1")), + ( + "PREFIX__INNER_CONFIG__ANOTHER_MULTIPART_NAME", + Some("value2"), + ), + ], + || { + let environment = Environment::default() + .prefix("PREFIX") + .convert_case(Case::Kebab) + .separator("__"); + + let config = Config::builder().add_source(environment).build().unwrap(); + + println!("{:#?}", config); + + let config: TestConfig = config.try_deserialize().unwrap(); + + assert_eq!(config.single, "test"); + assert_eq!(config.plain.val, "simple"); + assert_eq!(config.value_with_multipart_name, "value1"); + assert_eq!(config.inner_config.another_multipart_name, "value2"); + }, + ) +} + +#[test] fn test_parse_string() { // using a struct in an enum here to make serde use `deserialize_any` #[derive(Deserialize, Debug)] |