Skip to content

Commit 1c43e45

Browse files
authored
Func join (#499)
* Add join function * Fix a typo
1 parent 3cf25af commit 1c43e45

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
- Nothing yet.
10+
### Added
11+
- `join` function to join array elements into a single string with a specified separator. [See docs](https://daseldocs.tomwright.me/functions/join).
1112

1213
## [v3.1.4] - 2025-12-18
1314

execution/func.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var (
2929
FuncGet,
3030
FuncContains,
3131
FuncSum,
32+
FuncJoin,
3233
)
3334
)
3435

execution/func_join.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package execution
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/tomwright/dasel/v3/model"
9+
)
10+
11+
// FuncJoin is a function that joins the given data or args to a string.
12+
var FuncJoin = NewFunc(
13+
"join",
14+
func(ctx context.Context, data *model.Value, args model.Values) (*model.Value, error) {
15+
separator, err := args[0].StringValue()
16+
if err != nil {
17+
return nil, fmt.Errorf("join expects a string separator as the first argument: %w", err)
18+
}
19+
20+
var valuesToJoin []string
21+
22+
if len(args) == 2 && args[1].IsSlice() {
23+
if err := args[1].RangeSlice(func(i int, value *model.Value) error {
24+
strVal, err := value.StringValue()
25+
if err != nil {
26+
return fmt.Errorf("could not read string value of index %d: %w", i, err)
27+
}
28+
valuesToJoin = append(valuesToJoin, strVal)
29+
return nil
30+
}); err != nil {
31+
return nil, err
32+
}
33+
} else if len(args) > 1 {
34+
// Join the args
35+
for i := 1; i < len(args); i++ {
36+
strVal, err := args[i].StringValue()
37+
if err != nil {
38+
return nil, fmt.Errorf("could not read string value of argument index %d: %w", i, err)
39+
}
40+
valuesToJoin = append(valuesToJoin, strVal)
41+
}
42+
} else {
43+
if err := data.RangeSlice(func(i int, value *model.Value) error {
44+
strVal, err := value.StringValue()
45+
if err != nil {
46+
return fmt.Errorf("could not read string value of index %d: %w", i, err)
47+
}
48+
valuesToJoin = append(valuesToJoin, strVal)
49+
return nil
50+
}); err != nil {
51+
return nil, err
52+
}
53+
}
54+
55+
joined := strings.Join(valuesToJoin, separator)
56+
57+
return model.NewStringValue(joined), nil
58+
},
59+
ValidateArgsMin(1),
60+
)

execution/func_join_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package execution_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/tomwright/dasel/v3/model"
7+
_ "github.com/tomwright/dasel/v3/parsing/json"
8+
)
9+
10+
func TestFuncJoin(t *testing.T) {
11+
t.Run("chained input", testCase{
12+
s: `["a","b","c"].join(",")`,
13+
out: model.NewStringValue("a,b,c"),
14+
}.run)
15+
t.Run("vararg input", testCase{
16+
s: `join(",", "a", "b", "c")`,
17+
out: model.NewStringValue("a,b,c"),
18+
}.run)
19+
t.Run("array input", testCase{
20+
s: `join(",", ["a", "b", "c"])`,
21+
out: model.NewStringValue("a,b,c"),
22+
}.run)
23+
}

0 commit comments

Comments
 (0)