From b3d851878dd88a62c19cecf3e3bf8a8e2be5309a Mon Sep 17 00:00:00 2001 From: bigduu Date: Sat, 25 Mar 2023 23:24:02 +0800 Subject: [PATCH] Make the parse list key to lowercase when insert the keys Signed-off-by: bigduu --- src/env.rs | 7 +++++-- tests/env.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/env.rs b/src/env.rs index 8ede33df..78233839 100644 --- a/src/env.rs +++ b/src/env.rs @@ -153,10 +153,10 @@ impl Environment { /// To switch the default type back to type Strings you need to provide the keys which should be [`Vec`] using this function. pub fn with_list_parse_key(mut self, key: &str) -> Self { if self.list_parse_keys.is_none() { - self.list_parse_keys = Some(vec![key.into()]) + self.list_parse_keys = Some(vec![key.to_lowercase()]) } else { self.list_parse_keys = self.list_parse_keys.map(|mut keys| { - keys.push(key.into()); + keys.push(key.to_lowercase()); keys }); } @@ -287,6 +287,9 @@ impl Source for Environment { ValueKind::Float(parsed) } else if let Some(separator) = &self.list_separator { if let Some(keys) = &self.list_parse_keys { + #[cfg(feature = "convert-case")] + let key = key.to_lowercase(); + if keys.contains(&key) { let v: Vec = value .split(separator) diff --git a/tests/env.rs b/tests/env.rs index a144d080..12a68a92 100644 --- a/tests/env.rs +++ b/tests/env.rs @@ -463,6 +463,58 @@ fn test_parse_string_and_list() { ) } +#[test] +fn test_parse_string_and_list_ignore_list_parse_key_case() { + // using a struct in an enum here to make serde use `deserialize_any` + #[derive(Deserialize, Debug)] + #[serde(tag = "tag")] + enum TestStringEnum { + String(TestString), + } + + #[derive(Deserialize, Debug)] + struct TestString { + string_val: String, + string_list: Vec, + } + + temp_env::with_vars( + vec![ + ("LIST_STRING_LIST", Some("test,string")), + ("LIST_STRING_VAL", Some("test,string")), + ], + || { + let environment = Environment::default() + .prefix("LIST") + .list_separator(",") + .with_list_parse_key("STRING_LIST") + .try_parsing(true); + + let config = Config::builder() + .set_default("tag", "String") + .unwrap() + .add_source(environment) + .build() + .unwrap(); + + let config: TestStringEnum = config.try_deserialize().unwrap(); + + match config { + TestStringEnum::String(TestString { + string_val, + string_list, + }) => { + assert_eq!(String::from("test,string"), string_val); + assert_eq!( + vec![String::from("test"), String::from("string")], + string_list + ); + } + } + }, + ) +} + #[test] fn test_parse_nested_kebab() { use config::Case;