Add GetIntSlice functions

This commit is contained in:
mkrasilnikov 2017-03-02 10:52:48 +03:00
parent 5ed0fc31f7
commit 21e45f5ded
2 changed files with 37 additions and 0 deletions

View file

@ -684,6 +684,12 @@ func (v *Viper) GetStringSlice(key string) []string {
return cast.ToStringSlice(v.Get(key)) return cast.ToStringSlice(v.Get(key))
} }
// GetIntSlice returns the value associated with the key as a slice of ints.
func GetIntSlice(key string) []int { return v.GetIntSlice(key) }
func (v *Viper) GetIntSlice(key string) []int {
return cast.ToIntSlice(v.Get(key))
}
// GetStringMap returns the value associated with the key as a map of interfaces. // GetStringMap returns the value associated with the key as a map of interfaces.
func GetStringMap(key string) map[string]interface{} { return v.GetStringMap(key) } func GetStringMap(key string) map[string]interface{} { return v.GetStringMap(key) }
func (v *Viper) GetStringMap(key string) map[string]interface{} { func (v *Viper) GetStringMap(key string) map[string]interface{} {

View file

@ -818,6 +818,10 @@ hello:
- uk - uk
- fr - fr
- de - de
groups:
- 1
- 2
- 3
`) `)
var yamlMergeExampleSrc = []byte(` var yamlMergeExampleSrc = []byte(`
@ -827,6 +831,9 @@ hello:
universe: universe:
- mw - mw
- ad - ad
ages:
- 21
- 34
fu: bar fu: bar
`) `)
@ -853,6 +860,10 @@ func TestMergeConfig(t *testing.T) {
t.Fatalf("len(world) != 4, = %d", len(world)) t.Fatalf("len(world) != 4, = %d", len(world))
} }
if groups := v.GetIntSlice("hello.groups"); len(groups) != 3 {
t.Fatalf("len(groups) != 3, = %d", len(groups))
}
if fu := v.GetString("fu"); fu != "" { if fu := v.GetString("fu"); fu != "" {
t.Fatalf("fu != \"\", = %s", fu) t.Fatalf("fu != \"\", = %s", fu)
} }
@ -881,6 +892,14 @@ func TestMergeConfig(t *testing.T) {
t.Fatalf("len(universe) != 2, = %d", len(universe)) t.Fatalf("len(universe) != 2, = %d", len(universe))
} }
if groups := v.GetIntSlice("hello.groups"); len(groups) != 3 {
t.Fatalf("len(groups) != 3, = %d", len(groups))
}
if ages := v.GetIntSlice("hello.ages"); len(ages) != 2 {
t.Fatalf("len(ages) != 2, = %d", len(ages))
}
if fu := v.GetString("fu"); fu != "bar" { if fu := v.GetString("fu"); fu != "bar" {
t.Fatalf("fu != \"bar\", = %s", fu) t.Fatalf("fu != \"bar\", = %s", fu)
} }
@ -901,6 +920,10 @@ func TestMergeConfigNoMerge(t *testing.T) {
t.Fatalf("len(world) != 4, = %d", len(world)) t.Fatalf("len(world) != 4, = %d", len(world))
} }
if groups := v.GetIntSlice("hello.groups"); len(groups) != 3 {
t.Fatalf("len(groups) != 3, = %d", len(groups))
}
if fu := v.GetString("fu"); fu != "" { if fu := v.GetString("fu"); fu != "" {
t.Fatalf("fu != \"\", = %s", fu) t.Fatalf("fu != \"\", = %s", fu)
} }
@ -921,6 +944,14 @@ func TestMergeConfigNoMerge(t *testing.T) {
t.Fatalf("len(universe) != 2, = %d", len(universe)) t.Fatalf("len(universe) != 2, = %d", len(universe))
} }
if groups := v.GetIntSlice("hello.groups"); len(groups) != 0 {
t.Fatalf("len(groups) != 0, = %d", len(groups))
}
if ages := v.GetIntSlice("hello.ages"); len(ages) != 2 {
t.Fatalf("len(groups) != 2, = %d", len(ages))
}
if fu := v.GetString("fu"); fu != "bar" { if fu := v.GetString("fu"); fu != "bar" {
t.Fatalf("fu != \"bar\", = %s", fu) t.Fatalf("fu != \"bar\", = %s", fu)
} }