Skip to content
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
1 change: 1 addition & 0 deletions interpolation/interpolation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func TestValidUnexistentInterpolation(t *testing.T) {
{test: "{{{ ${FOO:?foo_} }}}", errMsg: "foo_"},
{test: "{{{ ${FOO:?foo-bar-value} }}}", errMsg: "foo-bar-value"},
{test: "{{{ ${FOO:?foo} ${BAR:-DEFAULT_VALUE} }}}", errMsg: "foo"},
{test: "${FOO:?foo} ${BAR:?bar}", errMsg: "foo"},
{test: "{{{ ${BAR} }}}", expected: "{{{ }}}"},
{test: "${FOO:?baz} }}}", errMsg: "baz"},
{test: "${FOO?baz} }}}", errMsg: "baz"},
Expand Down
9 changes: 8 additions & 1 deletion template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type SubstituteFunc func(string, Mapping) (string, bool, error)
// It accepts additional substitute function.
func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, subsFuncs ...SubstituteFunc) (string, error) {
var outerErr error
var returnErr error

result := pattern.ReplaceAllStringFunc(template, func(substring string) string {
_, subsFunc := getSubstitutionFunctionForTemplate(substring)
Expand Down Expand Up @@ -91,6 +92,9 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su

if substitution == "" {
outerErr = &InvalidTemplateError{Template: template}
if returnErr == nil {
returnErr = outerErr
}
return ""
}

Expand All @@ -101,6 +105,9 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su
)
value, applied, outerErr = subsFunc(substitution, mapping)
if outerErr != nil {
if returnErr == nil {
returnErr = outerErr
}
return ""
}
if applied {
Expand All @@ -119,7 +126,7 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su
return value
})

return result, outerErr
return result, returnErr
}

func getSubstitutionFunctionForTemplate(template string) (string, SubstituteFunc) {
Expand Down