diff --git a/viper.go b/viper.go index 2a221e5..5587827 100644 --- a/viper.go +++ b/viper.go @@ -691,6 +691,12 @@ func (v *Viper) GetStringSlice(key string) []string { 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. func GetStringMap(key string) map[string]interface{} { return v.GetStringMap(key) } func (v *Viper) GetStringMap(key string) map[string]interface{} { diff --git a/viper_test.go b/viper_test.go index 774ca11..6fa8413 100644 --- a/viper_test.go +++ b/viper_test.go @@ -856,6 +856,10 @@ hello: - uk - fr - de + groups: + - 1 + - 2 + - 3 `) var yamlMergeExampleSrc = []byte(` @@ -865,6 +869,9 @@ hello: universe: - mw - ad + ages: + - 21 + - 34 fu: bar `) @@ -891,6 +898,10 @@ func TestMergeConfig(t *testing.T) { 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 != "" { t.Fatalf("fu != \"\", = %s", fu) } @@ -919,6 +930,14 @@ func TestMergeConfig(t *testing.T) { 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" { t.Fatalf("fu != \"bar\", = %s", fu) } @@ -939,6 +958,10 @@ func TestMergeConfigNoMerge(t *testing.T) { 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 != "" { t.Fatalf("fu != \"\", = %s", fu) } @@ -959,6 +982,14 @@ func TestMergeConfigNoMerge(t *testing.T) { 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" { t.Fatalf("fu != \"bar\", = %s", fu) }