This commit is contained in:
Steeve 2025-03-30 16:49:09 +02:00 committed by GitHub
commit 20ebaa607f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 0 deletions

View file

@ -1409,6 +1409,16 @@ func (v *Viper) registerAlias(alias, key string) {
delete(v.override, alias)
v.override[key] = val
}
if strings.Contains(alias, v.keyDelim) {
path := strings.Split(alias, v.keyDelim)
source := v.find(path[0])
if source != nil && reflect.TypeOf(source).Kind() == reflect.Map {
val := v.searchMap(cast.ToStringMap(source), path[1:])
if val != nil {
v.config[key] = val
}
}
}
v.aliases[alias] = key
}
} else {

View file

@ -544,6 +544,15 @@ func TestAliasInConfigFile(t *testing.T) {
assert.Equal(t, false, v.Get("beard"))
}
func TestAliasNestedInConfigFile(t *testing.T) {
// the config file specifies "clothing.jacket". If we make this an alias for
// "clothing-jacket", we still want the old config file to work with "clothing.jacket".
RegisterAlias("clothing.jacket", "clothing-jacket")
assert.Equal(t, "leather", Get("clothing-jacket"))
Set("clothing-jacket", "nylon")
assert.Equal(t, "nylon", Get("clothing.jacket"))
}
func TestYML(t *testing.T) {
v := New()
v.SetConfigType("yaml")