mirror of https://github.com/spf13/viper.git
use aliases in all keys list to enable proper Unmarshaling
This commit is contained in:
parent
a212099cbe
commit
a53b9f1829
4
viper.go
4
viper.go
|
@ -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)
|
||||||
|
|
|
@ -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"})
|
||||||
|
}
|
Loading…
Reference in New Issue