-
Notifications
You must be signed in to change notification settings - Fork 4
/
internal.go
272 lines (266 loc) · 4.44 KB
/
internal.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
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
package datatable
import (
"fmt"
"reflect"
"time"
)
func safeToStrings(s ...interface{}) []string {
r := make([]string, len(s), len(s))
for i, v := range s {
r[i] = safeToString(v)
}
return r
}
func safeToString(s interface{}) string {
if s == nil {
return ""
}
switch r := s.(type) {
case string:
return r
case []byte:
return string(r)
default:
return fmt.Sprintf("%v", s)
}
}
//0-equ -1 less 1 large
func cmpValue(v1, v2 interface{}) int {
switch v1.(type) {
case []interface{}:
for i, e1 := range v1.([]interface{}) {
if i >= len(v2.([]interface{})) {
return 1
}
oneCmp := cmpValue(e1, v2.([]interface{})[i])
if oneCmp == 0 {
continue
}
return oneCmp
}
if len(v1.([]interface{})) == len(v2.([]interface{})) {
return 0
}
return -1
case string:
if v1.(string) == v2.(string) {
return 0
}
if v1.(string) < v2.(string) {
return -1
}
return 1
case bool:
if v1.(bool) && v2.(bool) || !v1.(bool) && !v2.(bool) {
return 0
}
if !v1.(bool) && v2.(bool) {
return -1
}
return 1
case float64:
if v1.(float64) == v2.(float64) {
return 0
}
if v1.(float64) < v2.(float64) {
return -1
}
return 1
case byte:
if v1.(byte) == v2.(byte) {
return 0
}
if v1.(byte) < v2.(byte) {
return -1
}
return 1
case int64:
if v1.(int64) == v2.(int64) {
return 0
}
if v1.(int64) < v2.(int64) {
return -1
}
return 1
case int:
if v1.(int) == v2.(int) {
return 0
}
if v1.(int) < v2.(int) {
return -1
}
return 1
case time.Time:
if v1.(time.Time).Equal(v2.(time.Time)) {
return 0
}
if v1.(time.Time).Before(v2.(time.Time)) {
return -1
}
return 1
case []byte:
return cmpBytea(v1.([]byte), v2.([]byte))
case []string:
return cmpStringSlice(v1.([]string), v2.([]string))
case []bool:
return cmpBoolSlice(v1.([]bool), v2.([]bool))
case []float64:
return cmpFloat64Slice(v1.([]float64), v2.([]float64))
case []float32:
return cmpFloat32Slice(v1.([]float32), v2.([]float32))
case []int64:
return cmpInt64Slice(v1.([]int64), v2.([]int64))
case []int:
return cmpIntSlice(v1.([]int), v2.([]int))
case []time.Time:
return cmpTimeSlice(v1.([]time.Time), v2.([]time.Time))
case [][]byte:
return cmpByteaSlice(v1.([][]byte), v2.([][]byte))
default:
panic(PrimaryKeyTypeError(reflect.TypeOf(v1).String()))
}
}
func cmpStringSlice(v1, v2 []string) int {
for i, e1 := range v1 {
if i >= len(v2) {
return 1
}
oneCmp := cmpValue(e1, v2[i])
if oneCmp == 0 {
continue
}
return oneCmp
}
if len(v1) == len(v2) {
return 0
}
return -1
}
func cmpBoolSlice(v1, v2 []bool) int {
for i, e1 := range v1 {
if i >= len(v2) {
return 1
}
oneCmp := cmpValue(e1, v2[i])
if oneCmp == 0 {
continue
}
return oneCmp
}
if len(v1) == len(v2) {
return 0
}
return -1
}
func cmpIntSlice(v1, v2 []int) int {
for i, e1 := range v1 {
if i >= len(v2) {
return 1
}
oneCmp := cmpValue(e1, v2[i])
if oneCmp == 0 {
continue
}
return oneCmp
}
if len(v1) == len(v2) {
return 0
}
return -1
}
func cmpInt64Slice(v1, v2 []int64) int {
for i, e1 := range v1 {
if i >= len(v2) {
return 1
}
oneCmp := cmpValue(e1, v2[i])
if oneCmp == 0 {
continue
}
return oneCmp
}
if len(v1) == len(v2) {
return 0
}
return -1
}
func cmpFloat64Slice(v1, v2 []float64) int {
for i, e1 := range v1 {
if i >= len(v2) {
return 1
}
oneCmp := cmpValue(e1, v2[i])
if oneCmp == 0 {
continue
}
return oneCmp
}
if len(v1) == len(v2) {
return 0
}
return -1
}
func cmpFloat32Slice(v1, v2 []float32) int {
for i, e1 := range v1 {
if i >= len(v2) {
return 1
}
oneCmp := cmpValue(e1, v2[i])
if oneCmp == 0 {
continue
}
return oneCmp
}
if len(v1) == len(v2) {
return 0
}
return -1
}
func cmpTimeSlice(v1, v2 []time.Time) int {
for i, e1 := range v1 {
if i >= len(v2) {
return 1
}
oneCmp := cmpValue(e1, v2[i])
if oneCmp == 0 {
continue
}
return oneCmp
}
if len(v1) == len(v2) {
return 0
}
return -1
}
func cmpBytea(v1, v2 []byte) int {
for i, e1 := range v1 {
if i >= len(v2) {
return 1
}
oneCmp := cmpValue(e1, v2[i])
if oneCmp == 0 {
continue
}
return oneCmp
}
if len(v1) == len(v2) {
return 0
}
return -1
}
func cmpByteaSlice(v1, v2 [][]byte) int {
for i, e1 := range v1 {
if i >= len(v2) {
return 1
}
oneCmp := cmpValue(e1, v2[i])
if oneCmp == 0 {
continue
}
return oneCmp
}
if len(v1) == len(v2) {
return 0
}
return -1
}