Merge pull request #5890 from meteor/jsonencode

config: new interpolation function jsonencode
This commit is contained in:
Radek Simko 2016-03-29 16:33:20 +01:00
commit c7a8f05e03
3 changed files with 64 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
@ -40,6 +41,7 @@ func Funcs() map[string]ast.Function {
"formatlist": interpolationFuncFormatList(),
"index": interpolationFuncIndex(),
"join": interpolationFuncJoin(),
"jsonencode": interpolationFuncJSONEncode(),
"length": interpolationFuncLength(),
"lower": interpolationFuncLower(),
"md5": interpolationFuncMd5(),
@ -364,6 +366,23 @@ func interpolationFuncJoin() ast.Function {
}
}
// interpolationFuncJSONEncode implements the "jsonencode" function that encodes
// a string as its JSON representation.
func interpolationFuncJSONEncode() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeString},
ReturnType: ast.TypeString,
Callback: func(args []interface{}) (interface{}, error) {
s := args[0].(string)
jEnc, err := json.Marshal(s)
if err != nil {
return "", fmt.Errorf("failed to encode JSON data '%s'", s)
}
return string(jEnc), nil
},
}
}
// interpolationFuncReplace implements the "replace" function that does
// string replacement.
func interpolationFuncReplace() ast.Function {

View File

@ -437,6 +437,48 @@ func TestInterpolateFuncJoin(t *testing.T) {
})
}
func TestInterpolateFuncJSONEncode(t *testing.T) {
testFunction(t, testFunctionConfig{
Vars: map[string]ast.Variable{
"easy": ast.Variable{
Value: "test",
Type: ast.TypeString,
},
"hard": ast.Variable{
Value: " foo \\ \n \t \" bar ",
Type: ast.TypeString,
},
},
Cases: []testFunctionCase{
{
`${jsonencode("test")}`,
`"test"`,
false,
},
{
`${jsonencode(easy)}`,
`"test"`,
false,
},
{
`${jsonencode(hard)}`,
`" foo \\ \n \t \" bar "`,
false,
},
{
`${jsonencode("")}`,
`""`,
false,
},
{
`${jsonencode()}`,
nil,
true,
},
},
})
}
func TestInterpolateFuncReplace(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{

View File

@ -150,6 +150,9 @@ The supported built-in functions are:
only possible with splat variables from resources with a count
greater than one. Example: `join(",", aws_instance.foo.*.id)`
* `jsonencode(string)` - Returns a JSON-encoded representation of the given
string (including double quotes).
* `length(list)` - Returns a number of members in a given list
or a number of characters in a given string.
* `${length(split(",", "a,b,c"))}` = 3