implement nested config alias -test included

This commit is contained in:
Steeve Chailloux 2015-07-03 19:37:40 -05:00
parent db7ff930a1
commit 12990ac1c8
2 changed files with 17 additions and 0 deletions

View file

@ -676,6 +676,14 @@ func (v *Viper) registerAlias(alias string, 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:])
v.config[key] = val
}
}
v.aliases[alias] = key v.aliases[alias] = key
} }
} else { } else {

View file

@ -197,6 +197,15 @@ func TestAliasInConfigFile(t *testing.T) {
assert.Equal(t, false, Get("beard")) 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) { func TestYML(t *testing.T) {
initYAML() initYAML()
assert.Equal(t, "steve", Get("name")) assert.Equal(t, "steve", Get("name"))