From 5a7871d158409bf0fa66c7595f8bc0a294670e6e Mon Sep 17 00:00:00 2001 From: Greg Scott Date: Fri, 16 May 2014 14:41:48 -0800 Subject: [PATCH 1/4] [skip ci] Add missing c# tests and tweak JSON egs This introduces test failures for c# that still need to be resolved. --- dot-net/Centroid.Tests/ConfigTest.cs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/dot-net/Centroid.Tests/ConfigTest.cs b/dot-net/Centroid.Tests/ConfigTest.cs index b7c2c56..ad11a01 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) { itemCount++; + Assert.That(item.TheKey, Is.EqualTo(config.TheArray[itemCount -1].TheKey)); } - Assert.That(itemCount, Is.EqualTo(2)); } [Test] From 42c685df6a9f4223e7feb2081d7bd6fccea59312 Mon Sep 17 00:00:00 2001 From: Jason Jones Date: Mon, 30 Jun 2014 12:21:02 -0800 Subject: [PATCH 2/4] Improve C# IEnumerable implementation --- dot-net/Centroid.Tests/ConfigTest.cs | 26 ++++++++++++++++++++++++-- dot-net/Centroid/Config.cs | 18 ++++++++++++++++-- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/dot-net/Centroid.Tests/ConfigTest.cs b/dot-net/Centroid.Tests/ConfigTest.cs index ad11a01..a50b8b8 100644 --- a/dot-net/Centroid.Tests/ConfigTest.cs +++ b/dot-net/Centroid.Tests/ConfigTest.cs @@ -126,8 +126,8 @@ public void test_enumerating_json_array() var itemCount = 0; foreach (var item in config.TheArray) { + Assert.That(item.TheKey, Is.EqualTo(config.TheArray[itemCount].TheKey)); itemCount++; - Assert.That(item.TheKey, Is.EqualTo(config.TheArray[itemCount -1].TheKey)); } } @@ -143,6 +143,28 @@ 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 (dynamic connection in config.Connections) { + Assert.That(connection.Password, Is.EqualTo("secret")); + } + } + [Test] public void test_all_environment_is_not_case_sensitive() { @@ -181,7 +203,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..6adb5a2 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 value; + } + } } public override IEnumerable GetDynamicMemberNames() @@ -178,4 +192,4 @@ static void MergeInto(JContainer left, JToken right) } } } -} \ No newline at end of file +} From c11cb1fd3f4987c6b889c81bfef735a10a342365 Mon Sep 17 00:00:00 2001 From: Jason Jones Date: Mon, 30 Jun 2014 15:59:16 -0800 Subject: [PATCH 3/4] Update IEnumerable return type for JObject values Return a KeyValuePair so the key isn't lost. --- dot-net/Centroid.Tests/ConfigTest.cs | 5 +++-- dot-net/Centroid/Config.cs | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/dot-net/Centroid.Tests/ConfigTest.cs b/dot-net/Centroid.Tests/ConfigTest.cs index a50b8b8..2d13cf5 100644 --- a/dot-net/Centroid.Tests/ConfigTest.cs +++ b/dot-net/Centroid.Tests/ConfigTest.cs @@ -160,8 +160,9 @@ public void test_enumerated_json_object_values_are_still_shiny() } }"; dynamic config = new Config(json); - foreach (dynamic connection in config.Connections) { - Assert.That(connection.Password, Is.EqualTo("secret")); + foreach (var kvp in config.Connections) + { + Assert.That(kvp.Value.Password, Is.EqualTo("secret")); } } diff --git a/dot-net/Centroid/Config.cs b/dot-net/Centroid/Config.cs index 6adb5a2..1054f97 100644 --- a/dot-net/Centroid/Config.cs +++ b/dot-net/Centroid/Config.cs @@ -105,7 +105,7 @@ public IEnumerator GetEnumerator() foreach (var name in GetDynamicMemberNames()) { dynamic value = GetValue(name); - yield return value; + yield return new KeyValuePair(name, value); } } } From a529c2ac809c8dc2d7867899d3d817d543dce324 Mon Sep 17 00:00:00 2001 From: Jason Jones Date: Tue, 1 Jul 2014 08:53:22 -0800 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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