github.com/Axway/agent-sdk@v1.1.101/pkg/filter/filter.go (about) 1 package filter 2 3 import ( 4 "strconv" 5 6 log "github.com/Axway/agent-sdk/pkg/util/log" 7 ) 8 9 const ( 10 Dash = "-" 11 DashPlaceHolder = "__DASH__" 12 ) 13 14 // Filter - Interface for filter 15 type Filter interface { 16 Evaluate(tags interface{}) bool 17 } 18 19 // AgentFilter - Represents the filter 20 type AgentFilter struct { 21 filterConditions []Condition 22 } 23 24 var defaultSupportedExpr = []CallType{GETVALUE, MATCHREGEX, CONTAINS, EXISTS, ANY} 25 26 var supportedExpr = defaultSupportedExpr 27 28 // SetSupportedCallExprTypes - Overrides the list of supported condition expression 29 func SetSupportedCallExprTypes(callTypes []CallType) { 30 supportedExpr = defaultSupportedExpr 31 overriddentSupportedExpr := []CallType{} 32 for _, callType := range callTypes { 33 switch callType { 34 case GETVALUE: 35 fallthrough 36 case MATCHREGEX: 37 fallthrough 38 case CONTAINS: 39 fallthrough 40 case EXISTS: 41 fallthrough 42 case ANY: 43 overriddentSupportedExpr = append(overriddentSupportedExpr, callType) 44 } 45 } 46 if len(overriddentSupportedExpr) > 0 { 47 supportedExpr = overriddentSupportedExpr 48 } 49 } 50 51 // NewFilter - Creates a new instance of the filter 52 func NewFilter(filterConfig string) (filter Filter, err error) { 53 conditionParser := NewConditionParser() 54 filterConditions, err := conditionParser.Parse(filterConfig) 55 if err != nil { 56 return 57 } 58 filter = &AgentFilter{ 59 filterConditions: filterConditions, 60 } 61 return 62 } 63 64 // Evaluate - Performs the evaluation of the filter against the data 65 func (af *AgentFilter) Evaluate(tags interface{}) (result bool) { 66 if len(af.filterConditions) > 0 { 67 fd := NewFilterData(tags, nil) 68 for _, filterCondition := range af.filterConditions { 69 result = filterCondition.Evaluate(fd) 70 log.Debug("Filter condition evaluation [Condition: " + filterCondition.String() + ", Result: " + strconv.FormatBool(result) + "]") 71 if result { 72 return 73 } 74 } 75 } else { 76 result = true 77 } 78 return 79 }