Skip to content

Commit

Permalink
Eager loading configuration section to avoid recursive
Browse files Browse the repository at this point in the history
  • Loading branch information
thohng committed May 15, 2024
1 parent 3f4aa7e commit 9164413
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/NetLah.Extensions.Configuration/AddFileConfigurationSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,23 @@ public IConfigurationProvider Build(IConfigurationBuilder builder)
#endif
configurationBuilder.SetFileProvider(builder.GetFileProvider());
var supportedExtensions = string.Join(", ", _options.ConfigureAddFiles.Keys.OrderBy(k => k));
var configSections = configurationSection.GetChildren().ToArray();

foreach (var item in configurationSection.GetChildren())
foreach (var configSection in configSections)
{
if (item.Value == null && item["Provider"] is { } typeValue1 && ("Settings".Equals(typeValue1, StringComparison.OrdinalIgnoreCase) || "DefaultSettings".Equals(typeValue1, StringComparison.OrdinalIgnoreCase)))
if (configSection.Value == null && configSection["Provider"] is { } typeValue1 && ("Settings".Equals(typeValue1, StringComparison.OrdinalIgnoreCase) || "DefaultSettings".Equals(typeValue1, StringComparison.OrdinalIgnoreCase)))
{
item.Bind(_defaultOptions);
configSection.Bind(_defaultOptions);
_defaultOptions.ResetCache();
logger.LogInformation("AddFile default settings {@settings}", _defaultOptions);
}
}

foreach (var item in configurationSection.GetChildren())
foreach (var configSection in configSections)
{
var context = new AddFileContext
{
Configuration = item,
Configuration = configSection,
ConfigurationBuilder = configurationBuilder,
Logger = logger,
SupportedExtensions = supportedExtensions,
Expand All @@ -75,7 +76,7 @@ public IConfigurationProvider Build(IConfigurationBuilder builder)

var processed = false;

if (item.Value is { } value1)
if (configSection.Value is { } value1)
{
var extensionOrProvider = Path.GetExtension(value1);
context.Provider = extensionOrProvider;
Expand All @@ -88,24 +89,24 @@ public IConfigurationProvider Build(IConfigurationBuilder builder)
OriginalPath = value1,
};
}
else if (item.Value == null && item["Provider"] is { } typeValue1 &&
else if (configSection.Value == null && configSection["Provider"] is { } typeValue1 &&
("Settings".Equals(typeValue1, StringComparison.OrdinalIgnoreCase) || "DefaultSettings".Equals(typeValue1, StringComparison.OrdinalIgnoreCase)))
{
// Settings and type already processed
processed = true;
}
else
{
var provider = item["Provider"];
var path = item["Path"];
var provider = configSection["Provider"];
var path = configSection["Path"];
var extensionOrProvider = provider ?? Path.GetExtension(path);
var addFileSource = new AddFileSource
{
LoggingLevel = _defaultOptions.LoggingLevel,
Optional = _defaultOptions.Optional,
ReloadOnChange = _defaultOptions.ReloadOnChange
};
item.Bind(addFileSource);
configSection.Bind(addFileSource);
addFileSource.OriginalPath = addFileSource.Path;
context.Provider = extensionOrProvider;
context.Source = addFileSource;
Expand All @@ -128,7 +129,7 @@ public IConfigurationProvider Build(IConfigurationBuilder builder)
{
if (_defaultOptions.IsEnableLogging())
{
logger.LogError("AddFile unknown entry {@entry}", FormatConfigurationSection(item));
logger.LogError("AddFile unknown entry {@entry}", FormatConfigurationSection(configSection));
}

if (_defaultOptions.ThrowIfNotSupport ?? _options.ThrowIfNotSupport ?? false)
Expand Down

0 comments on commit 9164413

Please sign in to comment.