Skip to content

Commit 2ebe44a

Browse files
Merge pull request #429 from bigduu/master
Make the parse list key to lowercase when insert the keys
2 parents 35f475c + b3d8518 commit 2ebe44a

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/env.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,10 @@ impl Environment {
153153
/// To switch the default type back to type Strings you need to provide the keys which should be [`Vec<String>`] using this function.
154154
pub fn with_list_parse_key(mut self, key: &str) -> Self {
155155
if self.list_parse_keys.is_none() {
156-
self.list_parse_keys = Some(vec![key.into()])
156+
self.list_parse_keys = Some(vec![key.to_lowercase()])
157157
} else {
158158
self.list_parse_keys = self.list_parse_keys.map(|mut keys| {
159-
keys.push(key.into());
159+
keys.push(key.to_lowercase());
160160
keys
161161
});
162162
}
@@ -287,6 +287,9 @@ impl Source for Environment {
287287
ValueKind::Float(parsed)
288288
} else if let Some(separator) = &self.list_separator {
289289
if let Some(keys) = &self.list_parse_keys {
290+
#[cfg(feature = "convert-case")]
291+
let key = key.to_lowercase();
292+
290293
if keys.contains(&key) {
291294
let v: Vec<Value> = value
292295
.split(separator)

tests/env.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,58 @@ fn test_parse_string_and_list() {
463463
)
464464
}
465465

466+
#[test]
467+
fn test_parse_string_and_list_ignore_list_parse_key_case() {
468+
// using a struct in an enum here to make serde use `deserialize_any`
469+
#[derive(Deserialize, Debug)]
470+
#[serde(tag = "tag")]
471+
enum TestStringEnum {
472+
String(TestString),
473+
}
474+
475+
#[derive(Deserialize, Debug)]
476+
struct TestString {
477+
string_val: String,
478+
string_list: Vec<String>,
479+
}
480+
481+
temp_env::with_vars(
482+
vec![
483+
("LIST_STRING_LIST", Some("test,string")),
484+
("LIST_STRING_VAL", Some("test,string")),
485+
],
486+
|| {
487+
let environment = Environment::default()
488+
.prefix("LIST")
489+
.list_separator(",")
490+
.with_list_parse_key("STRING_LIST")
491+
.try_parsing(true);
492+
493+
let config = Config::builder()
494+
.set_default("tag", "String")
495+
.unwrap()
496+
.add_source(environment)
497+
.build()
498+
.unwrap();
499+
500+
let config: TestStringEnum = config.try_deserialize().unwrap();
501+
502+
match config {
503+
TestStringEnum::String(TestString {
504+
string_val,
505+
string_list,
506+
}) => {
507+
assert_eq!(String::from("test,string"), string_val);
508+
assert_eq!(
509+
vec![String::from("test"), String::from("string")],
510+
string_list
511+
);
512+
}
513+
}
514+
},
515+
)
516+
}
517+
466518
#[test]
467519
fn test_parse_nested_kebab() {
468520
use config::Case;

0 commit comments

Comments
 (0)