Skip to content

Commit 48acfbe

Browse files
alec404Alec
authored andcommitted
fix: premature return when toType=true and multiple replacements exist
1 parent d9aa7ff commit 48acfbe

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

config/options.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,16 @@ func convertToType(input string) any {
181181
func expand(s string, mapping func(string) string, toType bool) any {
182182
r := regexp.MustCompile(`\${(.*?)}`)
183183
re := r.FindAllStringSubmatch(s, -1)
184-
var ct any
184+
185+
// Convert to type if the string is a single `${VAR}` placeholder.
186+
if toType && len(re) == 1 && regexp.MustCompile(`^\${(.*?)}$`).MatchString(s) {
187+
m := mapping(re[0][1])
188+
return convertToType(m)
189+
}
190+
185191
for _, i := range re {
186192
if len(i) == 2 { //nolint:mnd
187193
m := mapping(i[1])
188-
if toType {
189-
ct = convertToType(m)
190-
return ct
191-
}
192194
s = strings.ReplaceAll(s, i[0], m)
193195
}
194196
}

config/options_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func TestDefaultResolver(t *testing.T) {
6767
"value2": "$PORT",
6868
"value3": "abc${PORT}foo${COUNT}bar",
6969
"value4": "${foo${bar}}",
70+
"url_with_port": "${URL:http://example.com}:8080",
7071
},
7172
},
7273
"test": map[string]any{
@@ -144,6 +145,11 @@ func TestDefaultResolver(t *testing.T) {
144145
path: "foo.bar.value4",
145146
expect: "}",
146147
},
148+
{
149+
name: "test ${URL:http://example.com}:8080",
150+
path: "foo.bar.url_with_port",
151+
expect: "http://example.com:8080",
152+
},
147153
}
148154

149155
for _, test := range tests {
@@ -224,6 +230,7 @@ func TestNewDefaultResolver(t *testing.T) {
224230
"value2": "$PORT",
225231
"value3": "abc${PORT}foo${COUNT}bar",
226232
"value4": "${foo${bar}}",
233+
"url_with_port": "${URL:http://example.com}:8080",
227234
},
228235
},
229236
"test": map[string]any{
@@ -301,6 +308,11 @@ func TestNewDefaultResolver(t *testing.T) {
301308
path: "foo.bar.value4",
302309
expect: "",
303310
},
311+
{
312+
name: "test ${URL:http://example.com}:8080",
313+
path: "foo.bar.url_with_port",
314+
expect: "http://example.com:8080",
315+
},
304316
}
305317

306318
for _, test := range tests {

0 commit comments

Comments
 (0)