mirror of
https://github.com/spf13/viper
synced 2024-12-22 19:47:01 +00:00
use aliases in all keys list to enable proper Unmarshaling
This commit is contained in:
parent
a212099cbe
commit
a53b9f1829
2 changed files with 29 additions and 0 deletions
4
viper.go
4
viper.go
|
@ -1149,6 +1149,10 @@ func (v *Viper) AllKeys() []string {
|
|||
m[strings.ToLower(key)] = struct{}{}
|
||||
}
|
||||
|
||||
for key, _ := range v.aliases {
|
||||
m[strings.ToLower(key)] = struct{}{}
|
||||
}
|
||||
|
||||
a := []string{}
|
||||
for x, _ := range m {
|
||||
a = append(a, x)
|
||||
|
|
|
@ -838,3 +838,28 @@ func TestMergeConfigNoMerge(t *testing.T) {
|
|||
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 a new issue