Skip to content

Commit 11690c6

Browse files
authored
Fix bug with incorrect trimming of root key from config keys. (#62)
1 parent 3fc11c4 commit 11690c6

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

src/Winton.Extensions.Configuration.Consul/Extensions/KVPairExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ internal static IEnumerable<KeyValuePair<string, string>> ConvertToConfig(
2323
.Select(
2424
pair =>
2525
{
26-
string key = $"{kvPair.Key.TrimStart(rootKey.ToCharArray()).TrimEnd('/')}:{pair.Key}"
26+
string key = $"{kvPair.Key.RemoveStart(rootKey).TrimEnd('/')}:{pair.Key}"
2727
.Replace('/', ':')
2828
.Trim(':');
2929
if (string.IsNullOrEmpty(key))
@@ -46,5 +46,10 @@ internal static bool IsLeafNode(this KVPair kvPair)
4646
{
4747
return !kvPair.Key.EndsWith("/");
4848
}
49+
50+
private static string RemoveStart(this string s, string toRemove)
51+
{
52+
return s.StartsWith(toRemove) ? s.Remove(0, toRemove.Length) : s;
53+
}
4954
}
5055
}

test/Winton.Extensions.Configuration.Consul.Test/Extensions/KVPairExtensionsTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ public ConvertToConfig()
5353
}
5454
},
5555
new object[]
56+
{
57+
"RootKey/Settings",
58+
"RootKey/Settings/Root",
59+
new Dictionary<string, string> { { "Key", "value" } },
60+
new[]
61+
{
62+
new KeyValuePair<string, string>("Root:Key", "value")
63+
}
64+
},
65+
new object[]
5666
{
5767
"rootKey",
5868
"rootKey",

0 commit comments

Comments
 (0)