-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule.go
340 lines (283 loc) · 8.67 KB
/
rule.go
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
package rule
import (
"encoding/json"
"reflect"
"regexp"
"strings"
)
// Operator defines an interface for all operators
type Operator interface {
Apply(fieldValue, ruleValue interface{}) bool
}
// EqualsOperator checks if fieldValue equals ruleValue
type EqualsOperator struct{}
func (o EqualsOperator) Apply(fieldValue interface{}, ruleValue interface{}) bool {
return reflect.DeepEqual(fieldValue, ruleValue)
}
// NotEqualsOperator checks if fieldValue not equals ruleValue
type NotEqualsOperator struct{}
func (o NotEqualsOperator) Apply(fieldValue, ruleValue interface{}) bool {
return !reflect.DeepEqual(fieldValue, ruleValue)
}
// GreaterThanOperator checks if fieldValue is greater than ruleValue
type GreaterThanOperator struct{}
func (o GreaterThanOperator) Apply(fieldValue, ruleValue interface{}) bool {
return compare(fieldValue, ruleValue) > 0
}
// LessThanOperator checks if fieldValue is less than ruleValue
type LessThanOperator struct{}
func (o LessThanOperator) Apply(fieldValue, ruleValue interface{}) bool {
return compare(fieldValue, ruleValue) < 0
}
// GreaterThanInclusiveOperator checks if fieldValue is greater than or equals to ruleValue
type GreaterThanInclusiveOperator struct{}
func (o GreaterThanInclusiveOperator) Apply(fieldValue, ruleValue interface{}) bool {
return compare(fieldValue, ruleValue) >= 0
}
// LessThanInclusiveOperator checks if fieldValue is less than or equals to ruleValue
type LessThanInclusiveOperator struct{}
func (o LessThanInclusiveOperator) Apply(fieldValue, ruleValue interface{}) bool {
return compare(fieldValue, ruleValue) <= 0
}
// InOperator checks if fieldValue is in ruleValue array
type InOperator struct{}
func (o InOperator) Apply(fieldValue, ruleValue interface{}) bool {
return Contains(fieldValue, ruleValue)
}
// NotInOperator checks if fieldValue is not in ruleValue array
type NotInOperator struct{}
func (o NotInOperator) Apply(fieldValue, ruleValue interface{}) bool {
return !Contains(fieldValue, ruleValue)
}
// StartsWithOperator checks if fieldValue starts with ruleValue
type StartsWithOperator struct{}
func (o StartsWithOperator) Apply(fieldValue, ruleValue interface{}) bool {
return strings.HasPrefix(fieldValue.(string), ruleValue.(string))
}
// EndsWithOperator checks if fieldValue ends with ruleValue
type EndsWithOperator struct{}
func (o EndsWithOperator) Apply(fieldValue, ruleValue interface{}) bool {
return strings.HasSuffix(fieldValue.(string), ruleValue.(string))
}
// ContainsOperator checks if fieldValue contains ruleValue
type ContainsOperator struct{}
func (o ContainsOperator) Apply(fieldValue, ruleValue interface{}) bool {
return strings.Contains(fieldValue.(string), ruleValue.(string))
}
// NotContainsOperator checks if fieldValue does not contains ruleValue
type NotContainsOperator struct{}
func (o NotContainsOperator) Apply(fieldValue, ruleValue interface{}) bool {
return !strings.Contains(fieldValue.(string), ruleValue.(string))
}
// RegexOperator checks if fieldValue contains any match of the regular expression pattern
type RegexOperator struct{}
func (o RegexOperator) Apply(fieldValue, ruleValue interface{}) bool {
result, _ := regexp.MatchString(ruleValue.(string), fieldValue.(string))
return result
}
// OperatorFactory to create operators based on string representation
type OperatorFactory struct{}
func (f OperatorFactory) Create(operator string) Operator {
switch operator {
case "equals":
return EqualsOperator{}
case "notEquals":
return NotEqualsOperator{}
case "greaterThan":
return GreaterThanOperator{}
case "lessThan":
return LessThanOperator{}
case "greaterThanInclusive":
return GreaterThanInclusiveOperator{}
case "lessThanInclusive":
return LessThanInclusiveOperator{}
case "in":
return InOperator{}
case "notIn":
return NotInOperator{}
case "startsWith":
return StartsWithOperator{}
case "endsWith":
return EndsWithOperator{}
case "contains":
return ContainsOperator{}
case "notContains":
return NotContainsOperator{}
case "regex":
return RegexOperator{}
default:
return nil
}
}
// Rule represents a single condition
type Rule struct {
Field string `json:"field"`
Operator string `json:"operator"`
Value interface{} `json:"value"`
}
// ConditionSet represents a set of conditions with All/Any logic
type ConditionSet struct {
All []Rule `json:"all"`
Any []Rule `json:"any"`
}
// RuleSet represents the overall rule set with multiple condition sets
type RuleSet struct {
Conditions []ConditionSet `json:"conditions"`
}
// contains checks if a value is in an array of either strings or integers
func Contains(value, array interface{}) bool {
arr := reflect.ValueOf(array)
switch reflect.TypeOf(array).Kind() {
case reflect.Slice, reflect.Array:
for i := 0; i < arr.Len(); i++ {
if reflect.DeepEqual(arr.Index(i).Interface(), value) {
return true
}
}
}
return false
}
// compare compares two interface{} values
func compare(a, b interface{}) int {
switch a := a.(type) {
case float64:
b := b.(float64)
return compareValues(a, b)
case int:
b := b.(int)
return compareValues(a, b)
default:
return 0
}
}
// compareValues compares two values of the same type
func compareValues[T float64 | string | int](a, b T) int {
if a > b {
return 1
} else if a < b {
return -1
}
return 0
}
// RuleChecker checks rules against an object
type RuleChecker struct {
OperatorFactory OperatorFactory
}
func (rc RuleChecker) CheckRule(obj map[string]interface{}, rule Rule, custom map[string]CustomOperation) bool {
var fieldValue interface{}
var exists bool
if strings.HasPrefix(rule.Field, "external") {
fields := strings.Split(rule.Field, ".")
field := fields[1]
operation, exists := custom[field]
if !exists {
return false
}
fieldValue = operation.Execute(obj, rule.Field)
} else {
fieldValue, exists = obj[rule.Field]
if !exists {
return false
}
}
if strings.HasPrefix(rule.Operator, "custom") {
fields := strings.Split(rule.Operator, ".")
field := fields[1]
operation, exists := custom[field]
if !exists {
return false
}
fieldValue = operation.Execute(obj[rule.Field], rule.Value)
return fieldValue == true
} else {
operator := rc.OperatorFactory.Create(rule.Operator)
if operator == nil {
return false
}
return operator.Apply(fieldValue, rule.Value)
}
}
// ConditionSetChecker checks condition sets against an object
type ConditionSetChecker struct {
RuleChecker RuleChecker
}
func (cc ConditionSetChecker) CheckConditionSet(obj map[string]interface{}, conditionSet ConditionSet, custom map[string]CustomOperation) bool {
allChan := make(chan bool)
anyChan := make(chan bool)
// Check "all" conditions in parallel
go func() {
for _, rule := range conditionSet.All {
if !cc.RuleChecker.CheckRule(obj, rule, custom) {
allChan <- false
close(allChan)
return
}
}
allChan <- true
close(allChan)
}()
// Check "any" conditions in parallel
go func() {
for _, rule := range conditionSet.Any {
if cc.RuleChecker.CheckRule(obj, rule, custom) {
anyChan <- true
close(anyChan)
return
}
}
anyChan <- false
close(anyChan)
}()
allPass := true
anyPass := false
if len(conditionSet.All) > 0 {
allPass = <-allChan
}
if len(conditionSet.Any) > 0 {
anyPass = <-anyChan
} else {
anyPass = true
}
return allPass && anyPass
}
// RuleSetChecker checks rule sets against an object
type RuleSetChecker struct {
ConditionSetChecker ConditionSetChecker
}
func (rsc RuleSetChecker) CheckRuleSet(obj map[string]interface{}, ruleSet RuleSet, custom map[string]CustomOperation) bool {
for _, conditionSet := range ruleSet.Conditions {
if !rsc.ConditionSetChecker.CheckConditionSet(obj, conditionSet, custom) {
return false
}
}
return true
}
// Execute evaluates the ruleset based on the input data
func Execute(input interface{}, rules string, custom map[string]CustomOperation) bool {
var objs map[string]interface{}
switch data := input.(type) {
case string:
// If input is JSON string, parse it
if err := json.Unmarshal([]byte(data), &objs); err != nil {
return false
}
case map[string]interface{}:
// If input is already a map, use it directly
objs = data
default:
return false
}
var ruleSet RuleSet
if err := json.Unmarshal([]byte(rules), &ruleSet); err != nil {
return false
}
operatorFactory := OperatorFactory{}
ruleChecker := RuleChecker{OperatorFactory: operatorFactory}
conditionSetChecker := ConditionSetChecker{RuleChecker: ruleChecker}
ruleSetChecker := RuleSetChecker{ConditionSetChecker: conditionSetChecker}
return ruleSetChecker.CheckRuleSet(objs, ruleSet, custom)
}
// CustomOperation defines the interface for custom operations
type CustomOperation interface {
Execute(input, value interface{}) interface{}
}