Skip to content

Commit

Permalink
Added splice workflow function
Browse files Browse the repository at this point in the history
  • Loading branch information
petergrlica committed Aug 25, 2023
1 parent 7254628 commit 7bc36eb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
15 changes: 12 additions & 3 deletions server/pkg/expr/func_arr.go
Expand Up @@ -19,6 +19,7 @@ func ArrayFunctions() []gval.Language {
gval.Function("hasAll", hasAll),
gval.Function("find", find),
gval.Function("sort", sortSlice),
gval.Function("splice", splice),
}
}

Expand Down Expand Up @@ -210,9 +211,17 @@ func find(arr interface{}, v interface{}) (p int, err error) {
return -1, nil
}

// slice slices slices
func slice(arr interface{}, start, end int) interface{} {
arr = UntypedValue(arr)
// splice slices slice
func splice(arr interface{}, s, e float64) interface{} {
var (
err error
start = int(s)
end = int(e)
)

if arr, err = toSlice(arr); err != nil {
return nil
}

v := reflect.ValueOf(arr)

Expand Down
16 changes: 8 additions & 8 deletions server/pkg/expr/func_arr_test.go
Expand Up @@ -447,34 +447,34 @@ func Test_hasAll(t *testing.T) {
}
}

func Test_slice(t *testing.T) {
func Test_splice(t *testing.T) {
tcc := []struct {
vals []int
vals []float64
arr interface{}
expect interface{}
}{
{
vals: []int{0, 3},
vals: []float64{0, 3},
arr: []string{"1", "2", "3"},
expect: []string{"1", "2", "3"},
},
{
vals: []int{0, 1},
vals: []float64{0, 1},
arr: []string{"1", "2", "3"},
expect: []string{"1"},
},
{
vals: []int{2, 3},
vals: []float64{2, 3},
arr: []bool{true, true},
expect: []bool{true, true},
},
{
vals: []int{1, -1},
vals: []float64{1, -1},
arr: []int{4, 5, 6, 7},
expect: []int{5, 6, 7},
},
{
vals: []int{3, -1},
vals: []float64{3, -1},
arr: []float64{11.1, 12.4},
expect: []float64{11.1, 12.4},
},
Expand All @@ -484,7 +484,7 @@ func Test_slice(t *testing.T) {
t.Run(fmt.Sprintf("%d", p), func(t *testing.T) {
var (
req = require.New(t)
ss = slice(tc.arr, tc.vals[0], tc.vals[1])
ss = splice(tc.arr, tc.vals[0], tc.vals[1])
)

req.Equal(tc.expect, ss)
Expand Down

0 comments on commit 7bc36eb

Please sign in to comment.