Skip to content

Commit c100c6c

Browse files
authored
Merge pull request #12 from kevbite/sourcekeyformatters
Key formatters can now be used on single sources.
2 parents 3d7e1b9 + 2931123 commit c100c6c

16 files changed

+321
-7
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ var value = await configuration.GetAppSettingAysnc<string>("key");
6262
Ever been in config hell where you don't know what key is used where.
6363
This is where key formatters comes in useful, HumbleConfig has inbuilt support for a few key formatters.
6464

65-
#### KeyPrefixer
65+
##### KeyPrefixer
6666
The key prefixer allows you to specify a prefix that all your config keys should include.
6767
For example having a prefix of `HumbleConfig:` would have the following output:
6868

@@ -77,6 +77,29 @@ To setup this the key prefixer on our configuration object we just call `WithKey
7777
configuration.WithKeyPrefixer("HumbleConfig:")
7878
```
7979

80+
##### KeyPostfixer
81+
The key postfixer allows you to specify a postfix that all your config keys should include.
82+
For example having a postfix of `.HumbleConfig` would have the following output:
83+
84+
| Key | Source Key |
85+
| -------|------------------ |
86+
| Key1 | Key1.HumbleConfig |
87+
| Key2 | Key2.HumbleConfig |
88+
| Key3 | Key3.HumbleConfig |
89+
90+
To setup this the key postfixer on our configuration object we just call `WithKeyPostfixer`:
91+
```csharp
92+
configuration.WithKeyPostfixer(".HumbleConfig")
93+
```
94+
95+
#### Key formatters on sources
96+
97+
It is also possible to use key formatter on individual sources, for example:
98+
```
99+
configuration.AddEnvironmentVariables().WithKeyPostfixer(".production")
100+
```
101+
This will only apply the key formatter to the environment variables source.
102+
80103
### Contributing
81104

82105
1. Fork

appveyor.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
only:
44
- master
55
skip_tags: true
6-
version: 4.0.1
6+
version: 4.1.0
77

88
assembly_info:
99
assembly_version: '{version}'
@@ -59,7 +59,7 @@
5959

6060
-
6161
skip_tags: true
62-
version: 4.0.1-{branch}{build}
62+
version: 4.1.0-{branch}{build}
6363

6464
assembly_info:
6565
assembly_version: '{version}'
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using HumbleConfig.InMemory;
4+
using HumbleConfig.KeyFormatters;
5+
using NUnit.Framework;
6+
7+
namespace HumbleConfig.FunctionalTests
8+
{
9+
[TestFixture]
10+
public class ConfigurationTestsWithKeyPostfixer
11+
{
12+
private IConfiguration _configuration;
13+
14+
private string _key1Actual;
15+
16+
[OneTimeSetUp]
17+
public void GivenConfigurationWithKeyPostfixer()
18+
{
19+
_configuration = new Configuration()
20+
.WithKeyPostfixer(".production")
21+
.AddInMemory(new Dictionary<string, object>() { { "Key1.production", "InMemory" } })
22+
.GetConfiguration();
23+
}
24+
25+
[SetUp]
26+
public async Task WhenGettingAppSettings()
27+
{
28+
_key1Actual = await _configuration.GetAppSettingAsync<string>("Key1");
29+
}
30+
31+
[Test]
32+
public void ThenKey1PullsValueFromEnvironmentVariable()
33+
{
34+
Assert.That(_key1Actual, Is.EqualTo("InMemory"));
35+
}
36+
}
37+
}

src/HumbleConfig.FunctionalTests/ConfigurationTestsWithKeyPrefixer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Threading.Tasks;
23
using HumbleConfig.InMemory;
34
using HumbleConfig.KeyFormatters;
45
using NUnit.Framework;
@@ -22,9 +23,9 @@ public void GivenConfigurationWithKeyPrefixer()
2223
}
2324

2425
[SetUp]
25-
public void WhenGettingAppSettings()
26+
public async Task WhenGettingAppSettings()
2627
{
27-
_key1Actual = _configuration.GetAppSettingAsync<string>("Key1").Result;
28+
_key1Actual = await _configuration.GetAppSettingAsync<string>("Key1");
2829
}
2930

3031
[Test]

