package decoder
import (
"encoding/json"
"reflect"
"slices"
"strings"
"unsafe"
"github.com/goccy/go-json/internal/errors"
"github.com/goccy/go-json/internal/runtime"
)
func CompileToGetDecoder (typ unsafe .Pointer ) (Decoder , error ) {
typeptr := uintptr (typ )
if dec := cachedDecoder .Load (typeptr ); dec != nil {
return *dec , nil
}
dec , err := compileHead (runtime .TypeOfPtr (typ ), newCompiler (false ))
if err != nil {
return nil , err
}
return *cachedDecoder .Store (typeptr , &dec ), nil
}
var (
jsonNumberType = reflect .TypeOf (json .Number ("" ))
cachedDecoder runtime .TypeCache [Decoder ]
)
type compiler struct {
decoders map [uintptr ]Decoder
v2 bool
stringifyNumbers bool
stringTag bool
v1Options V2Flags
mapKeys bool
funcs *UnmarshalFuncs
}
func newCompiler(v2 bool ) *compiler {
return &compiler {decoders : map [uintptr ]Decoder {}, v2 : v2 }
}
func compileHead(typ reflect .Type , c *compiler ) (Decoder , error ) {
if typ .Kind () != reflect .Ptr {
return nil , &errors .InvalidUnmarshalError {Type : typ }
}
if c .v2 {
return compile (typ .Elem (), "" , "" , c )
}
switch {
case implementsUnmarshalJSONType (reflect .PointerTo (typ )):
return newUnmarshalJSONDecoder (reflect .PointerTo (typ )), nil
case reflect .PointerTo (typ ).Implements (unmarshalTextType ):
return newUnmarshalTextDecoder (reflect .PointerTo (typ )), nil
}
return compile (typ .Elem (), "" , "" , c )
}
func compile(typ reflect .Type , structName , fieldName string , c *compiler ) (Decoder , error ) {
if c .v2 {
return c .compileV2 (typ , structName , fieldName )
}
if implementsUnmarshalFrom (reflect .PointerTo (typ )) {
next , err := compileAfterFrom (typ , structName , fieldName , c )
if err != nil {
return nil , err
}
return newUnmarshalFromDecoder (reflect .PointerTo (typ ), next ), nil
}
return compileAfterFrom (typ , structName , fieldName , c )
}
func compileAfterFrom(typ reflect .Type , structName , fieldName string , c *compiler ) (Decoder , error ) {
switch {
case implementsUnmarshalJSONType (reflect .PointerTo (typ )):
return newUnmarshalJSONDecoder (reflect .PointerTo (typ )), nil
case reflect .PointerTo (typ ).Implements (unmarshalTextType ):
return newUnmarshalTextDecoder (reflect .PointerTo (typ )), nil
}
return compileKind (typ , structName , fieldName , c )
}
func compileKind(typ reflect .Type , structName , fieldName string , c *compiler ) (Decoder , error ) {
if dec , exists := c .decoders [uintptr (runtime .TypePtr (typ ))]; exists {
return dec , nil
}
switch typ .Kind () {
case reflect .Ptr :
return compilePtr (typ , structName , fieldName , c )
case reflect .Struct :
return compileStruct (typ , structName , fieldName , c )
case reflect .Slice :
elem := typ .Elem ()
if elem .Kind () == reflect .Uint8 {
return compileBytes (typ , structName , fieldName )
}
return compileSlice (typ , structName , fieldName , c )
case reflect .Array :
return compileArray (typ , structName , fieldName , c )
case reflect .Map :
return compileMap (typ , structName , fieldName , c )
case reflect .Interface :
return compileInterface (typ , structName , fieldName )
case reflect .Uintptr :
return compileUint (typ , structName , fieldName )
case reflect .Int :
return compileInt (typ , structName , fieldName )
case reflect .Int8 :
return compileInt8 (typ , structName , fieldName )
case reflect .Int16 :
return compileInt16 (typ , structName , fieldName )
case reflect .Int32 :
return compileInt32 (typ , structName , fieldName )
case reflect .Int64 :
return compileInt64 (typ , structName , fieldName )
case reflect .Uint :
return compileUint (typ , structName , fieldName )
case reflect .Uint8 :
return compileUint8 (typ , structName , fieldName )
case reflect .Uint16 :
return compileUint16 (typ , structName , fieldName )
case reflect .Uint32 :
return compileUint32 (typ , structName , fieldName )
case reflect .Uint64 :
return compileUint64 (typ , structName , fieldName )
case reflect .String :
return compileString (typ , structName , fieldName )
case reflect .Bool :
return compileBool (typ , structName , fieldName )
case reflect .Float32 :
return compileFloat32 (typ , structName , fieldName )
case reflect .Float64 :
return compileFloat64 (typ , structName , fieldName )
case reflect .Func :
return compileFunc (typ , structName , fieldName )
}
return newInvalidDecoder (typ , structName , fieldName ), nil
}
func isStringTagSupportedType(typ reflect .Type ) bool {
if typ .Kind () == reflect .Pointer {
typ = typ .Elem ()
}
switch {
case implementsUnmarshalFrom (reflect .PointerTo (typ )), implementsUnmarshalJSONType (reflect .PointerTo (typ )):
return false
case reflect .PointerTo (typ ).Implements (unmarshalTextType ):
return false
}
switch typ .Kind () {
case reflect .Bool ,
reflect .Int , reflect .Int8 , reflect .Int16 , reflect .Int32 , reflect .Int64 ,
reflect .Uint , reflect .Uint8 , reflect .Uint16 , reflect .Uint32 , reflect .Uint64 , reflect .Uintptr ,
reflect .Float32 , reflect .Float64 ,
reflect .String :
return true
}
return false
}
func compileMapKey(typ reflect .Type , structName , fieldName string , c *compiler ) (Decoder , error ) {
if reflect .PointerTo (typ ).Implements (unmarshalTextType ) {
return &textKeyDecoder {unmarshalTextDecoder {typ : reflect .PointerTo (typ )}}, nil
}
if typ .Kind () == reflect .String {
d := newStringDecoder (structName , fieldName )
d .typ = typ
return d , nil
}
if typ .Kind () == reflect .Pointer {
if isPointerCycle (typ ) {
return newInvalidDecoder (typ , structName , fieldName ), nil
}
dec , err := compileMapKey (typ .Elem (), structName , fieldName , c )
if err != nil {
return nil , err
}
if _ , ok := dec .(*invalidDecoder ); ok {
return newInvalidDecoder (typ , structName , fieldName ), nil
}
return newPtrDecoder (dec , typ .Elem (), structName , fieldName ), nil
}
switch typ .Kind () {
case reflect .Array , reflect .Struct , reflect .Slice , reflect .Map :
return newInvalidDecoder (typ , structName , fieldName ), nil
}
dec , err := compileKind (typ , structName , fieldName , c )
if err != nil {
return nil , err
}
switch dec .(type ) {
case *interfaceDecoder :
if typ .NumMethod () == 0 {
return dec , nil
}
case *boolDecoder , *intDecoder , *uintDecoder , *floatDecoder , *float32Decoder , *numberDecoder :
d := newWrappedStringDecoder (typ , dec , structName , fieldName )
d .isMapKey = true
return d , nil
}
return newInvalidDecoder (typ , structName , fieldName ), nil
}
func compilePtr(typ reflect .Type , structName , fieldName string , c *compiler ) (Decoder , error ) {
if isPointerCycle (typ ) {
return newPtrDecoder (newInvalidDecoder (typ .Elem (), structName , fieldName ), typ .Elem (), structName , fieldName ), nil
}
dec , err := compile (typ .Elem (), structName , fieldName , c )
if err != nil {
return nil , err
}
return newPtrDecoder (dec , typ .Elem (), structName , fieldName ), nil
}
func isPointerCycle(typ reflect .Type ) bool {
slow , fast := typ , typ
for fast .Kind () == reflect .Pointer && fast .Elem ().Kind () == reflect .Pointer {
slow , fast = slow .Elem (), fast .Elem ().Elem ()
if slow == fast {
return true
}
}
return false
}
func compileInt(typ reflect .Type , structName , fieldName string ) (Decoder , error ) {
return newIntDecoder (typ , structName , fieldName , func (p unsafe .Pointer , v int64 ) {
*(*int )(p ) = int (v )
}), nil
}
func compileInt8(typ reflect .Type , structName , fieldName string ) (Decoder , error ) {
return newIntDecoder (typ , structName , fieldName , func (p unsafe .Pointer , v int64 ) {
*(*int8 )(p ) = int8 (v )
}), nil
}
func compileInt16(typ reflect .Type , structName , fieldName string ) (Decoder , error ) {
return newIntDecoder (typ , structName , fieldName , func (p unsafe .Pointer , v int64 ) {
*(*int16 )(p ) = int16 (v )
}), nil
}
func compileInt32(typ reflect .Type , structName , fieldName string ) (Decoder , error ) {
return newIntDecoder (typ , structName , fieldName , func (p unsafe .Pointer , v int64 ) {
*(*int32 )(p ) = int32 (v )
}), nil
}
func compileInt64(typ reflect .Type , structName , fieldName string ) (Decoder , error ) {
return newIntDecoder (typ , structName , fieldName , func (p unsafe .Pointer , v int64 ) {
*(*int64 )(p ) = v
}), nil
}
func compileUint(typ reflect .Type , structName , fieldName string ) (Decoder , error ) {
return newUintDecoder (typ , structName , fieldName , func (p unsafe .Pointer , v uint64 ) {
*(*uint )(p ) = uint (v )
}), nil
}
func compileUint8(typ reflect .Type , structName , fieldName string ) (Decoder , error ) {
return newUintDecoder (typ , structName , fieldName , func (p unsafe .Pointer , v uint64 ) {
*(*uint8 )(p ) = uint8 (v )
}), nil
}
func compileUint16(typ reflect .Type , structName , fieldName string ) (Decoder , error ) {
return newUintDecoder (typ , structName , fieldName , func (p unsafe .Pointer , v uint64 ) {
*(*uint16 )(p ) = uint16 (v )
}), nil
}
func compileUint32(typ reflect .Type , structName , fieldName string ) (Decoder , error ) {
return newUintDecoder (typ , structName , fieldName , func (p unsafe .Pointer , v uint64 ) {
*(*uint32 )(p ) = uint32 (v )
}), nil
}
func compileUint64(typ reflect .Type , structName , fieldName string ) (Decoder , error ) {
return newUintDecoder (typ , structName , fieldName , func (p unsafe .Pointer , v uint64 ) {
*(*uint64 )(p ) = v
}), nil
}
func compileFloat32(typ reflect .Type , structName , fieldName string ) (Decoder , error ) {
d := newFloatDecoder (structName , fieldName , func (p unsafe .Pointer , v float64 ) {
*(*float32 )(p ) = float32 (v )
})
d .typ , d .is32 = typ , true
return &float32Decoder {floatDecoder : *d }, nil
}
func compileFloat64(typ reflect .Type , structName , fieldName string ) (Decoder , error ) {
d := newFloatDecoder (structName , fieldName , func (p unsafe .Pointer , v float64 ) {
*(*float64 )(p ) = v
})
d .typ = typ
return d , nil
}
func compileString(typ reflect .Type , structName , fieldName string ) (Decoder , error ) {
if typ == jsonNumberType {
return newNumberDecoder (structName , fieldName , func (p unsafe .Pointer , v json .Number ) {
*(*json .Number )(p ) = v
}), nil
}
d := newStringDecoder (structName , fieldName )
d .typ = typ
return d , nil
}
func compileBool(typ reflect .Type , structName , fieldName string ) (Decoder , error ) {
d := newBoolDecoder (structName , fieldName )
d .typ = typ
return d , nil
}
func compileBytes(typ reflect .Type , structName , fieldName string ) (Decoder , error ) {
d := newBytesDecoder (typ .Elem (), structName , fieldName )
d .sliceType = typ
return d , nil
}
func compileSlice(typ reflect .Type , structName , fieldName string , c *compiler ) (Decoder , error ) {
elem := typ .Elem ()
d := newSliceDecoder (nil , elem , elem .Size (), structName , fieldName )
d .typ = typ
var dec Decoder = d
if c .v2 {
dec = newV2SliceDecoder (d )
}
decoder , err := compileElem (typ , dec , structName , fieldName , c )
if err != nil {
return nil , err
}
d .valueDecoder = c .goOnDecoder (decoder )
return dec , nil
}
func compileElem(typ reflect .Type , dec Decoder , structName , fieldName string , c *compiler ) (Decoder , error ) {
typeptr := uintptr (runtime .TypePtr (typ ))
if _ , exists := c .decoders [typeptr ]; exists {
return compile (typ .Elem (), structName , fieldName , c )
}
c .decoders [typeptr ] = dec
elemDec , err := compile (typ .Elem (), structName , fieldName , c )
delete (c .decoders , typeptr )
return elemDec , err
}
func compileArray(typ reflect .Type , structName , fieldName string , c *compiler ) (Decoder , error ) {
d := newArrayDecoder (nil , typ , structName , fieldName )
decoder , err := compileElem (typ , d , structName , fieldName , c )
if err != nil {
return nil , err
}
d .valueDecoder = decoder
return d , nil
}
func compileMap(typ reflect .Type , structName , fieldName string , c *compiler ) (Decoder , error ) {
keyDec , err := compileMapKey (typ .Key (), structName , fieldName , c )
if err != nil {
return nil , err
}
d := newMapDecoder (typ , typ .Key (), keyDec , typ .Elem (), nil , structName , fieldName )
valueDec , err := compileElem (typ , d , structName , fieldName , c )
if err != nil {
return nil , err
}
d .valueDecoder = valueDec
return d , nil
}
func compileInterface(typ reflect .Type , structName , fieldName string ) (Decoder , error ) {
return newInterfaceDecoder (typ , structName , fieldName ), nil
}
func compileFunc(typ reflect .Type , strutName , fieldName string ) (Decoder , error ) {
return newFuncDecoder (typ , strutName , fieldName ), nil
}
type embeddedStruct struct {
typ reflect .Type
index []int
}
type fieldCandidate struct {
set *structFieldSet
index []int
}
func (c fieldCandidate ) Depth () int { return len (c .index ) - 1 }
func (c fieldCandidate ) IsTaggedKey () bool { return c .set .isTaggedKey }
func compileStruct(typ reflect .Type , structName , fieldName string , c *compiler ) (Decoder , error ) {
structDec := newStructDecoder (structName , fieldName )
structDec .typ = typ
c .decoders [uintptr (runtime .TypePtr (typ ))] = structDec
fields , err := structFields (typ , c )
if err != nil {
return nil , err
}
structDec .setFields (fields )
return structDec , nil
}
func structFields(typ reflect .Type , c *compiler ) ([]*structFieldSet , error ) {
var candidates []fieldCandidate
visited := map [reflect .Type ]bool {}
var current []embeddedStruct
next := []embeddedStruct {{typ : typ }}
count , nextCount := map [reflect .Type ]int {}, map [reflect .Type ]int {}
for len (next ) > 0 {
current , next = next , current [:0 ]
count , nextCount = nextCount , count
clear (nextCount )
for _ , e := range current {
if visited [e .typ ] {
continue
}
visited [e .typ ] = true
for i := 0 ; i < e .typ .NumField (); i ++ {
field := e .typ .Field (i )
if runtime .IsIgnoredStructField (field ) {
continue
}
tag := runtime .StructTagFromField (field )
index := append (slices .Clip (e .index ), i )
if st := promotingStruct (field , tag ); st != nil {
nextCount [st ]++
if nextCount [st ] == 1 {
next = append (next , embeddedStruct {typ : st , index : index })
}
continue
}
dec , err := compile (field .Type , e .typ .Name (), field .Name , c )
if err != nil {
return nil , err
}
if tag .IsString && isStringTagSupportedType (field .Type ) {
dec = newWrappedStringDecoder (field .Type , dec , e .typ .Name (), field .Name )
}
dec , offset , setErr := promotedField (typ , index , dec )
set := &structFieldSet {
dec : dec ,
offset : offset ,
isTaggedKey : tag .IsTaggedKey ,
key : tag .Key ,
keyLen : int64 (len (tag .Key )),
err : setErr ,
}
candidates = append (candidates , fieldCandidate {set : set , index : index })
if count [e .typ ] > 1 {
candidates = append (candidates , fieldCandidate {set : set , index : index })
}
}
}
}
slices .SortStableFunc (candidates , func (a , b fieldCandidate ) int { return strings .Compare (a .set .key , b .set .key ) })
taken := candidates [:0 ]
for i := 0 ; i < len (candidates ); {
j := i + 1
for j < len (candidates ) && candidates [j ].set .key == candidates [i ].set .key {
j ++
}
if dominant := runtime .DominantField (candidates [i :j ]); dominant >= 0 {
taken = append (taken , candidates [i +dominant ])
}
i = j
}
slices .SortFunc (taken , func (a , b fieldCandidate ) int { return slices .Compare (a .index , b .index ) })
fields := make ([]*structFieldSet , 0 , len (taken ))
for _ , c := range taken {
fields = append (fields , c .set )
}
return fields , nil
}
func promotingStruct(field reflect .StructField , tag *runtime .StructTag ) reflect .Type {
if !field .Anonymous || tag .IsTaggedKey {
return nil
}
typ := field .Type
if typ .Kind () == reflect .Pointer {
typ = typ .Elem ()
}
if typ .Kind () != reflect .Struct {
return nil
}
return typ
}
func promotedField(typ reflect .Type , index []int , dec Decoder ) (Decoder , uintptr , error ) {
field := typ .Field (index [0 ])
if len (index ) == 1 {
return dec , field .Offset , nil
}
if field .Type .Kind () != reflect .Pointer {
dec , offset , err := promotedField (field .Type , index [1 :], dec )
return dec , field .Offset + offset , err
}
embedded := field .Type .Elem ()
dec , offset , _ := promotedField (embedded , index [1 :], dec )
dec = newAnonymousFieldDecoder (embedded , offset , dec )
if !field .IsExported () {
return dec , field .Offset , errUnsettableEmbeddedPointer
}
return dec , field .Offset , nil
}
func implementsUnmarshalJSONType(typ reflect .Type ) bool {
return typ .Implements (unmarshalJSONType ) || typ .Implements (unmarshalJSONContextType )
}
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 .