diff --git a/README.md b/README.md index f4dc3bd..171f51c 100644 --- a/README.md +++ b/README.md @@ -496,6 +496,7 @@ The following functions and methods exist: * `GetBool(key string) : bool` * `GetFloat64(key string) : float64` * `GetInt(key string) : int` + * `GetIntSlice(key string) : []int` * `GetString(key string) : string` * `GetStringMap(key string) : map[string]interface{}` * `GetStringMapString(key string) : map[string]string` diff --git a/viper.go b/viper.go index 0699c4c..881bf0c 100644 --- a/viper.go +++ b/viper.go @@ -797,6 +797,12 @@ func (v *Viper) GetDuration(key string) time.Duration { return cast.ToDuration(v.Get(key)) } +// GetIntSlice returns the value associated with the key as a slice of strings. +func GetIntSlice(key string) []int { return v.GetIntSlice(key) } +func (v *Viper) GetIntSlice(key string) []int { + return cast.ToIntSlice(v.Get(key)) +} + // GetStringSlice returns the value associated with the key as a slice of strings. func GetStringSlice(key string) []string { return v.GetStringSlice(key) } func (v *Viper) GetStringSlice(key string) []string { diff --git a/viper_test.go b/viper_test.go index c40c971..75afaae 100644 --- a/viper_test.go +++ b/viper_test.go @@ -1262,6 +1262,9 @@ hello: universe: - mw - ad + ints: + - 1 + - 2 fu: bar `) @@ -1328,6 +1331,10 @@ func TestMergeConfig(t *testing.T) { t.Fatalf("len(universe) != 2, = %d", len(universe)) } + if ints := v.GetIntSlice("hello.ints"); len(ints) != 2 { + t.Fatalf("len(ints) != 2, = %d", len(ints)) + } + if fu := v.GetString("fu"); fu != "bar" { t.Fatalf("fu != \"bar\", = %s", fu) } @@ -1368,6 +1375,10 @@ func TestMergeConfigNoMerge(t *testing.T) { t.Fatalf("len(universe) != 2, = %d", len(universe)) } + if ints := v.GetIntSlice("hello.ints"); len(ints) != 2 { + t.Fatalf("len(ints) != 2, = %d", len(ints)) + } + if fu := v.GetString("fu"); fu != "bar" { t.Fatalf("fu != \"bar\", = %s", fu) }