diff --git a/CHANGELOG.md b/CHANGELOG.md index dc84283..9f88cae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ * Fix issue opening project in Xamarin Studio - [#49](https://github.com/ResourceDataInc/Centroid/pull/49) * Fix issue with lowercase `all` environment not being picked up - [#54](https://github.com/ResourceDataInc/Centroid/pull/54) +* Fix issue with enumerating over config values causing values to become non-Centroidy - [#60](https://github.com/ResourceDataInc/Centroid/pull/60) ## Python diff --git a/dot-net/Centroid.Tests/ConfigTest.cs b/dot-net/Centroid.Tests/ConfigTest.cs index b7c2c56..2d13cf5 100644 --- a/dot-net/Centroid.Tests/ConfigTest.cs +++ b/dot-net/Centroid.Tests/ConfigTest.cs @@ -9,9 +9,9 @@ namespace Centroid.Tests [TestFixture] public class ConfigTest { - const string JsonConfig = @"{""Environment"": {""TheKey"": ""TheValue""}}"; + const string JsonConfig = @"{""theEnvironment"": {""theKey"": ""TheValue""}}"; - const string JsonConfigWithArray = @"{""Array"": [{""Key"": ""Value1""}, {""Key"": ""Value2""}]}"; + const string JsonConfigWithArray = @"{""theArray"": [{""theKey"": ""Value1""}, {""theKey"": ""Value2""}]}"; readonly string sharedFilePath; @@ -24,7 +24,7 @@ public ConfigTest() public void test_create_from_string() { dynamic config = new Config(JsonConfig); - Assert.That(config.Environment.TheKey, Is.EqualTo("TheValue")); + Assert.That(config.TheEnvironment.TheKey, Is.EqualTo("TheValue")); } [Test] @@ -56,14 +56,14 @@ public void test_raises_if_duplicate_normalized_keys_exist() public void test_readable_using_snake_case_property() { dynamic config = new Config(JsonConfig); - Assert.That(config.environment.the_key, Is.EqualTo("TheValue")); + Assert.That(config.the_environment.the_key, Is.EqualTo("TheValue")); } [Test] public void test_environment_specific_config_is_included() { var config = new Config(JsonConfig); - dynamic environmentConfig = config.ForEnvironment("Environment"); + dynamic environmentConfig = config.ForEnvironment("theEnvironment"); Assert.That(environmentConfig.TheKey, Is.EqualTo("TheValue")); } @@ -99,8 +99,8 @@ public void test_iterating_raw_config() public void test_modifying_raw_config() { dynamic config = new Config(JsonConfig); - config.RawConfig["Environment"]["TheKey"] = "NotTheValue"; - Assert.That(config.Environment.TheKey, Is.EqualTo("NotTheValue")); + config.RawConfig["theEnvironment"]["theKey"] = "NotTheValue"; + Assert.That(config.TheEnvironment.TheKey, Is.EqualTo("NotTheValue")); } [Test] @@ -115,8 +115,8 @@ public void test_environment_specific_config_overrides_all() public void test_indexing_json_array() { dynamic config = new Config(JsonConfigWithArray); - Assert.That(config.Array[0].Key, Is.EqualTo("Value1")); - Assert.That(config.Array[1].Key, Is.EqualTo("Value2")); + Assert.That(config.TheArray[0].TheKey, Is.EqualTo("Value1")); + Assert.That(config.TheArray[1].TheKey, Is.EqualTo("Value2")); } [Test] @@ -124,11 +124,11 @@ public void test_enumerating_json_array() { dynamic config = new Config(JsonConfigWithArray); var itemCount = 0; - foreach (var item in config.Array) + foreach (var item in config.TheArray) { + Assert.That(item.TheKey, Is.EqualTo(config.TheArray[itemCount].TheKey)); itemCount++; } - Assert.That(itemCount, Is.EqualTo(2)); } [Test] @@ -143,6 +143,29 @@ public void test_enumerating_json_object() Assert.That(itemCount, Is.EqualTo(1)); } + [Test] + public void test_enumerated_json_object_values_are_still_shiny() + { + const string json = @" + { + ""connections"": { + ""firstConnection"": { + ""user"": ""firstUser"", + ""password"":""secret"" + }, + ""secondConnection"": { + ""user"": ""secondUser"", + ""password"":""secret"" + }, + } + }"; + dynamic config = new Config(json); + foreach (var kvp in config.Connections) + { + Assert.That(kvp.Value.Password, Is.EqualTo("secret")); + } + } + [Test] public void test_all_environment_is_not_case_sensitive() { @@ -181,7 +204,7 @@ public void supports_deep_merge() public void test_contains_key() { dynamic config = new Config(JsonConfig); - Assert.That(config.ContainsKey("Environment"), Is.True); + Assert.That(config.ContainsKey("theEnvironment"), Is.True); Assert.That(config.ContainsKey("DoesNotExist"), Is.False); } } diff --git a/dot-net/Centroid/Config.cs b/dot-net/Centroid/Config.cs index 689c666..1054f97 100644 --- a/dot-net/Centroid/Config.cs +++ b/dot-net/Centroid/Config.cs @@ -93,7 +93,21 @@ public override string ToString() public IEnumerator GetEnumerator() { - return RawConfig.GetEnumerator(); + if (RawConfig is JArray) + { + foreach (var element in RawConfig) + { + yield return GetValueFromContainer(element); + } + } + else + { + foreach (var name in GetDynamicMemberNames()) + { + dynamic value = GetValue(name); + yield return new KeyValuePair(name, value); + } + } } public override IEnumerable GetDynamicMemberNames() @@ -178,4 +192,4 @@ static void MergeInto(JContainer left, JToken right) } } } -} \ No newline at end of file +}