|
8 | 8 | ) |
9 | 9 |
|
10 | 10 | const ( |
11 | | - OperatorNameIpAddress = "IpAddress" |
| 11 | + OperatorNameIpAddress = "IpAddress" |
| 12 | + OperatorNameNotIpAddress = "NotIpAddress" |
12 | 13 | ) |
13 | 14 |
|
14 | 15 | var ( |
@@ -36,7 +37,10 @@ type ConditionOperator interface { |
36 | 37 |
|
37 | 38 | // IpAddressOperator handles IP address matching with CIDR notation support |
38 | 39 | // Dynamically checks all field names that contain IP addresses in the condition |
39 | | -type IpAddressOperator struct{} |
| 40 | +type IpAddressOperator struct { |
| 41 | + // negate determines whether to negate the matching result |
| 42 | + negate bool |
| 43 | +} |
40 | 44 |
|
41 | 45 | // Validate implements ConditionOperator. |
42 | 46 | func (op *IpAddressOperator) Validate(fields map[string][]string) error { |
@@ -114,21 +118,24 @@ func (op *IpAddressOperator) Evaluate(fields map[string][]string, conditionCtx * |
114 | 118 | return false, fmt.Errorf("%w in field %s: %s", ErrInvalidIPCIDRFormat, fieldName, value) |
115 | 119 | } |
116 | 120 |
|
117 | | - // If this field didn't match any values, entire condition fails (AND logic between fields) |
118 | | - if !fieldMatched { |
| 121 | + // For IpAddress: fail if field didn't match |
| 122 | + // For NotIpAddress: fail if field matched |
| 123 | + if fieldMatched == op.negate { |
119 | 124 | return false, nil |
120 | 125 | } |
121 | 126 | } |
122 | 127 |
|
123 | | - // All fields matched |
| 128 | + // All fields processed successfully - condition passes |
124 | 129 | return true, nil |
125 | 130 | } |
126 | 131 |
|
127 | 132 | // OperatorFactory returns the appropriate operator for a given operator name |
128 | 133 | func OperatorFactory(operatorName string) (ConditionOperator, error) { |
129 | 134 | switch operatorName { |
130 | 135 | case OperatorNameIpAddress: |
131 | | - return &IpAddressOperator{}, nil |
| 136 | + return &IpAddressOperator{negate: false}, nil |
| 137 | + case OperatorNameNotIpAddress: |
| 138 | + return &IpAddressOperator{negate: true}, nil |
132 | 139 | default: |
133 | 140 | return nil, fmt.Errorf("%w: %s", ErrUnsupportedConditionOperator, operatorName) |
134 | 141 | } |
|
0 commit comments