aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Common
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-07-24 14:39:40 -0500
committerGitHub <noreply@github.com>2021-07-24 14:39:40 -0500
commit58bc401dab5d7eb3bc6cb07a22e0ee9c97ddf4bd (patch)
tree64072f5eaa896335cc04de8ffe84fa79a50a2be7 /src/slskd/Common
parentfc7800409dfbe0cf26a72dd1de17cbae23d0cb35 (diff)
parent78b3b953d9c36c2cc2939ad00881b0cbf388865a (diff)
downloadslskd-58bc401dab5d7eb3bc6cb07a22e0ee9c97ddf4bd.tar.gz
Merge pull request #125 from slskd/options
Refactor configuration components to allow for run-time updates
Diffstat (limited to 'src/slskd/Common')
-rw-r--r--src/slskd/Common/Configuration/RequiresReconnectAttribute.cs26
-rw-r--r--src/slskd/Common/Configuration/RequiresRestartAttribute.cs26
-rw-r--r--src/slskd/Common/Configuration/YamlConfigurationSource.cs15
-rw-r--r--src/slskd/Common/Extensions.cs60
4 files changed, 121 insertions, 6 deletions
diff --git a/src/slskd/Common/Configuration/RequiresReconnectAttribute.cs b/src/slskd/Common/Configuration/RequiresReconnectAttribute.cs
new file mode 100644
index 00000000..cd5edd14
--- /dev/null
+++ b/src/slskd/Common/Configuration/RequiresReconnectAttribute.cs
@@ -0,0 +1,26 @@
+// <copyright file="RequiresReconnectAttribute.cs" company="slskd Team">
+// Copyright (c) slskd Team. All rights reserved.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see https://www.gnu.org/licenses/.
+// </copyright>
+
+namespace slskd.Configuration
+{
+ using System;
+
+ [AttributeUsage(AttributeTargets.Property)]
+ public class RequiresReconnectAttribute : Attribute
+ {
+ }
+}
diff --git a/src/slskd/Common/Configuration/RequiresRestartAttribute.cs b/src/slskd/Common/Configuration/RequiresRestartAttribute.cs
new file mode 100644
index 00000000..9b90eabe
--- /dev/null
+++ b/src/slskd/Common/Configuration/RequiresRestartAttribute.cs
@@ -0,0 +1,26 @@
+// <copyright file="RequiresRestartAttribute.cs" company="slskd Team">
+// Copyright (c) slskd Team. All rights reserved.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see https://www.gnu.org/licenses/.
+// </copyright>
+
+namespace slskd.Configuration
+{
+ using System;
+
+ [AttributeUsage(AttributeTargets.Property)]
+ public class RequiresRestartAttribute : Attribute
+ {
+ }
+}
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
/// <param name="path">
/// Path relative to the base path stored in <see cref="IConfigurationBuilder.Properties"/> of <paramref name="builder"/>.
/// </param>
+ /// <param name="targetType">The type from which to map properties.</param>
/// <param name="optional">Whether the file is optional.</param>
/// <param name="reloadOnChange">Whether the configuration should be reloaded if the file changes.</param>
/// <param name="normalizeKeys">
@@ -44,7 +45,7 @@ namespace slskd.Configuration
/// </param>
/// <param name="provider">The updated <see cref="IFileProvider"/> to use to access the file.</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
- 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)
@@ -162,6 +168,11 @@ namespace slskd.Configuration
public class YamlConfigurationSource : FileConfigurationSource
{
/// <summary>
+ /// Gets or sets the type from which to map properties.
+ /// </summary>
+ public Type TargetType { get; set; }
+
+ /// <summary>
/// Gets or sets a value indicating whether configuration keys should be normalized (_, - removed, changed to lowercase).
/// </summary>
public bool NormalizeKeys { get; set; }
diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs
index a7a89a71..3e4f6e68 100644
--- a/src/slskd/Common/Extensions.cs
+++ b/src/slskd/Common/Extensions.cs
@@ -19,12 +19,10 @@ namespace slskd
{
using System;
using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using slskd.Validation;
+ using System.Reflection;
+ using System.Text.Json;
/// <summary>
/// Extensions.
@@ -32,6 +30,60 @@ namespace slskd
public static class Extensions
{
/// <summary>
+ /// Serializes this object to json.
+ /// </summary>
+ /// <param name="obj">The object to serialize.</param>
+ /// <returns>A string containing the serialized object.</returns>
+ public static string ToJson(this object obj) => JsonSerializer.Serialize(obj);
+
+ /// <summary>
+ /// Deserializes this string from json to an object of type <typeparamref name="T"/>.
+ /// </summary>
+ /// <typeparam name="T">The type to which to deserialize the string.</typeparam>
+ /// <param name="str">The string to deserialize.</param>
+ /// <returns>The new object deserialzied from the string.</returns>
+ public static T ToObject<T>(this string str) => JsonSerializer.Deserialize<T>(str);
+
+ /// <summary>
+ /// Deeply compares this object with the specified object and returns a list of properties that are different.
+ /// </summary>
+ /// <param name="left">The left side of the comparison.</param>
+ /// <param name="right">The right side of the comparison.</param>
+ /// <param name="parentFqn">The root path for recursive calls.</param>
+ /// <returns>A list of differences between the two objects.</returns>
+ public static IEnumerable<(PropertyInfo Property, string FQN, object Left, object Right)> DiffWith(this object left, object right, string parentFqn = null)
+ {
+ if (left?.GetType() != right?.GetType())
+ {
+ throw new InvalidCastException($"Unable to diff types {left?.GetType()} and {right?.GetType()}");
+ }
+
+ var differences = new List<(PropertyInfo Property, string FQN, object Left, object Right)>();
+
+ foreach (var prop in left?.GetType().GetProperties())
+ {
+ var leftVal = prop.GetValue(left);
+ var rightVal = prop.GetValue(right);
+ var propType = prop.PropertyType;
+ var fqn = string.IsNullOrEmpty(parentFqn) ? prop.Name : string.Join(".", parentFqn, prop.Name);
+
+ if (propType.IsPrimitive || Nullable.GetUnderlyingType(propType) != null || new[] { typeof(string), typeof(decimal) }.Contains(propType))
+ {
+ if (!Equals(leftVal, rightVal))
+ {
+ differences.Add((prop, fqn, leftVal, rightVal));
+ }
+ }
+ else
+ {
+ differences.AddRange(DiffWith(leftVal, rightVal, fqn));
+ }
+ }
+
+ return differences;
+ }
+
+ /// <summary>
/// Returns the directory from the given path, regardless of separator format.
/// </summary>
/// <param name="path">The path.</param>