mirror of https://github.com/spf13/viper.git
properly handle various slice/array types
This commit is contained in:
parent
0967fc9ace
commit
945f4d6695
10
viper.go
10
viper.go
|
@ -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)
|
||||||
|
|
Loading…
Reference in New Issue