diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-01-25 18:46:13 -0600 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-01-25 18:46:13 -0600 |
| commit | 3834633fffc784bb06d4e69459c8c4748f0b6ae9 (patch) | |
| tree | 3496b8dbf2dc08770c0e48908f6703dfce58bfbe | |
| parent | 5d0d9eff5b21c30563c125d54a1be88ca57b2398 (diff) | |
| download | slskd-3834633fffc784bb06d4e69459c8c4748f0b6ae9.tar.gz | |
document patch, add extension to validate
| -rw-r--r-- | src/slskd/Common/Validation/Extensions.cs | 20 | ||||
| -rw-r--r-- | src/slskd/Core/OptionsPatch.cs | 28 |
2 files changed, 46 insertions, 2 deletions
diff --git a/src/slskd/Common/Validation/Extensions.cs b/src/slskd/Common/Validation/Extensions.cs index b7e396a3..6233d53b 100644 --- a/src/slskd/Common/Validation/Extensions.cs +++ b/src/slskd/Common/Validation/Extensions.cs @@ -44,5 +44,25 @@ namespace slskd.Validation return true; } + + /// <summary> + /// Validates options. + /// </summary> + /// <param name="patch">The options patch instance to validate.</param> + /// <param name="result">The result of the validation, if invalid.</param> + /// <returns>A value indicating whether the instance is valid.</returns> + public static bool TryValidate(this OptionsPatch patch, out CompositeValidationResult result) + { + result = null; + var results = new List<ValidationResult>(); + + if (!Validator.TryValidateObject(patch, new ValidationContext(patch), results, true)) + { + result = new CompositeValidationResult("Invalid configuration", results); + return false; + } + + return true; + } } }
\ No newline at end of file diff --git a/src/slskd/Core/OptionsPatch.cs b/src/slskd/Core/OptionsPatch.cs index 636264a6..6eee75e3 100644 --- a/src/slskd/Core/OptionsPatch.cs +++ b/src/slskd/Core/OptionsPatch.cs @@ -20,10 +20,32 @@ namespace slskd using System.ComponentModel.DataAnnotations; using slskd.Validation; + /// <summary> + /// Run-time patch for application <see cref="Options"/>. + /// </summary> + /// <remarks> + /// <para> + /// The values specified in this patch are applied at run-time only, are volatile (lost when the application restarts), + /// and take precedence over all other options, regardless of which method was used to define them. + /// </para> + /// <para> + /// Only options that can be applied while the application is running can be patched, given the nature of how + /// a patch is applied. + /// </para> + /// <para> + /// Every property in this class must be nullable, and must have a null default value; the application + /// selectively applies the patch using only information explicitly supplied. + /// </para> + /// </remarks> public class OptionsPatch { + public static OptionsPatch Current { get; private set; } = new(); + + [Validate] public SoulseekOptionsPatch Soulseek { get; init; } = null; + public static void SetCurrent(OptionsPatch patch) => Current = patch; + /// <summary> /// Soulseek client options. /// </summary> @@ -33,13 +55,15 @@ namespace slskd /// Gets the local IP address on which to listen for incoming connections. /// </summary> [IPAddress] - public string? ListenIpAddress { get; init; } = "0.0.0.0"; +#pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. + public string? ListenIpAddress { get; init; } = null; +#pragma warning restore CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. /// <summary> /// Gets the port on which to listen for incoming connections. /// </summary> [Range(1024, 65535)] - public int? ListenPort { get; init; } = 50300; + public int? ListenPort { get; init; } = null; } } }
\ No newline at end of file |
