From 97bfe856f4be99cc6c0b654d6af7251df2f18182 Mon Sep 17 00:00:00 2001 From: Finch Keung Date: Thu, 19 Sep 2024 18:34:08 +0800 Subject: [PATCH] support time.Duration --- unmarshaler.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/unmarshaler.go b/unmarshaler.go index c3df8bee..59843aec 100644 --- a/unmarshaler.go +++ b/unmarshaler.go @@ -1022,7 +1022,17 @@ func (d *decoder) unmarshalInteger(value *unstable.Node, v reflect.Value) error return nil } +var durationType = reflect.TypeOf((time.Duration)(0)) + func (d *decoder) unmarshalString(value *unstable.Node, v reflect.Value) error { + if v.Type() == durationType { + x, err := time.ParseDuration(string(value.Data)) + if err != nil { + return unstable.NewParserError(d.p.Raw(value.Raw), d.typeMismatchString("string", v.Type())) + } + v.Set(reflect.ValueOf(x)) + return nil + } switch v.Kind() { case reflect.String: v.SetString(string(value.Data))