Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
47 changes: 35 additions & 12 deletions dot-net/Centroid.Tests/ConfigTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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]
Expand Down Expand Up @@ -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"));
}

Expand Down Expand Up @@ -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]
Expand All @@ -115,20 +115,20 @@ 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]
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]
Expand All @@ -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()
{
Expand Down Expand Up @@ -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);
}
}
Expand Down
18 changes: 16 additions & 2 deletions dot-net/Centroid/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string,dynamic>(name, value);
}
}
}

public override IEnumerable<string> GetDynamicMemberNames()
Expand Down Expand Up @@ -178,4 +192,4 @@ static void MergeInto(JContainer left, JToken right)
}
}
}
}
}