From 4aa95dfbfd81c478c2872ec7320d20036ecd867a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 11 Jul 2021 21:39:29 -0500 Subject: derive config root from namespace --- src/slskd/Common/Configuration/YamlConfigurationSource.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Common/Configuration/YamlConfigurationSource.cs b/src/slskd/Common/Configuration/YamlConfigurationSource.cs index 332df647..82e2227d 100644 --- a/src/slskd/Common/Configuration/YamlConfigurationSource.cs +++ b/src/slskd/Common/Configuration/YamlConfigurationSource.cs @@ -37,6 +37,7 @@ namespace slskd.Configuration /// /// Path relative to the base path stored in of . /// + /// The type from which to map properties. /// Whether the file is optional. /// Whether the configuration should be reloaded if the file changes. /// @@ -44,7 +45,7 @@ namespace slskd.Configuration /// /// The updated to use to access the file. /// The . - public static IConfigurationBuilder AddYamlFile(this IConfigurationBuilder builder, string path, bool optional = true, bool reloadOnChange = false, bool normalizeKeys = true, IFileProvider provider = null) + public static IConfigurationBuilder AddYamlFile(this IConfigurationBuilder builder, string path, Type targetType, bool optional = true, bool reloadOnChange = false, bool normalizeKeys = true, IFileProvider provider = null) { if (builder == null) { @@ -59,6 +60,7 @@ namespace slskd.Configuration return builder.AddYamlFile(s => { s.Path = path; + s.TargetType = targetType; s.Optional = optional; s.ReloadOnChange = reloadOnChange; s.NormalizeKeys = normalizeKeys; @@ -89,9 +91,13 @@ namespace slskd.Configuration public YamlConfigurationProvider(YamlConfigurationSource source) : base(source) { + TargetType = source.TargetType; + Namespace = TargetType.Namespace.Split('.').First(); NormalizeKeys = source.NormalizeKeys; } + private Type TargetType { get; set; } + private string Namespace { get; set; } private bool NormalizeKeys { get; set; } private string[] NullValues { get; } = new[] { "~", "null", string.Empty }; @@ -111,7 +117,7 @@ namespace slskd.Configuration if (yaml.Documents.Count > 0) { var rootNode = (YamlMappingNode)yaml.Documents[0].RootNode; - Traverse(rootNode); + Traverse(rootNode, Namespace); } } catch (YamlException e) @@ -161,6 +167,11 @@ namespace slskd.Configuration /// public class YamlConfigurationSource : FileConfigurationSource { + /// + /// Gets or sets the type from which to map properties. + /// + public Type TargetType { get; set; } + /// /// Gets or sets a value indicating whether configuration keys should be normalized (_, - removed, changed to lowercase). /// -- cgit v1.2.3