Skip to content

Commit

Permalink
fix(reflect): not assignable to type
Browse files Browse the repository at this point in the history
  • Loading branch information
choyri committed Oct 25, 2023
1 parent 3f7bd80 commit edeaedd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion decode_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func ptrValueDecoder(typ reflect.Type) decoderFunc {
return func(d *Decoder, v reflect.Value) error {
if d.hasNilCode() {
if !v.IsNil() {
v.Set(d.newValue(typ))
v.Set(reflect.Zero(v.Type()))
}
return d.DecodeNil()
}
Expand Down
25 changes: 25 additions & 0 deletions msgpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,3 +528,28 @@ func TestEncodeWrappedValue(t *testing.T) {
require.Nil(t, msgpack.NewEncoder(&buf).Encode(v))
require.Nil(t, msgpack.NewEncoder(&buf).Encode(c))
}

func TestPtrValueDecode(t *testing.T) {
type Foo struct {
Bar *int
}

b, err := msgpack.Marshal(Foo{})
require.Nil(t, err)

bar1 := 123
foo := Foo{Bar: &bar1}

err = msgpack.Unmarshal(b, &foo)
require.Nil(t, err)
require.Nil(t, foo.Bar)

bar2 := 456
b, err = msgpack.Marshal(Foo{Bar: &bar2})
require.Nil(t, err)

err = msgpack.Unmarshal(b, &foo)
require.Nil(t, err)
require.NotNil(t, foo.Bar)
require.Equal(t, *foo.Bar, bar2)
}

0 comments on commit edeaedd

Please sign in to comment.