forked from TomWright/dasel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_join.go
60 lines (50 loc) · 1.3 KB
/
func_join.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
package dasel
import (
"github.com/tomwright/dasel/v2/util"
"strings"
)
var JoinFunc = BasicFunction{
name: "join",
runFn: func(c *Context, s *Step, args []string) (Values, error) {
if err := requireXOrMoreArgs("join", args, 1); err != nil {
return nil, err
}
input := s.inputs()
getValues := func(value Value, selector string) ([]string, error) {
gotValues, err := c.subSelect(value, selector)
if err != nil {
return []string{}, err
}
res := make([]string, len(gotValues))
for k, v := range gotValues {
res[k] = util.ToString(v.Interface())
}
return res, nil
}
res := make(Values, 0)
separator := args[0]
args = args[1:]
// No args - join all input values
if len(args) == 0 {
values := make([]string, len(input))
for k, v := range input {
values[k] = util.ToString(v.Interface())
}
res = append(res, ValueOf(strings.Join(values, separator)))
return res, nil
}
// There are args - use each as a selector and join any resulting values.
values := make([]string, 0)
for _, val := range input {
for _, cmp := range args {
vals, err := getValues(val, cmp)
if err != nil {
return nil, err
}
values = append(values, vals...)
}
}
res = append(res, ValueOf(strings.Join(values, separator)))
return res, nil
},
}