File: match.go

package info (click to toggle)
incus 6.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,392 kB
  • sloc: sh: 16,313; ansic: 3,121; python: 457; makefile: 337; ruby: 51; sql: 50; lisp: 6
file content (278 lines) | stat: -rw-r--r-- 6,954 bytes parent folder | download | duplicates (3)
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
package filter

import (
	"encoding/json"
	"errors"
	"fmt"
	"reflect"
	"regexp"
	"strconv"
	"strings"
)

const stringMultiValueDelimiter = ","

// Match returns true if the given object matches the given filter.
func Match(obj any, set ClauseSet) (bool, error) {
	if set.ParseInt == nil {
		set.ParseInt = DefaultParseInt
	}

	if set.ParseUint == nil {
		set.ParseUint = DefaultParseUint
	}

	if set.ParseString == nil {
		set.ParseString = DefaultParseString
	}

	if set.ParseBool == nil {
		set.ParseBool = DefaultParseBool
	}

	if set.ParseRegexp == nil {
		set.ParseRegexp = DefaultParseRegexp
	}

	if set.ParseStringSlice == nil {
		set.ParseStringSlice = DefaultParseStringSlice
	}

	match := true

	for _, clause := range set.Clauses {
		value := ValueOf(obj, clause.Field)
		clauseMatch, err := set.match(clause, value)
		if err != nil {
			return false, err
		}

		// Finish out logic
		if clause.Not {
			clauseMatch = !clauseMatch
		}

		switch clause.PrevLogical {
		case set.Ops.And:
			match = match && clauseMatch
		case set.Ops.Or:
			match = match || clauseMatch
		default:
			return false, errors.New("unexpected clause operator")
		}
	}

	return match, nil
}

// DefaultParseInt converts the value of the clause to int64.
func DefaultParseInt(c Clause) (int64, error) {
	return strconv.ParseInt(c.Value, 10, 0)
}

// DefaultParseUint converts the value of the clause to Uint64.
func DefaultParseUint(c Clause) (uint64, error) {
	return strconv.ParseUint(c.Value, 10, 0)
}

// DefaultParseString converts the value of the clause to string.
func DefaultParseString(c Clause) (string, error) {
	return c.Value, nil
}

// DefaultParseBool converts the value of the clause to boolean.
func DefaultParseBool(c Clause) (bool, error) {
	return strconv.ParseBool(c.Value)
}

// DefaultParseRegexp converts the value of the clause to regexp.
func DefaultParseRegexp(c Clause) (*regexp.Regexp, error) {
	regexpValue := c.Value
	if !(strings.Contains(regexpValue, "^") || strings.Contains(regexpValue, "$")) {
		regexpValue = "^" + regexpValue + "$"
	}

	return regexp.Compile("(?i)" + regexpValue)
}

// DefaultParseStringSlice converts the value of the clause to a slice of string.
func DefaultParseStringSlice(c Clause) ([]string, error) {
	var val []string

	err := json.Unmarshal([]byte(c.Value), &val)
	if err != nil {
		return nil, err
	}

	return val, nil
}

