mirror of
https://github.com/google/go-jsonnet.git
synced 2025-09-28 17:01:02 +02:00
Implement std.isEmpty for string (#678)
* Implement std.isEmpty for string
This commit is contained in:
parent
ff691b0f18
commit
c63eb61eb3
12
builtins.go
12
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"}},
|
||||
|
@ -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
1
testdata/builtinIsEmpty.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
true
|
1
testdata/builtinIsEmpty.jsonnet
vendored
Normal file
1
testdata/builtinIsEmpty.jsonnet
vendored
Normal file
@ -0,0 +1 @@
|
||||
std.isEmpty("")
|
0
testdata/builtinIsEmpty.linter.golden
vendored
Normal file
0
testdata/builtinIsEmpty.linter.golden
vendored
Normal file
1
testdata/builtinIsEmpty1.golden
vendored
Normal file
1
testdata/builtinIsEmpty1.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
false
|
1
testdata/builtinIsEmpty1.jsonnet
vendored
Normal file
1
testdata/builtinIsEmpty1.jsonnet
vendored
Normal file
@ -0,0 +1 @@
|
||||
std.isEmpty("non-empty string")
|
0
testdata/builtinIsEmpty1.linter.golden
vendored
Normal file
0
testdata/builtinIsEmpty1.linter.golden
vendored
Normal file
9
testdata/builtinIsEmpty2.golden
vendored
Normal file
9
testdata/builtinIsEmpty2.golden
vendored
Normal 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
1
testdata/builtinIsEmpty2.jsonnet
vendored
Normal file
@ -0,0 +1 @@
|
||||
std.isEmpty(10)
|
0
testdata/builtinIsEmpty2.linter.golden
vendored
Normal file
0
testdata/builtinIsEmpty2.linter.golden
vendored
Normal file
Loading…
x
Reference in New Issue
Block a user