Skip to content

Commit

Permalink
feat: implement std.contains (#691)
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitjangid authored May 3, 2023
1 parent 7903819 commit 76e4fc2
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 2 deletions.
22 changes: 22 additions & 0 deletions builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1935,6 +1935,27 @@ func builtinSum(i *interpreter, arrv value) (value, error) {
return makeValueNumber(sum), nil
}

func builtinContains(i *interpreter, arrv value, ev value) (value, error) {
arr, err := i.getArray(arrv)
if err != nil {
return nil, err
}
for _, 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 makeValueBoolean(true), nil
}
}
return makeValueBoolean(false), nil
}

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

type builtin interface {
Expand Down Expand Up @@ -2245,6 +2266,7 @@ var funcBuiltins = buildBuiltinMap([]builtin{
&generalBuiltin{name: "sort", function: builtinSort, params: []generalBuiltinParameter{{name: "arr"}, {name: "keyF", defaultValue: functionID}}},
&unaryBuiltin{name: "native", function: builtinNative, params: ast.Identifiers{"x"}},
&unaryBuiltin{name: "sum", function: builtinSum, params: ast.Identifiers{"arr"}},
&binaryBuiltin{name: "contains", function: builtinContains, params: ast.Identifiers{"arr", "elem"}},

// internal
&unaryBuiltin{name: "$objectFlatMerge", function: builtinUglyObjectFlatMerge, params: ast.Identifiers{"x"}},
Expand Down
5 changes: 3 additions & 2 deletions linter/internal/types/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ 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"),
"contains": g.newSimpleFuncType(boolType, "arr", "elem"),

// Sets

Expand Down Expand Up @@ -176,8 +177,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
1 change: 1 addition & 0 deletions testdata/builtinContains.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
1 change: 1 addition & 0 deletions testdata/builtinContains.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.contains([1, 2, 3], 2)
Empty file.
1 change: 1 addition & 0 deletions testdata/builtinContains2.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
false
1 change: 1 addition & 0 deletions testdata/builtinContains2.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.contains([1, 2, 3], "foo")
Empty file.

0 comments on commit 76e4fc2

Please sign in to comment.