-
Notifications
You must be signed in to change notification settings - Fork 2
/
collecion.go
158 lines (146 loc) · 3.73 KB
/
collecion.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
package collection
import (
"github.com/goal-web/contracts"
"github.com/goal-web/supports/utils"
"reflect"
)
type Collection[T any] struct {
array []T
mapArray []contracts.Fields
sorter func(i, j int) bool
}
func New[T any](data []T) contracts.Collection[T] {
return &Collection[T]{array: data}
}
func (col *Collection[T]) Index(index int) *T {
if col.Count() > index {
return &col.array[index]
}
return nil
}
func (col *Collection[T]) IsEmpty() bool {
return len(col.array) == 0
}
func (col *Collection[T]) Map(handler any) contracts.Collection[T] {
switch handle := handler.(type) {
case func(fields contracts.Fields):
for _, fields := range col.ToArrayFields() {
handle(fields)
}
case func(fields contracts.Fields, index int): // 聚合函数
for index, fields := range col.ToArrayFields() {
handle(fields, index)
}
case func(fields contracts.Fields) bool: // 用于 where
results := make([]T, 0)
for i, data := range col.ToArrayFields() {
if handle(data) {
results = append(results, col.array[i])
}
}
return New(results)
case func(T):
for _, data := range col.array {
handle(data)
}
case func(i int, fields T):
for i, data := range col.array {
handle(i, data)
}
case func(T) bool:
results := make([]T, 0)
for _, data := range col.array {
if handle(data) {
results = append(results, data)
}
}
return New(results)
case func(int, T) bool:
results := make([]T, 0)
for i, data := range col.array {
if handle(i, data) {
results = append(results, data)
}
}
return New(results)
case func(T) T:
for i, data := range col.array {
col.array[i] = handle(data)
}
col.mapArray = nil
case func(any):
for _, data := range col.array {
handle(data)
}
case func(any) any:
for index, data := range col.array {
result, ok := handle(data).(T)
if ok {
col.array[index] = result
col.mapArray = nil
}
}
default:
handlerType := reflect.TypeOf(handler)
handlerValue := reflect.ValueOf(handler)
argsGetter := func(index int, arg any) []reflect.Value {
return []reflect.Value{}
}
numOut := handlerType.NumOut()
switch handlerType.NumIn() {
case 1:
argsGetter = func(_ int, arg any) []reflect.Value {
return []reflect.Value{col.argumentConvertor(handlerType.In(0), arg)}
}
case 2:
argsGetter = func(index int, arg any) []reflect.Value {
return []reflect.Value{
col.argumentConvertor(handlerType.In(0), arg),
reflect.ValueOf(index),
}
}
case 3:
array := reflect.ValueOf(col.array)
argsGetter = func(index int, arg any) []reflect.Value {
return []reflect.Value{
col.argumentConvertor(handlerType.In(0), arg),
reflect.ValueOf(index),
array,
}
}
}
if numOut > 0 {
newCollection := &Collection[T]{}
for index, data := range col.array {
result := handlerValue.Call(argsGetter(index, data))[0].Interface()
newCollection.array = append(newCollection.array, result.(T))
}
return newCollection
} else {
for index, data := range col.array {
handlerValue.Call(argsGetter(index, data))
}
}
}
return col
}
func (col *Collection[T]) argumentConvertor(argType reflect.Type, arg any) reflect.Value {
switch argType.Kind() {
case reflect.String:
return reflect.ValueOf(utils.ToString(arg, ""))
case reflect.Int:
return reflect.ValueOf(utils.ToInt(arg, 0))
case reflect.Int64:
return reflect.ValueOf(utils.ToInt64(arg, 0))
case reflect.Float64:
return reflect.ValueOf(utils.ToFloat64(arg, 0))
case reflect.Float32:
return reflect.ValueOf(utils.ToFloat(arg, 0))
case reflect.Bool:
return reflect.ValueOf(utils.ToBool(arg, false))
}
if reflect.TypeOf(arg).ConvertibleTo(argType) {
return reflect.ValueOf(arg).Convert(argType)
}
return reflect.ValueOf(arg)
}