Skip to content

Commit

Permalink
support escaping in nested expansions
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerHelmuth committed Aug 6, 2024
1 parent 6caabcd commit e2167ad
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
8 changes: 8 additions & 0 deletions confmap/internal/e2e/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
func Test_EscapedEnvVars_NoDefaultScheme(t *testing.T) {
const expandedValue = "some expanded value"
t.Setenv("ENV_VALUE", expandedValue)
t.Setenv("ENV_LIST", "['$$ESCAPE_ME','$$ESCAPE_ME']")
t.Setenv("ENV_MAP", "{'key': '$$ESCAPE_ME'}")

expectedMap := map[string]any{
"test_map": map[string]any{
Expand All @@ -38,6 +40,8 @@ func Test_EscapedEnvVars_NoDefaultScheme(t *testing.T) {
"key13": "env:MAP_VALUE_2}${ENV_VALUE}{",
"key14": "$" + expandedValue,
"key15": "$ENV_VALUE",
"key16": []any{"$ESCAPE_ME", "$ESCAPE_ME"},
"key17": map[string]any{"key": "$ESCAPE_ME"},
},
}

Expand All @@ -59,6 +63,8 @@ func Test_EscapedEnvVars_NoDefaultScheme(t *testing.T) {
func Test_EscapedEnvVars_DefaultScheme(t *testing.T) {
const expandedValue = "some expanded value"
t.Setenv("ENV_VALUE", expandedValue)
t.Setenv("ENV_LIST", "['$$ESCAPE_ME','$$ESCAPE_ME']")
t.Setenv("ENV_MAP", "{'key': '$$ESCAPE_ME'}")

expectedMap := map[string]any{
"test_map": map[string]any{
Expand All @@ -77,6 +83,8 @@ func Test_EscapedEnvVars_DefaultScheme(t *testing.T) {
"key13": "env:MAP_VALUE_2}${ENV_VALUE}{",
"key14": "$" + expandedValue,
"key15": "$ENV_VALUE",
"key16": []any{"$ESCAPE_ME", "$ESCAPE_ME"},
"key17": map[string]any{"key": "$ESCAPE_ME"},
},
}

Expand Down
4 changes: 4 additions & 0 deletions confmap/internal/e2e/testdata/expand-escaped-env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ test_map:
key14: "$$${env:ENV_VALUE}"
# $ -> $
key15: "$ENV_VALUE"
# list is escaped
key16: "${env:ENV_LIST}"
# map is escaped
key17: "${env:ENV_MAP}"
31 changes: 26 additions & 5 deletions confmap/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,7 @@ func (mr *Resolver) Resolve(ctx context.Context) (*Conf, error) {
if err != nil {
return nil, err
}
if v, ok := val.(string); ok {
cfgMap[k] = strings.ReplaceAll(v, "$$", "$")
} else {
cfgMap[k] = val
}
cfgMap[k] = escapeDollarSign(val)
}
retMap = NewFromStringMap(cfgMap)

Expand All @@ -199,6 +195,31 @@ func (mr *Resolver) Resolve(ctx context.Context) (*Conf, error) {
return retMap, nil
}

func escapeDollarSign(val any) any {
switch v := val.(type) {
case string:
return strings.ReplaceAll(v, "$$", "$")
case expandedValue:
v.Original = strings.ReplaceAll(v.Original, "$$", "$")
v.Value = escapeDollarSign(v.Value)
return v
case []any:
nslice := make([]any, len(v), len(v))
for i, x := range v {
nslice[i] = escapeDollarSign(x)
}
return nslice
case map[string]any:
nmap := make(map[string]any, len(v))
for k, x := range v {
nmap[k] = escapeDollarSign(x)
}
return nmap
default:
return val
}
}

// Watch blocks until any configuration change was detected or an unrecoverable error
// happened during monitoring the configuration changes.
//
Expand Down

0 comments on commit e2167ad

Please sign in to comment.