|
17 | 17 | package template |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "errors" |
20 | 21 | "fmt" |
21 | 22 | "regexp" |
22 | 23 | "sort" |
@@ -71,77 +72,143 @@ type Mapping func(string) (string, bool) |
71 | 72 | // the substitution and an error. |
72 | 73 | type SubstituteFunc func(string, Mapping) (string, bool, error) |
73 | 74 |
|
74 | | -// SubstituteWith substitute variables in the string with their values. |
75 | | -// It accepts additional substitute function. |
76 | | -func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, subsFuncs ...SubstituteFunc) (string, error) { |
77 | | - var outerErr error |
78 | | - var returnErr error |
| 75 | +// ReplacementFunc is a user-supplied function that is apply to the matching |
| 76 | +// substring. Returns the value as a string and an error. |
| 77 | +type ReplacementFunc func(string, Mapping, *Config) (string, error) |
79 | 78 |
|
80 | | - result := pattern.ReplaceAllStringFunc(template, func(substring string) string { |
81 | | - _, subsFunc := getSubstitutionFunctionForTemplate(substring) |
82 | | - if len(subsFuncs) > 0 { |
83 | | - subsFunc = subsFuncs[0] |
84 | | - } |
| 79 | +type Config struct { |
| 80 | + pattern *regexp.Regexp |
| 81 | + substituteFunc SubstituteFunc |
| 82 | + replacementFunc ReplacementFunc |
| 83 | + logging bool |
| 84 | +} |
85 | 85 |
|
86 | | - closingBraceIndex := getFirstBraceClosingIndex(substring) |
87 | | - rest := "" |
88 | | - if closingBraceIndex > -1 { |
89 | | - rest = substring[closingBraceIndex+1:] |
90 | | - substring = substring[0 : closingBraceIndex+1] |
91 | | - } |
| 86 | +type Option func(*Config) |
92 | 87 |
|
93 | | - matches := pattern.FindStringSubmatch(substring) |
94 | | - groups := matchGroups(matches, pattern) |
95 | | - if escaped := groups["escaped"]; escaped != "" { |
96 | | - return escaped |
97 | | - } |
| 88 | +func WithPattern(pattern *regexp.Regexp) Option { |
| 89 | + return func(cfg *Config) { |
| 90 | + cfg.pattern = pattern |
| 91 | + } |
| 92 | +} |
98 | 93 |
|
99 | | - braced := false |
100 | | - substitution := groups["named"] |
101 | | - if substitution == "" { |
102 | | - substitution = groups["braced"] |
103 | | - braced = true |
104 | | - } |
| 94 | +func WithSubstitutionFunction(subsFunc SubstituteFunc) Option { |
| 95 | + return func(cfg *Config) { |
| 96 | + cfg.substituteFunc = subsFunc |
| 97 | + } |
| 98 | +} |
105 | 99 |
|
106 | | - if substitution == "" { |
107 | | - outerErr = &InvalidTemplateError{Template: template} |
108 | | - if returnErr == nil { |
109 | | - returnErr = outerErr |
110 | | - } |
111 | | - return "" |
112 | | - } |
| 100 | +func WithReplacementFunction(replacementFunc ReplacementFunc) Option { |
| 101 | + return func(cfg *Config) { |
| 102 | + cfg.replacementFunc = replacementFunc |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func WithoutLogging(cfg *Config) { |
| 107 | + cfg.logging = false |
| 108 | +} |
113 | 109 |
|
114 | | - if braced { |
115 | | - var ( |
116 | | - value string |
117 | | - applied bool |
118 | | - ) |
119 | | - value, applied, outerErr = subsFunc(substitution, mapping) |
120 | | - if outerErr != nil { |
121 | | - if returnErr == nil { |
122 | | - returnErr = outerErr |
| 110 | +// SubstituteWithOptions substitute variables in the string with their values. |
| 111 | +// It accepts additional options such as a custom function or pattern. |
| 112 | +func SubstituteWithOptions(template string, mapping Mapping, options ...Option) (string, error) { |
| 113 | + var returnErr error |
| 114 | + |
| 115 | + cfg := &Config{ |
| 116 | + pattern: defaultPattern, |
| 117 | + replacementFunc: DefaultReplacementFunc, |
| 118 | + logging: true, |
| 119 | + } |
| 120 | + for _, o := range options { |
| 121 | + o(cfg) |
| 122 | + } |
| 123 | + |
| 124 | + result := cfg.pattern.ReplaceAllStringFunc(template, func(substring string) string { |
| 125 | + replacement, err := cfg.replacementFunc(substring, mapping, cfg) |
| 126 | + if err != nil { |
| 127 | + // Add the template for template errors |
| 128 | + var tmplErr *InvalidTemplateError |
| 129 | + if errors.As(err, &tmplErr) { |
| 130 | + if tmplErr.Template == "" { |
| 131 | + tmplErr.Template = template |
123 | 132 | } |
124 | | - return "" |
125 | 133 | } |
126 | | - if applied { |
127 | | - interpolatedNested, err := SubstituteWith(rest, mapping, pattern) |
128 | | - if err != nil { |
129 | | - return "" |
130 | | - } |
131 | | - return value + interpolatedNested |
| 134 | + // Save the first error to be returned |
| 135 | + if returnErr == nil { |
| 136 | + returnErr = err |
132 | 137 | } |
133 | | - } |
134 | 138 |
|
135 | | - value, ok := mapping(substitution) |
136 | | - if !ok { |
137 | | - logrus.Warnf("The %q variable is not set. Defaulting to a blank string.", substitution) |
138 | 139 | } |
139 | | - return value |
| 140 | + return replacement |
140 | 141 | }) |
141 | 142 |
|
142 | 143 | return result, returnErr |
143 | 144 | } |
144 | 145 |
|
| 146 | +func DefaultReplacementFunc(substring string, mapping Mapping, cfg *Config) (string, error) { |
| 147 | + pattern := cfg.pattern |
| 148 | + subsFunc := cfg.substituteFunc |
| 149 | + if subsFunc == nil { |
| 150 | + _, subsFunc = getSubstitutionFunctionForTemplate(substring) |
| 151 | + } |
| 152 | + |
| 153 | + closingBraceIndex := getFirstBraceClosingIndex(substring) |
| 154 | + rest := "" |
| 155 | + if closingBraceIndex > -1 { |
| 156 | + rest = substring[closingBraceIndex+1:] |
| 157 | + substring = substring[0 : closingBraceIndex+1] |
| 158 | + } |
| 159 | + |
| 160 | + matches := pattern.FindStringSubmatch(substring) |
| 161 | + groups := matchGroups(matches, pattern) |
| 162 | + if escaped := groups["escaped"]; escaped != "" { |
| 163 | + return escaped, nil |
| 164 | + } |
| 165 | + |
| 166 | + braced := false |
| 167 | + substitution := groups["named"] |
| 168 | + if substitution == "" { |
| 169 | + substitution = groups["braced"] |
| 170 | + braced = true |
| 171 | + } |
| 172 | + |
| 173 | + if substitution == "" { |
| 174 | + return "", &InvalidTemplateError{} |
| 175 | + } |
| 176 | + |
| 177 | + if braced { |
| 178 | + value, applied, err := subsFunc(substitution, mapping) |
| 179 | + if err != nil { |
| 180 | + return "", err |
| 181 | + } |
| 182 | + if applied { |
| 183 | + interpolatedNested, err := SubstituteWith(rest, mapping, pattern) |
| 184 | + if err != nil { |
| 185 | + return "", err |
| 186 | + } |
| 187 | + return value + interpolatedNested, nil |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + value, ok := mapping(substitution) |
| 192 | + if !ok && cfg.logging { |
| 193 | + logrus.Warnf("The %q variable is not set. Defaulting to a blank string.", substitution) |
| 194 | + } |
| 195 | + |
| 196 | + return value, nil |
| 197 | +} |
| 198 | + |
| 199 | +// SubstituteWith substitute variables in the string with their values. |
| 200 | +// It accepts additional substitute function. |
| 201 | +func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, subsFuncs ...SubstituteFunc) (string, error) { |
| 202 | + options := []Option{ |
| 203 | + WithPattern(pattern), |
| 204 | + } |
| 205 | + if len(subsFuncs) > 0 { |
| 206 | + options = append(options, WithSubstitutionFunction(subsFuncs[0])) |
| 207 | + } |
| 208 | + |
| 209 | + return SubstituteWithOptions(template, mapping, options...) |
| 210 | +} |
| 211 | + |
145 | 212 | func getSubstitutionFunctionForTemplate(template string) (string, SubstituteFunc) { |
146 | 213 | interpolationMapping := []struct { |
147 | 214 | string |
|
0 commit comments