From e38b8a3bb3ba41b03b4e8870f979d29369809f6c Mon Sep 17 00:00:00 2001 From: Yauhen Lazurkin Date: Sat, 9 Jul 2016 15:37:52 +0300 Subject: [PATCH] Issue-105 Support time.Duration in viper.Unmarshal --- viper.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/viper.go b/viper.go index a2633b0..1b381dd 100644 --- a/viper.go +++ b/viper.go @@ -611,7 +611,19 @@ func (v *Viper) UnmarshalKey(key string, rawVal interface{}) error { // on the fields of the structure are properly set. func Unmarshal(rawVal interface{}) error { return v.Unmarshal(rawVal) } 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 { return err