From d18f7245a3ba9a50d36bc48854292360f875d36b Mon Sep 17 00:00:00 2001 From: Tejesh Raut Date: Fri, 31 Mar 2023 10:32:25 +0000 Subject: [PATCH 1/2] Implement std.isEmpty for string --- builtins.go | 12 +++++++++++- linter/internal/types/stdlib.go | 3 ++- testdata/builtinIsEmpty.golden | 1 + testdata/builtinIsEmpty.jsonnet | 1 + testdata/builtinIsEmpty.linter.golden | 0 testdata/builtinIsEmpty1.golden | 1 + testdata/builtinIsEmpty1.jsonnet | 1 + testdata/builtinIsEmpty1.linter.golden | 0 testdata/builtinIsEmpty2.golden | 9 +++++++++ testdata/builtinIsEmpty2.jsonnet | 1 + 10 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 testdata/builtinIsEmpty.golden create mode 100644 testdata/builtinIsEmpty.jsonnet create mode 100644 testdata/builtinIsEmpty.linter.golden create mode 100644 testdata/builtinIsEmpty1.golden create mode 100644 testdata/builtinIsEmpty1.jsonnet create mode 100644 testdata/builtinIsEmpty1.linter.golden create mode 100644 testdata/builtinIsEmpty2.golden create mode 100644 testdata/builtinIsEmpty2.jsonnet diff --git a/builtins.go b/builtins.go index d7267989f..692b7c2d4 100644 --- a/builtins.go +++ b/builtins.go @@ -1267,6 +1267,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 { @@ -1483,7 +1492,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 @@ -2205,6 +2214,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 5083fa9a9..e8e18d2ad 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_"), @@ -174,7 +175,7 @@ func prepareStdlib(g *typeGraph) { // Boolean - "xor": g.newSimpleFuncType(boolType, "x", "y"), + "xor": g.newSimpleFuncType(boolType, "x", "y"), } fieldContains := map[string][]placeholderID{} diff --git a/testdata/builtinIsEmpty.golden b/testdata/builtinIsEmpty.golden new file mode 100644 index 000000000..f32a5804e --- /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 000000000..c46ec8d5c --- /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 000000000..e69de29bb diff --git a/testdata/builtinIsEmpty1.golden b/testdata/builtinIsEmpty1.golden new file mode 100644 index 000000000..02e4a84d6 --- /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 000000000..0f0a4fdd9 --- /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 000000000..e69de29bb diff --git a/testdata/builtinIsEmpty2.golden b/testdata/builtinIsEmpty2.golden new file mode 100644 index 000000000..31530cfc8 --- /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 000000000..8ef480048 --- /dev/null +++ b/testdata/builtinIsEmpty2.jsonnet @@ -0,0 +1 @@ +std.isEmpty(10) From 726c923b22c12ca61df16fbb7a1ef11ff5cfccbd Mon Sep 17 00:00:00 2001 From: Tejesh Raut Date: Wed, 12 Apr 2023 10:11:31 +0000 Subject: [PATCH 2/2] added missing file testdata/builtinIsEmpty2.linter.golden --- testdata/builtinIsEmpty2.linter.golden | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 testdata/builtinIsEmpty2.linter.golden diff --git a/testdata/builtinIsEmpty2.linter.golden b/testdata/builtinIsEmpty2.linter.golden new file mode 100644 index 000000000..e69de29bb