-
Notifications
You must be signed in to change notification settings - Fork 1
/
comparator.go
104 lines (98 loc) · 2.04 KB
/
comparator.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package protopath
func comparator(leftVal any, rightVal any, op operator) bool {
switch lVal := leftVal.(type) {
case int, int64, float32, float64:
switch rVal := rightVal.(type) {
case int, int64, float32, float64:
lValFloat, err := toFloat64(lVal)
if err != nil {
return false
}
rValFloat, err := toFloat64(rVal)
if err != nil {
return false
}
return compareFloat64(lValFloat, rValFloat, op)
default:
return false
}
case string:
rVal, isString := rightVal.(string)
if !isString {
return false
}
return compareString(lVal, rVal, op)
case bool:
rVal, isBool := rightVal.(bool)
if !isBool {
return false
}
return compareBool(lVal, rVal, op)
}
return false
}
func compareString(leftVal, rightVal string, op operator) bool {
switch op {
case greater:
return leftVal > rightVal
case greaterEq:
return leftVal >= rightVal
case less:
return leftVal < rightVal
case lessEq:
return leftVal <= rightVal
case eq:
return leftVal == rightVal
case nEq:
return leftVal != rightVal
}
return false
}
func compareBool(leftVal, rightVal bool, op operator) bool {
switch op {
case greater, greaterEq, less, lessEq:
return false // operation not supported for bool type
case eq:
return leftVal == rightVal
case nEq:
return leftVal != rightVal
}
return false
}
func compareFloat64(leftVal, rightVal float64, op operator) bool {
switch op {
case greater:
return leftVal > rightVal
case greaterEq:
return leftVal >= rightVal
case less:
return leftVal < rightVal
case lessEq:
return leftVal <= rightVal
case eq:
return leftVal == rightVal
case nEq:
return leftVal != rightVal
}
return false
}
func logicalOperation(leftVal, rightVal any, op operator) bool {
var lBoolVal bool
var rBoolVal bool
var isBool bool
lBoolVal, isBool = leftVal.(bool)
if !isBool {
lBoolVal = leftVal != nil
}
rBoolVal, isBool = rightVal.(bool)
if !isBool {
rBoolVal = rightVal != nil
}
switch op {
case or:
return lBoolVal || rBoolVal
case and:
return lBoolVal && rBoolVal
}
return false
}