mirror of
https://github.com/spf13/viper
synced 2024-12-23 03:57:01 +00:00
clean up
This commit is contained in:
parent
66afe6ded0
commit
568aa46942
1 changed files with 29 additions and 23 deletions
66
viper.go
66
viper.go
|
@ -689,47 +689,53 @@ func (v *Viper) searchMap(source map[string]any, path []string) any {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// searchMapWithAliases recursively searches for slice field in source map and
|
||||||
|
// replace them with the environment variable value if it exists.
|
||||||
|
//
|
||||||
|
// Returns replaced values.
|
||||||
func (v *Viper) searchAndReplaceSliceValueWithEnv(source any, envKey string) any {
|
func (v *Viper) searchAndReplaceSliceValueWithEnv(source any, envKey string) any {
|
||||||
switch v1 := source.(type) {
|
switch sourceValue := source.(type) {
|
||||||
case []any:
|
case []any:
|
||||||
var newSlices []any
|
var newSliceValues []any
|
||||||
for i, value := range v1 {
|
for i, sliceValue := range sourceValue {
|
||||||
envKey := envKey + v.keyDelim + strconv.Itoa(i)
|
envKey := envKey + v.keyDelim + strconv.Itoa(i)
|
||||||
switch v2 := value.(type) {
|
switch existingValue := sliceValue.(type) {
|
||||||
case map[string]any:
|
case map[string]any:
|
||||||
val := v.searchAndReplaceSliceValueWithEnv(v2, envKey)
|
newVal := v.searchAndReplaceSliceValueWithEnv(existingValue, envKey)
|
||||||
newSlices = append(newSlices, val)
|
newSliceValues = append(newSliceValues, newVal)
|
||||||
default:
|
|
||||||
if val, ok := v.getEnv(v.mergeWithEnvPrefix(envKey)); ok {
|
|
||||||
newSlices = append(newSlices, val)
|
|
||||||
} else {
|
|
||||||
newSlices = append(newSlices, v2)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return newSlices
|
|
||||||
case map[string]any:
|
|
||||||
var newMapValue map[string]any = make(map[string]any)
|
|
||||||
|
|
||||||
for k, v2 := range v1 {
|
|
||||||
envKey := envKey + v.keyDelim + k
|
|
||||||
switch v3 := v2.(type) {
|
|
||||||
case map[string]any:
|
|
||||||
val := v.searchAndReplaceSliceValueWithEnv(v3, envKey)
|
|
||||||
newMapValue[k] = val
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if val, ok := v.getEnv(v.mergeWithEnvPrefix(envKey)); ok {
|
if newVal, ok := v.getEnv(v.mergeWithEnvPrefix(envKey)); ok {
|
||||||
newMapValue[k] = val
|
newSliceValues = append(newSliceValues, newVal)
|
||||||
} else {
|
} else {
|
||||||
newMapValue[k] = v3
|
newSliceValues = append(newSliceValues, existingValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return newMapValue
|
return newSliceValues
|
||||||
|
|
||||||
|
case map[string]any:
|
||||||
|
var newMapValues map[string]any = make(map[string]any)
|
||||||
|
for key, mapValue := range sourceValue {
|
||||||
|
envKey := envKey + v.keyDelim + key
|
||||||
|
switch existingValue := mapValue.(type) {
|
||||||
|
case map[string]any:
|
||||||
|
newVal := v.searchAndReplaceSliceValueWithEnv(existingValue, envKey)
|
||||||
|
newMapValues[key] = newVal
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if val, ok := v.getEnv(v.mergeWithEnvPrefix(envKey)); ok {
|
if newVal, ok := v.getEnv(v.mergeWithEnvPrefix(envKey)); ok {
|
||||||
return val
|
newMapValues[key] = newVal
|
||||||
|
} else {
|
||||||
|
newMapValues[key] = existingValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newMapValues
|
||||||
|
|
||||||
|
default:
|
||||||
|
if newVal, ok := v.getEnv(v.mergeWithEnvPrefix(envKey)); ok {
|
||||||
|
return newVal
|
||||||
} else {
|
} else {
|
||||||
return source
|
return source
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue