From e072d51737b175ac83688b04999b509edc7908e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Stanis=C5=82awski?= Date: Wed, 20 Jan 2016 22:15:43 +0100 Subject: [PATCH] use aliases in all keys list to enable proper Unmarshaling --- viper.go | 4 ++++ viper_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/viper.go b/viper.go index 00049ec..a056e61 100644 --- a/viper.go +++ b/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) diff --git a/viper_test.go b/viper_test.go index fcb9c4e..ebde5ba 100644 --- a/viper_test.go +++ b/viper_test.go @@ -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"}) +} \ No newline at end of file