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

Implement std.isEmpty for string #678

Merged
merged 3 commits into from
Apr 13, 2023
Merged
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
12 changes: 11 additions & 1 deletion builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,15 @@ func builtinStrReplace(i *interpreter, strv, fromv, tov value) (value, error) {
return makeValueString(strings.Replace(sStr, sFrom, sTo, -1)), nil
}

func builtinIsEmpty(i *interpreter, strv value) (value, error) {
str, err := i.getString(strv)
if err != nil {
return nil, err
}
sStr := str.getGoString()
return makeValueBoolean(len(sStr) == 0), nil
}

func base64DecodeGoBytes(i *interpreter, str string) ([]byte, error) {
strLen := len(str)
if strLen%4 != 0 {
Expand Down Expand Up @@ -1495,7 +1504,7 @@ func tomlEncodeString(s string) string {
}

// tomlEncodeKey encodes a key - returning same string if it does not need quoting,
// otherwise return it quoted; returns empty key as ''
// otherwise return it quoted; returns empty key as
func tomlEncodeKey(s string) string {
bareAllowed := true

Expand Down Expand Up @@ -2218,6 +2227,7 @@ var funcBuiltins = buildBuiltinMap([]builtin{
&ternaryBuiltin{name: "substr", function: builtinSubstr, params: ast.Identifiers{"str", "from", "len"}},
&ternaryBuiltin{name: "splitLimit", function: builtinSplitLimit, params: ast.Identifiers{"str", "c", "maxsplits"}},
&ternaryBuiltin{name: "strReplace", function: builtinStrReplace, params: ast.Identifiers{"str", "from", "to"}},
&unaryBuiltin{name: "isEmpty", function: builtinIsEmpty, params: ast.Identifiers{"str"}},
&unaryBuiltin{name: "base64Decode", function: builtinBase64Decode, params: ast.Identifiers{"str"}},
&unaryBuiltin{name: "base64DecodeBytes", function: builtinBase64DecodeBytes, params: ast.Identifiers{"str"}},
&unaryBuiltin{name: "parseInt", function: builtinParseInt, params: ast.Identifiers{"str"}},
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 @@ -88,6 +88,7 @@ func prepareStdlib(g *typeGraph) {
"asciiLower": g.newSimpleFuncType(stringType, "str"),
"stringChars": g.newSimpleFuncType(stringType, "str"),
"format": g.newSimpleFuncType(stringType, "str", "vals"),
"isEmpty": g.newSimpleFuncType(boolType, "str"),
// TODO(sbarzowski) Fix when they match the documentation
"escapeStringBash": g.newSimpleFuncType(stringType, "str_"),
"escapeStringDollars": g.newSimpleFuncType(stringType, "str_"),
Expand Down
1 change: 1 addition & 0 deletions testdata/builtinIsEmpty.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
1 change: 1 addition & 0 deletions testdata/builtinIsEmpty.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.isEmpty("")
Empty file.
1 change: 1 addition & 0 deletions testdata/builtinIsEmpty1.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
false
1 change: 1 addition & 0 deletions testdata/builtinIsEmpty1.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.isEmpty("non-empty string")
Empty file.
9 changes: 9 additions & 0 deletions testdata/builtinIsEmpty2.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
RUNTIME ERROR: Unexpected type number, expected string
-------------------------------------------------
testdata/builtinIsEmpty2:1:1-16 $

std.isEmpty(10)

-------------------------------------------------
During evaluation

1 change: 1 addition & 0 deletions testdata/builtinIsEmpty2.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.isEmpty(10)
Empty file.