-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
181 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/NetLah.Extensions.Configuration/MapConfigurationProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace NetLah.Extensions.Configuration; | ||
|
||
public class MapConfigurationProvider(MapConfigurationSource source) : ConfigurationProvider | ||
{ | ||
private readonly MapConfigurationSource _source = source; | ||
private object? _lock = null; | ||
private IChangeToken? _token; | ||
private IConfigurationSection? _configurationSection; | ||
|
||
private void OnChange(object? obj) | ||
{ | ||
Data.Clear(); | ||
InternalLoad(); | ||
OnReload(); | ||
} | ||
|
||
public override void Load() | ||
{ | ||
InternalLoad(); | ||
|
||
if (_configurationSection != null && Interlocked.CompareExchange(ref _lock, new object(), null) == null) | ||
{ | ||
_token = _configurationSection.GetReloadToken(); | ||
_token.RegisterChangeCallback(OnChange, this); | ||
} | ||
} | ||
|
||
private void InternalLoad() | ||
{ | ||
var configuration = _source.Configuration; | ||
_configurationSection ??= configuration.GetSection(_source.SectionKey); | ||
|
||
foreach (var item in _configurationSection.GetChildren()) | ||
{ | ||
if (item.Value is { } keyValue) | ||
{ | ||
TryParse(keyValue); | ||
} | ||
else | ||
{ | ||
var key1 = item["From"] ?? item["Source"]; | ||
var key2 = item["To"] ?? item["Destination"] ?? item["Dest"]; | ||
if (!string.IsNullOrEmpty(key1) && !string.IsNullOrEmpty(key2) && configuration[key1] is { } value) | ||
{ | ||
Data[key2] = value; | ||
} | ||
} | ||
} | ||
|
||
void TryParse(string keyValue) | ||
{ | ||
var pos = keyValue.IndexOf('='); | ||
var key1 = keyValue[..pos]; | ||
var key2 = keyValue[(pos + 1)..]; | ||
if (configuration[key1] is { } value) | ||
{ | ||
Data[key2] = value; | ||
} | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/NetLah.Extensions.Configuration/MapConfigurationSource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace NetLah.Extensions.Configuration; | ||
|
||
public class MapConfigurationSource(IConfiguration configuration, string sectionKey) : IConfigurationSource | ||
{ | ||
public IConfiguration Configuration { get; } = configuration; | ||
|
||
public string SectionKey { get; } = sectionKey; | ||
|
||
public IConfigurationProvider Build(IConfigurationBuilder builder) | ||
{ | ||
return new MapConfigurationProvider(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 4 additions & 1 deletion
5
test/NetLah.Extensions.Configuration.Test/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
{ | ||
"EnvironmentKey": "EnvironmentDevelopmentValue1" | ||
"EnvironmentKey": "EnvironmentDevelopmentValue1", | ||
"DevelopmentSection": { | ||
"SubKey": "DevelopmentSubValue1" | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
test/NetLah.Extensions.Configuration.Test/appsettings.Map.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"MapConfiguration": [ | ||
"MainKey=SecondaryKey", | ||
{ | ||
"From": "CommandLineSection:SubKey", | ||
"To": "MapSection:SubKey" | ||
} | ||
] | ||
} |
5 changes: 4 additions & 1 deletion
5
test/NetLah.Extensions.Configuration.Test/appsettings.Production.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
{ | ||
"EnvironmentKey": "EnvironmentProductionValue1" | ||
"EnvironmentKey": "EnvironmentProductionValue1", | ||
"ProductionSection": { | ||
"SubKey": "ProductionSubValue1" | ||
} | ||
} |