Add GetIntSlice helper method

This commit is contained in:
AGirard 2019-06-19 16:05:58 +02:00 committed by Márk Sági-Kazár
parent 72cbe340cb
commit b8221cf4ee
3 changed files with 18 additions and 0 deletions

View File

@ -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`

View File

@ -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 {

View File

@ -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)
}