Implement std.isEmpty for string (#678)

* Implement std.isEmpty for string
This commit is contained in:
Tejesh Raut 2023-04-13 19:54:47 +05:30 committed by GitHub
parent ff691b0f18
commit c63eb61eb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 26 additions and 1 deletions

View File

@ -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 {
@ -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
@ -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"}},

View File

@ -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_"),

1
testdata/builtinIsEmpty.golden vendored Normal file
View File

@ -0,0 +1 @@
true

1
testdata/builtinIsEmpty.jsonnet vendored Normal file
View File

@ -0,0 +1 @@
std.isEmpty("")

0
testdata/builtinIsEmpty.linter.golden vendored Normal file
View File

1
testdata/builtinIsEmpty1.golden vendored Normal file
View File

@ -0,0 +1 @@
false

1
testdata/builtinIsEmpty1.jsonnet vendored Normal file
View File

@ -0,0 +1 @@
std.isEmpty("non-empty string")

View File

9
testdata/builtinIsEmpty2.golden vendored Normal file
View File

@ -0,0 +1,9 @@
RUNTIME ERROR: Unexpected type number, expected string
-------------------------------------------------
testdata/builtinIsEmpty2:1:1-16 $
std.isEmpty(10)
-------------------------------------------------
During evaluation

1
testdata/builtinIsEmpty2.jsonnet vendored Normal file
View File

@ -0,0 +1 @@
std.isEmpty(10)

View File