Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: premature return when toType=true and multiple replacements exist #3548

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 7 additions & 5 deletions config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,16 @@ func convertToType(input string) interface{} {
func expand(s string, mapping func(string) string, toType bool) interface{} {
r := regexp.MustCompile(`\${(.*?)}`)
re := r.FindAllStringSubmatch(s, -1)
var ct interface{}

// Convert to type if the string is a single `${VAR}` placeholder.
if toType && len(re) == 1 && regexp.MustCompile(`^\${(.*?)}$`).MatchString(s) {
m := mapping(re[0][1])
return convertToType(m)
}

for _, i := range re {
if len(i) == 2 { //nolint:mnd
m := mapping(i[1])
if toType {
ct = convertToType(m)
return ct
}
s = strings.ReplaceAll(s, i[0], m)
}
}
Expand Down
12 changes: 12 additions & 0 deletions config/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func TestDefaultResolver(t *testing.T) {
"value2": "$PORT",
"value3": "abc${PORT}foo${COUNT}bar",
"value4": "${foo${bar}}",
"url_with_port": "${URL:http://example.com}:8080",
},
},
"test": map[string]interface{}{
Expand Down Expand Up @@ -144,6 +145,11 @@ func TestDefaultResolver(t *testing.T) {
path: "foo.bar.value4",
expect: "}",
},
{
name: "test ${URL:http://example.com}:8080",
path: "foo.bar.url_with_port",
expect: "http://example.com:8080",
},
}

for _, test := range tests {
Expand Down Expand Up @@ -224,6 +230,7 @@ func TestNewDefaultResolver(t *testing.T) {
"value2": "$PORT",
"value3": "abc${PORT}foo${COUNT}bar",
"value4": "${foo${bar}}",
"url_with_port": "${URL:http://example.com}:8080",
},
},
"test": map[string]interface{}{
Expand Down Expand Up @@ -301,6 +308,11 @@ func TestNewDefaultResolver(t *testing.T) {
path: "foo.bar.value4",
expect: "",
},
{
name: "test ${URL:http://example.com}:8080",
path: "foo.bar.url_with_port",
expect: "http://example.com:8080",
},
}

for _, test := range tests {
Expand Down