Debian Codegraph @

compare.go

raw symbols SCIP
1 package check
2
3 import (
4 "bytes"
5 "fmt"
6 "reflect"
7 "time"
8 )
9
10 type compareFunc func(v1 interface{}, v2 interface{}) (bool, error)
11
12 type valueCompare struct {
13 Name string
14
15 Func compareFunc
16
17 Operator string
18 }
19
20 // v1 and v2 must have the same type
21 // return >0 if v1 > v2
22 // return 0 if v1 = v2
23 // return <0 if v1 < v2
24 // now we only support int, uint, float64, string and []byte comparison
25 func compare(v1 interface{}, v2 interface{}) (int, error) {
26 value1 := reflect.ValueOf(v1)
27 value2 := reflect.ValueOf(v2)
28
29 switch v1.(type) {
30 case int, int8, int16, int32, int64:
31 a1 := value1.Int()
32 a2 := value2.Int()
33 if a1 > a2 {
34 return 1, nil
35 } else if a1 == a2 {
36 return 0, nil
37 }
38 return -1, nil
39 case uint, uint8, uint16, uint32, uint64:
40 a1 := value1.Uint()
41 a2 := value2.Uint()
42 if a1 > a2 {
43 return 1, nil
44 } else if a1 == a2 {
45 return 0, nil
46 }
47 return -1, nil
48 case float32, float64:
49 a1 := value1.Float()
50 a2 := value2.Float()
51 if a1 > a2 {
52 return 1, nil
53 } else if a1 == a2 {
54 return 0, nil
55 }
56 return -1, nil
57 case string:
58 a1 := value1.String()
59 a2 := value2.String()
60 if a1 > a2 {
61 return 1, nil
62 } else if a1 == a2 {
63 return 0, nil
64 }
65 return -1, nil
66 case []byte:
67 a1 := value1.Bytes()
68 a2 := value2.Bytes()
69 return bytes.Compare(a1, a2), nil
70 case time.Time:
71 a1 := v1.(time.Time)
72 a2 := v2.(time.Time)
73 if a1.After(a2) {
74 return 1, nil
75 } else if a1.Equal(a2) {
76 return 0, nil
77 }
78 return -1, nil
79 case time.Duration:
80 a1 := v1.(time.Duration)
81 a2 := v2.(time.Duration)
82 if a1 > a2 {
83 return 1, nil
84 } else if a1 == a2 {
85 return 0, nil
86 }
87 return -1, nil
88 default:
89 return 0, fmt.Errorf("type %T is not supported now", v1)
90 }
91 }
92
93 func less(v1 interface{}, v2 interface{}) (bool, error) {
94 n, err := compare(v1, v2)
95 if err != nil {
96 return false, err
97 }
98
99 return n < 0, nil
100 }
101
102 func lessEqual(v1 interface{}, v2 interface{}) (bool, error) {
103 n, err := compare(v1, v2)
104 if err != nil {
105 return false, err
106 }
107
108 return n <= 0, nil
109 }
110
111 func greater(v1 interface{}, v2 interface{}) (bool, error) {
112 n, err := compare(v1, v2)
113 if err != nil {
114 return false, err
115 }
116
117 return n > 0, nil
118 }
119
120 func greaterEqual(v1 interface{}, v2 interface{}) (bool, error) {
121 n, err := compare(v1, v2)
122 if err != nil {
123 return false, err
124 }
125
126 return n >= 0, nil
127 }
128
129 func (v *valueCompare) Check(params []interface{}, names []string) (bool, string) {
130 if len(params) != 2 {
131 return false, fmt.Sprintf("%s needs 2 arguments", v.Name)
132 }
133
134 v1 := params[0]
135 v2 := params[1]
136 v1Type := reflect.TypeOf(v1)
137 v2Type := reflect.TypeOf(v2)
138
139 if v1Type.Kind() != v2Type.Kind() {
140 return false, fmt.Sprintf("%s needs two same type, but %s != %s", v.Name, v1Type.Kind(), v2Type.Kind())
141 }
142
143 b, err := v.Func(v1, v2)
144 if err != nil {
145 return false, fmt.Sprintf("%s check err %v", v.Name, err)
146 }
147
148 return b, ""
149 }
150
151 func (v *valueCompare) Info() *CheckerInfo {
152 return &CheckerInfo{
153 Name: v.Name,
154 Params: []string{"compare_one", "compare_two"},
155 }
156 }
157
158 var Less = &valueCompare{Name: "Less", Func: less, Operator: "<"}
159 var LessEqual = &valueCompare{Name: "LessEqual", Func: lessEqual, Operator: "<="}
160 var Greater = &valueCompare{Name: "Greater", Func: greater, Operator: ">"}
161 var GreaterEqual = &valueCompare{Name: "GreaterEqual", Func: greaterEqual, Operator: ">="}