From 01583791f785e2883dc19b92c8c97b5fd31af0b6 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Tue, 13 Jun 2017 19:02:33 -0700 Subject: Ensure config keys are case insensitive --- src/config.rs | 4 ++-- src/file/format/json.rs | 2 +- src/file/format/toml.rs | 2 +- src/file/format/yaml.rs | 9 +++++++-- tests/set.rs | 16 ++++++++++++++++ 5 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/config.rs b/src/config.rs index 0d84906..c355f2d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -121,7 +121,7 @@ impl Config { ref mut defaults, .. } => { - defaults.insert(key.parse()?, value.into()); + defaults.insert(key.to_lowercase().parse()?, value.into()); } ConfigKind::Frozen => { @@ -140,7 +140,7 @@ impl Config { ref mut overrides, .. } => { - overrides.insert(key.parse()?, value.into()); + overrides.insert(key.to_lowercase().parse()?, value.into()); } ConfigKind::Frozen => { diff --git a/src/file/format/json.rs b/src/file/format/json.rs index a3d80a7..4a94943 100644 --- a/src/file/format/json.rs +++ b/src/file/format/json.rs @@ -54,7 +54,7 @@ fn from_json_value(uri: Option<&String>, value: &serde_json::Value) -> Value { let mut m = HashMap::new(); for (key, value) in table { - m.insert(key.clone(), from_json_value(uri, value)); + m.insert(key.to_lowercase().clone(), from_json_value(uri, value)); } Value::new(uri, ValueKind::Table(m)) diff --git a/src/file/format/toml.rs b/src/file/format/toml.rs index f4a4196..633f39e 100644 --- a/src/file/format/toml.rs +++ b/src/file/format/toml.rs @@ -45,7 +45,7 @@ fn from_toml_value(uri: Option<&String>, value: &toml::Value) -> Value { let mut m = HashMap::new(); for (key, value) in table { - m.insert(key.clone(), from_toml_value(uri, value)); + m.insert(key.to_lowercase().clone(), from_toml_value(uri, value)); } Value::new(uri, m) diff --git a/src/file/format/yaml.rs b/src/file/format/yaml.rs index 2840438..7c3dd71 100644 --- a/src/file/format/yaml.rs +++ b/src/file/format/yaml.rs @@ -54,7 +54,7 @@ fn from_yaml_value(uri: Option<&String>, value: &yaml::Yaml) -> Value { let mut m = HashMap::new(); for (key, value) in table { if let Some(k) = key.as_str() { - m.insert(k.to_owned(), from_yaml_value(uri, value)); + m.insert(k.to_lowercase().to_owned(), from_yaml_value(uri, value)); } // TODO: should we do anything for non-string keys? } @@ -69,7 +69,12 @@ fn from_yaml_value(uri: Option<&String>, value: &yaml::Yaml) -> Value { Value::new(uri, ValueKind::Array(l)) } - // TODO: how should we handle Null and BadValue? + + yaml::Yaml::Null => { + Value::new(uri, ValueKind::Nil) + } + + // TODO: how should we BadValue? _ => { unimplemented!(); } diff --git a/tests/set.rs b/tests/set.rs index a575138..e02814b 100644 --- a/tests/set.rs +++ b/tests/set.rs @@ -38,3 +38,19 @@ fn test_set_scalar_path() { assert_eq!(c.get("place.favorite").ok(), Some(false)); assert_eq!(c.get("place.blocked").ok(), Some(true)); } + +#[test] +fn test_set_capital() { + let mut c = Config::default(); + + c.set_default("tHiS", false).unwrap(); + c.set("THAT", true).unwrap(); + c.merge(File::from_str("{\"loGleVel\": 5}", FileFormat::Json)).unwrap(); + + assert_eq!(c.get("this").ok(), Some(false)); + assert_eq!(c.get("ThIs").ok(), Some(false)); + assert_eq!(c.get("that").ok(), Some(true)); + assert_eq!(c.get("THAT").ok(), Some(true)); + assert_eq!(c.get("logLevel").ok(), Some(5)); + assert_eq!(c.get("loglevel").ok(), Some(5)); +} -- cgit v1.2.3