Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package filter |
| 17 | |
| 18 | import ( |
| 19 | "fmt" |
| 20 | "reflect" |
| 21 | "regexp" |
| 22 | "strings" |
| 23 | ) |
| 24 | |
| 25 | type Operation int |
| 26 | |
| 27 | const ( |
| 28 | UK Operation = iota |
| 29 | EQ |
| 30 | NE |
| 31 | GT |
| 32 | LT |
| 33 | GE |
| 34 | LE |
| 35 | RE |
| 36 | ) |
| 37 | |
| 38 | func 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 | |
| 59 | type FilterTerm struct { |
| 60 | Op Operation |
| 61 | Value string |
| 62 | re *regexp.Regexp |
| 63 | } |
| 64 | |
| 65 | type Filter map[string]FilterTerm |
| 66 | |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 67 | var termRE = regexp.MustCompile(`^\s*([a-zA-Z_][.a-zA-Z0-9_]*)\s*(~|<=|>=|<|>|!=|=)\s*(.+)\s*$`) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 68 | |
| 69 | // Parse parses a comma separated list of filter terms |
| 70 | func 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 | |
| 96 | func (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 | |
| 116 | func (f Filter) Evaluate(item interface{}) bool { |
| 117 | val := reflect.ValueOf(item) |
| 118 | |
| 119 | for k, v := range f { |
| 120 | field := val.FieldByName(k) |
| 121 | if !field.IsValid() { |
| 122 | return false |
| 123 | } |
| 124 | |
| 125 | switch v.Op { |
| 126 | case RE: |
| 127 | if !v.re.MatchString(fmt.Sprintf("%v", field)) { |
| 128 | return false |
| 129 | } |
| 130 | case EQ: |
| 131 | // This seems to work for most comparisons |
| 132 | if fmt.Sprintf("%v", field) != v.Value { |
| 133 | return false |
| 134 | } |
David Bainbridge | 4b7bafd | 2019-12-12 19:47:47 +0000 | [diff] [blame] | 135 | case NE: |
| 136 | // This seems to work for most comparisons |
| 137 | if fmt.Sprintf("%v", field) == v.Value { |
| 138 | return false |
| 139 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 140 | default: |
| 141 | // For unsupported operations, always pass |
| 142 | } |
| 143 | } |
| 144 | return true |
| 145 | } |