2021-09-10 14:42:14 +02:00
|
|
|
package decoder
|
2021-08-12 21:03:24 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"unsafe"
|
2021-09-10 14:42:14 +02:00
|
|
|
|
|
|
|
"github.com/goccy/go-json/internal/runtime"
|
2021-08-12 21:03:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type wrappedStringDecoder struct {
|
2021-09-10 14:42:14 +02:00
|
|
|
typ *runtime.Type
|
|
|
|
dec Decoder
|
2021-08-12 21:03:24 +02:00
|
|
|
stringDecoder *stringDecoder
|
|
|
|
structName string
|
|
|
|
fieldName string
|
|
|
|
isPtrType bool
|
|
|
|
}
|
|
|
|
|
2021-09-10 14:42:14 +02:00
|
|
|
func newWrappedStringDecoder(typ *runtime.Type, dec Decoder, structName, fieldName string) *wrappedStringDecoder {
|
2021-08-12 21:03:24 +02:00
|
|
|
return &wrappedStringDecoder{
|
|
|
|
typ: typ,
|
|
|
|
dec: dec,
|
|
|
|
stringDecoder: newStringDecoder(structName, fieldName),
|
|
|
|
structName: structName,
|
|
|
|
fieldName: fieldName,
|
|
|
|
isPtrType: typ.Kind() == reflect.Ptr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 14:42:14 +02:00
|
|
|
func (d *wrappedStringDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) error {
|
2021-08-12 21:03:24 +02:00
|
|
|
bytes, err := d.stringDecoder.decodeStreamByte(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if bytes == nil {
|
|
|
|
if d.isPtrType {
|
|
|
|
*(*unsafe.Pointer)(p) = nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
b := make([]byte, len(bytes)+1)
|
|
|
|
copy(b, bytes)
|
2021-09-10 14:42:14 +02:00
|
|
|
if _, err := d.dec.Decode(&RuntimeContext{Buf: b}, 0, depth, p); err != nil {
|
2021-08-12 21:03:24 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 14:42:14 +02:00
|
|
|
func (d *wrappedStringDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) {
|
|
|
|
bytes, c, err := d.stringDecoder.decodeByte(ctx.Buf, cursor)
|
2021-08-12 21:03:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if bytes == nil {
|
|
|
|
if d.isPtrType {
|
|
|
|
*(*unsafe.Pointer)(p) = nil
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
bytes = append(bytes, nul)
|
2021-09-10 14:42:14 +02:00
|
|
|
oldBuf := ctx.Buf
|
|
|
|
ctx.Buf = bytes
|
|
|
|
if _, err := d.dec.Decode(ctx, 0, depth, p); err != nil {
|
2021-08-12 21:03:24 +02:00
|
|
|
return 0, err
|
|
|
|
}
|
2021-09-10 14:42:14 +02:00
|
|
|
ctx.Buf = oldBuf
|
2021-08-12 21:03:24 +02:00
|
|
|
return c, nil
|
|
|
|
}
|