From 3cf55ab8dc58f76b739b801c053b6711b9cd4ded Mon Sep 17 00:00:00 2001 From: SR Date: Mon, 11 Sep 2023 21:07:30 +0300 Subject: [PATCH] fix bug #310 --- decode_value.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/decode_value.go b/decode_value.go index 556c6ac..be27437 100644 --- a/decode_value.go +++ b/decode_value.go @@ -56,14 +56,6 @@ func getDecoder(typ reflect.Type) decoderFunc { } func _getDecoder(typ reflect.Type) decoderFunc { - kind := typ.Kind() - - if kind == reflect.Ptr { - if _, ok := typeDecMap.Load(typ.Elem()); ok { - return ptrValueDecoder(typ) - } - } - if typ.Implements(customDecoderType) { return nilAwareDecoder(typ, decodeCustomValue) } @@ -77,6 +69,14 @@ func _getDecoder(typ reflect.Type) decoderFunc { return nilAwareDecoder(typ, unmarshalTextValue) } + kind := typ.Kind() + + if kind == reflect.Ptr { + if _, ok := typeDecMap.Load(typ.Elem()); ok { + return ptrValueDecoder(typ) + } + } + // Addressable struct field value. if kind != reflect.Ptr { ptr := reflect.PtrTo(typ) @@ -127,7 +127,7 @@ func ptrValueDecoder(typ reflect.Type) decoderFunc { decoder := getDecoder(typ.Elem()) return func(d *Decoder, v reflect.Value) error { if d.hasNilCode() { - if !v.IsNil() { + if !v.IsNil() && v.CanSet() { v.Set(d.newValue(typ)) } return d.DecodeNil()