func (s ClauseSet) match(c Clause, objValue any) (bool, error) {
	var valueStr string
	var valueRegexp *regexp.Regexp
	var valueInt int64
	var valueUint uint64
	var valueBool bool
	var valueSlice []string
	var err error

	// If 'value' is type of string try to test value as a regexp.
	valInfo := reflect.ValueOf(objValue)
	kind := valInfo.Kind()
	switch kind {
	case reflect.String:
		valueStr, err = s.ParseString(c)
		if !strings.Contains(valueStr, ",") {
			valueRegexp, _ = s.ParseRegexp(c)

			if valueRegexp != nil {
				valueStr = ""
			}
		}

	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		valueInt, err = s.ParseInt(c)
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		valueUint, err = s.ParseUint(c)
	case reflect.Bool:
		valueBool, err = s.ParseBool(c)
	case reflect.Slice:
		if reflect.TypeOf(objValue).Elem().Kind() == reflect.String {
			valueSlice, err = s.ParseStringSlice(c)
		} else {
			return false, fmt.Errorf("Invalid slice type %q for field %q", reflect.TypeOf(objValue).Elem().Kind(), c.Field)
		}

	default:
		return false, fmt.Errorf("Invalid type %q for field %q", kind.String(), c.Field)
	}

	if err != nil {
		return false, fmt.Errorf("Failed to parse value: %w", err)
	}

	switch c.Operator {
	case s.Ops.Equals:
		if valueRegexp != nil {
			return valueRegexp.MatchString(objValue.(string)), nil
		}

		switch val := objValue.(type) {
		case string:
			// Comparison is case insensitive.
			for _, curValue := range strings.Split(valueStr, stringMultiValueDelimiter) {
				if strings.EqualFold(val, curValue) {
					return true, nil
				}
			}

			return strings.EqualFold(val, valueStr), nil
		case int, int8, int16, int32, int64:
			return objValue == valueInt, nil
		case uint, uint8, uint16, uint32, uint64:
			return objValue == valueUint, nil
		case bool:
			return objValue == valueBool, nil
		case []string:
			match := func() bool {
				if len(objValue.([]string)) != len(valueSlice) {
					return false
				}

				for k, v := range objValue.([]string) {
					if valueSlice[k] != v {
						return false
					}
				}

				return true
			}()

			return match, nil
		}

	case s.Ops.NotEquals:
		if valueRegexp != nil {
			return !valueRegexp.MatchString(objValue.(string)), nil
		}

		switch val := objValue.(type) {
		case string:
			// Comparison is case insensitive.
			for _, curValue := range strings.Split(valueStr, stringMultiValueDelimiter) {
				if !strings.EqualFold(val, curValue) {
					return true, nil
				}
			}

			return !strings.EqualFold(val, valueStr), nil
		case int, int8, int16, int32, int64:
			return objValue != valueInt, nil
		case uint, uint8, uint16, uint32, uint64:
			return objValue != valueUint, nil
		case bool:
			return objValue != valueBool, nil
		case []string:
			match := func() bool {
				if len(objValue.([]string)) != len(valueSlice) {
					return false
				}

				for k, v := range objValue.([]string) {
					if valueSlice[k] != v {
						return false
					}
				}

				return true
			}()

			return !match, nil
		}

	case s.Ops.GreaterThan:
		switch objValue.(type) {
		case string, bool, []string:
			return false, fmt.Errorf("Invalid operator %q for field %q", c.Operator, c.Field)
		case int, int8, int16, int32, int64:
			return valInfo.Int() > valueInt, nil
		case uint, uint8, uint16, uint32, uint64:
			return valInfo.Uint() > valueUint, nil
		}

	case s.Ops.LessThan:
		switch objValue.(type) {
		case string, bool, []string:
			return false, fmt.Errorf("Invalid operator %q for field %q", c.Operator, c.Field)
		case int, int8, int16, int32, int64:
			return valInfo.Int() < valueInt, nil
		case uint, uint8, uint16, uint32, uint64:
			return valInfo.Uint() < valueUint, nil
		}

	case s.Ops.GreaterEqual:
		switch objValue.(type) {
		case string, bool, []string:
			return false, fmt.Errorf("Invalid operator %q for field %q", c.Operator, c.Field)
		case int, int8, int16, int32, int64:
			return valInfo.Int() >= valueInt, nil
		case uint, uint8, uint16, uint32, uint64:
			return valInfo.Uint() >= valueUint, nil
		}

	case s.Ops.LessEqual:
		switch objValue.(type) {
		case string, bool, []string:
			return false, fmt.Errorf("Invalid operator %q for field %q", c.Operator, c.Field)
		case int, int8, int16, int32, int64:
			return valInfo.Int() <= valueInt, nil
		case uint, uint8, uint16, uint32, uint64:
			return valInfo.Uint() <= valueUint, nil
		}

	default:
		return false, errors.New("Unsupported operation")
	}

	return false, fmt.Errorf("Unsupported filter type %q for field %q", kind.String(), c.Field)
}