-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDisableFeatureMutation.cs
More file actions
46 lines (40 loc) · 1.66 KB
/
Copy pathDisableFeatureMutation.cs
File metadata and controls
46 lines (40 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
39
40
41
42
43
44
45
46
using FeatureFlags.State;
using ModularityKit.Mutator.Abstractions.Changes;
using ModularityKit.Mutator.Abstractions.Context;
using ModularityKit.Mutator.Abstractions.Engine;
using ModularityKit.Mutator.Abstractions.Intent;
using ModularityKit.Mutator.Abstractions.Results;
namespace FeatureFlags.Mutations;
/// <summary>
/// Mutation that disables a feature flag in the current <see cref="FeatureFlagsState"/>.
/// </summary>
internal sealed class DisableFeatureMutation(string featureName, MutationContext context)
: MutationBase<FeatureFlagsState>(
CreateIntent(
operationName: "DisableFeature",
category: "Configuration",
description: "Disables a feature flag.",
riskLevel: MutationRiskLevel.High),
context)
{
public string FeatureName { get; } = featureName;
public override MutationResult<FeatureFlagsState> Apply(FeatureFlagsState state)
{
var newFlags = new Dictionary<string, bool>(state.Flags);
if (newFlags.ContainsKey(FeatureName))
newFlags[FeatureName] = false;
var newState = state with { Flags = newFlags };
return Success(
newState,
StateChange.Modified($"Flags.{FeatureName}", true, false));
}
public override ValidationResult Validate(FeatureFlagsState state)
{
var result = new ValidationResult();
if (string.IsNullOrEmpty(FeatureName))
result.AddError("FeatureName", "Feature name cannot be empty");
if (!state.Flags.ContainsKey(FeatureName))
result.AddError("FeatureName", $"Feature '{FeatureName}' does not exist");
return result;
}
}