Skip to content

feat: implement std.isNull #806

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -2533,6 +2533,11 @@ func builtInObjectRemoveKey(i *interpreter, objv value, keyv value) (value, erro
), nil
}

func builtinIsNull(i *interpreter, v value) (value, error) {
_, isNull := v.(*valueNull)
return makeValueBoolean(isNull), nil
}

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

type builtin interface {
Expand Down Expand Up @@ -2890,6 +2895,7 @@ var funcBuiltins = buildBuiltinMap([]builtin{
&unaryBuiltin{name: "sum", function: builtinSum, params: ast.Identifiers{"arr"}},
&unaryBuiltin{name: "avg", function: builtinAvg, params: ast.Identifiers{"arr"}},
&binaryBuiltin{name: "contains", function: builtinContains, params: ast.Identifiers{"arr", "elem"}},
&unaryBuiltin{name: "isNull", function: builtinIsNull, params: ast.Identifiers{"x"}},

// internal
&unaryBuiltin{name: "$objectFlatMerge", function: builtinUglyObjectFlatMerge, params: ast.Identifiers{"x"}},
Expand Down
1 change: 1 addition & 0 deletions linter/internal/types/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func prepareStdlib(g *typeGraph) {
"isOdd": g.newSimpleFuncType(boolType, "x"),
"isInteger": g.newSimpleFuncType(boolType, "x"),
"isDecimal": g.newSimpleFuncType(boolType, "x"),
"isNull": g.newSimpleFuncType(boolType, "x"),

// Mathematical utilities
"abs": g.newSimpleFuncType(numberType, "n"),
Expand Down
1 change: 1 addition & 0 deletions testdata/builtinIsNull.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
1 change: 1 addition & 0 deletions testdata/builtinIsNull.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.isNull(null)
Empty file.
1 change: 1 addition & 0 deletions testdata/builtinIsNull2.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
false
1 change: 1 addition & 0 deletions testdata/builtinIsNull2.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.isNull("foo")
Empty file.