Skip to content

Commit a99f01b

Browse files
committed
Merge branch 'feature/varinvar' into 'master'
Feature/varinvar See merge request cosmic9studios/C9S.Extensions.Configuration!1
2 parents 08d72ea + 0e6e6e1 commit a99f01b

File tree

5 files changed

+144
-18
lines changed

5 files changed

+144
-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
}

Samples/Samples.sln

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26124.0
5+
MinimumVisualStudioVersion = 15.0.26124.0
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VariableInVariable", "VariableInVariable\VariableInVariable.csproj", "{457E88B8-BBC0-4342-82F0-D4A7E12B1C73}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EnvironmentVariables", "EnvironmentVariables\EnvironmentVariables.csproj", "{DACEC79B-DF40-4FB7-90DC-9FF6CE58A0CC}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Debug|x64 = Debug|x64
14+
Debug|x86 = Debug|x86
15+
Release|Any CPU = Release|Any CPU
16+
Release|x64 = Release|x64
17+
Release|x86 = Release|x86
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
23+
{457E88B8-BBC0-4342-82F0-D4A7E12B1C73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{457E88B8-BBC0-4342-82F0-D4A7E12B1C73}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{457E88B8-BBC0-4342-82F0-D4A7E12B1C73}.Debug|x64.ActiveCfg = Debug|x64
26+
{457E88B8-BBC0-4342-82F0-D4A7E12B1C73}.Debug|x64.Build.0 = Debug|x64
27+
{457E88B8-BBC0-4342-82F0-D4A7E12B1C73}.Debug|x86.ActiveCfg = Debug|x86
28+
{457E88B8-BBC0-4342-82F0-D4A7E12B1C73}.Debug|x86.Build.0 = Debug|x86
29+
{457E88B8-BBC0-4342-82F0-D4A7E12B1C73}.Release|Any CPU.ActiveCfg = Release|Any CPU
30+
{457E88B8-BBC0-4342-82F0-D4A7E12B1C73}.Release|Any CPU.Build.0 = Release|Any CPU
31+
{457E88B8-BBC0-4342-82F0-D4A7E12B1C73}.Release|x64.ActiveCfg = Release|x64
32+
{457E88B8-BBC0-4342-82F0-D4A7E12B1C73}.Release|x64.Build.0 = Release|x64
33+
{457E88B8-BBC0-4342-82F0-D4A7E12B1C73}.Release|x86.ActiveCfg = Release|x86
34+
{457E88B8-BBC0-4342-82F0-D4A7E12B1C73}.Release|x86.Build.0 = Release|x86
35+
{DACEC79B-DF40-4FB7-90DC-9FF6CE58A0CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
36+
{DACEC79B-DF40-4FB7-90DC-9FF6CE58A0CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
37+
{DACEC79B-DF40-4FB7-90DC-9FF6CE58A0CC}.Debug|x64.ActiveCfg = Debug|x64
38+
{DACEC79B-DF40-4FB7-90DC-9FF6CE58A0CC}.Debug|x64.Build.0 = Debug|x64
39+
{DACEC79B-DF40-4FB7-90DC-9FF6CE58A0CC}.Debug|x86.ActiveCfg = Debug|x86
40+
{DACEC79B-DF40-4FB7-90DC-9FF6CE58A0CC}.Debug|x86.Build.0 = Debug|x86
41+
{DACEC79B-DF40-4FB7-90DC-9FF6CE58A0CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
42+
{DACEC79B-DF40-4FB7-90DC-9FF6CE58A0CC}.Release|Any CPU.Build.0 = Release|Any CPU
43+
{DACEC79B-DF40-4FB7-90DC-9FF6CE58A0CC}.Release|x64.ActiveCfg = Release|x64
44+
{DACEC79B-DF40-4FB7-90DC-9FF6CE58A0CC}.Release|x64.Build.0 = Release|x64
45+
{DACEC79B-DF40-4FB7-90DC-9FF6CE58A0CC}.Release|x86.ActiveCfg = Release|x86
46+
{DACEC79B-DF40-4FB7-90DC-9FF6CE58A0CC}.Release|x86.Build.0 = Release|x86
47+
EndGlobalSection
48+
EndGlobal
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using C9S.Extensions.Configuration;
3+
using Microsoft.Extensions.Configuration;
4+
5+
namespace VariableInVariable
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
var builder = new ConfigurationBuilder()
12+
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
13+
.AddJsonFile($"appsettings.json", optional: true)
14+
.AddEnvironmentVariables();
15+
16+
var configuration = builder.Build();
17+
18+
configuration.ResolveVariables();
19+
20+
Console.WriteLine(configuration.GetSection("Auth:ClientID"));
21+
}
22+
}
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.0" />
8+
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.1.0" />
9+
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.1.0" />
10+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.0" />
11+
</ItemGroup>
12+
<ItemGroup>
13+
<ProjectReference Include="..\..\C9S.Extensions.Configuration\C9S.Extensions.Configuration.csproj" />
14+
</ItemGroup>
15+
<ItemGroup>
16+
<None Update="appsettings.json" CopyToOutputDirectory="Always" />
17+
</ItemGroup>
18+
</Project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"App":
3+
{
4+
"Development": {
5+
"ClientId": "FooId"
6+
}
7+
},
8+
"Auth":
9+
{
10+
"ClientID": "{{App.{{ASPNETCORE_ENVIRONMENT}}.ClientId}}"
11+
}
12+
}

0 commit comments

Comments
 (0)