package decoder
import (
"fmt"
"reflect"
"unsafe"
)
type ptrDecoder struct {
dec Decoder
typ reflect .Type
structName string
fieldName string
}
func newPtrDecoder(dec Decoder , typ reflect .Type , structName , fieldName string ) Decoder {
d := ptrDecoder {
dec : dec ,
typ : typ ,
structName : structName ,
fieldName : fieldName ,
}
if alloc := allocatorOf (typ ); alloc != nil {
return &basicPtrDecoder {ptrDecoder : d , alloc : alloc }
}
return &d
}
func (d *ptrDecoder ) Decode (ctx *RuntimeContext , cursor , depth int64 , p unsafe .Pointer ) (int64 , error ) {
buf := ctx .Buf
cursor = skipWhiteSpace (buf , cursor )
if buf [cursor ] == 'n' {
if err := validateNull (buf , cursor ); err != nil {
return 0 , err
}
if p != nil {
*(*unsafe .Pointer )(p ) = nil
}
cursor += 4
return cursor , nil
}
var newptr unsafe .Pointer
if *(*unsafe .Pointer )(p ) == nil {
newptr = newValue (d .typ )
*(*unsafe .Pointer )(p ) = newptr
} else {
newptr = *(*unsafe .Pointer )(p )
}
return d .dec .Decode (ctx , cursor , depth , newptr )
}
type basicPtrDecoder struct {
ptrDecoder
alloc func () unsafe .Pointer
}
func (d *basicPtrDecoder ) Decode (ctx *RuntimeContext , cursor , depth int64 , p unsafe .Pointer ) (int64 , error ) {
buf := ctx .Buf
cursor = skipWhiteSpace (buf , cursor )
if buf [cursor ] == 'n' {
if err := validateNull (buf , cursor ); err != nil {
return 0 , err
}
if p != nil {
*(*unsafe .Pointer )(p ) = nil
}
return cursor + 4 , nil
}
newptr := *(*unsafe .Pointer )(p )
if newptr == nil {
newptr = d .alloc ()
*(*unsafe .Pointer )(p ) = newptr
}
return d .dec .Decode (ctx , cursor , depth , newptr )
}
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" )
}
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 .