blob: 0c18394fcb4696d4dfa2f7c370428878c3273045 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
2 * Copyright 2019-present Ciena Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package filter
17
18import (
19 "fmt"
20 "reflect"
21 "regexp"
22 "strings"
23)
24
25type Operation int
26
27const (
28 UK Operation = iota
29 EQ
30 NE
31 GT
32 LT
33 GE
34 LE
35 RE
36)
37
38func toOp(op string) Operation {
39 switch op {
40 case "=":
41 return EQ
42 case "!=":
43 return NE
44 case ">":
45 return GT
46 case "<":
47 return LT
48 case ">=":
49 return GE
50 case "<=":
51 return LE
52 case "~":
53 return RE
54 default:
55 return UK
56 }
57}
58
59type FilterTerm struct {
60 Op Operation
61 Value string
62 re *regexp.Regexp
63}
64
65type Filter map[string]FilterTerm
66
David Bainbridge12f036f2019-10-15 22:09:04 +000067var termRE = regexp.MustCompile(`^\s*([a-zA-Z_][.a-zA-Z0-9_]*)\s*(~|<=|>=|<|>|!=|=)\s*(.+)\s*$`)
Zack Williamse940c7a2019-08-21 14:25:39 -070068
69// Parse parses a comma separated list of filter terms
70func Parse(spec string) (Filter, error) {
71 filter := make(map[string]FilterTerm)
72 terms := strings.Split(spec, ",")
73 var err error
74
75 // Each term is in the form <key><op><value>
76 for _, term := range terms {
77 parts := termRE.FindAllStringSubmatch(term, -1)
78 if parts == nil {
79 return nil, fmt.Errorf("Unable to parse filter term '%s'", term)
80 }
81 ft := FilterTerm{
82 Op: toOp(parts[0][2]),
83 Value: parts[0][3],
84 }
85 if ft.Op == RE {
86 ft.re, err = regexp.Compile(ft.Value)
87 if err != nil {
88 return nil, fmt.Errorf("Unable to parse regexp filter value '%s'", ft.Value)
89 }
90 }
91 filter[parts[0][1]] = ft
92 }
93 return filter, nil
94}
95
96func (f Filter) Process(data interface{}) (interface{}, error) {
97 slice := reflect.ValueOf(data)
98 if slice.Kind() != reflect.Slice {
99 if f.Evaluate(data) {
100 return data, nil
101 }
102 return nil, nil
103 }
104
105 var result []interface{}
106
107 for i := 0; i < slice.Len(); i++ {
108 if f.Evaluate(slice.Index(i).Interface()) {
109 result = append(result, slice.Index(i).Interface())
110 }
111 }
112
113 return result, nil
114}
115
Scott Bakered4efab2020-01-13 19:12:25 -0800116// returns False if the filter does not match
117// returns true if the filter does match or the operation is unsupported
118func testField(v FilterTerm, field reflect.Value) bool {
119 switch v.Op {
120 case RE:
121 if !v.re.MatchString(fmt.Sprintf("%v", field)) {
122 return false
123 }
124 case EQ:
125 // This seems to work for most comparisons
126 if fmt.Sprintf("%v", field) != v.Value {
127 return false
128 }
129 case NE:
130 // This seems to work for most comparisons
131 if fmt.Sprintf("%v", field) == v.Value {
132 return false
133 }
134 default:
135 // For unsupported operations, always pass
136 }
137
138 return true
139}
140
Zack Williamse940c7a2019-08-21 14:25:39 -0700141func (f Filter) Evaluate(item interface{}) bool {
142 val := reflect.ValueOf(item)
143
144 for k, v := range f {
145 field := val.FieldByName(k)
146 if !field.IsValid() {
147 return false
148 }
149
Scott Bakered4efab2020-01-13 19:12:25 -0800150 if (field.Kind() == reflect.Slice) || (field.Kind() == reflect.Array) {
151 // For an array, check to see if any item matches
152 someMatch := false
153 for i := 0; i < field.Len(); i++ {
154 arrayElem := field.Index(i)
155 if testField(v, arrayElem) {
156 someMatch = true
157 }
158 }
159 if !someMatch {
Zack Williamse940c7a2019-08-21 14:25:39 -0700160 return false
161 }
Scott Bakered4efab2020-01-13 19:12:25 -0800162 } else {
163 if !testField(v, field) {
Zack Williamse940c7a2019-08-21 14:25:39 -0700164 return false
165 }
Zack Williamse940c7a2019-08-21 14:25:39 -0700166 }
167 }
168 return true
169}