@@ -61,13 +61,14 @@ type SubstituteFunc func(string, Mapping) (string, bool, error)
6161// SubstituteWith substitute variables in the string with their values.
6262// It accepts additional substitute function.
6363func SubstituteWith (template string , mapping Mapping , pattern * regexp.Regexp , subsFuncs ... SubstituteFunc ) (string , error ) {
64- var err error
64+ var outerErr error
6565
66- if len (subsFuncs ) == 0 {
67- _ , subsFunc := getSubstitutionFunctionForTemplate (template )
68- subsFuncs = []SubstituteFunc {subsFunc }
69- }
7066 result := pattern .ReplaceAllStringFunc (template , func (substring string ) string {
67+ _ , subsFunc := getSubstitutionFunctionForTemplate (substring )
68+ if len (subsFuncs ) > 0 {
69+ subsFunc = subsFuncs [0 ]
70+ }
71+
7172 closingBraceIndex := getFirstBraceClosingIndex (substring )
7273 rest := ""
7374 if closingBraceIndex > - 1 {
@@ -89,24 +90,21 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su
8990 }
9091
9192 if substitution == "" {
92- err = & InvalidTemplateError {Template : template }
93+ outerErr = & InvalidTemplateError {Template : template }
9394 return ""
9495 }
9596
9697 if braced {
97- for _ , f := range subsFuncs {
98- var (
99- value string
100- applied bool
101- )
102- value , applied , err = f (substitution , mapping )
103- if err != nil {
104- return ""
105- }
106- if ! applied {
107- continue
108- }
109- interpolatedNested , err := SubstituteWith (rest , mapping , pattern , subsFuncs ... )
98+ var (
99+ value string
100+ applied bool
101+ )
102+ value , applied , outerErr = subsFunc (substitution , mapping )
103+ if outerErr != nil {
104+ return ""
105+ }
106+ if applied {
107+ interpolatedNested , err := SubstituteWith (rest , mapping , pattern )
110108 if err != nil {
111109 return ""
112110 }
@@ -121,7 +119,7 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su
121119 return value
122120 })
123121
124- return result , err
122+ return result , outerErr
125123}
126124
127125func getSubstitutionFunctionForTemplate (template string ) (string , SubstituteFunc ) {
0 commit comments