-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
264 lines (223 loc) · 7.15 KB
/
root.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
package pick
import (
"errors"
"fmt"
"reflect"
)
// This file contains the top-level functions that operates to `Picker` and `SelectorMustAPI`
//
// Top level functions that use default API.
//
// Each iterates over all elements selected by the given selector and applies the provided operation function to each element.
// It returns An error if any step in the selection or operation process fails, otherwise nil.
func Each(p *Picker, selector string, operation func(index int, p *Picker, totalLength int) error) error {
item, err := p.Any(selector)
if err != nil {
return err
}
return forEach(
item,
func(item any, meta iterationOpMeta) error {
return operation(meta.Index, p.Wrap(item), meta.Length)
},
)
}
//nolint:ireturn
func Map[Output any](p *Picker, selector string, transform func(*Picker) (Output, error)) ([]Output, error) {
item, err := p.Any(selector)
if err != nil {
return nil, err
}
return mapTo(
item,
func(item any, _ iterationOpMeta) (Output, error) {
return transform(p.Wrap(item))
},
)
}
//nolint:ireturn
func MapFilter[Output any](p *Picker, selector string, transform func(*Picker) (Output, bool, error)) ([]Output, error) {
item, err := p.Any(selector)
if err != nil {
return nil, err
}
return mapFilterTo(
item,
func(item any, _ iterationOpMeta) (Output, bool, error) {
return transform(p.Wrap(item))
},
)
}
//nolint:ireturn
func FlatMap[Output any](p *Picker, selector string, transform func(*Picker) ([]Output, error)) ([]Output, error) {
item, err := p.Any(selector)
if err != nil {
return nil, err
}
doubleSlice, err := mapTo(
item,
func(item any, _ iterationOpMeta) ([]Output, error) {
return transform(p.Wrap(item))
},
)
return flatten[Output](doubleSlice), err
}
// Path traverses with the provided path and if found,
// it resolves the cast type from the generic type.
func Path[Output any](p *Picker, path ...Key) (Output, error) { //nolint:ireturn
item, err := p.Path(path)
if err != nil {
var o Output
return o, err
}
var defaultValue Output
return castAs(p.Caster, item, defaultValue)
}
// OrDefault will return the default value if any error occurs. If the error is ErrFieldNotFound the error will not be returned.
func OrDefault[Output any](p *Picker, selector string, defaultValue Output) (Output, error) { //nolint:ireturn
item, err := p.Any(selector)
if err != nil {
if errors.Is(err, ErrFieldNotFound) {
return defaultValue, nil
}
return defaultValue, err
}
return castAs(p.Caster, item, defaultValue)
}
// Get parses the selector, traverses with the provided path and if found,
// it resolves the cast type from the generic type.
func Get[Output any](p *Picker, selector string) (Output, error) { //nolint:ireturn
var defaultValue Output
item, err := p.Any(selector)
if err != nil {
return defaultValue, err
}
return castAs(p.Caster, item, defaultValue)
}
//
// Top level functions that use must API. (They have the `Must` prefix in name)
//
// MustEach applies operation function to each element of the given selector.
// The operation functions receives the index of the element, a SelectorMustAPI
// and the total length of the slice (or 1 if input is a single element and not a slice).
func MustEach(a SelectorMustAPI, selector string, operation func(index int, item SelectorMustAPI, length int) error) {
item, path, err := parseSelectorAndTraverse(a.Picker, selector)
if err != nil {
a.gather(selector, err)
return
}
err = forEach(item, func(item any, meta iterationOpMeta) error {
opErr := operation(meta.Index, a.Wrap(item), meta.Length)
if opErr != nil {
path = append(path, Index(meta.Index))
a.gather(a.notation.Format(path...), opErr)
path = path[:len(path)-1]
}
return nil
})
if err != nil {
a.gather(selector, err)
}
}
// MustMap transform each element of a slice (or a the single element if selector leads to not slice)
// by applying the transform function.
// It also gathers any possible error of Must API to `multipleError` and returns it.
// Example:
//
// itemsSlice, err := MustMap(p.Must(), "near_earth_objects.2023-01-07", func(sm SelectorMustAPI) Item {
// return Item{
// Name: sm.String("name"),
// Sentry: sm.Bool("is_sentry_object"),
// }
// })
//
//nolint:ireturn
func MustMap[Output any](a SelectorMustAPI, selector string, transform func(SelectorMustAPI) (Output, error)) []Output {
return MustMapFilter[Output](a, selector, func(sma SelectorMustAPI) (Output, bool, error) {
o, err := transform(sma)
return o, true, err
})
}
//nolint:ireturn
func MustMapFilter[Output any](a SelectorMustAPI, selector string, transform func(SelectorMustAPI) (Output, bool, error)) []Output {
item, path, err := parseSelectorAndTraverse(a.Picker, selector)
if err != nil {
a.gather(selector, err)
return nil
}
sl, err := mapFilterTo(item, func(item any, meta iterationOpMeta) (Output, bool, error) {
t, keep, opErr := transform(a.Wrap(item))
if opErr != nil {
path = append(path, Index(meta.Index))
a.gather(a.notation.Format(path...), opErr)
path = path[:len(path)-1]
}
return t, keep, nil
})
if err != nil {
a.gather(selector, err)
}
return sl
}
//nolint:ireturn
func MustFlatMap[Output any](a SelectorMustAPI, selector string, transform func(SelectorMustAPI) ([]Output, error)) []Output {
item := MustMap[[]Output](a, selector, transform)
return flatten[Output](item)
}
// MustPath is the version of [Path] that uses SelectorMustAPI.
func MustPath[Output any](a SelectorMustAPI, path ...Key) Output { //nolint:ireturn
casted, err := Path[Output](a.Picker, path...)
if err != nil {
selector := DotNotation{}.Format(path...)
a.gather(selector, err)
}
return casted
}
// MustOrDefault will return the default value if any error occurs. Version of [OrDefault] that uses SelectorMustAPI.
func MustOrDefault[Output any](a SelectorMustAPI, selector string, defaultValue Output) Output { //nolint:ireturn
item, err := OrDefault(a.Picker, selector, defaultValue)
if err != nil {
a.gather(selector, err)
}
return item
}
// MustGet resolves the cast type from the generic type. Version of [Get] that uses SelectorMustAPI.
func MustGet[Output any](a SelectorMustAPI, selector string) Output { //nolint:ireturn
item, err := Get[Output](a.Picker, selector)
if err != nil {
a.gather(selector, err)
}
return item
}
func flatten[Output any](doubleSlice [][]Output) []Output {
// calculate total capacity
l := 0
for i := range doubleSlice {
l += len(doubleSlice[i])
}
// flatten
outputSlice := make([]Output, 0, l)
for _, ds := range doubleSlice {
outputSlice = append(outputSlice, ds...)
}
return outputSlice
}
func parseSelectorAndTraverse(p *Picker, selector string) (any, []Key, error) {
path, err := p.notation.Parse(selector)
if err != nil {
return nil, path, err
}
item, err := p.Path(path)
return item, path, err
}
func castAs[Output any](caster Caster, data any, defaultValue Output) (Output, error) { //nolint:ireturn
c, err := caster.ByType(data, reflect.TypeOf(defaultValue))
if err != nil {
return defaultValue, err
}
asOutput, is := c.(Output)
if !is {
return defaultValue, fmt.Errorf("casted value cannot be asserted to type: %w", ErrCastInvalidType) // this is not possible
}
return asOutput, nil
}