properly handle various slice/array types

This commit is contained in:
Roman Volosatovs 2017-04-18 15:52:21 +02:00
parent 0967fc9ace
commit 945f4d6695
No known key found for this signature in database
GPG Key ID: 3AC661943D80C89E
1 changed files with 8 additions and 2 deletions

View File

@ -620,8 +620,14 @@ func (v *Viper) Get(key string) interface{} {
return cast.ToTime(val) return cast.ToTime(val)
case time.Duration: case time.Duration:
return cast.ToDuration(val) return cast.ToDuration(val)
case []bool:
return cast.ToBoolSlice(val)
case []string: case []string:
return cast.ToStringSlice(val) return cast.ToStringSlice(val)
case []int:
return cast.ToIntSlice(val)
case []time.Duration:
return cast.ToDurationSlice(val)
} }
return val return val
} }
@ -893,7 +899,7 @@ func (v *Viper) find(lcaseKey string) interface{} {
return cast.ToInt(flag.ValueString()) return cast.ToInt(flag.ValueString())
case "bool": case "bool":
return cast.ToBool(flag.ValueString()) return cast.ToBool(flag.ValueString())
case "stringSlice": case "stringSlice", "stringArray", "boolSlice", "ipSlice", "uintSlice", "intSlice":
s := strings.TrimPrefix(flag.ValueString(), "[") s := strings.TrimPrefix(flag.ValueString(), "[")
s = strings.TrimSuffix(s, "]") s = strings.TrimSuffix(s, "]")
res, _ := readAsCSV(s) res, _ := readAsCSV(s)
@ -962,7 +968,7 @@ func (v *Viper) find(lcaseKey string) interface{} {
return cast.ToInt(flag.ValueString()) return cast.ToInt(flag.ValueString())
case "bool": case "bool":
return cast.ToBool(flag.ValueString()) return cast.ToBool(flag.ValueString())
case "stringSlice": case "stringSlice", "stringArray", "boolSlice", "ipSlice", "uintSlice", "intSlice":
s := strings.TrimPrefix(flag.ValueString(), "[") s := strings.TrimPrefix(flag.ValueString(), "[")
s = strings.TrimSuffix(s, "]") s = strings.TrimSuffix(s, "]")
res, _ := readAsCSV(s) res, _ := readAsCSV(s)