/
array.go
100 lines (80 loc) · 1.81 KB
/
array.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
package array
import (
"bytes"
"container/list"
"fmt"
"reflect"
"github.com/Kretech/xgo/dict"
)
type any = interface{}
// Stream 提供对一组数据集的操作,接口 api 参考自 Laravel 的 Collection 和 Java 的 stream
type Array struct {
*list.List
}
func (a *Array) String() string {
b := bytes.NewBuffer([]byte{})
a.each(func(it *list.Element) {
b.WriteString(fmt.Sprintf("%v", it.Value))
if it.Next() != nil {
b.WriteByte(' ')
}
})
return b.String()
}
func newArray() *Array {
return &Array{
list.New(),
}
}
// Values 通过传入一组任意元素来构造 Array
func Values(elements ...any) *Array {
a := newArray()
for idx, _ := range elements {
a.PushBack(elements[idx])
}
return a
}
// Slice 通过 slice 构造 Array
func Slice(slice any) *Array {
e := reflect.ValueOf(slice)
if e.Kind() != reflect.Slice {
panic("array.Slice() must receive a slice ([]type)")
}
l := newArray()
for i := 0; i < e.Len(); i++ {
l.PushBack(e.Index(i).Interface())
}
return l
}
func (this *Array) KeyBy(field string) *dict.MapDict {
d := dict.NewMapDict()
for it := this.Front(); it != nil; it = it.Next() {
key := getField(it.Value, field)
d.Set(key, it.Value)
}
return d
}
func (this *Array) each(fn func(*list.Element)) {
for it := this.Front(); it != nil; it = it.Next() {
fn(it)
}
}
func getField(v any, field string) any {
elem := reflect.ValueOf(v).Elem()
switch elem.Kind() {
case reflect.Struct:
return getStructField(elem, field)
case reflect.Map:
return getMapField(elem, field)
}
panic("")
}
func getMapField(v reflect.Value, field string) any {
return v.MapIndex(reflect.ValueOf(field)).Interface()
}
func getStructField(v reflect.Value, field string) any {
return v.FieldByName(field).Interface()
}
func toString(v any) string {
return fmt.Sprintf("%v", v)
}