mirror of
https://github.com/spf13/viper
synced 2025-04-28 08:17:17 +00:00
Merge 31f218ede6
into 1508a7ba44
This commit is contained in:
commit
20ebaa607f
2 changed files with 19 additions and 0 deletions
10
viper.go
10
viper.go
|
@ -1409,6 +1409,16 @@ func (v *Viper) registerAlias(alias, key string) {
|
||||||
delete(v.override, alias)
|
delete(v.override, alias)
|
||||||
v.override[key] = val
|
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
|
v.aliases[alias] = key
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -544,6 +544,15 @@ func TestAliasInConfigFile(t *testing.T) {
|
||||||
assert.Equal(t, false, v.Get("beard"))
|
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) {
|
func TestYML(t *testing.T) {
|
||||||
v := New()
|
v := New()
|
||||||
v.SetConfigType("yaml")
|
v.SetConfigType("yaml")
|
||||||
|
|
Loading…
Add table
Reference in a new issue