-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconditions.go
More file actions
346 lines (315 loc) · 9.43 KB
/
conditions.go
File metadata and controls
346 lines (315 loc) · 9.43 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package featurevisor
import (
"regexp"
"strings"
"time"
)
// GetRegex is a function type for getting regex patterns
type GetRegex func(regexString string, regexFlags string) *regexp.Regexp
// PathExists checks if a path exists in a context object
func PathExists(obj map[string]interface{}, path string) bool {
if !strings.Contains(path, ".") {
_, exists := obj[path]
return exists
}
parts := strings.Split(path, ".")
var current interface{} = obj
for _, part := range parts {
if current == nil {
return false
}
if mapValue, ok := current.(map[string]interface{}); ok {
if _, exists := mapValue[part]; !exists {
return false
}
current = mapValue[part]
} else {
return false
}
}
return true
}
// GetValueFromContext extracts a value from a context object using a dot-separated path
func GetValueFromContext(obj map[string]interface{}, path string) interface{} {
if !strings.Contains(path, ".") {
return obj[path]
}
parts := strings.Split(path, ".")
var current interface{} = obj
for _, part := range parts {
if current == nil {
return nil
}
if mapValue, ok := current.(map[string]interface{}); ok {
current = mapValue[part]
} else {
return nil
}
}
return current
}
// ConditionIsMatched checks if a condition is matched given a context
func ConditionIsMatched(
condition PlainCondition,
context Context,
getRegex GetRegex,
) bool {
contextValueFromPath := GetValueFromContext(context, string(condition.Attribute))
// Handle nil values
if condition.Value == nil {
if condition.Operator == OperatorExists {
return contextValueFromPath != nil
} else if condition.Operator == OperatorNotExists {
return contextValueFromPath == nil
}
return false
}
value := *condition.Value
// equals / notEquals
if condition.Operator == OperatorEquals {
return contextValueFromPath == value
} else if condition.Operator == OperatorNotEquals {
return contextValueFromPath != value
}
// before / after (date comparisons)
if condition.Operator == OperatorBefore || condition.Operator == OperatorAfter {
var dateInContext time.Time
var dateInCondition time.Time
var err error
// Parse context value
switch v := contextValueFromPath.(type) {
case time.Time:
dateInContext = v
case string:
dateInContext, err = time.Parse(time.RFC3339, v)
if err != nil {
// Try other common formats
dateInContext, err = time.Parse("2006-01-02", v)
if err != nil {
return false
}
}
default:
return false
}
// Parse condition value
switch v := value.(type) {
case time.Time:
dateInCondition = v
case string:
dateInCondition, err = time.Parse(time.RFC3339, v)
if err != nil {
// Try other common formats
dateInCondition, err = time.Parse("2006-01-02", v)
if err != nil {
return false
}
}
default:
return false
}
if condition.Operator == OperatorBefore {
return dateInContext.Before(dateInCondition)
} else {
return dateInContext.After(dateInCondition)
}
}
// in / notIn (where condition value is an array)
if valueArray, ok := value.([]interface{}); ok {
if contextValueFromPath == nil {
if condition.Operator == OperatorIn {
return false
} else if condition.Operator == OperatorNotIn {
// Check if the path exists in the context first (like PHP implementation)
if !PathExists(context, string(condition.Attribute)) {
return false
}
// null is not in the array, so return true
return true
}
}
// Handle case where context value is also an array
if contextArray, ok := contextValueFromPath.([]interface{}); ok {
// For arrays in context, check if any element from context array is in condition array
if condition.Operator == OperatorIn {
for _, contextItem := range contextArray {
for _, conditionItem := range valueArray {
if contextItem == conditionItem {
return true
}
}
}
return false
} else if condition.Operator == OperatorNotIn {
// For notIn with array context values, return false (like PHP implementation)
// PHP only handles notIn for string, numeric, or null context values
return false
}
} else {
// Context value is a single value
valueInContext := contextValueFromPath
// Only handle in/notIn for string, numeric, or null context values (like PHP implementation)
switch valueInContext.(type) {
case string, int, float64, bool:
if condition.Operator == OperatorIn {
// Check if context value is in the condition's array
for _, item := range valueArray {
if item == valueInContext {
return true
}
}
return false
} else if condition.Operator == OperatorNotIn {
// Check if the path exists in the context first (like PHP implementation)
if !PathExists(context, string(condition.Attribute)) {
return false
}
// Check if context value is NOT in the condition's array
for _, item := range valueArray {
if item == valueInContext {
return false
}
}
return true
}
default:
// For other types (like objects, arrays), don't match in/notIn conditions
return false
}
}
}
// String operations
if contextValueStr, ok := contextValueFromPath.(string); ok {
if valueStr, ok := value.(string); ok {
switch condition.Operator {
case OperatorContains:
return strings.Contains(contextValueStr, valueStr)
case OperatorNotContains:
return !strings.Contains(contextValueStr, valueStr)
case OperatorStartsWith:
return strings.HasPrefix(contextValueStr, valueStr)
case OperatorEndsWith:
return strings.HasSuffix(contextValueStr, valueStr)
case OperatorSemverEquals:
result, err := CompareVersions(contextValueStr, valueStr)
return err == nil && result == 0
case OperatorSemverNotEquals:
result, err := CompareVersions(contextValueStr, valueStr)
return err == nil && result != 0
case OperatorSemverGreaterThan:
result, err := CompareVersions(contextValueStr, valueStr)
return err == nil && result == 1
case OperatorSemverGreaterThanOrEquals:
result, err := CompareVersions(contextValueStr, valueStr)
return err == nil && result >= 0
case OperatorSemverLessThan:
result, err := CompareVersions(contextValueStr, valueStr)
return err == nil && result == -1
case OperatorSemverLessThanOrEquals:
result, err := CompareVersions(contextValueStr, valueStr)
return err == nil && result <= 0
case OperatorMatches:
regexFlags := ""
if condition.RegexFlags != nil {
regexFlags = *condition.RegexFlags
}
regex := getRegex(valueStr, regexFlags)
return regex.MatchString(contextValueStr)
case OperatorNotMatches:
regexFlags := ""
if condition.RegexFlags != nil {
regexFlags = *condition.RegexFlags
}
regex := getRegex(valueStr, regexFlags)
return !regex.MatchString(contextValueStr)
}
}
}
// Numeric operations
if contextValueNum, ok := contextValueFromPath.(float64); ok {
if valueNum, ok := value.(float64); ok {
switch condition.Operator {
case OperatorGreaterThan:
return contextValueNum > valueNum
case OperatorGreaterThanOrEquals:
return contextValueNum >= valueNum
case OperatorLessThan:
return contextValueNum < valueNum
case OperatorLessThanOrEquals:
return contextValueNum <= valueNum
}
}
}
// Handle integer types
if contextValueInt, ok := contextValueFromPath.(int); ok {
if valueInt, ok := value.(int); ok {
switch condition.Operator {
case OperatorGreaterThan:
return contextValueInt > valueInt
case OperatorGreaterThanOrEquals:
return contextValueInt >= valueInt
case OperatorLessThan:
return contextValueInt < valueInt
case OperatorLessThanOrEquals:
return contextValueInt <= valueInt
}
}
}
// Handle mixed numeric types
if contextValueFloat, ok := contextValueFromPath.(float64); ok {
if valueInt, ok := value.(int); ok {
switch condition.Operator {
case OperatorGreaterThan:
return contextValueFloat > float64(valueInt)
case OperatorGreaterThanOrEquals:
return contextValueFloat >= float64(valueInt)
case OperatorLessThan:
return contextValueFloat < float64(valueInt)
case OperatorLessThanOrEquals:
return contextValueFloat <= float64(valueInt)
}
}
}
if contextValueInt, ok := contextValueFromPath.(int); ok {
if valueFloat, ok := value.(float64); ok {
switch condition.Operator {
case OperatorGreaterThan:
return float64(contextValueInt) > valueFloat
case OperatorGreaterThanOrEquals:
return float64(contextValueInt) >= valueFloat
case OperatorLessThan:
return float64(contextValueInt) < valueFloat
case OperatorLessThanOrEquals:
return float64(contextValueInt) <= valueFloat
}
}
}
// exists / notExists
if condition.Operator == OperatorExists {
return contextValueFromPath != nil
} else if condition.Operator == OperatorNotExists {
return contextValueFromPath == nil
}
// includes / notIncludes (where context value is an array)
if contextValueArray, ok := contextValueFromPath.([]interface{}); ok {
if valueStr, ok := value.(string); ok {
switch condition.Operator {
case OperatorIncludes:
for _, item := range contextValueArray {
if itemStr, ok := item.(string); ok && itemStr == valueStr {
return true
}
}
return false
case OperatorNotIncludes:
for _, item := range contextValueArray {
if itemStr, ok := item.(string); ok && itemStr == valueStr {
return false
}
}
return true
}
}
}
return false
}