-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool.go
492 lines (427 loc) · 11.3 KB
/
tool.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package go_tool
import (
"crypto/md5"
"encoding/hex"
"errors"
"reflect"
"runtime"
"strconv"
"strings"
)
func Md5(str string) string {
var m = md5.New()
m.Write([]byte(str))
return hex.EncodeToString(m.Sum(nil))
}
func Uint8sToBytes(src []uint8) []byte {
var dst []byte
for _, b := range src {
dst = append(dst, byte(b))
}
return dst
}
func TryInterfacePtr(data interface{}) reflect.Value {
v := reflect.ValueOf(data)
if v.Kind() == reflect.Ptr {
return v.Elem()
} else {
return v
}
}
func AssetsMapOrS(kind reflect.Kind, msg string) {
if kind != reflect.Map && kind != reflect.Struct {
AssetsError(errors.New(msg))
}
}
func AssetsSlice(kind reflect.Kind, msg string) {
if kind != reflect.Slice {
AssetsError(errors.New(msg))
}
}
func AssetsMap(kind reflect.Kind, msg string) {
if kind != reflect.Map {
AssetsError(errors.New(msg))
}
}
func AssetsPtr(kind reflect.Kind, msg string) {
if kind != reflect.Ptr {
AssetsError(errors.New(msg))
}
}
func StringJoin(ss ...string) string {
buffer := new(strings.Builder)
for _, s := range ss {
buffer.WriteString(s)
}
return buffer.String()
}
func MapToInterface(data interface{}) map[interface{}]interface{} {
v := TryInterfacePtr(data)
AssetsMap(v.Kind(), "map转换接口错误, 非map接口")
keys := v.MapKeys()
dst := make(map[interface{}]interface{}, len(keys))
for _, kV := range keys {
dst[kV.Interface()] = v.MapIndex(kV).Interface()
}
return dst
}
func MapMap(data interface{}, cb func(interface{}) interface{}) interface{} {
v := TryInterfacePtr(data)
AssetsMap(v.Kind(), "map转换接口错误, 非map接口")
keys := v.MapKeys()
dst := make(map[interface{}]interface{}, v.Len())
for _, key := range keys {
dst[key.Type().Name()] = cb(v.MapIndex(key).Interface())
}
return dst
}
func MapKeyFilter(data interface{}, cb func(interface{}) bool) interface{} {
v := TryInterfacePtr(data)
AssetsMap(v.Kind(), "map转换接口错误, 非map接口")
keys := v.MapKeys()
dst := make(map[interface{}]interface{}, v.Len())
for _, key := range keys {
k := key.Interface()
if cb(k) {
dst[k] = v.MapIndex(key).Interface()
}
}
return dst
}
func MapKeyFilterToArray(data interface{}, cb func(interface{}) bool) interface{} {
v := TryInterfacePtr(data)
AssetsMap(v.Kind(), "map转换接口错误, 非map接口")
keys := v.MapKeys()
dst := make([]interface{}, 0, v.Len())
for _, key := range keys {
k := key.Interface()
if cb(k) {
dst = append(dst, v.MapIndex(key).Interface())
}
}
return dst
}
func MapValueFilter(data interface{}, cb func(interface{}) bool) interface{} {
v := TryInterfacePtr(data)
AssetsMap(v.Kind(), "map转换接口错误, 非map接口")
keys := v.MapKeys()
dst := make(map[interface{}]interface{}, v.Len())
for _, key := range keys {
value := v.MapIndex(key).Interface()
if cb(value) {
dst[key.Interface()] = value
}
}
return dst
}
func MapValueFilterToArray(data interface{}, cb func(interface{}) bool) interface{} {
v := TryInterfacePtr(data)
AssetsMap(v.Kind(), "map转换接口错误, 非map接口")
keys := v.MapKeys()
dst := make([]interface{}, 0, v.Len())
for _, key := range keys {
value := v.MapIndex(key).Interface()
if cb(value) {
dst = append(dst, value)
}
}
return dst
}
func MapKeys(data interface{}, cb func(interface{}) interface{}) interface{} {
v := TryInterfacePtr(data)
AssetsMap(v.Kind(), "map转换接口错误, 非map接口")
keys := v.MapKeys()
return ArrayMap(&keys, func(d interface{}) interface{} {
k := d.(reflect.Value).Interface()
if cb != nil {
return cb(k)
}
return d.(reflect.Value).Interface()
})
}
func MapValues(data interface{}, cb func(interface{}) interface{}) interface{} {
v := TryInterfacePtr(data)
AssetsMap(v.Kind(), "map转换接口错误, 非map接口")
keys := v.MapKeys()
return ArrayMap(&keys, func(d interface{}) interface{} {
value := v.MapIndex(d.(reflect.Value)).Interface()
if cb != nil {
return cb(value)
}
return value
})
}
func AssetMapSet(do bool, m interface{}, key interface{}, value interface{}) {
if do {
v := reflect.ValueOf(m)
if v.Kind() != reflect.Ptr {
AssetsSlice(v.Kind(), "非指针, 无法set map")
} else {
obj := v.Elem()
obj.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(value))
}
}
}
func ArrayMap(data interface{}, cb func(interface{}) interface{}) interface{} {
v := TryInterfacePtr(data)
AssetsSlice(v.Kind(), "数组转换接口错误, 非数组接口")
vl := v.Len()
dst := make([]interface{}, vl)
for i := 0; i < vl; i++ {
dst[i] = cb(v.Index(i).Interface())
}
return dst
}
func ArrayFilter(data interface{}, cb func(interface{}) bool) interface{} {
v := TryInterfacePtr(data)
AssetsSlice(v.Kind(), "数组转换接口错误, 非数组接口")
vl := v.Len()
dst := make([]interface{}, 0, vl)
for i := 0; i < vl; i++ {
tmp := v.Index(i).Interface()
if cb(tmp) {
dst = append(dst, tmp)
}
}
return dst
}
func ArrayReduce(data interface{}, cb func(float64, interface{}) float64, start float64) float64 {
v := TryInterfacePtr(data)
AssetsSlice(v.Kind(), "数组转换接口错误, 非数组接口")
vl := v.Len()
for i := 0; i < vl; i++ {
start = cb(start, v.Index(i).Interface())
}
return start
}
func ArrayKeyBy(data interface{}, key string) map[interface{}][]interface{} {
v := TryInterfacePtr(data)
AssetsSlice(v.Kind(), "数组转换接口错误, 非数组接口")
vl := v.Len()
dst := make(map[interface{}][]interface{}, vl)
for i := 0; i < vl; i++ {
tmp := v.Index(i).Interface()
// 相信宝贝你不会存在不存在的情况 - -
k, _ := Get(tmp, key)
dst[k] = append(dst[k], tmp)
}
return dst
}
func ArrayKeyFuncBy(data interface{}, key string, cb func(interface{}) interface{}) map[interface{}][]interface{} {
v := TryInterfacePtr(data)
AssetsSlice(v.Kind(), "数组转换接口错误, 非数组接口")
vl := v.Len()
dst := make(map[interface{}][]interface{}, vl)
for i := 0; i < vl; i++ {
tmp := v.Index(i).Interface()
// 相信宝贝你不会存在不存在的情况 - -
k, _ := Get(tmp, key)
kt := cb(k)
dst[kt] = append(dst[kt], tmp)
}
return dst
}
func ArrayKeyByFunc(data interface{}, key string, cb func(interface{}, interface{}) interface{}) map[interface{}]interface{} {
v := TryInterfacePtr(data)
AssetsSlice(v.Kind(), "数组转换接口错误, 非数组接口")
vl := v.Len()
dst := make(map[interface{}]interface{}, vl)
for i := 0; i < vl; i++ {
tmp := v.Index(i).Interface()
// 相信宝贝你不会存在不存在的情况 - -
k, _ := Get(tmp, key)
dst[k] = cb(dst[k], tmp)
}
return dst
}
func ArrayMakeKey(data interface{}, key string) map[interface{}]interface{} {
v := TryInterfacePtr(data)
AssetsSlice(v.Kind(), "数组转换接口错误, 非数组接口")
vl := v.Len()
dst := make(map[interface{}]interface{}, vl)
for i := 0; i < vl; i++ {
tmp := v.Index(i).Interface()
// 相信宝贝你不会存在不存在的情况 - -
k, _ := Get(tmp, key)
dst[k] = tmp
}
return dst
}
func ArrayMakeKeyFunc(data interface{}, key string, cb func(d interface{}) interface{}) map[interface{}]interface{} {
v := TryInterfacePtr(data)
AssetsSlice(v.Kind(), "数组转换接口错误, 非数组接口")
vl := v.Len()
dst := make(map[interface{}]interface{}, vl)
for i := 0; i < vl; i++ {
tmp := v.Index(i).Interface()
// 相信宝贝你不会存在不存在的情况 - -
k, _ := Get(tmp, key)
dst[k] = cb(tmp)
}
return dst
}
func ArrayFirst(data interface{}) interface{} {
v := TryInterfacePtr(data)
AssetsSlice(v.Kind(), "数组转换接口错误, 非数组接口")
if v.Len() == 0 {
AssetsError(errors.New("数组为空, 无法识别子元素类型"))
}
return v.Index(0).Interface()
}
func GetArrayType(data interface{}) reflect.Type {
return reflect.TypeOf(ArrayFirst(data))
}
func GetTypeFieldNum(t reflect.Type) int {
kind := t.Kind()
AssetsMapOrS(kind, "key提取接口错误, 非可提取接口")
if kind == reflect.Map {
return t.Len()
} else {
return t.NumField()
}
}
func GetTypeFieldBySet(data interface{}, keys []string) []string {
v := reflect.ValueOf(data)
kind := v.Kind()
AssetsMapOrS(kind, "无法提取字段类型")
fieldNum := GetTypeFieldNum(v.Type())
fields := make([]string, 0, fieldNum)
if kind == reflect.Map {
fields = ArrayMap(v.MapKeys(), func(d interface{}) interface{} {
return d.(reflect.Value).Interface()
}).([]string)
} else {
t := reflect.TypeOf(data)
for i := 0; i < fieldNum; i++ {
fields = append(fields, t.Field(i).Name)
}
}
dstKeys := make([]string, 0, fieldNum)
for _, k := range fields {
if !InArray(keys, k) {
dstKeys = append(dstKeys, k)
}
}
return dstKeys
}
func InArray(data interface{}, find interface{}) bool {
v := TryInterfacePtr(data)
AssetsSlice(v.Kind(), "数组转换接口错误, 非数组接口")
dNum := v.Len()
exist := false
for i := 0; i < dNum; i++ {
exist = v.Index(i).Interface() == find
if exist {
break
}
}
return exist
}
type ArrayPickS []map[string]interface{}
func ArrayPick(data interface{}, keys []string) ArrayPickS {
v := TryInterfacePtr(data)
AssetsSlice(v.Kind(), "数组转换接口错误, 非数组接口")
dNum := v.Len()
dst := make(ArrayPickS, dNum)
dstT := GetArrayType(data).Kind()
fieldNum := len(keys)
for i := 0; i < dNum; i++ {
tmp := make(map[string]interface{}, fieldNum)
d := v.Index(i).Interface()
v := reflect.ValueOf(d)
for _, k := range keys {
switch dstT {
case reflect.Map:
{
kV := reflect.ValueOf(k)
if v.MapIndex(kV).IsValid() {
tmp[k] = v.MapIndex(kV).Interface()
} else {
tmp[k] = nil
}
}
case reflect.Struct:
{
if v.FieldByName(k).IsValid() {
tmp[k] = v.FieldByName(k).Interface()
} else {
tmp[k] = nil
}
}
}
}
dst[i] = tmp
}
return dst
}
func ArrayOmit(data interface{}, keys []string) ArrayPickS {
return ArrayPick(data, GetTypeFieldBySet(ArrayFirst(data), keys))
}
func Set() bool {
AssetsError(errors.New("不要尝试这样, 宝贝"))
return true
}
func Has(data interface{}, path string) bool {
_, exist := Get(data, path)
return exist
}
func Get(data interface{}, path string) (interface{}, bool) {
paths := strings.SplitN(path, ".", 2)
v := TryInterfacePtr(data)
kind := v.Kind()
k := paths[0]
shouldNext := len(paths) > 1
if !InArray([]reflect.Kind{reflect.Slice, reflect.Map, reflect.Struct}, kind) {
return nil, false
} else {
item := reflect.Value{}
switch kind {
case reflect.Slice:
{
if index, err := strconv.Atoi(k); err != nil || index > v.Len()-1 || index < 0 {
return nil, false
} else {
item = v.Index(index)
}
}
case reflect.Map:
{
item = v.MapIndex(reflect.ValueOf(k))
}
case reflect.Struct:
{
item = v.FieldByName(k)
}
}
if item.IsValid() {
if shouldNext {
return Get(item.Interface(), paths[1])
}
return item.Interface(), true
} else {
return nil, false
}
}
}
func AssetsReturn(assets bool, s interface{}, t interface{}) interface{} {
if assets {
return s
} else {
return t
}
}
func AssetsError(err error) {
if err != nil {
_, file, line, ok := runtime.Caller(1)
buffer := new(strings.Builder)
if ok {
buffer.WriteString("file: ")
buffer.WriteString(file)
buffer.WriteString(" line: ")
buffer.WriteString(strconv.Itoa(line))
buffer.WriteString(" err: ")
}
buffer.WriteString(err.Error())
panic(errors.New(buffer.String()))
}
}