Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement std.minArray #685

Merged
merged 6 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1907,6 +1907,42 @@ func builtinExtVar(i *interpreter, name value) (value, error) {
return nil, i.Error("Undefined external variable: " + string(index))
}

func builtinMinArray(i *interpreter, arguments []value) (value, error) {
arrv := arguments[0]
keyFv := arguments[1]

arr, err := i.getArray(arrv)
if err != nil {
return nil, err
}
keyF, err := i.getFunction(keyFv)
if err != nil {
return nil, err
}
num := arr.length()
if num == 0 {
return nil, i.Error("Expected atleast one element in array. Got none")
deepgoel17 marked this conversation as resolved.
Show resolved Hide resolved
}
minVal, err := keyF.call(i, args(arr.elements[0]))
if err != nil {
return nil, err
}
for index := 1; index < num; index++ {
current, err := keyF.call(i, args(arr.elements[index]))
if err != nil {
return nil, err
}
cmp, err := valueCmp(i, minVal, current)
if err != nil {
return nil, err
}
if cmp > 0 {
minVal = current
}
}
return minVal, nil
}

func builtinNative(i *interpreter, name value) (value, error) {
str, err := i.getString(name)
if err != nil {
Expand Down Expand Up @@ -2243,6 +2279,7 @@ var funcBuiltins = buildBuiltinMap([]builtin{
&unaryBuiltin{name: "encodeUTF8", function: builtinEncodeUTF8, params: ast.Identifiers{"str"}},
&unaryBuiltin{name: "decodeUTF8", function: builtinDecodeUTF8, params: ast.Identifiers{"arr"}},
&generalBuiltin{name: "sort", function: builtinSort, params: []generalBuiltinParameter{{name: "arr"}, {name: "keyF", defaultValue: functionID}}},
&generalBuiltin{name: "minArray", function: builtinMinArray, 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"}},

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"),
"minArray": g.newSimpleFuncType(anyType, "arr"),

// 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/builtinMinArray.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-1
1 change: 1 addition & 0 deletions testdata/builtinMinArray.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.minArray([1,-1,0])
Empty file.