forked from mirror/go-json
Fix decoding of map type that contains indirect element type
This commit is contained in:
parent
284c108638
commit
d494b03b74
|
@ -3767,6 +3767,7 @@ func TestIssue282(t *testing.T) {
|
||||||
}
|
}
|
||||||
type T0 map[string]T2
|
type T0 map[string]T2
|
||||||
|
|
||||||
|
// T2 size is 136 bytes. This is indirect type.
|
||||||
var v T0
|
var v T0
|
||||||
if err := json.Unmarshal(J, &v); err != nil {
|
if err := json.Unmarshal(J, &v); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|
|
@ -20,14 +20,11 @@ type mapDecoder struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func newMapDecoder(mapType *runtime.Type, keyType *runtime.Type, keyDec Decoder, valueType *runtime.Type, valueDec Decoder, structName, fieldName string) *mapDecoder {
|
func newMapDecoder(mapType *runtime.Type, keyType *runtime.Type, keyDec Decoder, valueType *runtime.Type, valueDec Decoder, structName, fieldName string) *mapDecoder {
|
||||||
isStructIndirect := valueType.Kind() == reflect.Struct && runtime.IfaceIndir(valueType)
|
|
||||||
stringKeyType := keyType.Kind() == reflect.String
|
|
||||||
canUseAssignFaststrType := stringKeyType && !isStructIndirect
|
|
||||||
return &mapDecoder{
|
return &mapDecoder{
|
||||||
mapType: mapType,
|
mapType: mapType,
|
||||||
keyDecoder: keyDec,
|
keyDecoder: keyDec,
|
||||||
keyType: keyType,
|
keyType: keyType,
|
||||||
canUseAssignFaststrType: canUseAssignFaststrType,
|
canUseAssignFaststrType: canUseAssignFaststrType(keyType, valueType),
|
||||||
valueType: valueType,
|
valueType: valueType,
|
||||||
valueDecoder: valueDec,
|
valueDecoder: valueDec,
|
||||||
structName: structName,
|
structName: structName,
|
||||||
|
@ -35,6 +32,19 @@ func newMapDecoder(mapType *runtime.Type, keyType *runtime.Type, keyDec Decoder,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
mapMaxElemSize = 128
|
||||||
|
)
|
||||||
|
|
||||||
|
// See detail: https://github.com/goccy/go-json/pull/283
|
||||||
|
func canUseAssignFaststrType(key *runtime.Type, value *runtime.Type) bool {
|
||||||
|
indirectElem := value.Size() > mapMaxElemSize
|
||||||
|
if indirectElem {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return key.Kind() == reflect.String
|
||||||
|
}
|
||||||
|
|
||||||
//go:linkname makemap reflect.makemap
|
//go:linkname makemap reflect.makemap
|
||||||
func makemap(*runtime.Type, int) unsafe.Pointer
|
func makemap(*runtime.Type, int) unsafe.Pointer
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue