diff --git a/interpolation/interpolation_test.go b/interpolation/interpolation_test.go index 592211636..322bfe24a 100644 --- a/interpolation/interpolation_test.go +++ b/interpolation/interpolation_test.go @@ -113,6 +113,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"}, diff --git a/template/template.go b/template/template.go index 22e4e95ad..3e62326a1 100644 --- a/template/template.go +++ b/template/template.go @@ -63,8 +63,9 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su if len(subsFuncs) == 0 { subsFuncs = getDefaultSortedSubstitutionFunctions(template) } - var err error + var errReturn error result := pattern.ReplaceAllStringFunc(template, func(substring string) string { + var err error closingBraceIndex := getFirstBraceClosingIndex(substring) rest := "" if closingBraceIndex > -1 { @@ -87,6 +88,9 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su if substitution == "" { err = &InvalidTemplateError{Template: template} + if errReturn == nil { + errReturn = err + } return "" } @@ -98,6 +102,9 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su ) value, applied, err = f(substitution, mapping) if err != nil { + if errReturn == nil { + errReturn = err + } return "" } if !applied { @@ -105,6 +112,9 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su } interpolatedNested, err := SubstituteWith(rest, mapping, pattern, subsFuncs...) if err != nil { + if errReturn == nil { + errReturn = err + } return "" } return value + interpolatedNested @@ -118,7 +128,7 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su return value }) - return result, err + return result, errReturn } func getDefaultSortedSubstitutionFunctions(template string, fns ...SubstituteFunc) []SubstituteFunc {