Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed bugs #309

Open
wants to merge 2 commits into
base: v5
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions decode_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,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)
Expand All @@ -76,6 +69,15 @@ func _getDecoder(typ reflect.Type) decoderFunc {
return nilAwareDecoder(typ, unmarshalTextValue)
}

kind := typ.Kind()
if kind == reflect.Ptr {
//if kind is ptr, like &msgpack.RawMessage, will use wrong cache msgpack.RawMessage => ptrValueDecoder
//and goto ptrValueDecoder if has nil code then panic
if _, ok := typeDecMap.Load(typ.Elem()); ok {
return ptrValueDecoder(typ)
}
}

// Addressable struct field value.
if kind != reflect.Ptr {
ptr := reflect.PtrTo(typ)
Expand Down Expand Up @@ -126,7 +128,8 @@ func ptrValueDecoder(typ reflect.Type) decoderFunc {
decoder := getDecoder(typ.Elem())
return func(d *Decoder, v reflect.Value) error {
if d.hasNilCode() {
if !v.IsNil() {
//nil code but v can't set and panic error
if !v.IsNil() && v.CanSet() {
v.Set(reflect.Zero(v.Type()))
}
return d.DecodeNil()
Expand Down