src/HumbleConfig.FunctionalTests/HumbleConfig.FunctionalTests.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
<Reference Include="AWSSDK.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604, processorArchitecture=MSIL">
3535
<HintPath>..\packages\AWSSDK.Core.3.3.17.9\lib\net45\AWSSDK.Core.dll</HintPath>
3636
</Reference>
37+
<Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
38+
<HintPath>..\packages\Castle.Core.4.2.1\lib\net45\Castle.Core.dll</HintPath>
39+
</Reference>
3740
<Reference Include="Common.Logging, Version=3.3.1.0, Culture=neutral, PublicKeyToken=af08829b84f0328e, processorArchitecture=MSIL">
3841
<HintPath>..\packages\Common.Logging.3.3.1\lib\net40\Common.Logging.dll</HintPath>
3942
<Private>True</Private>
@@ -86,6 +89,9 @@
8689
<HintPath>..\packages\MongoDB.Driver.Core.2.2.4\lib\net45\MongoDB.Driver.Core.dll</HintPath>
8790
<Private>True</Private>
8891
</Reference>
92+
<Reference Include="Moq, Version=4.7.145.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
93+
<HintPath>..\packages\Moq.4.7.145\lib\net45\Moq.dll</HintPath>
94+
</Reference>
8995
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
9096
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
9197
<Private>True</Private>
@@ -123,6 +129,7 @@
123129
<Private>True</Private>
124130
</Reference>
125131
<Reference Include="System.ComponentModel.Composition" />
132+
<Reference Include="System.Configuration" />
126133
<Reference Include="System.Core" />
127134
<Reference Include="System.Reflection.Metadata, Version=1.0.18.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
128135
<HintPath>..\packages\ScriptCs.Engine.Roslyn.0.16.1\lib\net45\System.Reflection.Metadata.dll</HintPath>
@@ -138,10 +145,12 @@
138145
</ItemGroup>
139146
<ItemGroup>
140147
<Compile Include="CacheConfigurationTests.cs" />
148+
<Compile Include="ConfigurationTestsWithKeyPostfixer.cs" />
141149
<Compile Include="ConfigurationTestsWithKeyPrefixer.cs" />
142150
<Compile Include="ConfigurationTests.cs" />
143151
<Compile Include="CredstashConfigurationTests.cs" />
144152
<Compile Include="ExpiredCacheConfigurationTests.cs" />
153+
<Compile Include="KeyFormatterConfigurationTests.cs" />
145154
<Compile Include="MultiCacheConfigurationTests.cs" />
146155
<Compile Include="Properties\AssemblyInfo.cs" />
147156
</ItemGroup>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using HumbleConfig.Caching;
5+
using HumbleConfig.InMemory;
6+
using HumbleConfig.KeyFormatters;
7+
using HumbleConfig.MongoDb;
8+
using MongoDB.Driver;
9+
using Moq;
10+
using NUnit.Framework;
11+
12+
namespace HumbleConfig.FunctionalTests
13+
{
14+
[TestFixture]
15+
class KeyFormatterConfigurationTests
16+
{
17+
private IConfiguration _configuration;
18+
19+
private string key1 = "key1";
20+
private string key2 = "key2";
21+
private string key3 = "key3";
22+
23+
private string _key1Actual;
24+
private string _key2Actual;
25+
private string _key3Actual;
26+
27+
[OneTimeSetUp]
28+
public void GivenConfigurationWithEnvironmentVaribleAndConfigurationManager()
29+
{
30+
var newKey3 = "key3.new";
31+
var keyFormatter = new Mock<IKeyFormatter>();
32+
keyFormatter.Setup(x => x.FormatKey(key3))
33+
.Returns(newKey3);
34+
35+
_configuration = new Configuration()
36+
.AddInMemory(new Dictionary<string, object>(){{"pre." + key1, "InMemory1"}}).WithKeyPrefixer("pre.")
37+
.AddInMemory(new Dictionary<string, object>(){{key2 + ".post", "InMemory2"}}).WithKeyPostfixer(".post")
38+
.AddInMemory(new Dictionary<string, object>(){{newKey3, "InMemory3"}}).WithKeyFormatter(keyFormatter.Object)
39+
.GetConfiguration();
40+
}
41+
42+
[SetUp]
43+
public async Task WhenGettingAppSettings()
44+
{
45+
_key1Actual = await _configuration.GetAppSettingAsync<string>(key1);
46+
_key2Actual = await _configuration.GetAppSettingAsync<string>(key2);
47+
_key3Actual = await _configuration.GetAppSettingAsync<string>(key3);
48+
}
49+
50+
[Test]
51+
public void ThenTheCorrectValueIsPullWithPrefix()
52+
{
53+
Assert.That(_key1Actual, Is.EqualTo("InMemory1"));
54+
}
55+
56+
[Test]
57+
public void ThenTheCorrectValueIsPullWithPostfix()
58+
{
59+
Assert.That(_key2Actual, Is.EqualTo("InMemory2"));
60+
}
61+
62+
[Test]
63+
public void ThenTheCorrectValueIsPullWithCustomFormatter()
64+
{
65+
Assert.That(_key3Actual, Is.EqualTo("InMemory3"));
66+
}
67+
}
68+
}

