go-json/internal/decoder/map.go

188 lines
4.7 KiB
Go
Raw Normal View History

2021-06-03 12:49:01 +03:00
package decoder
2020-04-25 16:48:16 +03:00
import (
"reflect"
2020-04-25 16:48:16 +03:00
"unsafe"
2021-06-03 12:49:01 +03:00
"github.com/goccy/go-json/internal/errors"
"github.com/goccy/go-json/internal/runtime"
2020-04-25 16:48:16 +03:00
)
type mapDecoder struct {
2021-08-30 05:40:10 +03:00
mapType *runtime.Type
keyType *runtime.Type
valueType *runtime.Type
canUseAssignFaststrType bool
keyDecoder Decoder
valueDecoder Decoder
structName string
fieldName string
2020-04-25 16:48:16 +03:00
}
2021-06-03 13:10:17 +03:00
func newMapDecoder(mapType *runtime.Type, keyType *runtime.Type, keyDec Decoder, valueType *runtime.Type, valueDec Decoder, structName, fieldName string) *mapDecoder {
2020-04-25 16:48:16 +03:00
return &mapDecoder{
2021-08-30 05:40:10 +03:00
mapType: mapType,
keyDecoder: keyDec,
keyType: keyType,
canUseAssignFaststrType: canUseAssignFaststrType(keyType, valueType),
2021-08-30 05:40:10 +03:00
valueType: valueType,
valueDecoder: valueDec,
structName: structName,
fieldName: fieldName,
2020-04-25 16:48:16 +03:00
}
}
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
}
2020-04-25 16:48:16 +03:00
//go:linkname makemap reflect.makemap
2021-06-03 12:49:01 +03:00
func makemap(*runtime.Type, int) unsafe.Pointer
2020-04-25 16:48:16 +03:00
2021-06-23 11:42:30 +03:00
//nolint:golint
//go:linkname mapassign_faststr runtime.mapassign_faststr
//go:noescape
func mapassign_faststr(t *runtime.Type, m unsafe.Pointer, s string) unsafe.Pointer
2020-04-25 16:48:16 +03:00
//go:linkname mapassign reflect.mapassign
//go:noescape
func mapassign(t *runtime.Type, m unsafe.Pointer, k, v unsafe.Pointer)
func (d *mapDecoder) mapassign(t *runtime.Type, m, k, v unsafe.Pointer) {
2021-08-30 05:40:10 +03:00
if d.canUseAssignFaststrType {
mapV := mapassign_faststr(t, m, *(*string)(k))
typedmemmove(d.valueType, mapV, v)
} else {
mapassign(t, m, k, v)
}
}
2020-04-25 16:48:16 +03:00
2021-06-03 12:49:01 +03:00
func (d *mapDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) error {
depth++
if depth > maxDecodeNestingDepth {
2021-06-03 12:49:01 +03:00
return errors.ErrExceededMaxDepth(s.char(), s.cursor)
}
2021-06-04 08:59:43 +03:00
switch s.skipWhiteSpace() {
2020-08-08 07:20:42 +03:00
case 'n':
if err := nullBytes(s); err != nil {
return err
}
2021-02-16 19:51:42 +03:00
**(**unsafe.Pointer)(unsafe.Pointer(&p)) = nil
2020-08-08 07:20:42 +03:00
return nil
case '{':
default:
2021-06-03 12:49:01 +03:00
return errors.ErrExpected("{ character for map value", s.totalOffset())
2020-07-30 16:41:53 +03:00
}
2021-02-18 06:13:49 +03:00
mapValue := *(*unsafe.Pointer)(p)
if mapValue == nil {
mapValue = makemap(d.mapType, 0)
}
s.cursor++
if s.equalChar('}') {
2020-12-05 16:27:33 +03:00
*(*unsafe.Pointer)(p) = mapValue
s.cursor++
2020-08-12 10:54:15 +03:00
return nil
}
2020-07-31 11:10:03 +03:00
for {
2020-12-22 15:55:59 +03:00
k := unsafe_New(d.keyType)
2021-06-03 12:49:01 +03:00
if err := d.keyDecoder.DecodeStream(s, depth, k); err != nil {
2020-07-30 16:41:53 +03:00
return err
}
s.skipWhiteSpace()
if !s.equalChar(':') {
2021-06-03 12:49:01 +03:00
return errors.ErrExpected("colon after object key", s.totalOffset())
2020-07-30 16:41:53 +03:00
}
2020-07-31 11:10:03 +03:00
s.cursor++
2020-12-22 15:55:59 +03:00
v := unsafe_New(d.valueType)
2021-06-03 12:49:01 +03:00
if err := d.valueDecoder.DecodeStream(s, depth, v); err != nil {
2020-07-30 16:41:53 +03:00
return err
}
d.mapassign(d.mapType, mapValue, k, v)
2020-07-30 16:41:53 +03:00
s.skipWhiteSpace()
if s.equalChar('}') {
**(**unsafe.Pointer)(unsafe.Pointer(&p)) = mapValue
2020-08-08 07:43:24 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
return nil
}
if !s.equalChar(',') {
2021-06-03 12:49:01 +03:00
return errors.ErrExpected("comma after object value", s.totalOffset())
2020-07-30 16:41:53 +03:00
}
s.cursor++
2020-07-30 16:41:53 +03:00
}
}
2021-06-04 19:08:27 +03:00
func (d *mapDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) {
buf := ctx.Buf
depth++
if depth > maxDecodeNestingDepth {
2021-06-03 12:49:01 +03:00
return 0, errors.ErrExceededMaxDepth(buf[cursor], cursor)
}
2020-05-06 20:37:29 +03:00
cursor = skipWhiteSpace(buf, cursor)
2020-05-23 06:51:09 +03:00
buflen := int64(len(buf))
2020-04-25 16:48:16 +03:00
if buflen < 2 {
2021-06-03 12:49:01 +03:00
return 0, errors.ErrExpected("{} for map", cursor)
2020-04-25 16:48:16 +03:00
}
2020-08-08 07:20:42 +03:00
switch buf[cursor] {
case 'n':
2021-05-08 21:05:36 +03:00
if err := validateNull(buf, cursor); err != nil {
return 0, err
2020-08-08 07:20:42 +03:00
}
cursor += 4
2021-02-16 19:51:42 +03:00
**(**unsafe.Pointer)(unsafe.Pointer(&p)) = nil
2020-08-08 07:20:42 +03:00
return cursor, nil
case '{':
default:
2021-06-03 12:49:01 +03:00
return 0, errors.ErrExpected("{ character for map value", cursor)
2020-04-25 16:48:16 +03:00
}
cursor++
2020-08-12 10:54:15 +03:00
cursor = skipWhiteSpace(buf, cursor)
2021-02-18 06:13:49 +03:00
mapValue := *(*unsafe.Pointer)(p)
if mapValue == nil {
mapValue = makemap(d.mapType, 0)
}
2020-08-12 10:54:15 +03:00
if buf[cursor] == '}' {
**(**unsafe.Pointer)(unsafe.Pointer(&p)) = mapValue
2020-08-12 10:54:15 +03:00
cursor++
return cursor, nil
}
2021-02-15 12:45:41 +03:00
for {
k := unsafe_New(d.keyType)
2021-06-04 19:08:27 +03:00
keyCursor, err := d.keyDecoder.Decode(ctx, cursor, depth, k)
2020-05-06 20:37:29 +03:00
if err != nil {
return 0, err
2020-04-25 16:48:16 +03:00
}
2021-02-15 12:45:41 +03:00
cursor = skipWhiteSpace(buf, keyCursor)
2020-04-25 16:48:16 +03:00
if buf[cursor] != ':' {
2021-06-03 12:49:01 +03:00
return 0, errors.ErrExpected("colon after object key", cursor)
2020-04-25 16:48:16 +03:00
}
cursor++
2021-02-15 12:45:41 +03:00
v := unsafe_New(d.valueType)
2021-06-04 19:08:27 +03:00
valueCursor, err := d.valueDecoder.Decode(ctx, cursor, depth, v)
2020-05-06 20:37:29 +03:00
if err != nil {
return 0, err
2020-04-25 16:48:16 +03:00
}
d.mapassign(d.mapType, mapValue, k, v)
2020-05-06 20:37:29 +03:00
cursor = skipWhiteSpace(buf, valueCursor)
2020-04-25 16:48:16 +03:00
if buf[cursor] == '}' {
**(**unsafe.Pointer)(unsafe.Pointer(&p)) = mapValue
2020-08-08 07:43:24 +03:00
cursor++
2020-05-06 20:37:29 +03:00
return cursor, nil
2020-04-25 16:48:16 +03:00
}
if buf[cursor] != ',' {
2021-06-03 12:49:01 +03:00
return 0, errors.ErrExpected("comma after object value", cursor)
2020-04-25 16:48:16 +03:00
}
2021-02-15 12:45:41 +03:00
cursor++
2020-04-25 16:48:16 +03:00
}
}