From 76e4fc22212e39fdef87531bb316e415ac635543 Mon Sep 17 00:00:00 2001 From: Rohit Jangid Date: Wed, 3 May 2023 23:36:07 +0530 Subject: [PATCH] feat: implement std.contains (#691) --- builtins.go | 22 ++++++++++++++++++++++ linter/internal/types/stdlib.go | 5 +++-- testdata/builtinContains.golden | 1 + testdata/builtinContains.jsonnet | 1 + testdata/builtinContains.linter.golden | 0 testdata/builtinContains2.golden | 1 + testdata/builtinContains2.jsonnet | 1 + testdata/builtinContains2.linter.golden | 0 8 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 testdata/builtinContains.golden create mode 100644 testdata/builtinContains.jsonnet create mode 100644 testdata/builtinContains.linter.golden create mode 100644 testdata/builtinContains2.golden create mode 100644 testdata/builtinContains2.jsonnet create mode 100644 testdata/builtinContains2.linter.golden diff --git a/builtins.go b/builtins.go index 251ca40da..47c918c08 100644 --- a/builtins.go +++ b/builtins.go @@ -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 { @@ -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"}}, diff --git a/linter/internal/types/stdlib.go b/linter/internal/types/stdlib.go index d181c1266..264b1ec84 100644 --- a/linter/internal/types/stdlib.go +++ b/linter/internal/types/stdlib.go @@ -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 @@ -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{} diff --git a/testdata/builtinContains.golden b/testdata/builtinContains.golden new file mode 100644 index 000000000..27ba77dda --- /dev/null +++ b/testdata/builtinContains.golden @@ -0,0 +1 @@ +true diff --git a/testdata/builtinContains.jsonnet b/testdata/builtinContains.jsonnet new file mode 100644 index 000000000..11cf253c1 --- /dev/null +++ b/testdata/builtinContains.jsonnet @@ -0,0 +1 @@ +std.contains([1, 2, 3], 2) \ No newline at end of file diff --git a/testdata/builtinContains.linter.golden b/testdata/builtinContains.linter.golden new file mode 100644 index 000000000..e69de29bb diff --git a/testdata/builtinContains2.golden b/testdata/builtinContains2.golden new file mode 100644 index 000000000..c508d5366 --- /dev/null +++ b/testdata/builtinContains2.golden @@ -0,0 +1 @@ +false diff --git a/testdata/builtinContains2.jsonnet b/testdata/builtinContains2.jsonnet new file mode 100644 index 000000000..7f644c3f0 --- /dev/null +++ b/testdata/builtinContains2.jsonnet @@ -0,0 +1 @@ +std.contains([1, 2, 3], "foo") \ No newline at end of file diff --git a/testdata/builtinContains2.linter.golden b/testdata/builtinContains2.linter.golden new file mode 100644 index 000000000..e69de29bb