forked from TomWright/dasel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_merge.go
45 lines (37 loc) · 1.02 KB
/
func_merge.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 "reflect"
var MergeFunc = BasicFunction{
name: "merge",
runFn: func(c *Context, s *Step, args []string) (Values, error) {
input := s.inputs()
res := make(Values, 0)
if len(args) == 0 {
// Merge all inputs into a slice.
resSlice := reflect.MakeSlice(sliceInterfaceType, len(input), len(input))
for i, val := range input {
resSlice.Index(i).Set(val.Value)
}
resPointer := reflect.New(resSlice.Type())
resPointer.Elem().Set(resSlice)
res = append(res, ValueOf(resPointer))
return res, nil
}
// Merge all inputs into a slice.
resSlice := reflect.MakeSlice(sliceInterfaceType, 0, 0)
for _, val := range input {
for _, a := range args {
gotValues, err := c.subSelect(val, a)
if err != nil {
return nil, err
}
for _, gotVal := range gotValues {
resSlice = reflect.Append(resSlice, gotVal.Value)
}
}
}
resPointer := reflect.New(resSlice.Type())
resPointer.Elem().Set(resSlice)
res = append(res, ValueOf(resPointer))
return res, nil
},
}