use aliases in all keys list to enable proper Unmarshaling

This commit is contained in:
Marcin Stanisławski 2016-01-20 22:15:43 +01:00
parent a212099cbe
commit a53b9f1829
2 changed files with 29 additions and 0 deletions

View file

@ -1149,6 +1149,10 @@ func (v *Viper) AllKeys() []string {
m[strings.ToLower(key)] = struct{}{} m[strings.ToLower(key)] = struct{}{}
} }
for key, _ := range v.aliases {
m[strings.ToLower(key)] = struct{}{}
}
a := []string{} a := []string{}
for x, _ := range m { for x, _ := range m {
a = append(a, x) a = append(a, x)

View file

@ -838,3 +838,28 @@ func TestMergeConfigNoMerge(t *testing.T) {
t.Fatalf("fu != \"bar\", = %s", fu) t.Fatalf("fu != \"bar\", = %s", fu)
} }
} }
func TestUnmarshalingWithAliases(t *testing.T) {
SetDefault("Id", 1)
Set("name", "Steve")
Set("lastname", "Owen")
RegisterAlias("UserID","Id")
RegisterAlias("Firstname","name")
RegisterAlias("Surname","lastname")
type config struct {
Id int
FirstName string
Surname string
}
var C config
err := Unmarshal(&C)
if err != nil {
t.Fatalf("unable to decode into struct, %v", err)
}
assert.Equal(t, &C, &config{Id: 1, FirstName: "Steve", Surname: "Owen"})
}