aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-02-11 20:20:13 -0600
committerJP Dillingham <jpdillingham@gmail.com>2021-02-11 20:20:13 -0600
commit0ee4fcbec326784f05ee2393d5f15f8755858ac8 (patch)
treeab7f3e965d149030ff5a76993ab2bb7a8a201b4e /src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs
parent018edf61fe14582f31284f294163414fdc05c35f (diff)
downloadslskd-0ee4fcbec326784f05ee2393d5f15f8755858ac8.tar.gz
use reflection instead of map
Diffstat (limited to 'src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs')
-rw-r--r--src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs67
1 files changed, 41 insertions, 26 deletions
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.