diff --git a/builtins.go b/builtins.go index 3930604e..1e0cdde5 100644 --- a/builtins.go +++ b/builtins.go @@ -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"}}, diff --git a/linter/internal/types/stdlib.go b/linter/internal/types/stdlib.go index 5984b43f..5c279fda 100644 --- a/linter/internal/types/stdlib.go +++ b/linter/internal/types/stdlib.go @@ -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_"), diff --git a/testdata/builtinIsEmpty.golden b/testdata/builtinIsEmpty.golden new file mode 100644 index 00000000..f32a5804 --- /dev/null +++ b/testdata/builtinIsEmpty.golden @@ -0,0 +1 @@ +true \ No newline at end of file diff --git a/testdata/builtinIsEmpty.jsonnet b/testdata/builtinIsEmpty.jsonnet new file mode 100644 index 00000000..c46ec8d5 --- /dev/null +++ b/testdata/builtinIsEmpty.jsonnet @@ -0,0 +1 @@ +std.isEmpty("") diff --git a/testdata/builtinIsEmpty.linter.golden b/testdata/builtinIsEmpty.linter.golden new file mode 100644 index 00000000..e69de29b diff --git a/testdata/builtinIsEmpty1.golden b/testdata/builtinIsEmpty1.golden new file mode 100644 index 00000000..02e4a84d --- /dev/null +++ b/testdata/builtinIsEmpty1.golden @@ -0,0 +1 @@ +false \ No newline at end of file diff --git a/testdata/builtinIsEmpty1.jsonnet b/testdata/builtinIsEmpty1.jsonnet new file mode 100644 index 00000000..0f0a4fdd --- /dev/null +++ b/testdata/builtinIsEmpty1.jsonnet @@ -0,0 +1 @@ +std.isEmpty("non-empty string") diff --git a/testdata/builtinIsEmpty1.linter.golden b/testdata/builtinIsEmpty1.linter.golden new file mode 100644 index 00000000..e69de29b diff --git a/testdata/builtinIsEmpty2.golden b/testdata/builtinIsEmpty2.golden new file mode 100644 index 00000000..31530cfc --- /dev/null +++ b/testdata/builtinIsEmpty2.golden @@ -0,0 +1,9 @@ +RUNTIME ERROR: Unexpected type number, expected string +------------------------------------------------- + testdata/builtinIsEmpty2:1:1-16 $ + +std.isEmpty(10) + +------------------------------------------------- + During evaluation + diff --git a/testdata/builtinIsEmpty2.jsonnet b/testdata/builtinIsEmpty2.jsonnet new file mode 100644 index 00000000..8ef48004 --- /dev/null +++ b/testdata/builtinIsEmpty2.jsonnet @@ -0,0 +1 @@ +std.isEmpty(10) diff --git a/testdata/builtinIsEmpty2.linter.golden b/testdata/builtinIsEmpty2.linter.golden new file mode 100644 index 00000000..e69de29b