-
Notifications
You must be signed in to change notification settings - Fork 11
/
rule.go
409 lines (319 loc) · 9.16 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package roulette
import (
"fmt"
"reflect"
"sort"
"strconv"
"strings"
"sync"
"text/template"
)
// Ruleset ...
type Ruleset interface {
Execute(vals interface{}) error
}
type ruleConfig struct {
delimLeft string
delimRight string
defaultfuncs template.FuncMap
userfuncs template.FuncMap
allfuncs template.FuncMap
expectTypes []string
expectTypesErr error
noResultFunc bool
template *template.Template
templateErr error
}
// Rule is a single rule expression. A rule expression is a valid go text/template
type Rule struct {
Name string `xml:"name,attr"`
Priority int `xml:"priority,attr"`
Expr string `xml:",innerxml"`
config ruleConfig
}
func (r Rule) hasType(typeName string) bool {
j := sort.SearchStrings(r.config.expectTypes, typeName)
return j < len(r.config.expectTypes) && r.config.expectTypes[j] == typeName
}
func (r Rule) isValid(vals interface{}) error {
if r.config.expectTypes == nil || vals == nil {
return r.config.expectTypesErr
}
switch vals.(type) {
case []interface{}:
var typeName string
typedVals := vals.([]interface{})
size := len(typedVals)
if size == 0 || len(r.config.expectTypes) == 0 {
return r.config.expectTypesErr
}
if size == 1 && typedVals[0] == "map[string]interface {}" {
return nil
}
if size < len(r.config.expectTypes) {
return r.config.expectTypesErr
}
foundCount := 0
for _, v := range typedVals {
if reflect.ValueOf(v).Kind() == reflect.Ptr || reflect.ValueOf(v).Kind() == reflect.Interface {
typeName = reflect.TypeOf(v).Elem().String()
} else {
typeName = reflect.TypeOf(v).String()
}
if foundCount == len(r.config.expectTypes) {
return nil
}
if r.hasType(typeName) {
foundCount++
}
}
if foundCount != len(r.config.expectTypes) {
return r.config.expectTypesErr
}
default:
typeName := reflect.TypeOf(vals).Elem().String()
hasType := r.hasType(typeName)
if !hasType {
return r.config.expectTypesErr
}
}
return nil
}
type textTemplateRulesetConfig struct {
result Result
filterTypesArr []string
workflowMatch bool
}
// TextTemplateRuleset is a collection of rules for a valid go type
type TextTemplateRuleset struct {
Name string `xml:"name,attr"`
FilterTypes string `xml:"filterTypes,attr"`
FilterStrict bool `xml:"filterStrict,attr"`
DataKey string `xml:"dataKey,attr"`
ResultKey string `xml:"resultKey,attr"`
Rules []Rule `xml:"rule"`
PrioritiesCount string `xml:"prioritiesCount,attr"`
Workflow string `xml:"workflow,attr"`
config textTemplateRulesetConfig
bytesBuf *bytesPool
mapBuf *mapPool
sameTypeIndex map[int]string
limit int
}
// sort rules by priority
func (t TextTemplateRuleset) Len() int {
return len(t.Rules)
}
func (t TextTemplateRuleset) Swap(i, j int) {
t.Rules[i], t.Rules[j] = t.Rules[j], t.Rules[i]
}
func (t TextTemplateRuleset) Less(i, j int) bool {
return t.Rules[i].Priority < t.Rules[j].Priority
}
func (t TextTemplateRuleset) isValid(vals interface{}) bool {
switch vals.(type) {
case []interface{}:
var typeName string
typedVals := vals.([]interface{})
size := len(typedVals)
if size == 0 || len(t.config.filterTypesArr) == 0 {
return false
}
if t.FilterStrict {
if size < len(t.config.filterTypesArr) {
return false
}
}
for _, v := range vals.([]interface{}) {
if reflect.ValueOf(v).Kind() == reflect.Ptr || reflect.ValueOf(v).Kind() == reflect.Interface {
typeName = reflect.TypeOf(v).Elem().String()
} else {
typeName = reflect.TypeOf(v).String()
}
hasType := t.hasType(typeName)
if t.FilterStrict && !hasType {
return false
}
if hasType {
return true
}
}
break
default:
typeName := reflect.TypeOf(vals).Elem().String()
hasType := t.hasType(typeName)
if t.FilterStrict && !hasType {
return false
}
if hasType {
return true
}
}
return false
}
func (t TextTemplateRuleset) hasType(typeName string) bool {
j := sort.SearchStrings(t.config.filterTypesArr, typeName)
return j < len(t.config.filterTypesArr) && t.config.filterTypesArr[j] == typeName
}
type templateData struct {
sync.RWMutex
data map[string]interface{}
}
func (td *templateData) Get(key string) interface{} {
td.RLock()
defer td.RUnlock()
return td.data[key]
}
func (td *templateData) Set(key string, val interface{}) bool {
td.Lock()
defer td.Unlock()
td.data[key] = val
return true
}
func (t TextTemplateRuleset) getTemplateData(tmplData, userTmplData map[string]interface{}, vals interface{}) {
//fmt.Println("getTemplateData", reflect.TypeOf(vals))
// flatten multiple types in template map so that they can be referred by
// dataKey
valsData := t.mapBuf.get()
defer t.mapBuf.put(valsData)
// index array of same types
typeArrayIndex := make(map[string]int)
switch vals.(type) {
case []interface{}:
nestedMap := t.mapBuf.get()
defer t.mapBuf.put(nestedMap)
indexPkgTypeName := t.bytesBuf.get()
defer t.bytesBuf.put(indexPkgTypeName)
for i, val := range vals.([]interface{}) {
switch val.(type) {
case []string, []int, []int32, []int64, []bool, []float32, []float64, []interface{}:
typ := reflect.TypeOf(val).String()
typeName := strings.Trim(typ, "[]")
typeName = strings.Trim(typ, "{}")
valsData[typeName+"slice"+strconv.Itoa(i)] = val
break
case map[string]int, map[string]string, map[string]bool:
typ := reflect.TypeOf(val).String()
typeName := strings.Trim(typ, "[")
typeName = strings.Trim(typ, "]")
valsData[typeName+strconv.Itoa(i)] = val
break
case map[string]interface{}:
valsData = val.(map[string]interface{})
break
case bool, int, int32, int64, float32, float64:
typeName := reflect.TypeOf(val).String()
valsData[typeName+strconv.Itoa(i)] = val
break
default:
var pkgPath string
var typeName string
if reflect.ValueOf(val).Kind() == reflect.Ptr || reflect.ValueOf(val).Kind() == reflect.Interface {
pkgTypeName := reflect.TypeOf(val).Elem().String()
periodIndex := strings.Index(pkgTypeName, ".")
typeName = pkgTypeName[periodIndex+1:]
pkgPath = pkgTypeName[:periodIndex]
} else {
typeName = reflect.TypeOf(val).String()
}
indexPkgTypeName.WriteString(typeName)
_, ok := typeArrayIndex[typeName]
if !ok {
typeArrayIndex[typeName] = 0
nestedMap[typeName] = val
} else {
typeArrayIndex[typeName]++
}
nextIndex, ok := t.sameTypeIndex[typeArrayIndex[typeName]]
if !ok {
nextIndex = strconv.Itoa(typeArrayIndex[typeName])
t.sameTypeIndex[typeArrayIndex[typeName]] = nextIndex
}
indexPkgTypeName.WriteString(nextIndex)
key := indexPkgTypeName.String()
nestedMap[key] = val
valsData[pkgPath] = nestedMap
indexPkgTypeName.Reset()
}
}
break
default:
pkgTypeName := reflect.TypeOf(vals).Elem().String()
periodIndex := strings.Index(pkgTypeName, ".")
typeName := pkgTypeName[periodIndex+1:]
pkgPath := pkgTypeName[:periodIndex]
//fmt.Println("default", pkgPaths)
valsData[pkgPath] = map[string]interface{}{
typeName: vals,
}
}
valsData[t.ResultKey] = t.config.result
templateData := &templateData{data: userTmplData}
valsData["R"] = templateData
tmplData[t.DataKey] = valsData
//fmt.Println("map", tmplData)
}
var mutex = &sync.RWMutex{}
// Execute ...
func (t TextTemplateRuleset) Execute(vals interface{}) error {
if !t.config.workflowMatch {
//log.Infof("ruleset %s is not valid for the current parser %s %s", t.Name, t.Workflow)
return fmt.Errorf("ruleset %s is not valid for the current parser %s", t.Name, t.Workflow)
}
if !t.isValid(vals) {
// //log.Infof("invalid types %s skipping ruleset %s", types, t.Name)
return fmt.Errorf("invalid types %s skipping ruleset", t.Name)
}
mutex.Lock()
defer mutex.Unlock()
// fmt.Println("types:", types)
tmplData := t.mapBuf.get()
userTmplData := t.mapBuf.get()
defer t.mapBuf.put(tmplData)
defer t.mapBuf.put(userTmplData)
t.getTemplateData(tmplData, userTmplData, vals)
successCount := 0
for i := range t.Rules {
rule := t.Rules[i]
////log.Infof("rule %s", rule.Name)
if rule.config.noResultFunc {
//log.Infof("rule expression contains result func but no type Result interface was set %s", rule.Name)
continue
}
// validate if one of the types exist in the expression.
err := rule.isValid(vals)
if err != nil {
//log.Infof("invalid rule %s, error: %v", rule.Name, err)
continue
}
if rule.config.templateErr != nil {
//log.Infof("invalid rule %s, error: %v", rule.Name, rule.config.templateErr)
continue
}
buf := t.bytesBuf.get()
defer t.bytesBuf.put(buf)
err = rule.config.template.Execute(buf, tmplData)
if err != nil {
//log.Infof("invalid rule %s, error: %v", rule.Name, err)
continue
}
var result bool
res := strings.TrimSpace(buf.String())
result, err = strconv.ParseBool(res)
if err != nil {
////log.Infof("parse result error", err)
continue
}
// n high priority rules successful, break
if result {
//log.Infof("rule passed %s", rule.Name)
successCount++
if successCount == t.limit {
break
}
} else {
//log.Infof("rule failed %s", rule.Name)
}
}
return nil
}