mirror of https://github.com/spf13/viper.git
implement nested config alias -test included
This commit is contained in:
parent
db7ff930a1
commit
12990ac1c8
8
viper.go
8
viper.go
|
@ -676,6 +676,14 @@ func (v *Viper) registerAlias(alias string, 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:])
|
||||
v.config[key] = val
|
||||
}
|
||||
}
|
||||
v.aliases[alias] = key
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -197,6 +197,15 @@ func TestAliasInConfigFile(t *testing.T) {
|
|||
assert.Equal(t, false, 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) {
|
||||
initYAML()
|
||||
assert.Equal(t, "steve", Get("name"))
|
||||
|
|
Loading…
Reference in New Issue