-
Notifications
You must be signed in to change notification settings - Fork 0
/
iter.go
232 lines (201 loc) · 5.78 KB
/
iter.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
package pick
import (
"errors"
"reflect"
"github.com/moukoublen/pick/internal/errorsx"
)
type iterationOpMeta struct {
Index int
Length int
}
// forEach applies the given operation to each element of the input if it is a collection
// (slice or array), or directly if it is a single item (pointer, interface, or other type).
//
// The function first tries to handle slices of basic types directly by avoiding reflection for performance reasons.
// If the input is not one of the directly handled types, it uses reflection to determine the input type and
// iterates over elements if it is a slice or array. For pointers or interfaces, it dereferences the input and
// applies the operation to the dereferenced value. For other types, it applies the operation directly.
//
// The function uses deferred recovery to capture and return any panic as an error.
func forEach(input any, operation func(item any, meta iterationOpMeta) error) (rErr error) { //nolint:gocyclo
defer errorsx.RecoverPanicToError(&rErr)
// attempt to quick return on slice of basic types by avoiding reflect.
switch cc := input.(type) {
case []any:
return forEachSlice(cc, operation)
case []string:
return forEachSlice(cc, operation)
case []int:
return forEachSlice(cc, operation)
case []int8:
return forEachSlice(cc, operation)
case []int16:
return forEachSlice(cc, operation)
case []int32:
return forEachSlice(cc, operation)
case []int64:
return forEachSlice(cc, operation)
case []uint:
return forEachSlice(cc, operation)
case []uint8:
return forEachSlice(cc, operation)
case []uint16:
return forEachSlice(cc, operation)
case []uint32:
return forEachSlice(cc, operation)
case []uint64:
return forEachSlice(cc, operation)
case []float32:
return forEachSlice(cc, operation)
case []float64:
return forEachSlice(cc, operation)
case []bool:
return forEachSlice(cc, operation)
}
typeOfInput := reflect.TypeOf(input)
if typeOfInput == nil {
return nil // no op
}
kindOfInput := typeOfInput.Kind()
switch kindOfInput {
case reflect.Array, reflect.Slice:
valueOfInput := reflect.ValueOf(input)
length := valueOfInput.Len()
for i := range length {
item := valueOfInput.Index(i)
if err := operation(item.Interface(), iterationOpMeta{Index: i, Length: length}); err != nil {
return err
}
}
return rErr
case reflect.Pointer, reflect.Interface:
valueOfInput := reflect.ValueOf(input)
if valueOfInput.IsNil() {
return rErr
}
// deref
el := valueOfInput.Elem()
if !el.IsValid() {
return rErr
}
// single operation call attempt
return operation(el.Interface(), iterationOpMeta{Index: 0, Length: 1})
default:
// single operation call attempt
return operation(input, iterationOpMeta{Index: 0, Length: 1})
}
}
func forEachSlice[T any](s []T, operation func(item any, meta iterationOpMeta) error) error {
l := len(s)
for i := range s {
if err := operation(s[i], iterationOpMeta{Index: i, Length: l}); err != nil {
return err
}
}
return nil
}
func mapTo[T any](input any, castOp func(item any, meta iterationOpMeta) (T, error)) ([]T, error) {
// quick returns just in case its already slice of T.
if ss, is := input.([]T); is {
return ss, nil
}
return mapFilterTo(input, func(item any, meta iterationOpMeta) (T, bool, error) {
casted, err := castOp(item, meta)
return casted, err == nil, err
})
}
func mapFilterTo[T any](input any, castFilterOp func(item any, meta iterationOpMeta) (T, bool, error)) ([]T, error) {
var castedSlice []T
if input == nil {
return castedSlice, nil
}
err := forEach(input, func(item any, meta iterationOpMeta) error {
casted, keep, err := castFilterOp(item, meta)
switch {
case err != nil:
return err
case !keep:
return nil
default:
if meta.Index == 0 && meta.Length > 0 {
castedSlice = make([]T, 0, meta.Length)
} else if meta.Length == 0 {
return nil
}
castedSlice = append(castedSlice, casted)
return nil
}
})
if err != nil {
return nil, err
}
return castedSlice, nil
}
func mapOpFn[T any](fn func(item any) (T, error)) func(item any, meta iterationOpMeta) (T, error) {
return func(item any, _ iterationOpMeta) (T, error) {
return fn(item)
}
}
// itemLen returns the result of built in len function if the input is of type slice, array, map, string or channel.
// If the input is pointer and not nil, it dereferences the destination.
func itemLen(input any) (l int, rErr error) {
defer errorsx.RecoverPanicToError(&rErr)
// attempt to quick return on slice of basic types by avoiding reflect.
switch cc := input.(type) {
case []any:
return len(cc), nil
case []map[string]any:
return len(cc), nil
case []string:
return len(cc), nil
case []int:
return len(cc), nil
case []int8:
return len(cc), nil
case []int16:
return len(cc), nil
case []int32:
return len(cc), nil
case []int64:
return len(cc), nil
case []uint:
return len(cc), nil
case []uint8:
return len(cc), nil
case []uint16:
return len(cc), nil
case []uint32:
return len(cc), nil
case []uint64:
return len(cc), nil
case []float32:
return len(cc), nil
case []float64:
return len(cc), nil
case []bool:
return len(cc), nil
case string:
return len(cc), nil
case map[string]any:
return len(cc), nil
}
typeOfInput := reflect.TypeOf(input)
if typeOfInput == nil {
return -1, ErrNoLength
}
kindOfInput := typeOfInput.Kind()
switch kindOfInput {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
valueOfInput := reflect.ValueOf(input)
return valueOfInput.Len(), nil
case reflect.Pointer, reflect.Interface:
valueOfInput := reflect.ValueOf(input)
if valueOfInput.IsNil() {
return -1, ErrNoLength
}
elemValue := valueOfInput.Elem()
return itemLen(elemValue.Interface())
}
return -1, ErrNoLength
}
var ErrNoLength = errors.New("type does not have length")