This commit is contained in:
ebauer 2021-06-23 15:22:40 +02:00
parent d34d79600a
commit a7d041a3d4
1 changed files with 108 additions and 108 deletions

View File

@ -1,108 +1,108 @@
package decoder package decoder
import ( import (
"bytes" "bytes"
"unsafe" "unsafe"
"github.com/goccy/go-json/internal/errors" "github.com/goccy/go-json/internal/errors"
"github.com/goccy/go-json/internal/runtime" "github.com/goccy/go-json/internal/runtime"
) )
type funcDecoder struct { type funcDecoder struct {
typ *runtime.Type typ *runtime.Type
structName string structName string
fieldName string fieldName string
} }
func newFuncDecoder(typ *runtime.Type, structName, fieldName string) *funcDecoder { func newFuncDecoder(typ *runtime.Type, structName, fieldName string) *funcDecoder {
fnDecoder := &funcDecoder{typ, structName, fieldName} fnDecoder := &funcDecoder{typ, structName, fieldName}
return fnDecoder return fnDecoder
} }
func (d *funcDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) error { func (d *funcDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) error {
s.skipWhiteSpace() s.skipWhiteSpace()
start := s.cursor start := s.cursor
if err := s.skipValue(depth); err != nil { if err := s.skipValue(depth); err != nil {
return err return err
} }
src := s.buf[start:s.cursor] src := s.buf[start:s.cursor]
if len(src) > 0 { if len(src) > 0 {
switch src[0] { switch src[0] {
case '"': case '"':
return &errors.UnmarshalTypeError{ return &errors.UnmarshalTypeError{
Value: "string", Value: "string",
Type: runtime.RType2Type(d.typ), Type: runtime.RType2Type(d.typ),
Offset: s.totalOffset(), Offset: s.totalOffset(),
} }
case '[': case '[':
return &errors.UnmarshalTypeError{ return &errors.UnmarshalTypeError{
Value: "array", Value: "array",
Type: runtime.RType2Type(d.typ), Type: runtime.RType2Type(d.typ),
Offset: s.totalOffset(), Offset: s.totalOffset(),
} }
case '{': case '{':
return &errors.UnmarshalTypeError{ return &errors.UnmarshalTypeError{
Value: "object", Value: "object",
Type: runtime.RType2Type(d.typ), Type: runtime.RType2Type(d.typ),
Offset: s.totalOffset(), Offset: s.totalOffset(),
} }
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return &errors.UnmarshalTypeError{ return &errors.UnmarshalTypeError{
Value: "number", Value: "number",
Type: runtime.RType2Type(d.typ), Type: runtime.RType2Type(d.typ),
Offset: s.totalOffset(), Offset: s.totalOffset(),
} }
case 'n': case 'n':
if bytes.Equal(src, nullbytes) { if bytes.Equal(src, nullbytes) {
*(*unsafe.Pointer)(p) = nil *(*unsafe.Pointer)(p) = nil
return nil return nil
} }
} }
} }
return errors.ErrNotAtBeginningOfValue(start) return errors.ErrNotAtBeginningOfValue(start)
} }
func (d *funcDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) { func (d *funcDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) {
buf := ctx.Buf buf := ctx.Buf
cursor = skipWhiteSpace(buf, cursor) cursor = skipWhiteSpace(buf, cursor)
start := cursor start := cursor
end, err := skipValue(buf, cursor, depth) end, err := skipValue(buf, cursor, depth)
if err != nil { if err != nil {
return 0, err return 0, err
} }
src := buf[start:end] src := buf[start:end]
if len(src) > 0 { if len(src) > 0 {
switch src[0] { switch src[0] {
case '"': case '"':
return 0, &errors.UnmarshalTypeError{ return 0, &errors.UnmarshalTypeError{
Value: "string", Value: "string",
Type: runtime.RType2Type(d.typ), Type: runtime.RType2Type(d.typ),
Offset: start, Offset: start,
} }
case '[': case '[':
return 0, &errors.UnmarshalTypeError{ return 0, &errors.UnmarshalTypeError{
Value: "array", Value: "array",
Type: runtime.RType2Type(d.typ), Type: runtime.RType2Type(d.typ),
Offset: start, Offset: start,
} }
case '{': case '{':
return 0, &errors.UnmarshalTypeError{ return 0, &errors.UnmarshalTypeError{
Value: "object", Value: "object",
Type: runtime.RType2Type(d.typ), Type: runtime.RType2Type(d.typ),
Offset: start, Offset: start,
} }
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return 0, &errors.UnmarshalTypeError{ return 0, &errors.UnmarshalTypeError{
Value: "number", Value: "number",
Type: runtime.RType2Type(d.typ), Type: runtime.RType2Type(d.typ),
Offset: start, Offset: start,
} }
case 'n': case 'n':
if bytes.Equal(src, nullbytes) { if bytes.Equal(src, nullbytes) {
*(*unsafe.Pointer)(p) = nil *(*unsafe.Pointer)(p) = nil
return end, nil return end, nil
} }
} }
} }
return 0, errors.ErrNotAtBeginningOfValue(start) return 0, errors.ErrNotAtBeginningOfValue(start)
} }