forked from viant/toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collections.go
735 lines (677 loc) · 20.4 KB
/
collections.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
package toolbox
import (
"fmt"
"github.com/pkg/errors"
"reflect"
"sort"
"strings"
)
//TrueValueProvider is a function that returns true, it takes one parameters which ignores,
//This provider can be used to make map from slice like map[some type]bool
var TrueValueProvider = func(ignore interface{}) bool {
return true
}
//CopyStringValueProvider is a function that returns passed in string
//This provider can be used to make map from slice like map[string]some type
var CopyStringValueProvider = func(source string) string {
return source
}
//ProcessSlice iterates over any slice, it calls handler with each element unless handler returns false,
func ReverseSlice(source interface{}) {
if source == nil {
return
}
sliceValue := reflect.ValueOf(source)
if sliceValue.IsNil() || !sliceValue.IsValid() {
return
}
if sliceValue.Kind() == reflect.Ptr {
sliceValue = sliceValue.Elem()
}
var sliceLen = sliceValue.Len()
if sliceLen <= 1 {
return
}
var j = 0
for i := sliceLen - 1; i >= (sliceLen / 2); i-- {
indexItem := sliceValue.Index(i)
indexItemValue := indexItem.Elem()
if indexItem.Kind() == reflect.Ptr {
sliceValue.Index(i).Set(sliceValue.Index(j).Elem().Addr())
sliceValue.Index(j).Set(indexItemValue.Addr())
} else {
sliceValue.Index(i).Set(sliceValue.Index(j).Elem())
sliceValue.Index(j).Set(indexItemValue)
}
j++
}
}
//ProcessSlice iterates over any slice, it calls handler with each element unless handler returns false,
func ProcessSlice(slice interface{}, handler func(item interface{}) bool) {
//The common cases with reflection for speed
if aSlice, ok := slice.([]interface{}); ok {
for _, item := range aSlice {
if !handler(item) {
break
}
}
return
}
if aSlice, ok := slice.([]map[string]interface{}); ok {
for _, item := range aSlice {
if !handler(item) {
break
}
}
return
}
//The common cases with reflection for speed
if aSlice, ok := slice.([]string); ok {
for _, item := range aSlice {
if !handler(item) {
break
}
}
return
}
//The common cases with reflection for speed
if aSlice, ok := slice.([]int); ok {
for _, item := range aSlice {
if !handler(item) {
break
}
}
return
}
sliceValue := DiscoverValueByKind(reflect.ValueOf(slice), reflect.Slice)
for i := 0; i < sliceValue.Len(); i++ {
if !handler(sliceValue.Index(i).Interface()) {
break
}
}
}
//ProcessSliceWithIndex iterates over any slice, it calls handler with every index and item unless handler returns false
func ProcessSliceWithIndex(slice interface{}, handler func(index int, item interface{}) bool) {
if aSlice, ok := slice.([]interface{}); ok {
for i, item := range aSlice {
if !handler(i, item) {
break
}
}
return
}
if aSlice, ok := slice.([]string); ok {
for i, item := range aSlice {
if !handler(i, item) {
break
}
}
return
}
if aSlice, ok := slice.([]int); ok {
for i, item := range aSlice {
if !handler(i, item) {
break
}
}
return
}
sliceValue := DiscoverValueByKind(reflect.ValueOf(slice), reflect.Slice)
for i := 0; i < sliceValue.Len(); i++ {
if !handler(i, sliceValue.Index(i).Interface()) {
break
}
}
}
//AsSlice converts underlying slice as []interface{}
func AsSlice(sourceSlice interface{}) []interface{} {
var result, ok = sourceSlice.([]interface{})
if ok {
return result
}
if resultPointer, ok := sourceSlice.(*[]interface{}); ok {
return *resultPointer
}
result = make([]interface{}, 0)
CopySliceElements(sourceSlice, &result)
return result
}
//IndexSlice reads passed in slice and applies function that takes a slice item as argument to return a key value.
//passed in resulting map needs to match key type return by a key function, and accept slice item type as argument.
func IndexSlice(slice, resultingMap, keyFunction interface{}) {
mapValue := DiscoverValueByKind(resultingMap, reflect.Map)
ProcessSlice(slice, func(item interface{}) bool {
result := CallFunction(keyFunction, item)
mapValue.SetMapIndex(reflect.ValueOf(result[0]), reflect.ValueOf(item))
return true
})
}
//CopySliceElements appends elements from source slice into target
//This function comes handy if you want to copy from generic []interface{} slice to more specific slice like []string, if source slice element are of the same time
func CopySliceElements(sourceSlice, targetSlicePointer interface{}) {
if aTargetSlicePointer, ok := targetSlicePointer.(*[]interface{}); ok {
ProcessSlice(sourceSlice, func(item interface{}) bool {
*(aTargetSlicePointer) = append(*aTargetSlicePointer, item)
return true
})
return
}
if aTargetSlicePointer, ok := targetSlicePointer.(*[]string); ok {
ProcessSlice(sourceSlice, func(item interface{}) bool {
*(aTargetSlicePointer) = append(*aTargetSlicePointer, AsString(item))
return true
})
return
}
AssertPointerKind(targetSlicePointer, reflect.Slice, "targetSlicePointer")
sliceValue := reflect.ValueOf(targetSlicePointer).Elem()
ProcessSlice(sourceSlice, func(item interface{}) bool {
sliceValue.Set(reflect.Append(sliceValue, reflect.ValueOf(item)))
return true
})
}
//TransformSlice appends transformed elements from source slice into target, transformer take as argument item of source slice and return value of target slice.
func TransformSlice(sourceSlice, targetSlicePointer, transformer interface{}) {
AssertPointerKind(targetSlicePointer, reflect.Slice, "targetSlicePointer")
sliceValue := reflect.ValueOf(targetSlicePointer).Elem()
ProcessSlice(sourceSlice, func(item interface{}) bool {
result := CallFunction(transformer, item)
sliceValue.Set(reflect.Append(sliceValue, reflect.ValueOf(result[0])))
return true
})
}
//FilterSliceElements copies elements from sourceSlice to targetSlice if predicate function returns true. Predicate function needs to accept source slice element type and return true.
func FilterSliceElements(sourceSlice interface{}, predicate interface{}, targetSlicePointer interface{}) {
//The most common case witout reflection
if aTargetSlicePointer, ok := targetSlicePointer.(*[]string); ok {
aPredicate, ok := predicate.(func(item string) bool)
if !ok {
panic("Invalid predicate")
}
ProcessSlice(sourceSlice, func(item interface{}) bool {
if aPredicate(AsString(item)) {
*(aTargetSlicePointer) = append(*aTargetSlicePointer, AsString(item))
}
return true
})
return
}
AssertPointerKind(targetSlicePointer, reflect.Slice, "targetSlicePointer")
slicePointerValue := reflect.ValueOf(targetSlicePointer).Elem()
ProcessSlice(sourceSlice, func(item interface{}) bool {
result := CallFunction(predicate, item)
if AsBoolean(result[0]) {
slicePointerValue.Set(reflect.Append(slicePointerValue, reflect.ValueOf(item)))
}
return true
})
}
//HasSliceAnyElements checks if sourceSlice has any of passed in elements. This method iterates through elements till if finds the first match.
func HasSliceAnyElements(sourceSlice interface{}, elements ...interface{}) (result bool) {
ProcessSlice(sourceSlice, func(item interface{}) bool {
for _, element := range elements {
if item == element {
result = true
return false
}
}
return true
})
return result
}
//SliceToMap reads passed in slice to to apply the key and value function for each item. Result of these calls is placed in the resulting map.
func SliceToMap(sourceSlice, targetMap, keyFunction, valueFunction interface{}) {
//optimized case
if stringBoolMap, ok := targetMap.(map[string]bool); ok {
if stringSlice, ok := sourceSlice.([]string); ok {
if valueFunction, ok := keyFunction.(func(string) bool); ok {
if keyFunction, ok := keyFunction.(func(string) string); ok {
for _, item := range stringSlice {
stringBoolMap[keyFunction(item)] = valueFunction(item)
}
return
}
}
}
}
mapValue := DiscoverValueByKind(targetMap, reflect.Map)
ProcessSlice(sourceSlice, func(item interface{}) bool {
key := CallFunction(keyFunction, item)
value := CallFunction(valueFunction, item)
mapValue.SetMapIndex(reflect.ValueOf(key[0]), reflect.ValueOf(value[0]))
return true
})
}
//GroupSliceElements reads source slice and transfer all values returned by keyFunction to a slice in target map.
func GroupSliceElements(sourceSlice, targetMap, keyFunction interface{}) {
mapValue := DiscoverValueByKind(targetMap, reflect.Map)
mapValueType := mapValue.Type().Elem()
ProcessSlice(sourceSlice, func(item interface{}) bool {
result := CallFunction(keyFunction, item)
keyValue := reflect.ValueOf(result[0])
sliceForThisKey := mapValue.MapIndex(keyValue)
if !sliceForThisKey.IsValid() {
sliceForThisKeyPoiner := reflect.New(mapValueType)
sliceForThisKey = sliceForThisKeyPoiner.Elem()
mapValue.SetMapIndex(keyValue, sliceForThisKey)
}
mapValue.SetMapIndex(keyValue, reflect.Append(sliceForThisKey, reflect.ValueOf(item)))
return true
})
}
//SliceToMultimap reads source slice and transfer all values by valueFunction and returned by keyFunction to a slice in target map.
//Key and value function result type need to agree with target map type.
func SliceToMultimap(sourceSlice, targetMap, keyFunction, valueFunction interface{}) {
mapValue := DiscoverValueByKind(targetMap, reflect.Map)
mapValueType := mapValue.Type().Elem()
ProcessSlice(sourceSlice, func(item interface{}) bool {
keyResult := CallFunction(keyFunction, item)
keyValue := reflect.ValueOf(keyResult[0])
valueResult := CallFunction(valueFunction, item)
value := reflect.ValueOf(valueResult[0])
sliceForThisKey := mapValue.MapIndex(keyValue)
if !sliceForThisKey.IsValid() {
sliceForThisKeyPoiner := reflect.New(mapValueType)
sliceForThisKey = sliceForThisKeyPoiner.Elem()
mapValue.SetMapIndex(keyValue, sliceForThisKey)
}
mapValue.SetMapIndex(keyValue, reflect.Append(sliceForThisKey, value))
return true
})
}
//SetSliceValue sets value at slice index
func SetSliceValue(slice interface{}, index int, value interface{}) {
if aSlice, ok := slice.([]string); ok {
aSlice[index] = AsString(value)
return
}
if aSlice, ok := slice.([]interface{}); ok {
aSlice[index] = value
return
}
sliceValue := DiscoverValueByKind(reflect.ValueOf(slice), reflect.Slice)
sliceValue.Index(index).Set(reflect.ValueOf(value))
}
//GetSliceValue gets value from passed in index
func GetSliceValue(slice interface{}, index int) interface{} {
if aSlice, ok := slice.([]string); ok {
return aSlice[index]
}
if aSlice, ok := slice.([]interface{}); ok {
return aSlice[index]
}
sliceValue := DiscoverValueByKind(reflect.ValueOf(slice), reflect.Slice)
return sliceValue.Index(index).Interface()
}
var errSliceDoesNotHoldKeyValuePairs = errors.New("unable process map, not key value pairs")
//ProcessMap iterates over any map, it calls handler with every key, value pair unless handler returns false.
func ProcessMap(source interface{}, handler func(key, value interface{}) bool) error {
switch aSlice := source.(type) {
case map[string]string:
for key, value := range aSlice {
if !handler(key, value) {
break
}
}
return nil
case map[string]interface{}:
for key, value := range aSlice {
if !handler(key, value) {
break
}
}
return nil
case map[string]bool:
for key, value := range aSlice {
if !handler(key, value) {
break
}
}
return nil
case map[string]int:
for key, value := range aSlice {
if !handler(key, value) {
break
}
}
return nil
case map[interface{}]interface{}:
for key, value := range aSlice {
if !handler(key, value) {
break
}
}
return nil
case map[int]interface{}:
for key, value := range aSlice {
if !handler(key, value) {
break
}
}
return nil
}
if IsSlice(source) {
var err error
var entryMap map[string]interface{}
ProcessSlice(source, func(item interface{}) bool {
entryMap, err = ToMap(item)
if err != nil {
return false
}
var key, value interface{}
key, value, err = entryMapToKeyValue(entryMap)
if err != nil {
return false
}
return handler(key, value)
})
if err != nil {
return errSliceDoesNotHoldKeyValuePairs
}
return nil
}
if !IsMap(source) {
return errSliceDoesNotHoldKeyValuePairs
}
mapValue := DiscoverValueByKind(reflect.ValueOf(source), reflect.Map)
for _, key := range mapValue.MapKeys() {
value := mapValue.MapIndex(key)
if !handler(key.Interface(), value.Interface()) {
break
}
}
return nil
}
//ToMap converts underlying map/struct/[]KV as map[string]interface{}
func ToMap(source interface{}) (map[string]interface{}, error) {
var result map[string]interface{}
switch candidate := source.(type) {
case map[string]interface{}:
return candidate, nil
case *map[string]interface{}:
return *candidate, nil
case map[interface{}]interface{}:
result = make(map[string]interface{})
for k, v := range candidate {
result[AsString(k)] = v
}
return result, nil
}
if IsStruct(source) {
var result = make(map[string]interface{})
if err := DefaultConverter.AssignConverted(&result, source); err != nil {
return nil, err
}
return result, nil
} else if IsSlice(source) {
var result = make(map[string]interface{})
if err := DefaultConverter.AssignConverted(&result, source); err != nil {
return nil, err
}
return result, nil
}
sourceMapValue := reflect.ValueOf(source)
mapType := reflect.TypeOf(result)
if sourceMapValue.Type().AssignableTo(mapType) {
result, ok := sourceMapValue.Convert(mapType).Interface().(map[string]interface{})
if !ok {
return nil, fmt.Errorf("unable to convert: %T to %T", source, map[string]interface{}{})
}
return result, nil
}
result = make(map[string]interface{})
CopyMapEntries(source, result)
return result, nil
}
//AsMap converts underlying map as map[string]interface{}
func AsMap(source interface{}) map[string]interface{} {
if result, err := ToMap(source); err == nil {
return result
}
return nil
}
//CopyMapEntries appends map entry from source map to target map
func CopyMapEntries(sourceMap, targetMap interface{}) {
targetMapValue := reflect.ValueOf(targetMap)
if targetMapValue.Kind() == reflect.Ptr {
targetMapValue = targetMapValue.Elem()
}
if target, ok := targetMap.(map[string]interface{}); ok {
ProcessMap(sourceMap, func(key, value interface{}) bool {
target[AsString(key)] = value
return true
})
return
}
ProcessMap(sourceMap, func(key, value interface{}) bool {
targetMapValue.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(value))
return true
})
}
//MapKeysToSlice appends all map keys to targetSlice
func MapKeysToSlice(sourceMap interface{}, targetSlicePointer interface{}) {
AssertPointerKind(targetSlicePointer, reflect.Slice, "targetSlicePointer")
slicePointerValue := reflect.ValueOf(targetSlicePointer).Elem()
ProcessMap(sourceMap, func(key, value interface{}) bool {
slicePointerValue.Set(reflect.Append(slicePointerValue, reflect.ValueOf(key)))
return true
})
}
//MapKeysToStringSlice creates a string slice from sourceMap keys, keys do not need to be of a string type.
func MapKeysToStringSlice(sourceMap interface{}) []string {
if stringKeyMap, ok := sourceMap.(map[string]interface{}); ok {
var keys = make([]string, 0)
for k := range stringKeyMap {
keys = append(keys, k)
}
return keys
}
var keys = make([]string, 0)
ProcessMap(sourceMap, func(key interface{}, value interface{}) bool {
keys = append(keys, AsString(key))
return true
})
return keys
}
//Process2DSliceInBatches iterates over any 2 dimensional slice, it calls handler with batch.
func Process2DSliceInBatches(slice [][]interface{}, size int, handler func(batchedSlice [][]interface{})) {
batchCount := (len(slice) / size) + 1
fromIndex, toIndex := 0, 0
for i := 0; i < batchCount; i++ {
toIndex = size * (i + 1)
isLastBatch := toIndex >= len(slice)
if isLastBatch {
toIndex = len(slice)
}
handler(slice[fromIndex:toIndex])
fromIndex = toIndex
}
}
//SortStrings creates a new copy of passed in slice and sorts it.
func SortStrings(source []string) []string {
var result = make([]string, 0)
result = append(result, source...)
sort.Strings(result)
return result
}
//JoinAsString joins all items of a slice, with separator, it takes any slice as argument,
func JoinAsString(slice interface{}, separator string) string {
result := ""
ProcessSlice(slice, func(item interface{}) bool {
if len(result) > 0 {
result = result + separator
}
result = fmt.Sprintf("%v%v", result, item)
return true
})
return result
}
//MakeStringMap creates a mapstring]string from string,
func MakeStringMap(text string, valueSeparator string, itemSeparator string) map[string]string {
var result = make(map[string]string)
for _, item := range strings.Split(text, itemSeparator) {
if len(item) == 0 {
continue
}
keyValue := strings.SplitN(item, valueSeparator, 2)
if len(keyValue) == 2 {
result[strings.Trim(keyValue[0], " \t")] = strings.Trim(keyValue[1], " \n\t")
}
}
return result
}
//MakeMap creates a mapstring]interface{} from string,
func MakeMap(text string, valueSeparator string, itemSeparator string) map[string]interface{} {
var result = make(map[string]interface{})
for _, item := range strings.Split(text, itemSeparator) {
if len(item) == 0 {
continue
}
keyValue := strings.SplitN(item, valueSeparator, 2)
if len(keyValue) == 2 {
result[strings.Trim(keyValue[0], " \t")] = strings.Trim(keyValue[1], " \n\t")
}
}
return result
}
//MakeReverseStringMap creates a mapstring]string from string, the values become key, and key values
func MakeReverseStringMap(text string, valueSepartor string, itemSeparator string) map[string]string {
var result = make(map[string]string)
for _, item := range strings.Split(text, itemSeparator) {
if len(item) == 0 {
continue
}
keyValue := strings.SplitN(item, valueSepartor, 2)
if len(keyValue) == 2 {
result[strings.Trim(keyValue[1], " \t")] = strings.Trim(keyValue[0], " \n\t")
}
}
return result
}
func isNilOrEmpty(v interface{}) bool {
if v == nil {
return true
}
switch value := v.(type) {
case string:
if value == "" {
return true
}
case int:
if value == 0 {
return true
}
case map[string]interface{}:
if len(value) == 0 {
return true
}
case map[interface{}]interface{}:
if len(value) == 0 {
return true
}
case []map[string]interface{}:
if len(value) == 0 {
return true
}
case []map[interface{}]interface{}:
if len(value) == 0 {
return true
}
case []interface{}:
if len(value) == 0 {
return true
}
case interface{}:
if value == nil {
return true
}
}
return AsString(v) == ""
}
//CloneNonEmptyMap removes empty keys from map result
func CopyNonEmptyMapEntries(input, output interface{}) (err error) {
var mutator func(k, v interface{})
if aMap, ok := output.(map[interface{}]interface{}); ok {
mutator = func(k, v interface{}) {
aMap[k] = v
}
} else if aMap, ok := output.(map[string]interface{}); ok {
mutator = func(k, v interface{}) {
aMap[AsString(k)] = v
}
} else {
return fmt.Errorf("unsupported map type: %v", output)
}
mapProvider := func(source interface{}) func() interface{} {
if _, ok := source.(map[interface{}]interface{}); ok {
return func() interface{} {
return map[interface{}]interface{}{}
}
}
return func() interface{} {
return map[string]interface{}{}
}
}
ProcessMap(input, func(k, v interface{}) bool {
if isNilOrEmpty(v) {
return true
}
if IsMap(v) {
transformed := mapProvider(v)()
err = CopyNonEmptyMapEntries(v, transformed)
if err != nil {
return false
}
if isNilOrEmpty(transformed) {
return true
}
v = transformed
} else if IsSlice(v) {
aSlice := AsSlice(v)
var transformed = []interface{}{}
for _, item := range aSlice {
if isNilOrEmpty(item) {
continue
}
if IsMap(item) {
transformedItem := mapProvider(item)()
err = CopyNonEmptyMapEntries(item, transformedItem)
if err != nil {
return false
}
if isNilOrEmpty(transformedItem) {
return true
}
transformed = append(transformed, transformedItem)
} else {
transformed = append(transformed, item)
}
}
if len(transformed) == 0 {
return true
}
v = transformed
}
mutator(k, v)
return true
})
return err
}
//DeleteEmptyKeys removes empty keys from map result
func DeleteEmptyKeys(input interface{}) map[string]interface{} {
result := map[string]interface{}{}
err := CopyNonEmptyMapEntries(input, result)
if err == nil {
return result
}
return AsMap(input)
}
//Pairs returns map for pairs.
func Pairs(params ...interface{}) map[string]interface{} {
var result = make(map[string]interface{})
for i := 0; i+1 < len(params); i += 2 {
var key = AsString(params[i])
result[key] = params[i+1]
}
return result
}