Skip to content

Commit 3fc11c4

Browse files
authored
Bug fix: Root key path not being properly removed from config keys. (#58)
1 parent 0b0a71d commit 3fc11c4

2 files changed

Lines changed: 289 additions & 261 deletions

File tree

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,50 @@
1-
// Copyright (c) Winton. All rights reserved.
2-
// Licensed under the Apache License, Version 2.0. See LICENCE in the project root for license information.
3-
4-
using System.Collections.Generic;
5-
using System.IO;
6-
using System.Linq;
7-
using Consul;
8-
using Winton.Extensions.Configuration.Consul.Parsers;
9-
10-
namespace Winton.Extensions.Configuration.Consul.Extensions
11-
{
12-
internal static class KVPairExtensions
13-
{
14-
internal static IEnumerable<KeyValuePair<string, string>> ConvertToConfig(
15-
this KVPair kvPair,
16-
string rootKey,
17-
IConfigurationParser parser)
18-
{
19-
using (Stream stream = new MemoryStream(kvPair.Value))
20-
{
21-
return parser
22-
.Parse(stream)
23-
.Select(
24-
pair =>
25-
{
26-
string key = $"{kvPair.Key.TrimEnd('/')}:{pair.Key}"
27-
.Replace('/', ':')
28-
.TrimStart(rootKey.ToCharArray())
29-
.TrimStart(':')
30-
.TrimEnd(':');
31-
if (string.IsNullOrEmpty(key))
32-
{
33-
throw new InvalidKeyPairException(
34-
"The key must not be null or empty. Ensure that there is at least one key under the root of the config or that the data there contains more than just a single value.");
35-
}
36-
37-
return new KeyValuePair<string, string>(key, pair.Value);
38-
});
39-
}
40-
}
41-
42-
internal static bool HasValue(this KVPair kvPair)
43-
{
44-
return kvPair.IsLeafNode() && kvPair.Value != null && kvPair.Value.Any();
45-
}
46-
47-
internal static bool IsLeafNode(this KVPair kvPair)
48-
{
49-
return !kvPair.Key.EndsWith("/");
50-
}
51-
}
1+
// Copyright (c) Winton. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENCE in the project root for license information.
3+
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Linq;
7+
using Consul;
8+
using Winton.Extensions.Configuration.Consul.Parsers;
9+
10+
namespace Winton.Extensions.Configuration.Consul.Extensions
11+
{
12+
internal static class KVPairExtensions
13+
{
14+
internal static IEnumerable<KeyValuePair<string, string>> ConvertToConfig(
15+
this KVPair kvPair,
16+
string rootKey,
17+
IConfigurationParser parser)
18+
{
19+
using (Stream stream = new MemoryStream(kvPair.Value))
20+
{
21+
return parser
22+
.Parse(stream)
23+
.Select(
24+
pair =>
25+
{
26+
string key = $"{kvPair.Key.TrimStart(rootKey.ToCharArray()).TrimEnd('/')}:{pair.Key}"
27+
.Replace('/', ':')
28+
.Trim(':');
29+
if (string.IsNullOrEmpty(key))
30+
{
31+
throw new InvalidKeyPairException(
32+
"The key must not be null or empty. Ensure that there is at least one key under the root of the config or that the data there contains more than just a single value.");
33+
}
34+
35+
return new KeyValuePair<string, string>(key, pair.Value);
36+
});
37+
}
38+
}
39+
40+
internal static bool HasValue(this KVPair kvPair)
41+
{
42+
return kvPair.IsLeafNode() && kvPair.Value != null && kvPair.Value.Any();
43+
}
44+
45+
internal static bool IsLeafNode(this KVPair kvPair)
46+
{
47+
return !kvPair.Key.EndsWith("/");
48+
}
49+
}
5250
}

0 commit comments

Comments
 (0)