diff --git a/viper.go b/viper.go index 3db9cd2..172e324 100644 --- a/viper.go +++ b/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 { diff --git a/viper_test.go b/viper_test.go index 8d7d152..148a44a 100644 --- a/viper_test.go +++ b/viper_test.go @@ -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"))