Skip to content

Commit 0e6e6e1

Browse files
Updated Extension method to support Variable in Variable
1 parent 80d4b5a commit 0e6e6e1

File tree

1 file changed

+43
-18
lines changed

1 file changed

+43
-18
lines changed

C9S.Extensions.Configuration/ConfigurationRootExtensions.cs

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,59 @@ namespace C9S.Extensions.Configuration
88
{
99
public static class ConfigurationRootExtensions
1010
{
11-
private static Regex variableRegex = new Regex(@"\{{(?<var>[^}}]+)\}}");
11+
private static List<IConfigurationSection> configSections = new List<IConfigurationSection>();
1212

13-
public static void ResolveVariables(this IConfiguration configuration)
13+
public static void ResolveVariables(this IConfiguration configuration, string open = "{{", string close = "}}")
14+
{
15+
GetAllConfigurationSections(configuration.GetChildren());
16+
VariableResolver(configuration, open, close);
17+
}
18+
19+
private static void GetAllConfigurationSections(IEnumerable<IConfigurationSection> configurationSections)
1420
{
15-
foreach (var configSection in configuration.GetChildren())
21+
foreach (var section in configurationSections ?? new List<IConfigurationSection>())
1622
{
17-
foreach (Match match in variableRegex.Matches(configSection.Value ?? ""))
23+
configSections.Add(section);
24+
GetAllConfigurationSections(section.GetChildren());
25+
}
26+
}
27+
28+
private static void VariableResolver(IConfiguration Configuration, string open, string close)
29+
{
30+
var variableRegex = new Regex($@"({open})(?!.*{open})(?<var>[^{close}]+)\{close}");
31+
while (configSections.Any())
32+
{
33+
var currentSection = configSections.First();
34+
configSections.RemoveAt(0);
35+
36+
Match match = variableRegex.Match(currentSection.Value ?? "");
37+
if (!match.Success)
1838
{
19-
var variable = match.Groups["var"].Value;
20-
var sections = new List<string>(variable.Split(new [] { '.' }, StringSplitOptions.RemoveEmptyEntries));
21-
IConfigurationSection section = configSection;
39+
continue;
40+
}
41+
42+
var variable = match.Groups["var"].Value;
43+
var sections = new List<string>(variable.Split(new [] { '.' }, System.StringSplitOptions.RemoveEmptyEntries));
44+
IConfigurationSection section = currentSection;
45+
if (sections.Any())
46+
{
47+
section = Configuration.GetSection(sections[0]);
48+
var key = sections.Last();
49+
50+
sections.RemoveAt(0);
2251
if (sections.Any())
2352
{
24-
section = configuration.GetSection(sections[0]);
25-
var key = sections.Last();
26-
27-
sections.RemoveAt(0);
2853
sections.RemoveAt(sections.Count - 1);
29-
foreach (var sec in sections)
30-
{
31-
section = section.GetSection(sec);
32-
}
54+
}
3355

34-
configSection.Value = configSection.Value.Replace($"{{{{{variable}}}}}", section[key]);
56+
foreach (var sec in sections)
57+
{
58+
section = section.GetSection(sec);
3559
}
36-
}
3760

38-
ResolveVariables(configSection);
61+
currentSection.Value = currentSection.Value.Replace($"{{{{{variable}}}}}", section.Value ?? section[key]);
62+
configSections.Add(currentSection);
63+
}
3964
}
4065
}
4166
}

0 commit comments

Comments
 (0)