Skip to content

Commit 5d42a7d

Browse files
committed
Merge remote-tracking branch 'tchajed/unmarshal-ptr' into develop
Conflicts: encoding/decoder_types.go
2 parents df105e0 + cfa7fa0 commit 5d42a7d

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

encoding/decoder_types.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99

1010
// newTypeDecoder constructs an decoderFunc for a type.
1111
func newTypeDecoder(dt, st reflect.Type) decoderFunc {
12-
if dt.Implements(unmarshalerType) {
12+
if reflect.PtrTo(dt).Implements(unmarshalerType) ||
13+
dt.Implements(unmarshalerType) {
1314
return unmarshalerDecoder
1415
}
1516

@@ -204,11 +205,9 @@ func newPtrDecoder(dt, st reflect.Type) decoderFunc {
204205
}
205206

206207
func unmarshalerDecoder(dv, sv reflect.Value) {
207-
if dv.Kind() != reflect.Ptr {
208-
panic(&InvalidUnmarshalError{sv.Type()})
209-
}
210-
if dv.IsNil() {
211-
dv.Set(reflect.New(dv.Type().Elem()))
208+
// modeled off of https://golang.org/src/encoding/json/decode.go?#L325
209+
if dv.Kind() != reflect.Ptr && dv.Type().Name() != "" && dv.CanAddr() {
210+
dv = dv.Addr()
212211
}
213212

214213
u := dv.Interface().(Unmarshaler)

0 commit comments

Comments
 (0)