Skip to content

Commit

Permalink
Transform reference
Browse files Browse the repository at this point in the history
  • Loading branch information
thohng committed May 30, 2024
1 parent 61eb206 commit e8d8cab
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static TConfigurationBuilder AddTransformConfiguration<TConfigurationBuil

var configurationSection = configuration.GetSection(sectionKey);

configBuilder.Add(new TransformConfigurationSource(configurationSection));
configBuilder.Add(new TransformConfigurationSource(configurationSection, configuration));

return configBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

namespace NetLah.Extensions.Configuration;

public class TransformConfigurationProvider(IConfigurationSection configurationSection) : ConfigurationProvider
public class TransformConfigurationProvider(IConfigurationSection configurationSection, IConfigurationRoot configurationRoot) : ConfigurationProvider
{
private readonly IConfigurationSection _configurationSection = configurationSection;
private readonly IConfigurationRoot _configurationRoot = configurationRoot;
private object? _lock = null;
private IChangeToken? _token;

Expand Down Expand Up @@ -41,14 +42,25 @@ private void InternalLoad()
{
TryParse(key + ":", value);
}
else

if (item["Ref"] is { } refValue && _configurationRoot[refValue] is { } refConfigValue1)
{
TryParse(key + ":", refConfigValue1);
}

foreach (var section in item.GetSection("Value").GetChildren().Concat(item.GetSection("Values").GetChildren()))
{
if (section.Value is { } sectionValue)
{
TryParse(key + ":", sectionValue);
}
}

foreach (var section in item.GetSection("Ref").GetChildren())
{
foreach (var section in item.GetSection("Values").GetChildren())
if (section.Value is { } sectionValue && _configurationRoot[sectionValue] is { } refConfigValue2)
{
if (section.Value is { } sectionValue)
{
TryParse(key + ":", sectionValue);
}
TryParse(key + ":", refConfigValue2);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace NetLah.Extensions.Configuration;

public class TransformConfigurationSource(IConfigurationSection configurationSection) : IConfigurationSource
public class TransformConfigurationSource(IConfigurationSection configurationSection, IConfigurationRoot configurationRoot) : IConfigurationSource
{
private readonly IConfigurationSection _configurationSection = configurationSection;
private readonly IConfigurationRoot _configurationRoot = configurationRoot;

public IConfigurationProvider Build(IConfigurationBuilder builder)
{
return new TransformConfigurationProvider(_configurationSection);
return new TransformConfigurationProvider(_configurationSection, _configurationRoot);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ private static void AssertTransform(IConfiguration configuration)
Assert.Equal("Warning", configuration["Serilog:MinimumLevel:Override:Microsoft.Extensions.Configuration"]);
}

private static void AssertTransformEx(IConfiguration configuration)
{
Assert.Equal("Verbose2", configuration["Serilog:MinimumLevel:Override:Microsoft.Hosting.Lifetime2"]);
Assert.Equal("Warning2", configuration["Serilog:MinimumLevel:Override:Microsoft.Extensions.Configuration2"]);
Assert.Equal("Verbose3", configuration["Serilog:MinimumLevel:Override:Microsoft.Hosting.Lifetime3"]);
Assert.Equal("Verbose4", configuration["Serilog:MinimumLevel:Override:Microsoft.Hosting.Lifetime4"]);
Assert.Equal("Warning5", configuration["Serilog:MinimumLevel:Override:Microsoft.Extensions.Configuration5"]);
Assert.Equal("Verbose6", configuration["Serilog:MinimumLevel:Override:Microsoft.Hosting.Lifetime6"]);
Assert.Equal("Verbose7", configuration["Serilog:MinimumLevel:Override:Microsoft.Hosting.Lifetime7"]);
Assert.Equal("Warning8", configuration["Serilog:MinimumLevel:Override:Microsoft.Extensions.Configuration8"]);
}

[Fact]
public void Default_Success()
{
Expand Down Expand Up @@ -902,6 +914,7 @@ public void EnvironmentNameTransform()
]);

AssertTransform(configuration);
AssertTransformEx(configuration);
}

[Fact]
Expand Down Expand Up @@ -1160,6 +1173,7 @@ public void AddFileConfiguration_Transform()

AssertProduction(configuration);
AssertTransform(configuration);
AssertTransformEx(configuration);
}

[Fact]
Expand Down Expand Up @@ -1351,6 +1365,7 @@ public void ConfigurationBuilder_AddFileConfiguration_Transform()

AssertProduction(configuration);
AssertTransform(configuration);
AssertTransformEx(configuration);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,42 @@
"Microsoft.Hosting.Lifetime=Verbose",
"Microsoft.Extensions.Configuration=Warning"
]
},
{
"Key": "Serilog:MinimumLevel:Override",
"Value": [
"Microsoft.Hosting.Lifetime2=Verbose2",
"Microsoft.Extensions.Configuration2=Warning2"
]
},
{
"Key": "Serilog:MinimumLevel:Override",
"Ref": "ListTransform:0"
},
{
"Key": "Serilog:MinimumLevel:Override",
"Ref": [
"ListTransform:1",
"ListTransform:2"
]
},
{
"Key": "Serilog:MinimumLevel:Override",
"Value": [
"Microsoft.Hosting.Lifetime6=Verbose6"
],
"Values": [
"Microsoft.Hosting.Lifetime7=Verbose7"
],
"Ref": [
"ListTransform:3"
]
}
],
"ListTransform": [
"Microsoft.Hosting.Lifetime3=Verbose3",
"Microsoft.Hosting.Lifetime4=Verbose4",
"Microsoft.Extensions.Configuration5=Warning5",
"Microsoft.Extensions.Configuration8=Warning8"
]
}

0 comments on commit e8d8cab

Please sign in to comment.