This repository has been archived by the owner on Apr 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
isit.go
315 lines (282 loc) · 7.31 KB
/
isit.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
package isit
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"regexp"
"strings"
)
// RuleGroup represents a collection of rules
type RuleGroup struct {
Logic string `json:"logic"`
Rules []Rule `json:"rules"`
}
// Rule represents one rule or a sub-collection of rules
type Rule struct {
Property string `json:"property,omitempty"`
Operator string `json:"operator,omitempty"`
Value interface{} `json:"value,omitempty"`
RuleGroup *RuleGroup `json:"rule_group,omitempty"`
}
// NewRuleGroupFromJSON creates a new rule group from JSON
func NewRuleGroupFromJSON(j []byte) (*RuleGroup, error) {
rg := new(RuleGroup)
err := json.Unmarshal(j, rg)
return rg, err
}
// Test runs a rule group against a group of values
func (rg *RuleGroup) Test(values map[string]interface{}) (bool, error) {
logic := strings.ToUpper(rg.Logic)
if logic == `AND` {
return rulesAnd(rg.Rules, values)
} else if logic == `OR` {
return rulesOr(rg.Rules, values)
}
return false, fmt.Errorf(`unsupported logic "%s" logic must be "and" or "or"`, rg.Logic)
}
// And allows two rule groups to be "anded" together
func (rg *RuleGroup) And(andGroup *RuleGroup) *RuleGroup {
newGroup := RuleGroup{
Logic: "and",
Rules: []Rule{
{RuleGroup: rg},
{RuleGroup: andGroup},
},
}
return &newGroup
}
// Or allows two rule groups to be "or" together
func (rg *RuleGroup) Or(orGroup *RuleGroup) *RuleGroup {
newGroup := RuleGroup{
Logic: "or",
Rules: []Rule{
{RuleGroup: rg},
{RuleGroup: orGroup},
},
}
return &newGroup
}
func rulesAnd(rules []Rule, values map[string]interface{}) (bool, error) {
if len(rules) == 0 {
return false, errors.New("A rule group may not have an empty list of rules.")
}
for _, r := range rules {
result, err := ruleTest(r, values)
if err != nil {
return false, err
}
if !result {
return false, nil
}
}
return true, nil
}
func rulesOr(rules []Rule, values map[string]interface{}) (bool, error) {
if len(rules) == 0 {
return false, errors.New("A rule group may not have an empty list of rules.")
}
for _, r := range rules {
result, err := ruleTest(r, values)
if err != nil {
return false, err
}
if result {
return true, nil
}
}
return false, nil
}
func ruleTest(rule Rule, values map[string]interface{}) (bool, error) {
if rule.RuleGroup != nil {
return rule.RuleGroup.Test(values)
}
actual, ok := values[rule.Property]
if !ok {
return false, fmt.Errorf("property %s not found in values", rule.Property)
}
switch t := actual.(type) {
default:
if isSlice(actual) {
if ss, err := toStringSlice(actual); err == nil {
return ruleTestStringSlice(ss, rule)
}
}
return false, fmt.Errorf("unexpected type %T in rule value", t)
case bool:
v, _ := values[rule.Property].(bool)
return ruleTestBool(v, rule)
case string:
v, _ := values[rule.Property].(string)
return ruleTestString(v, rule)
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64:
v, _ := floatFromInterface(values[rule.Property])
return ruleTestNumeric(actual, v, rule)
}
}
func ruleTestStringSlice(ss []string, rule Rule) (bool, error) {
expected, ok := rule.Value.(string)
if !ok {
return false, fmt.Errorf("Invalid rule value type (%T) for property type []string", expected)
}
switch strings.ToUpper(rule.Operator) {
default:
return false, fmt.Errorf("unsupported operator: %s for type []string", rule.Operator)
case "HAS":
return stringSliceContains(ss, expected), nil
case "DOES_NOT_HAVE":
return !stringSliceContains(ss, expected), nil
}
}
func ruleTestNumeric(actual interface{}, v float64, rule Rule) (bool, error) {
expected, err := floatFromInterface(rule.Value)
if err != nil {
return false, err
}
switch strings.ToUpper(rule.Operator) {
default:
return false, fmt.Errorf("unsupported operator: %s for type %T", rule.Operator, actual)
case "EQ":
return v == expected, nil
case "NOT_EQ":
return v != expected, nil
case "GT":
return v > expected, nil
case "GT_EQ":
return v >= expected, nil
case "LT":
return v < expected, nil
case "LT_EQ":
return v <= expected, nil
}
}
func ruleTestString(v string, rule Rule) (bool, error) {
op := strings.ToUpper(rule.Operator)
// in and not_int are special in that the rule value must be a slice of
// strings
if op == "IN" || op == "NOT_IN" {
arr, err := toStringSlice(rule.Value)
if err != nil {
return false, fmt.Errorf(`for the operators "in" and "not_in" the rule value must be []string %T given`, rule.Value)
}
if op == "IN" {
for _, s := range arr {
if v == s {
return true, nil
}
}
return false, nil
}
for _, s := range arr {
if v == s {
return false, nil
}
}
return true, nil
}
// the rest of the string operators expect the rule value to be a string
expected, ok := rule.Value.(string)
if !ok {
return false, fmt.Errorf("type mismatch actual value type string expected type %T", rule.Value)
}
switch op {
default:
return false, fmt.Errorf("unsupported operator: %s for type string", rule.Operator)
case "EQ":
return v == expected, nil
case "NOT_EQ":
return v != expected, nil
case "GT": // TODO are gt, lt, etc... a good idea for string operators?
return v > expected, nil
case "GT_EQ":
return v >= expected, nil
case "LT":
return v < expected, nil
case "LT_EQ":
return v <= expected, nil
case "REGEX":
re, err := regexp.Compile(expected)
if err != nil {
return false, fmt.Errorf("the regex: %s failed to compile", expected)
}
return re.MatchString(v), nil
case "NOT_REGEX":
re, err := regexp.Compile(expected)
if err != nil {
return false, fmt.Errorf("the regex: %s failed to compile", expected)
}
return !re.MatchString(v), nil
}
}
func ruleTestBool(v bool, rule Rule) (bool, error) {
switch strings.ToUpper(rule.Operator) {
default:
return false, fmt.Errorf("unsupported operator: %s for type bool", rule.Operator)
case "EQ", "NOT_EQ":
expected, ok := rule.Value.(bool)
if !ok {
return false, fmt.Errorf("type mismatch actual value type bool expected type %T", rule.Value)
}
if strings.ToUpper(rule.Operator) == "EQ" {
return v == expected, nil
}
return v != expected, nil
}
}
func floatFromInterface(val interface{}) (float64, error) {
switch t := val.(type) {
default:
return 0.0, fmt.Errorf("Expected numeric value, got \"%v\"\n", val)
case float32:
return float64(t), nil
case float64:
return t, nil
case int:
return float64(t), nil
case int8:
return float64(t), nil
case int16:
return float64(t), nil
case int32:
return float64(t), nil
case int64:
return float64(t), nil
case uint:
return float64(t), nil
case uint8:
return float64(t), nil
case uint16:
return float64(t), nil
case uint32:
return float64(t), nil
case uint64:
return float64(t), nil
}
}
func isSlice(o interface{}) bool {
return reflect.ValueOf(o).Kind() == reflect.Slice
}
func toStringSlice(o interface{}) ([]string, error) {
if !isSlice(o) {
return nil, fmt.Errorf("%v is not a slice", o)
}
s := reflect.ValueOf(o)
ret := make([]string, s.Len())
for i := 0; i < s.Len(); i++ {
v := s.Index(i).Interface()
var ok bool
ret[i], ok = v.(string)
if !ok {
return nil, fmt.Errorf("%v is not a string", v)
}
}
return ret, nil
}
func stringSliceContains(ss []string, s string) bool {
for _, v := range ss {
if s == v {
return true
}
}
return false
}