aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Common/Configuration
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/Configuration
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/Configuration')
-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
3 files changed, 65 insertions, 2 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; }