Skip to content

Commit

Permalink
Merge pull request #359 from choyri/bugfix/reflect-set
Browse files Browse the repository at this point in the history
fix(reflect): not assignable to type
  • Loading branch information
vmihailenco authored Oct 26, 2023
2 parents 3f7bd80 + edeaedd commit 6eedee3
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 6eedee3

Please sign in to comment.