-
Notifications
You must be signed in to change notification settings - Fork 7
/
evaluator.go
82 lines (67 loc) · 1.38 KB
/
evaluator.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
75
76
77
78
79
80
81
82
package filter
import (
"fmt"
"reflect"
"github.com/Vonage/gosrvlib/pkg/typeutil"
)
// Evaluator is the interface to provide functions for a filter type.
type Evaluator interface {
// Evaluate determines if two given values match.
Evaluate(value any) bool
}
func isNil(v any) bool {
return typeutil.IsNil(v)
}
//nolint:gocyclo,cyclop
func convertValue(v any) any {
switch v := v.(type) {
case int:
return float64(v)
case int8:
return float64(v)
case int16:
return float64(v)
case int32:
return float64(v)
case int64:
return float64(v)
case uint:
return float64(v)
case uint8:
return float64(v)
case uint16:
return float64(v)
case uint32:
return float64(v)
case uint64:
return float64(v)
case float32:
return float64(v)
}
if s, ok := convertStringValue(v); ok {
return s
}
return v
}
func convertStringValue(v any) (string, bool) {
if v == nil {
return "", false
}
if s, ok := v.(string); ok {
return s, true
}
// Convert string aliases back to string
vv := reflect.ValueOf(v)
st := reflect.TypeOf("")
if !vv.CanConvert(st) {
return "", false
}
return vv.Convert(st).String(), true
}
func convertFloatValue(v any) (float64, error) {
v = convertValue(v)
if reflect.ValueOf(v).Kind() != reflect.Float64 {
return 0, fmt.Errorf("rule value must be numerical (got %v (%v))", v, reflect.TypeOf(v))
}
return reflect.ValueOf(v).Float(), nil
}