Skip to content

Commit 15fd58a

Browse files
Tejesh-Rautvhata
authored andcommitted
Implement std.isEmpty for string (google#678)
* Implement std.isEmpty for string
1 parent 64d150b commit 15fd58a

11 files changed

+26
-1
lines changed

builtins.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,15 @@ func builtinStrReplace(i *interpreter, strv, fromv, tov value) (value, error) {
12791279
return makeValueString(strings.Replace(sStr, sFrom, sTo, -1)), nil
12801280
}
12811281

1282+
func builtinIsEmpty(i *interpreter, strv value) (value, error) {
1283+
str, err := i.getString(strv)
1284+
if err != nil {
1285+
return nil, err
1286+
}
1287+
sStr := str.getGoString()
1288+
return makeValueBoolean(len(sStr) == 0), nil
1289+
}
1290+
12821291
func base64DecodeGoBytes(i *interpreter, str string) ([]byte, error) {
12831292
strLen := len(str)
12841293
if strLen%4 != 0 {
@@ -1495,7 +1504,7 @@ func tomlEncodeString(s string) string {
14951504
}
14961505

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

@@ -2218,6 +2227,7 @@ var funcBuiltins = buildBuiltinMap([]builtin{
22182227
&ternaryBuiltin{name: "substr", function: builtinSubstr, params: ast.Identifiers{"str", "from", "len"}},
22192228
&ternaryBuiltin{name: "splitLimit", function: builtinSplitLimit, params: ast.Identifiers{"str", "c", "maxsplits"}},
22202229
&ternaryBuiltin{name: "strReplace", function: builtinStrReplace, params: ast.Identifiers{"str", "from", "to"}},
2230+
&unaryBuiltin{name: "isEmpty", function: builtinIsEmpty, params: ast.Identifiers{"str"}},
22212231
&unaryBuiltin{name: "base64Decode", function: builtinBase64Decode, params: ast.Identifiers{"str"}},
22222232
&unaryBuiltin{name: "base64DecodeBytes", function: builtinBase64DecodeBytes, params: ast.Identifiers{"str"}},
22232233
&unaryBuiltin{name: "parseInt", function: builtinParseInt, params: ast.Identifiers{"str"}},

linter/internal/types/stdlib.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func prepareStdlib(g *typeGraph) {
8888
"asciiLower": g.newSimpleFuncType(stringType, "str"),
8989
"stringChars": g.newSimpleFuncType(stringType, "str"),
9090
"format": g.newSimpleFuncType(stringType, "str", "vals"),
91+
"isEmpty": g.newSimpleFuncType(boolType, "str"),
9192
// TODO(sbarzowski) Fix when they match the documentation
9293
"escapeStringBash": g.newSimpleFuncType(stringType, "str_"),
9394
"escapeStringDollars": g.newSimpleFuncType(stringType, "str_"),

testdata/builtinIsEmpty.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
true

testdata/builtinIsEmpty.jsonnet

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
std.isEmpty("")

testdata/builtinIsEmpty.linter.golden

Whitespace-only changes.

testdata/builtinIsEmpty1.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
false

testdata/builtinIsEmpty1.jsonnet

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
std.isEmpty("non-empty string")

testdata/builtinIsEmpty1.linter.golden

Whitespace-only changes.

testdata/builtinIsEmpty2.golden

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
RUNTIME ERROR: Unexpected type number, expected string
2+
-------------------------------------------------
3+
testdata/builtinIsEmpty2:1:1-16 $
4+
5+
std.isEmpty(10)
6+
7+
-------------------------------------------------
8+
During evaluation
9+

testdata/builtinIsEmpty2.jsonnet

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
std.isEmpty(10)

0 commit comments

Comments
 (0)