Skip to content

Commit

Permalink
Implement std.isEmpty for string (#678)
Browse files Browse the repository at this point in the history
* Implement std.isEmpty for string
  • Loading branch information
Tejesh-Raut authored Apr 13, 2023
1 parent ff691b0 commit c63eb61
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 1 deletion.
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.

0 comments on commit c63eb61

Please sign in to comment.