From 1d33b640d9945d3911e9ae4bcde897b10251f508 Mon Sep 17 00:00:00 2001 From: M Date: Mon, 2 Apr 2018 12:49:05 -0400 Subject: [PATCH 1/3] Adds Getter and related code for IntSlices --- viper.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/viper.go b/viper.go index e9966ba..da6881b 100644 --- a/viper.go +++ b/viper.go @@ -641,6 +641,8 @@ func (v *Viper) Get(key string) interface{} { return cast.ToDuration(val) case []string: return cast.ToStringSlice(val) + case []int: + return cast.ToIntSlice(val) } } @@ -682,6 +684,12 @@ func (v *Viper) GetInt(key string) int { return cast.ToInt(v.Get(key)) } +// GetIntSlice returns the value associated with the key as an integer slice. +func GetIntSlice(key string) []int { return v.GetIntSlice(key) } +func (v *Viper) GetIntSlice(key string) []int { + return cast.ToIntSlice(v.Get(key)) +} + // GetInt64 returns the value associated with the key as an integer. func GetInt64(key string) int64 { return v.GetInt64(key) } func (v *Viper) GetInt64(key string) int64 { From bd294940f9e16f10cc0888f7db744d1104b85044 Mon Sep 17 00:00:00 2001 From: M Date: Mon, 2 Apr 2018 13:30:26 -0400 Subject: [PATCH 2/3] Handles the case of merging pflag intSlices --- viper.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/viper.go b/viper.go index da6881b..d17f2b9 100644 --- a/viper.go +++ b/viper.go @@ -930,6 +930,11 @@ func (v *Viper) find(lcaseKey string) interface{} { s = strings.TrimSuffix(s, "]") res, _ := readAsCSV(s) return res + case "intSlice": + s := strings.TrimPrefix(flag.ValueString(), "[") + s = strings.TrimSuffix(s, "]") + res, _ := readAsCSV(s) + return res default: return flag.ValueString() } From 7b016062432be5a4c316c965c1604bc3be1f87f4 Mon Sep 17 00:00:00 2001 From: M Date: Mon, 2 Apr 2018 13:37:57 -0400 Subject: [PATCH 3/3] Removes superfluous block for intSlice, and appends to stringSlice instead --- viper.go | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/viper.go b/viper.go index d17f2b9..35d61c9 100644 --- a/viper.go +++ b/viper.go @@ -925,12 +925,7 @@ func (v *Viper) find(lcaseKey string) interface{} { return cast.ToInt(flag.ValueString()) case "bool": return cast.ToBool(flag.ValueString()) - case "stringSlice": - s := strings.TrimPrefix(flag.ValueString(), "[") - s = strings.TrimSuffix(s, "]") - res, _ := readAsCSV(s) - return res - case "intSlice": + case "stringSlice", "intSlice": s := strings.TrimPrefix(flag.ValueString(), "[") s = strings.TrimSuffix(s, "]") res, _ := readAsCSV(s) @@ -999,7 +994,7 @@ func (v *Viper) find(lcaseKey string) interface{} { return cast.ToInt(flag.ValueString()) case "bool": return cast.ToBool(flag.ValueString()) - case "stringSlice": + case "stringSlice", "intSlice": s := strings.TrimPrefix(flag.ValueString(), "[") s = strings.TrimSuffix(s, "]") res, _ := readAsCSV(s)