2021-06-03 12:49:01 +03:00
|
|
|
package decoder
|
2020-04-25 16:48:16 +03:00
|
|
|
|
|
|
|
import (
|
2021-06-23 11:23:09 +03:00
|
|
|
"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,
|
2021-08-31 06:21:08 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 06:21:08 +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
|
2021-06-23 11:23:09 +03:00
|
|
|
//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
|
2021-06-23 11:23:09 +03:00
|
|
|
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))
|
2021-06-23 11:23:09 +03:00
|
|
|
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 {
|
2021-02-18 13:05:06 +03:00
|
|
|
depth++
|
|
|
|
if depth > maxDecodeNestingDepth {
|
2021-06-03 12:49:01 +03:00
|
|
|
return errors.ErrExceededMaxDepth(s.char(), s.cursor)
|
2021-02-18 13:05:06 +03:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2022-02-22 13:10:26 +03:00
|
|
|
s.cursor++
|
2023-03-13 14:01:01 +03:00
|
|
|
if s.skipWhiteSpace() == '}' {
|
2020-12-05 16:27:33 +03:00
|
|
|
*(*unsafe.Pointer)(p) = mapValue
|
2022-02-22 13:10:26 +03:00
|
|
|
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()
|
2021-05-07 17:15:16 +03:00
|
|
|
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
|
|
|
|
}
|
2021-06-23 11:23:09 +03:00
|
|
|
d.mapassign(d.mapType, mapValue, k, v)
|
2020-07-30 16:41:53 +03:00
|
|
|
s.skipWhiteSpace()
|
2021-05-07 17:15:16 +03:00
|
|
|
if s.equalChar('}') {
|
2020-11-14 23:27:15 +03:00
|
|
|
**(**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
|
|
|
|
}
|
2021-05-07 17:15:16 +03:00
|
|
|
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
|
|
|
}
|
2022-02-22 13:10:26 +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
|
2021-02-18 13:05:06 +03:00
|
|
|
depth++
|
|
|
|
if depth > maxDecodeNestingDepth {
|
2021-06-03 12:49:01 +03:00
|
|
|
return 0, errors.ErrExceededMaxDepth(buf[cursor], cursor)
|
2021-02-18 13:05:06 +03:00
|
|
|
}
|
|
|
|
|
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] == '}' {
|
2020-11-14 23:27:15 +03:00
|
|
|
**(**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
|
|
|
}
|
2021-06-23 11:23:09 +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] == '}' {
|
2020-11-14 23:27:15 +03:00
|
|
|
**(**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
|
|
|
}
|
|
|
|
}
|
2022-11-28 21:55:56 +03:00
|
|
|
|
|
|
|
func (d *mapDecoder) DecodePath(ctx *RuntimeContext, cursor, depth int64) ([][]byte, int64, error) {
|
|
|
|
buf := ctx.Buf
|
|
|
|
depth++
|
|
|
|
if depth > maxDecodeNestingDepth {
|
|
|
|
return nil, 0, errors.ErrExceededMaxDepth(buf[cursor], cursor)
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor = skipWhiteSpace(buf, cursor)
|
|
|
|
buflen := int64(len(buf))
|
|
|
|
if buflen < 2 {
|
|
|
|
return nil, 0, errors.ErrExpected("{} for map", cursor)
|
|
|
|
}
|
|
|
|
switch buf[cursor] {
|
|
|
|
case 'n':
|
|
|
|
if err := validateNull(buf, cursor); err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
cursor += 4
|
|
|
|
return [][]byte{nullbytes}, cursor, nil
|
|
|
|
case '{':
|
|
|
|
default:
|
|
|
|
return nil, 0, errors.ErrExpected("{ character for map value", cursor)
|
|
|
|
}
|
|
|
|
cursor++
|
|
|
|
cursor = skipWhiteSpace(buf, cursor)
|
|
|
|
if buf[cursor] == '}' {
|
|
|
|
cursor++
|
|
|
|
return nil, cursor, nil
|
|
|
|
}
|
|
|
|
keyDecoder, ok := d.keyDecoder.(*stringDecoder)
|
|
|
|
if !ok {
|
|
|
|
return nil, 0, &errors.UnmarshalTypeError{
|
|
|
|
Value: "string",
|
|
|
|
Type: reflect.TypeOf(""),
|
|
|
|
Offset: cursor,
|
|
|
|
Struct: d.structName,
|
|
|
|
Field: d.fieldName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret := [][]byte{}
|
|
|
|
for {
|
|
|
|
key, keyCursor, err := keyDecoder.decodeByte(buf, cursor)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
cursor = skipWhiteSpace(buf, keyCursor)
|
|
|
|
if buf[cursor] != ':' {
|
|
|
|
return nil, 0, errors.ErrExpected("colon after object key", cursor)
|
|
|
|
}
|
|
|
|
cursor++
|
|
|
|
child, found, err := ctx.Option.Path.Field(string(key))
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
if found {
|
|
|
|
if child != nil {
|
|
|
|
oldPath := ctx.Option.Path.node
|
|
|
|
ctx.Option.Path.node = child
|
|
|
|
paths, c, err := d.valueDecoder.DecodePath(ctx, cursor, depth)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
ctx.Option.Path.node = oldPath
|
|
|
|
ret = append(ret, paths...)
|
|
|
|
cursor = c
|
|
|
|
} else {
|
|
|
|
start := cursor
|
|
|
|
end, err := skipValue(buf, cursor, depth)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
ret = append(ret, buf[start:end])
|
|
|
|
cursor = end
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c, err := skipValue(buf, cursor, depth)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
2022-11-29 15:44:55 +03:00
|
|
|
cursor = c
|
2022-11-28 21:55:56 +03:00
|
|
|
}
|
2022-11-29 15:44:55 +03:00
|
|
|
cursor = skipWhiteSpace(buf, cursor)
|
2022-11-28 21:55:56 +03:00
|
|
|
if buf[cursor] == '}' {
|
|
|
|
cursor++
|
|
|
|
return ret, cursor, nil
|
|
|
|
}
|
|
|
|
if buf[cursor] != ',' {
|
|
|
|
return nil, 0, errors.ErrExpected("comma after object value", cursor)
|
|
|
|
}
|
|
|
|
cursor++
|
|
|
|
}
|
|
|
|
}
|