-
-
Notifications
You must be signed in to change notification settings - Fork 141
/
func_append.go
45 lines (38 loc) · 839 Bytes
/
func_append.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
package dasel
import (
"fmt"
"reflect"
)
var AppendFunc = BasicFunction{
name: "append",
runFn: func(c *Context, s *Step, args []string) (Values, error) {
if err := requireNoArgs("append", args); err != nil {
return nil, err
}
input := s.inputs()
if c.CreateWhenMissing() {
input = input.initEmptySlices()
}
res := make(Values, 0)
for _, val := range input {
switch val.Kind() {
case reflect.Slice, reflect.Array:
val = val.Append()
value := val.Index(val.Len() - 1)
res = append(res, value)
default:
return nil, fmt.Errorf("cannot use append selector on non slice/array types")
}
}
return res, nil
},
alternativeSelectorFn: func(part string) *Selector {
if part == "[]" {
return &Selector{
funcName: "append",
funcArgs: []string{},
}
}
return nil
},
}