Merge pull request #131 from goccy/feature/fix-interface

Fix decoding of pointer of empty interface type
This commit is contained in:
Masaaki Goshima 2021-02-15 00:19:00 +09:00 committed by GitHub
commit 402bd55e2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 11 deletions

View File

@ -32,6 +32,7 @@ func (d *interfaceDecoder) numDecoder(s *stream) decoder {
} }
var ( var (
emptyInterfaceType = type2rtype(reflect.TypeOf((*interface{})(nil)).Elem())
interfaceMapType = type2rtype( interfaceMapType = type2rtype(
reflect.TypeOf((*map[string]interface{})(nil)).Elem(), reflect.TypeOf((*map[string]interface{})(nil)).Elem(),
) )
@ -82,7 +83,7 @@ func (d *interfaceDecoder) decodeStreamEmptyInterface(s *stream, p unsafe.Pointe
stringType, stringType,
newStringDecoder(d.structName, d.fieldName), newStringDecoder(d.structName, d.fieldName),
interfaceMapType.Elem(), interfaceMapType.Elem(),
newInterfaceDecoder(d.typ, d.structName, d.fieldName), newInterfaceDecoder(emptyInterfaceType, d.structName, d.fieldName),
d.structName, d.structName,
d.fieldName, d.fieldName,
).decodeStream(s, ptr); err != nil { ).decodeStream(s, ptr); err != nil {
@ -94,9 +95,9 @@ func (d *interfaceDecoder) decodeStreamEmptyInterface(s *stream, p unsafe.Pointe
var v []interface{} var v []interface{}
ptr := unsafe.Pointer(&v) ptr := unsafe.Pointer(&v)
if err := newSliceDecoder( if err := newSliceDecoder(
newInterfaceDecoder(d.typ, d.structName, d.fieldName), newInterfaceDecoder(emptyInterfaceType, d.structName, d.fieldName),
d.typ, emptyInterfaceType,
d.typ.Size(), emptyInterfaceType.Size(),
d.structName, d.structName,
d.fieldName, d.fieldName,
).decodeStream(s, ptr); err != nil { ).decodeStream(s, ptr); err != nil {
@ -174,7 +175,7 @@ func (d *interfaceDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
iface := rv.Interface() iface := rv.Interface()
ifaceHeader := (*interfaceHeader)(unsafe.Pointer(&iface)) ifaceHeader := (*interfaceHeader)(unsafe.Pointer(&iface))
typ := ifaceHeader.typ typ := ifaceHeader.typ
if d.typ == typ || typ == nil { if ifaceHeader.ptr == nil || d.typ == typ || typ == nil {
// concrete type is empty interface // concrete type is empty interface
return d.decodeStreamEmptyInterface(s, p) return d.decodeStreamEmptyInterface(s, p)
} }
@ -204,7 +205,7 @@ func (d *interfaceDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (i
iface := rv.Interface() iface := rv.Interface()
ifaceHeader := (*interfaceHeader)(unsafe.Pointer(&iface)) ifaceHeader := (*interfaceHeader)(unsafe.Pointer(&iface))
typ := ifaceHeader.typ typ := ifaceHeader.typ
if d.typ == typ || typ == nil { if ifaceHeader.ptr == nil || d.typ == typ || typ == nil {
// concrete type is empty interface // concrete type is empty interface
return d.decodeEmptyInterface(buf, cursor, p) return d.decodeEmptyInterface(buf, cursor, p)
} }
@ -246,7 +247,7 @@ func (d *interfaceDecoder) decodeEmptyInterface(buf []byte, cursor int64, p unsa
stringType, stringType,
newStringDecoder(d.structName, d.fieldName), newStringDecoder(d.structName, d.fieldName),
interfaceMapType.Elem(), interfaceMapType.Elem(),
newInterfaceDecoder(d.typ, d.structName, d.fieldName), newInterfaceDecoder(emptyInterfaceType, d.structName, d.fieldName),
d.structName, d.fieldName, d.structName, d.fieldName,
) )
cursor, err := dec.decode(buf, cursor, ptr) cursor, err := dec.decode(buf, cursor, ptr)
@ -259,9 +260,9 @@ func (d *interfaceDecoder) decodeEmptyInterface(buf []byte, cursor int64, p unsa
var v []interface{} var v []interface{}
ptr := unsafe.Pointer(&v) ptr := unsafe.Pointer(&v)
dec := newSliceDecoder( dec := newSliceDecoder(
newInterfaceDecoder(d.typ, d.structName, d.fieldName), newInterfaceDecoder(emptyInterfaceType, d.structName, d.fieldName),
d.typ, emptyInterfaceType,
d.typ.Size(), emptyInterfaceType.Size(),
d.structName, d.fieldName, d.structName, d.fieldName,
) )
cursor, err := dec.decode(buf, cursor, ptr) cursor, err := dec.decode(buf, cursor, ptr)