forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.go
81 lines (73 loc) · 1.65 KB
/
check.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
package mathutil
// Compare intX,floatX value by given op. returns `srcVal op(=,!=,<,<=,>,>=) dstVal`
//
// Usage:
//
// mathutil.Compare(2, 3, ">") // false
// mathutil.Compare(2, 1.3, ">") // true
// mathutil.Compare(2.2, 1.3, ">") // true
// mathutil.Compare(2.1, 2, ">") // true
func Compare(srcVal, dstVal any, op string) (ok bool) {
if srcVal == nil || dstVal == nil {
return false
}
// float
if srcFlt, ok := srcVal.(float64); ok {
if dstFlt, err := ToFloat(dstVal); err == nil {
return CompFloat(srcFlt, dstFlt, op)
}
return false
}
if srcFlt, ok := srcVal.(float32); ok {
if dstFlt, err := ToFloat(dstVal); err == nil {
return CompFloat(float64(srcFlt), dstFlt, op)
}
return false
}
// as int64
srcInt, err := ToInt64(srcVal)
if err != nil {
return false
}
dstInt, err := ToInt64(dstVal)
if err != nil {
return false
}
return CompInt64(srcInt, dstInt, op)
}
// CompInt64 compare int64, returns the srcI64 op dstI64
func CompInt64(srcI64, dstI64 int64, op string) (ok bool) {
switch op {
case "<", "lt":
ok = srcI64 < dstI64
case "<=", "lte":
ok = srcI64 <= dstI64
case ">", "gt":
ok = srcI64 > dstI64
case ">=", "gte":
ok = srcI64 >= dstI64
case "=", "eq":
ok = srcI64 == dstI64
case "!=", "ne", "neq":
ok = srcI64 != dstI64
}
return
}
// CompFloat compare float64
func CompFloat(srcF64, dstF64 float64, op string) (ok bool) {
switch op {
case "<", "lt":
ok = srcF64 < dstF64
case "<=", "lte":
ok = srcF64 <= dstF64
case ">", "gt":
ok = srcF64 > dstF64
case ">=", "gte":
ok = srcF64 >= dstF64
case "=", "eq":
ok = srcF64 == dstF64
case "!=", "ne", "neq":
ok = srcF64 != dstF64
}
return
}