mirror of
https://github.com/spf13/viper
synced 2025-05-06 12:17:18 +00:00
Issue-105 Support time.Duration in viper.Unmarshal
This commit is contained in:
parent
c1ccc378a0
commit
e38b8a3bb3
1 changed files with 13 additions and 1 deletions
14
viper.go
14
viper.go
|
@ -611,7 +611,19 @@ func (v *Viper) UnmarshalKey(key string, rawVal interface{}) error {
|
||||||
// on the fields of the structure are properly set.
|
// on the fields of the structure are properly set.
|
||||||
func Unmarshal(rawVal interface{}) error { return v.Unmarshal(rawVal) }
|
func Unmarshal(rawVal interface{}) error { return v.Unmarshal(rawVal) }
|
||||||
func (v *Viper) Unmarshal(rawVal interface{}) error {
|
func (v *Viper) Unmarshal(rawVal interface{}) error {
|
||||||
err := mapstructure.WeakDecode(v.AllSettings(), rawVal)
|
config := &mapstructure.DecoderConfig{
|
||||||
|
Metadata: nil,
|
||||||
|
Result: rawVal,
|
||||||
|
WeaklyTypedInput: true,
|
||||||
|
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
|
||||||
|
}
|
||||||
|
|
||||||
|
decoder, err := mapstructure.NewDecoder(config)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = decoder.Decode(v.AllSettings())
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
Loading…
Add table
Reference in a new issue