package decoder
import (
"reflect"
"sync"
"unsafe"
"github.com/goccy/go-json/internal/errors"
"github.com/goccy/go-json/internal/runtime"
)
type mapDecoder struct {
mapType reflect .Type
keyType reflect .Type
valueType reflect .Type
mapTypePtr unsafe .Pointer
keyPtrType unsafe .Pointer
valuePtrType unsafe .Pointer
isStringAnyMap bool
keyDecoder Decoder
valueDecoder Decoder
keyUnsupported bool
structName string
fieldName string
temps sync .Pool
}
type mapTemps struct {
k, v unsafe .Pointer
kv, vv reflect .Value
}
func newMapDecoder(mapType reflect .Type , keyType reflect .Type , keyDec Decoder , valueType reflect .Type , valueDec Decoder , structName , fieldName string ) *mapDecoder {
d := &mapDecoder {
mapType : mapType ,
keyDecoder : keyDec ,
keyType : keyType ,
valueType : valueType ,
mapTypePtr : runtime .TypePtr (mapType ),
keyPtrType : ptrTypeOf (keyType ),
valuePtrType : ptrTypeOf (valueType ),
isStringAnyMap : mapType == interfaceMapType ,
valueDecoder : valueDec ,
keyUnsupported : !mapKeySupported (keyType , keyDec ),
structName : structName ,
fieldName : fieldName ,
}
d .temps .New = func () any {
t := &mapTemps {k : newValue (keyType ), v : newValue (valueType )}
t .kv , t .vv = valueAt (d .keyPtrType , t .k ), valueAt (d .valuePtrType , t .v )
return t
}
return d
}
func (d *mapDecoder ) mapValue (m unsafe .Pointer ) reflect .Value {
return reflect .ValueOf (*(*any )(unsafe .Pointer (&emptyInterface {typ : d .mapTypePtr , ptr : m })))
}
func (d *mapDecoder ) decodeEntries (ctx *RuntimeContext , cursor , depth int64 , p , mapValue , k , v unsafe .Pointer , afterKey bool ) (int64 , error ) {
buf := ctx .Buf
mv := d .mapValue (mapValue )
kv := valueAt (d .keyPtrType , k )
vv := valueAt (d .valuePtrType , v )
for {
drop := afterKey
if !afterKey {
keyCursor , err := d .keyDecoder .Decode (ctx , cursor , depth , k )
if err != nil && err != errMapKeyType {
return 0 , err
}
if buf [keyCursor -1 ] != '"' {
return 0 , errKeyNotString (buf , cursor )
}
drop = err != nil
cursor = keyCursor
}
afterKey = false
cursor = skipWhiteSpace (buf , cursor )
if buf [cursor ] != ':' {
return 0 , errors .ErrExpected ("colon after object key" , cursor )
}
var valueCursor int64
var err error
if drop {
valueCursor , err = skipValue (buf , cursor +1 , depth )
} else if valueCursor , err = d .valueDecoder .Decode (ctx , cursor +1 , depth , v ); err == nil {
mv .SetMapIndex (kv , vv )
}
if err != nil {
return 0 , err
}
kv .SetZero ()
vv .SetZero ()
cursor = skipWhiteSpace (buf , valueCursor )
if buf [cursor ] == '}' {
**(**unsafe .Pointer )(unsafe .Pointer (&p )) = mapValue
return cursor + 1 , nil
}
if buf [cursor ] != ',' {
return 0 , errors .ErrExpected ("comma after object value" , cursor )
}
cursor ++
}
}
func errKeyNotString(buf []byte , cursor int64 ) error {
cursor = skipWhiteSpace (buf , cursor )
return errors .ErrInvalidBeginningOfValue (buf [cursor ], cursor )
}
func (d *mapDecoder ) decodeOther (ctx *RuntimeContext , cursor , depth int64 ) (int64 , error ) {
if isOtherValue (ctx .Buf [cursor ], objectValue ) {
return ctx .skipTypeError (cursor , depth , d .mapType )
}
return 0 , errors .ErrExpected ("{ character for map value" , cursor )
}
func (d *mapDecoder ) Decode (ctx *RuntimeContext , cursor , depth int64 , p unsafe .Pointer ) (int64 , error ) {
buf := ctx .Buf
cursor = skipWhiteSpace (buf , cursor )
buflen := int64 (len (buf ))
if buflen < 2 {
return 0 , errors .ErrExpected ("{} for map" , cursor )
}
switch buf [cursor ] {
case 'n' :
if err := validateNull (buf , cursor ); err != nil {
return 0 , err
}
cursor += 4
**(**unsafe .Pointer )(unsafe .Pointer (&p )) = nil
return cursor , nil
case '{' :
depth ++
if depth > maxDecodeNestingDepth {
return 0 , errors .ErrExceededMaxDepth (buf [cursor ], cursor )
}
default :
return d .decodeOther (ctx , cursor , depth )
}
if d .keyUnsupported {
return ctx .unsupportedMapKeys (d , cursor , depth -1 , p )
}
if d .isStringAnyMap {
m := *(*map [string ]any )(p )
if m == nil {
m , c , err := decodeNewStringAnyMap (ctx , d .valueDecoder .(*interfaceDecoder ), cursor , depth )
if err != nil {
return 0 , err
}
*(*map [string ]any )(p ) = m
return c , nil
}
c , err := decodeStringAnyMap (ctx , d .valueDecoder .(*interfaceDecoder ), m , cursor , depth )
if err != nil {
return 0 , err
}
return c , nil
}
cursor ++
cursor = skipWhiteSpace (buf , cursor )
mapValue := *(*unsafe .Pointer )(p )
if mapValue == nil {
mapValue = reflect .MakeMap (d .mapType ).UnsafePointer ()
}
if buf [cursor ] == '}' {
**(**unsafe .Pointer )(unsafe .Pointer (&p )) = mapValue
cursor ++
return cursor , nil
}
t := d .temps .Get ().(*mapTemps )
k , v , kv , vv := t .k , t .v , t .kv , t .vv
mv := d .mapValue (mapValue )
for {
keyCursor , err := d .keyDecoder .Decode (ctx , cursor , depth , k )
if err != nil {
if err == errMapKeyType {
return d .decodeEntries (ctx , keyCursor , depth , p , mapValue , k , v , true )
}
return 0 , err
}
if buf [keyCursor -1 ] != '"' {
return 0 , errKeyNotString (buf , cursor )
}
cursor = skipWhiteSpace (buf , keyCursor )
if buf [cursor ] != ':' {
return 0 , errors .ErrExpected ("colon after object key" , cursor )
}
cursor ++
valueCursor , err := d .valueDecoder .Decode (ctx , cursor , depth , v )
if err != nil {
return 0 , err
}
mv .SetMapIndex (kv , vv )
kv .SetZero ()
vv .SetZero ()
cursor = skipWhiteSpace (buf , valueCursor )
if buf [cursor ] == '}' {
**(**unsafe .Pointer )(unsafe .Pointer (&p )) = mapValue
d .temps .Put (t )
cursor ++
return cursor , nil
}
if buf [cursor ] != ',' {
return 0 , errors .ErrExpected ("comma after object value" , cursor )
}
cursor ++
}
}
func (d *mapDecoder ) DecodePath (ctx *RuntimeContext , cursor , depth int64 ) ([][]byte , int64 , error ) {
buf := ctx .Buf
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 '{' :
depth ++
if depth > maxDecodeNestingDepth {
return nil , 0 , errors .ErrExceededMaxDepth (buf [cursor ], cursor )
}
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 {
if buf [skipWhiteSpace (buf , cursor )] != '"' {
return nil , 0 , errKeyNotString (buf , cursor )
}
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 ++
node := ctx .Option .Path .node
child , found , err := ctx .Option .Path .Field (string (key ))
if err != nil {
return nil , 0 , err
}
_ , recursive := node .(*PathRecursiveNode )
start := cursor
if found && child != nil {
ctx .Option .Path .node = child
paths , c , err := d .valueDecoder .DecodePath (ctx , cursor , depth )
ctx .Option .Path .node = node
if _ , mismatch := err .(*errors .PathError ); mismatch && recursive {
paths = nil
c , err = skipValue (buf , cursor , depth )
}
if err != nil {
return nil , 0 , err
}
ret = append (ret , paths ...)
cursor = c
}
switch {
case recursive :
paths , c , err := d .valueDecoder .DecodePath (ctx , start , depth )
if err != nil {
return nil , 0 , err
}
if found && child == nil {
ret = append (ret , buf [start :c ])
}
ret = append (ret , paths ...)
cursor = c
case found && child == nil :
end , err := skipValue (buf , cursor , depth )
if err != nil {
return nil , 0 , err
}
ret = append (ret , buf [start :end ])
cursor = end
case !found :
c , err := skipValue (buf , cursor , depth )
if err != nil {
return nil , 0 , err
}
cursor = c
}
cursor = skipWhiteSpace (buf , cursor )
if buf [cursor ] == '}' {
cursor ++
return ret , cursor , nil
}
if buf [cursor ] != ',' {
return nil , 0 , errors .ErrExpected ("comma after object value" , cursor )
}
cursor ++
}
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .