-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_FeatureConfig.cs
More file actions
38 lines (33 loc) · 1.66 KB
/
_FeatureConfig.cs
File metadata and controls
38 lines (33 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
namespace CasCap.Models;
/// <summary>Strongly-typed configuration for the comma-separated feature-flag string.</summary>
/// <remarks>
/// Binds from <c>CasCap:FeatureConfig:EnabledFeatures</c> (environment variable or
/// <c>appsettings.json</c>). The raw value is a comma-separated list of feature names
/// (e.g. <c>"Knx,Fronius,DoorBird"</c>) which <see cref="GetEnabledFeatures"/> splits
/// into a case-insensitive <see cref="HashSet{T}"/>.
/// </remarks>
public record FeatureConfig : IAppConfig
{
/// <inheritdoc/>
public static string ConfigurationSectionName => $"{nameof(CasCap)}:{nameof(FeatureConfig)}";
/// <summary>Comma-separated list of enabled feature names.</summary>
[Required]
public required string EnabledFeatures { get; init; }
/// <summary>Parses <see cref="EnabledFeatures"/> into a case-insensitive set of feature names.</summary>
/// <exception cref="InvalidOperationException">
/// Thrown when <see cref="EnabledFeatures"/> contains a value not present in
/// <see cref="FeatureNames.ValidNames"/>.
/// </exception>
public HashSet<string> GetEnabledFeatures()
{
var features = EnabledFeatures
.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.ToHashSet(StringComparer.OrdinalIgnoreCase);
var unknown = features.Where(f => !FeatureNames.ValidNames.Contains(f)).ToList();
if (unknown.Count > 0)
throw new InvalidOperationException(
$"Unrecognised feature name(s): {string.Join(", ", unknown)}. " +
$"Valid names: {string.Join(", ", FeatureNames.ValidNames)}.");
return features;
}
}