2021-06-03 12:49:01 +03:00
|
|
|
package decoder
|
2020-04-23 19:39:20 +03:00
|
|
|
|
|
|
|
import (
|
2022-11-28 21:55:56 +03:00
|
|
|
"fmt"
|
2020-04-23 19:39:20 +03:00
|
|
|
"unsafe"
|
2021-06-03 12:49:01 +03:00
|
|
|
|
|
|
|
"github.com/goccy/go-json/internal/runtime"
|
2020-04-23 19:39:20 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type ptrDecoder struct {
|
2021-06-03 13:10:17 +03:00
|
|
|
dec Decoder
|
2021-06-03 12:49:01 +03:00
|
|
|
typ *runtime.Type
|
2020-11-23 11:16:31 +03:00
|
|
|
structName string
|
|
|
|
fieldName string
|
2020-04-23 19:39:20 +03:00
|
|
|
}
|
|
|
|
|
2021-06-03 13:10:17 +03:00
|
|
|
func newPtrDecoder(dec Decoder, typ *runtime.Type, structName, fieldName string) *ptrDecoder {
|
2020-11-23 11:16:31 +03:00
|
|
|
return &ptrDecoder{
|
|
|
|
dec: dec,
|
|
|
|
typ: typ,
|
|
|
|
structName: structName,
|
|
|
|
fieldName: fieldName,
|
|
|
|
}
|
2020-04-23 19:39:20 +03:00
|
|
|
}
|
|
|
|
|
2021-06-03 13:10:17 +03:00
|
|
|
func (d *ptrDecoder) contentDecoder() Decoder {
|
2020-12-22 15:55:59 +03:00
|
|
|
dec, ok := d.dec.(*ptrDecoder)
|
|
|
|
if !ok {
|
|
|
|
return d.dec
|
|
|
|
}
|
|
|
|
return dec.contentDecoder()
|
|
|
|
}
|
|
|
|
|
2021-02-01 16:31:39 +03:00
|
|
|
//nolint:golint
|
2020-04-24 14:23:26 +03:00
|
|
|
//go:linkname unsafe_New reflect.unsafe_New
|
2021-06-03 12:49:01 +03:00
|
|
|
func unsafe_New(*runtime.Type) unsafe.Pointer
|
2020-04-24 14:23:26 +03:00
|
|
|
|
2022-11-28 21:55:56 +03:00
|
|
|
func UnsafeNew(t *runtime.Type) unsafe.Pointer {
|
|
|
|
return unsafe_New(t)
|
|
|
|
}
|
|
|
|
|
2021-06-03 12:49:01 +03:00
|
|
|
func (d *ptrDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) error {
|
2021-06-04 08:59:43 +03:00
|
|
|
if s.skipWhiteSpace() == nul {
|
2020-11-27 11:11:53 +03:00
|
|
|
s.read()
|
|
|
|
}
|
|
|
|
if s.char() == 'n' {
|
|
|
|
if err := nullBytes(s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-22 19:29:13 +03:00
|
|
|
*(*unsafe.Pointer)(p) = nil
|
2020-11-27 11:11:53 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-02-16 18:03:07 +03:00
|
|
|
var newptr unsafe.Pointer
|
|
|
|
if *(*unsafe.Pointer)(p) == nil {
|
|
|
|
newptr = unsafe_New(d.typ)
|
|
|
|
*(*unsafe.Pointer)(p) = newptr
|
|
|
|
} else {
|
|
|
|
newptr = *(*unsafe.Pointer)(p)
|
|
|
|
}
|
2021-06-03 12:49:01 +03:00
|
|
|
if err := d.dec.DecodeStream(s, depth, newptr); err != nil {
|
2020-07-30 16:41:53 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-04 19:08:27 +03:00
|
|
|
func (d *ptrDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) {
|
|
|
|
buf := ctx.Buf
|
2020-11-27 11:11:53 +03:00
|
|
|
cursor = skipWhiteSpace(buf, cursor)
|
|
|
|
if buf[cursor] == 'n' {
|
2021-05-08 21:05:36 +03:00
|
|
|
if err := validateNull(buf, cursor); err != nil {
|
|
|
|
return 0, err
|
2020-11-27 11:11:53 +03:00
|
|
|
}
|
2021-02-09 16:13:58 +03:00
|
|
|
if p != nil {
|
|
|
|
*(*unsafe.Pointer)(p) = nil
|
|
|
|
}
|
2020-11-27 11:11:53 +03:00
|
|
|
cursor += 4
|
|
|
|
return cursor, nil
|
|
|
|
}
|
2021-02-16 18:03:07 +03:00
|
|
|
var newptr unsafe.Pointer
|
|
|
|
if *(*unsafe.Pointer)(p) == nil {
|
|
|
|
newptr = unsafe_New(d.typ)
|
|
|
|
*(*unsafe.Pointer)(p) = newptr
|
|
|
|
} else {
|
|
|
|
newptr = *(*unsafe.Pointer)(p)
|
|
|
|
}
|
2021-06-04 19:08:27 +03:00
|
|
|
c, err := d.dec.Decode(ctx, cursor, depth, newptr)
|
2020-05-06 20:37:29 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2020-04-23 19:39:20 +03:00
|
|
|
}
|
2020-05-06 20:37:29 +03:00
|
|
|
cursor = c
|
|
|
|
return cursor, nil
|
2020-04-23 19:39:20 +03:00
|
|
|
}
|
2022-11-28 21:55:56 +03:00
|
|
|
|
|
|
|
func (d *ptrDecoder) DecodePath(ctx *RuntimeContext, cursor, depth int64) ([][]byte, int64, error) {
|
|
|
|
return nil, 0, fmt.Errorf("json: ptr decoder does not support decode path")
|
|
|
|
}
|