diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-02-11 20:20:13 -0600 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2021-02-11 20:20:13 -0600 |
| commit | 0ee4fcbec326784f05ee2393d5f15f8755858ac8 (patch) | |
| tree | ab7f3e965d149030ff5a76993ab2bb7a8a201b4e /src | |
| parent | 018edf61fe14582f31284f294163414fdc05c35f (diff) | |
| download | slskd-0ee4fcbec326784f05ee2393d5f15f8755858ac8.tar.gz | |
use reflection instead of map
Diffstat (limited to 'src')
3 files changed, 121 insertions, 64 deletions
diff --git a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs index 417e5729..28d9907d 100644 --- a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs +++ b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs @@ -18,8 +18,8 @@ namespace slskd.Configuration { using System; - using System.Collections.Generic; using System.Linq; + using System.Reflection; using Microsoft.Extensions.Configuration; using Utility.CommandLine; @@ -32,19 +32,24 @@ namespace slskd.Configuration /// Adds a command line argument configuration soruce to <paramref name="builder"/>. /// </summary> /// <param name="builder">The <see cref="IConfigurationBuilder"/> to which to add.</param> - /// <param name="map">A list of command line argument mappings.</param> + /// <param name="targetType">The type from which to map property values.</param> /// <param name="commandLine">The command line string from which to parse arguments.</param> /// <returns>The updated <see cref="IConfigurationBuilder"/>.</returns> - public static IConfigurationBuilder AddCommandLine(this IConfigurationBuilder builder, IEnumerable<Option> map, string commandLine = null) + public static IConfigurationBuilder AddCommandLine(this IConfigurationBuilder builder, Type targetType, string commandLine = null) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } + if (targetType == null) + { + throw new ArgumentNullException(nameof(targetType)); + } + return builder.AddCommandLine(s => { - s.Map = map; + s.TargetType = targetType; s.CommandLine = commandLine ?? Environment.CommandLine; }); } @@ -70,12 +75,14 @@ namespace slskd.Configuration /// <param name="source">The source settings.</param> public CommandLineConfigurationProvider(CommandLineConfigurationSource source) { - Map = source.Map; + TargetType = source.TargetType; + Namespace = TargetType.Namespace.Split('.').First(); CommandLine = source.CommandLine; } private string CommandLine { get; set; } - private IEnumerable<Option> Map { get; set; } + private string Namespace { get; set; } + private Type TargetType { get; set; } /// <summary> /// Parses command line arguments from the specified string and maps them to the specified keys. @@ -84,30 +91,44 @@ namespace slskd.Configuration { var dictionary = Arguments.Parse(CommandLine).ArgumentDictionary; - foreach (Option item in Map) + void Map(Type type, string path) { - if (string.IsNullOrEmpty(item.Key)) - { - continue; - } - - var arguments = new[] { item.ShortName.ToString(), item.LongName }.Where(i => !string.IsNullOrEmpty(i)); + var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); - foreach (var argument in arguments) + foreach (PropertyInfo property in props) { - if (dictionary.ContainsKey(argument)) + var attribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(ArgumentAttribute)); + var key = ConfigurationPath.Combine(path, property.Name.ToLowerInvariant()); + + if (attribute != default) { - var value = dictionary[argument].ToString(); + var shortName = ((char)attribute.ConstructorArguments[0].Value).ToString(); + var longName = (string)attribute.ConstructorArguments[1].Value; + var arguments = new[] { shortName, longName }.Where(i => !string.IsNullOrEmpty(i)); - if (item.Type == typeof(bool) && string.IsNullOrEmpty(value)) + foreach (var argument in arguments) { - value = "true"; - } + if (dictionary.ContainsKey(argument)) + { + var value = dictionary[argument].ToString(); - Data[item.Key] = value; + if (property.PropertyType == typeof(bool) && string.IsNullOrEmpty(value)) + { + value = "true"; + } + + Data[key] = value; + } + } + } + else + { + Map(property.PropertyType, key); } } } + + Map(TargetType, Namespace); } } @@ -121,10 +142,7 @@ namespace slskd.Configuration /// </summary> public string CommandLine { get; set; } - /// <summary> - /// Gets or sets a list of command line argument mappings. - /// </summary> - public IEnumerable<Option> Map { get; set; } + public Type TargetType { get; set; } /// <summary> /// Builds the <see cref="CommandLineConfigurationProvider"/> for this source. diff --git a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs b/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs index 32a87616..8db204a2 100644 --- a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs +++ b/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs @@ -18,8 +18,8 @@ namespace slskd.Configuration { using System; - using System.Collections.Generic; using System.Linq; + using System.Reflection; using Microsoft.Extensions.Configuration; /// <summary> @@ -31,18 +31,23 @@ namespace slskd.Configuration /// Adds a default value configuration source to <paramref name="builder"/>. /// </summary> /// <param name="builder">The <see cref="IConfigurationBuilder"/> to which to add.</param> - /// <param name="map">A list of default value mappings.</param> + /// <param name="targetType">The type from which to load default values.</param> /// <returns>The updated <see cref="IConfigurationBuilder"/>.</returns> - public static IConfigurationBuilder AddDefaultValues(this IConfigurationBuilder builder, IEnumerable<Option> map) + public static IConfigurationBuilder AddDefaultValues(this IConfigurationBuilder builder, Type targetType) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } + if (targetType == null) + { + throw new ArgumentNullException(nameof(targetType)); + } + return builder.AddDefaultValues(s => { - s.Map = map ?? Enumerable.Empty<Option>(); + s.TargetType = targetType; }); } @@ -67,25 +72,44 @@ namespace slskd.Configuration /// <param name="source">The source settings.</param> public DefaultValueConfigurationProvider(DefaultValueConfigurationSource source) { - Map = source.Map; + TargetType = source.TargetType; + Namespace = TargetType.Namespace.Split('.').First(); } - private IEnumerable<Option> Map { get; set; } + private string Namespace { get; set; } + private Type TargetType { get; set; } /// <summary> - /// Sets the keys specified in the map to the specified default values. + /// Sets loads default values from the specified <see cref="TargetType"/>. /// </summary> public override void Load() { - foreach (var item in Map) + void Map(Type type, string path) { - if (string.IsNullOrEmpty(item.Key) || item.Default == null) + var defaults = Activator.CreateInstance(type); + var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); + + foreach (PropertyInfo property in props) { - continue; - } + var key = ConfigurationPath.Combine(path, property.Name.ToLowerInvariant()); + + if (property.PropertyType.Namespace.StartsWith(Namespace)) + { + Map(property.PropertyType, key); + } + else + { + var value = property.GetValue(defaults); - Data[item.Key] = item.Default.ToString(); + if (value != null) + { + Data[key] = value.ToString(); + } + } + } } + + Map(TargetType, Namespace); } } @@ -95,9 +119,9 @@ namespace slskd.Configuration public class DefaultValueConfigurationSource : IConfigurationSource { /// <summary> - /// Gets or sets a list of default value mappings. + /// Gets or sets the type from which to map properties. /// </summary> - public IEnumerable<Option> Map { get; set; } + public Type TargetType { get; set; } /// <summary> /// Builds the <see cref="DefaultValueConfigurationProvider"/> for this source. diff --git a/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs b/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs index 3a77ee9c..d983e6b0 100644 --- a/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs +++ b/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs @@ -20,7 +20,10 @@ namespace slskd.Configuration using System; using System.Collections.Generic; using System.Linq; + using System.Reflection; + using System.Text.Json; using Microsoft.Extensions.Configuration; + using Utility.EnvironmentVariables; /// <summary> /// Extension methods for adding <see cref="EnvironmentVariableConfigurationProvider"/>. @@ -31,19 +34,24 @@ namespace slskd.Configuration /// Adds an environment variable configuration source to <paramref name="builder"/>. /// </summary> /// <param name="builder">The <see cref="IConfigurationBuilder"/> to which to add.</param> - /// <param name="map">A list of environment variable mappings.</param> + /// <param name="targetType">The type from which to map property values.</param> /// <param name="prefix">A prefix to prepend to all variable names.</param> /// <returns>The updated <see cref="IConfigurationBuilder"/>.</returns> - public static IConfigurationBuilder AddEnvironmentVariables(this IConfigurationBuilder builder, IEnumerable<Option> map, string prefix = null) + public static IConfigurationBuilder AddEnvironmentVariables(this IConfigurationBuilder builder, Type targetType, string prefix = null) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } + if (targetType == null) + { + throw new ArgumentNullException(nameof(targetType)); + } + return builder.AddEnvironmentVariables(s => { - s.Map = map ?? Enumerable.Empty<Option>(); + s.TargetType = targetType; s.Prefix = prefix ?? string.Empty; }); } @@ -69,11 +77,13 @@ namespace slskd.Configuration /// <param name="source">The source settings.</param> public EnvironmentVariableConfigurationProvider(EnvironmentVariableConfigurationSource source) { - Map = source.Map; + TargetType = source.TargetType; + Namespace = TargetType.Namespace.Split('.').First(); Prefix = source.Prefix; } - private IEnumerable<Option> Map { get; set; } + private Type TargetType { get; set; } + private string Namespace { get; set; } private string Prefix { get; set; } /// <summary> @@ -81,27 +91,37 @@ namespace slskd.Configuration /// </summary> public override void Load() { - foreach (var item in Map) + void Map(Type type, string path) { - if (string.IsNullOrEmpty(item.Key) || string.IsNullOrEmpty(item.EnvironmentVariable)) + var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); + + foreach (PropertyInfo property in props) { - continue; - } + var attribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(EnvironmentVariableAttribute)); + var key = ConfigurationPath.Combine(path, property.Name.ToLowerInvariant()); - var value = Environment.GetEnvironmentVariable(Prefix + item.EnvironmentVariable); + if (attribute != default) + { + var name = (string)attribute.ConstructorArguments[0].Value; - if (string.IsNullOrEmpty(value)) - { - continue; - } + if (!string.IsNullOrEmpty(name)) + { + var value = Environment.GetEnvironmentVariable(Prefix + name); - if (item.Type == typeof(bool)) - { - value = value.Equals("true", StringComparison.InvariantCultureIgnoreCase) ? value : "false"; + if (value != null) + { + Data[key] = value.ToString(); + } + } + } + else + { + Map(property.PropertyType, key); + } } - - Data[item.Key] = value; } + + Map(TargetType, Namespace); } } @@ -111,14 +131,9 @@ namespace slskd.Configuration public class EnvironmentVariableConfigurationSource : IConfigurationSource { /// <summary> - /// Gets or sets a list of enviroment variable mappings. - /// </summary> - public IEnumerable<Option> Map { get; set; } - - /// <summary> - /// Gets or sets a value indicating whether configuration keys should be normalized (_, - removed, changed to lowercase). + /// Gets or sets the type from which to map properties. /// </summary> - public bool NormalizeKey { get; set; } + public Type TargetType { get; set; } /// <summary> /// Gets or sets a prefix to prepend to all variable names. |
