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

Fix broken interpolation with JSON (curly braces) default values #581

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion template/template.go
Expand Up @@ -258,7 +258,7 @@ func getFirstBraceClosingIndex(s string) int {
return i
}
}
if strings.HasPrefix(s[i:], "${") {
if s[i] == '{' {
openVariableBraces++
i++
}
Expand Down
21 changes: 19 additions & 2 deletions template/template_test.go
Expand Up @@ -26,8 +26,9 @@ import (
)

var defaults = map[string]string{
"FOO": "first",
"BAR": "",
"FOO": "first",
"BAR": "",
"JSON": `{"json":2}`,
}

func defaultMapping(name string) (string, bool) {
Expand Down Expand Up @@ -630,3 +631,19 @@ func TestSubstitutionFunctionChoice(t *testing.T) {
})
}
}

func TestNoValueWithCurlyBracesDefault(t *testing.T) {
for _, template := range []string{`ok ${missing:-{"json":1}}`, `ok ${missing-{"json":1}}`} {
result, err := Substitute(template, defaultMapping)
assert.NilError(t, err)
assert.Check(t, is.Equal(`ok {"json":1}`, result))
}
}

func TestValueWithCurlyBracesDefault(t *testing.T) {
for _, template := range []string{`ok ${JSON:-{"json":1}}`, `ok ${JSON-{"json":1}}`} {
result, err := Substitute(template, defaultMapping)
assert.NilError(t, err)
assert.Check(t, is.Equal(`ok {"json":2}`, result))
}
}