src/HumbleConfig.FunctionalTests/packages.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
<packages>
33
<package id="AutoFixture" version="3.49.0" targetFramework="net45" />
44
<package id="AWSSDK.Core" version="3.3.17.9" targetFramework="net462" />
5+
<package id="Castle.Core" version="4.2.1" targetFramework="net462" />
56
<package id="Common.Logging" version="3.3.1" targetFramework="net45" />
67
<package id="Common.Logging.Core" version="3.3.1" targetFramework="net45" />
78
<package id="ConfigR" version="0.14.1" targetFramework="net45" />
89
<package id="MongoDB.Bson" version="2.2.4" targetFramework="net45" />
910
<package id="MongoDB.Driver" version="2.2.4" targetFramework="net45" />
1011
<package id="MongoDB.Driver.Core" version="2.2.4" targetFramework="net45" />
12+
<package id="Moq" version="4.7.145" targetFramework="net462" />
1113
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />
1214
<package id="NUnit" version="3.9.0" targetFramework="net45" />
1315
<package id="Roslyn.Compilers.Common" version="1.2.20906.2" targetFramework="net45" />

src/HumbleConfig.Tests/ConfigurationTests/ConfigurationTestsForMultipleSourcesAndOnlyTheLastHasAMatchingKey.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using HumbleConfig.Tests.Stubs;
1+

2+
using HumbleConfig.Tests.Stubs;
23
using NUnit.Framework;
34
using Ploeh.AutoFixture;
45

src/HumbleConfig.Tests/HumbleConfig.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
<Reference Include="System.Xml" />
5656
</ItemGroup>
5757
<ItemGroup>
58+
<Compile Include="KeyFormatters\KeyFormatterConfigurationSourceDecoratorTests.cs" />
59+
<Compile Include="KeyFormatters\KeyPostfixerTests.cs" />
5860
<Compile Include="NonNullableTestFixtureCases.cs" />
5961
<Compile Include="ConfigurationSourceTestsForNoneExistingKey`1.cs" />
6062
<Compile Include="ConfigurationSourceTestsForExistingKey`1.cs" />
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
using HumbleConfig.KeyFormatters;
4+
using Moq;
5+
using NUnit.Framework;
6+
using Ploeh.AutoFixture;
7+
8+
namespace HumbleConfig.Tests.KeyFormatters
9+
{
10+
[TestFixture]
11+
public class KeyFormatterConfigurationSourceDecoratorTests
12+
{
13+
private KeyFormatterConfigurationSourceDecorator _decorator;
14+
private Mock<IKeyFormatter> _keyFormatter;
15+
private Mock<IConfigurationSource> _configurationSource;
16+
private Fixture _fixture;
17+
18+
[OneTimeSetUp]
19+
public void GivenAKeyFormatterConfigurationSourceDecorator()
20+
{
21+
_fixture = new Fixture();
22+
_keyFormatter = new Mock<IKeyFormatter>();
23+
_configurationSource = new Mock<IConfigurationSource>();
24+
_decorator = new KeyFormatterConfigurationSourceDecorator(_configurationSource.Object, _keyFormatter.Object);
25+
}
26+
27+
[Test]
28+
public async Task WhenGettingValue_ThenConfigurationSourceIsCalledWithNewKeyAndCorrectValueReturned()
29+
{
30+
var key = _fixture.Create("key");
31+
var newKey = _fixture.Create("newKey");
32+
_keyFormatter.Setup(x => x.FormatKey(key))
33+
.Returns(newKey);
34+
35+
var cancellationToken = new CancellationToken();
36+
var value = _fixture.Create("value");
37+
_configurationSource.Setup(x => x.GetAppSettingAsync<string>(newKey, cancellationToken))
38+
.ReturnsAsync(ConfigurationSourceResult<string>.SuccessResult(value));
39+
40+
var actual = await _decorator.GetAppSettingAsync<string>(key, cancellationToken);
41+
42+
Assert.IsTrue(actual.KeyExists);
43+
Assert.AreEqual(value, actual.Value);
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)