Skip to content

Commit

Permalink
feat: implement std.remove and std.removeAt
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitjangid committed Apr 18, 2023
1 parent c484aec commit 252aae8
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 2 deletions.
37 changes: 37 additions & 0 deletions builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1935,6 +1935,41 @@ func builtinSum(i *interpreter, arrv value) (value, error) {
return makeValueNumber(sum), nil
}

func builtinRemove(i *interpreter, arrv value, ev value) (value, error) {
arr, err := i.getArray(arrv)
if err != nil {
return nil, err
}
for idx, elem := range arr.elements {
val, err := elem.getValue(i)
if err != nil {
return nil, err
}
eq, err := rawEquals(i, val, ev)
if err != nil {
return nil, err
}
if eq {
return builtinRemoveAt(i, arrv, intToValue(idx))
}
}
return arr, nil
}

func builtinRemoveAt(i *interpreter, arrv value, idxv value) (value, error) {
arr, err := i.getArray(arrv)
if err != nil {
return nil, err
}
idx, err := i.getInt(idxv)
if err != nil {
return nil, err
}

newArr := append(arr.elements[:idx], arr.elements[idx+1:]...)
return makeValueArray(newArr), nil
}

// Utils for builtins - TODO(sbarzowski) move to a separate file in another commit

type builtin interface {
Expand Down Expand Up @@ -2196,6 +2231,8 @@ var funcBuiltins = buildBuiltinMap([]builtin{
&ternaryBuiltin{name: "foldl", function: builtinFoldl, params: ast.Identifiers{"func", "arr", "init"}},
&ternaryBuiltin{name: "foldr", function: builtinFoldr, params: ast.Identifiers{"func", "arr", "init"}},
&binaryBuiltin{name: "member", function: builtinMember, params: ast.Identifiers{"arr", "x"}},
&binaryBuiltin{name: "remove", function: builtinRemove, params: ast.Identifiers{"arr", "elem"}},
&binaryBuiltin{name: "removeAt", function: builtinRemoveAt, params: ast.Identifiers{"arr", "i"}},
&binaryBuiltin{name: "range", function: builtinRange, params: ast.Identifiers{"from", "to"}},
&binaryBuiltin{name: "primitiveEquals", function: primitiveEquals, params: ast.Identifiers{"x", "y"}},
&binaryBuiltin{name: "equals", function: builtinEquals, params: ast.Identifiers{"x", "y"}},
Expand Down
6 changes: 4 additions & 2 deletions linter/internal/types/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ func prepareStdlib(g *typeGraph) {
"sort": g.newFuncType(anyArrayType, []ast.Parameter{required("arr"), optional("keyF")}),
"uniq": g.newFuncType(anyArrayType, []ast.Parameter{required("arr"), optional("keyF")}),
"sum": g.newSimpleFuncType(numberType, "arr"),
"remove": g.newSimpleFuncType(anyArrayType, "arr", "elem"),
"removeAt": g.newSimpleFuncType(anyArrayType, "arr", "i"),

// Sets

Expand Down Expand Up @@ -176,8 +178,8 @@ func prepareStdlib(g *typeGraph) {

// Boolean

"xor": g.newSimpleFuncType(boolType, "x", "y"),
"xnor": g.newSimpleFuncType(boolType, "x", "y"),
"xor": g.newSimpleFuncType(boolType, "x", "y"),
"xnor": g.newSimpleFuncType(boolType, "x", "y"),
}

fieldContains := map[string][]placeholderID{}
Expand Down
4 changes: 4 additions & 0 deletions testdata/builtinRemove.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
1,
3
]
1 change: 1 addition & 0 deletions testdata/builtinRemove.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.remove([1,2,3],2)
Empty file.
4 changes: 4 additions & 0 deletions testdata/builtinRemoveAt.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
1,
3
]
1 change: 1 addition & 0 deletions testdata/builtinRemoveAt.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.removeAt([1,2,3],1)
Empty file.

0 comments on commit 252aae8

Please sign in to comment.