-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
func_all.go
47 lines (41 loc) · 1 KB
/
func_all.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
package dasel
import (
"fmt"
"github.com/tomwright/dasel/v2/dencoding"
"reflect"
)
var AllFunc = BasicFunction{
name: "all",
runFn: func(c *Context, s *Step, args []string) (Values, error) {
if err := requireNoArgs("all", args); err != nil {
return nil, err
}
input := s.inputs()
res := make(Values, 0)
for _, val := range input {
switch val.Kind() {
case reflect.String:
for _, r := range val.String() {
res = append(res, ValueOf(string(r)))
}
case reflect.Slice, reflect.Array:
for i := 0; i < val.Len(); i++ {
res = append(res, val.Index(i))
}
case reflect.Map:
for _, key := range val.MapKeys() {
res = append(res, val.MapIndex(key))
}
default:
if val.IsDencodingMap() {
for _, k := range val.Interface().(*dencoding.Map).Keys() {
res = append(res, val.dencodingMapIndex(ValueOf(k)))
}
} else {
return nil, fmt.Errorf("cannot use all selector on non slice/array/map types")
}
}
}
return res, nil
},
}