/
filter.go
74 lines (65 loc) · 1.81 KB
/
filter.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
package filter
import (
"strconv"
log "github.com/Axway/agent-sdk/pkg/util/log"
)
// Filter - Interface for filter
type Filter interface {
Evaluate(tags interface{}) bool
}
// AgentFilter - Represents the filter
type AgentFilter struct {
filterConditions []Condition
}
var defaultSupportedExpr = []CallType{GETVALUE, MATCHREGEX, CONTAINS, EXISTS, ANY}
var supportedExpr = defaultSupportedExpr
// SetSupportedCallExprTypes - Overrides the list of supported condition expression
func SetSupportedCallExprTypes(callTypes []CallType) {
supportedExpr = defaultSupportedExpr
overriddentSupportedExpr := []CallType{}
for _, callType := range callTypes {
switch callType {
case GETVALUE:
fallthrough
case MATCHREGEX:
fallthrough
case CONTAINS:
fallthrough
case EXISTS:
fallthrough
case ANY:
overriddentSupportedExpr = append(overriddentSupportedExpr, callType)
}
}
if len(overriddentSupportedExpr) > 0 {
supportedExpr = overriddentSupportedExpr
}
}
// NewFilter - Creates a new instance of the filter
func NewFilter(filterConfig string) (filter Filter, err error) {
conditionParser := NewConditionParser()
filterConditions, err := conditionParser.Parse(filterConfig)
if err != nil {
return
}
filter = &AgentFilter{
filterConditions: filterConditions,
}
return
}
// Evaluate - Performs the evaluation of the filter against the data
func (af *AgentFilter) Evaluate(tags interface{}) (result bool) {
if af.filterConditions != nil && len(af.filterConditions) > 0 {
fd := NewFilterData(tags, nil)
for _, filterCondition := range af.filterConditions {
result = filterCondition.Evaluate(fd)
log.Debug("Filter condition evaluation [Condition: " + filterCondition.String() + ", Result: " + strconv.FormatBool(result) + "]")
if result {
return
}
}
} else {
result = true
}
return
}