1+ using FeatureFlags . State ;
2+ using ModularityKit . Mutator . Abstractions . Changes ;
3+ using ModularityKit . Mutator . Abstractions . Context ;
4+ using ModularityKit . Mutator . Abstractions . Engine ;
5+ using ModularityKit . Mutator . Abstractions . Intent ;
6+ using ModularityKit . Mutator . Abstractions . Results ;
7+
8+ namespace FeatureFlags . Mutations ;
9+
10+ /// <summary>
11+ /// Mutation that enables feature flag in the current <see cref="FeatureFlagsState"/>.
12+ /// </summary>
13+ internal sealed record EnableFeatureMutation ( string FeatureName , MutationContext Context ) : IMutation < FeatureFlagsState >
14+ {
15+ public MutationIntent Intent { get ; } = new ( )
16+ {
17+ OperationName = "EnableFeature" ,
18+ Category = "Security" ,
19+ Tags = new HashSet < string > { "auth" } ,
20+ RiskLevel = MutationRiskLevel . High ,
21+ Description = "Enables a feature flag."
22+ } ;
23+
24+ public MutationResult < FeatureFlagsState > Apply ( FeatureFlagsState state )
25+ {
26+ if ( state . Flags . TryGetValue ( FeatureName , out var oldValue ) && oldValue )
27+ return MutationResult < FeatureFlagsState > . Success ( state , ChangeSet . Empty ) ;
28+
29+ var newFlags = new Dictionary < string , bool > ( state . Flags )
30+ {
31+ [ FeatureName ] = true
32+ } ;
33+ var newState = state with { Flags = newFlags } ;
34+ var changes = ChangeSet . Single (
35+ StateChange . Modified ( $ "Flags.{ FeatureName } ", oldValue , true )
36+ ) ;
37+ return MutationResult < FeatureFlagsState > . Success ( newState , changes ) ;
38+ }
39+
40+ public ValidationResult Validate ( FeatureFlagsState state )
41+ {
42+ var result = new ValidationResult ( ) ;
43+ if ( string . IsNullOrEmpty ( FeatureName ) )
44+ {
45+ result . AddError ( "FeatureName" , "Feature name cannot be empty" ) ;
46+ }
47+ else if ( ! state . Flags . ContainsKey ( FeatureName ) )
48+ {
49+ result . AddError ( "FeatureName" , $ "Feature '{ FeatureName } ' does not exist") ;
50+ }
51+ return result ;
52+ }
53+
54+ public MutationResult < FeatureFlagsState > Simulate ( FeatureFlagsState state ) => Apply ( state ) ;
55+ }
0 commit comments