Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: new interpolation function jsonencode #5890

Merged
merged 1 commit into from
Mar 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions config/interpolate_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -40,6 +41,7 @@ func Funcs() map[string]ast.Function {
"formatlist": interpolationFuncFormatList(),
"index": interpolationFuncIndex(),
"join": interpolationFuncJoin(),
"jsonencode": interpolationFuncJSONEncode(),
"length": interpolationFuncLength(),
"lower": interpolationFuncLower(),
"md5": interpolationFuncMd5(),
Expand Down Expand Up @@ -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 {
Expand Down
42 changes: 42 additions & 0 deletions config/interpolate_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding one more test case with an empty string to confirm that an error is returned in such cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add a test case for the empty string, but why should it be an error rather than ""?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a test case for the empty string (no error) and for no argument given (error).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty string should remain empty string, naturally. 👍 I was more looking for any test covering the erroneous state.

{
`${jsonencode("")}`,
`""`,
false,
},
{
`${jsonencode()}`,
nil,
true,
},
},
})
}

func TestInterpolateFuncReplace(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
Expand Down
3 changes: 3 additions & 0 deletions website/source/docs/configuration/interpolation.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down