2021-09-10 12:42:14 +00:00
|
|
|
package decoder
|
2021-08-12 19:03:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
2021-09-10 12:42:14 +00:00
|
|
|
"sync/atomic"
|
2021-08-12 19:03:24 +00:00
|
|
|
"unicode"
|
|
|
|
"unsafe"
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
"github.com/goccy/go-json/internal/errors"
|
2021-08-12 19:03:24 +00:00
|
|
|
"github.com/goccy/go-json/internal/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-09-10 12:42:14 +00:00
|
|
|
jsonNumberType = reflect.TypeOf(json.Number(""))
|
|
|
|
typeAddr *runtime.TypeAddr
|
|
|
|
cachedDecoderMap unsafe.Pointer // map[uintptr]decoder
|
|
|
|
cachedDecoder []Decoder
|
2021-08-12 19:03:24 +00:00
|
|
|
)
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func init() {
|
|
|
|
typeAddr = runtime.AnalyzeTypeAddr()
|
|
|
|
if typeAddr == nil {
|
|
|
|
typeAddr = &runtime.TypeAddr{}
|
|
|
|
}
|
|
|
|
cachedDecoder = make([]Decoder, typeAddr.AddrRange>>typeAddr.AddrShift)
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadDecoderMap() map[uintptr]Decoder {
|
|
|
|
p := atomic.LoadPointer(&cachedDecoderMap)
|
|
|
|
return *(*map[uintptr]Decoder)(unsafe.Pointer(&p))
|
|
|
|
}
|
|
|
|
|
|
|
|
func storeDecoder(typ uintptr, dec Decoder, m map[uintptr]Decoder) {
|
|
|
|
newDecoderMap := make(map[uintptr]Decoder, len(m)+1)
|
|
|
|
newDecoderMap[typ] = dec
|
|
|
|
|
|
|
|
for k, v := range m {
|
|
|
|
newDecoderMap[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
atomic.StorePointer(&cachedDecoderMap, *(*unsafe.Pointer)(unsafe.Pointer(&newDecoderMap)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func compileToGetDecoderSlowPath(typeptr uintptr, typ *runtime.Type) (Decoder, error) {
|
2021-08-12 19:03:24 +00:00
|
|
|
decoderMap := loadDecoderMap()
|
|
|
|
if dec, exists := decoderMap[typeptr]; exists {
|
|
|
|
return dec, nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
dec, err := compileHead(typ, map[uintptr]Decoder{})
|
2021-08-12 19:03:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
storeDecoder(typeptr, dec, decoderMap)
|
|
|
|
return dec, nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compileHead(typ *runtime.Type, structTypeToDecoder map[uintptr]Decoder) (Decoder, error) {
|
2021-08-12 19:03:24 +00:00
|
|
|
switch {
|
2021-09-10 12:42:14 +00:00
|
|
|
case implementsUnmarshalJSONType(runtime.PtrTo(typ)):
|
|
|
|
return newUnmarshalJSONDecoder(runtime.PtrTo(typ), "", ""), nil
|
|
|
|
case runtime.PtrTo(typ).Implements(unmarshalTextType):
|
|
|
|
return newUnmarshalTextDecoder(runtime.PtrTo(typ), "", ""), nil
|
2021-08-12 19:03:24 +00:00
|
|
|
}
|
2021-09-10 12:42:14 +00:00
|
|
|
return compile(typ.Elem(), "", "", structTypeToDecoder)
|
2021-08-12 19:03:24 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compile(typ *runtime.Type, structName, fieldName string, structTypeToDecoder map[uintptr]Decoder) (Decoder, error) {
|
2021-08-12 19:03:24 +00:00
|
|
|
switch {
|
2021-09-10 12:42:14 +00:00
|
|
|
case implementsUnmarshalJSONType(runtime.PtrTo(typ)):
|
|
|
|
return newUnmarshalJSONDecoder(runtime.PtrTo(typ), structName, fieldName), nil
|
|
|
|
case runtime.PtrTo(typ).Implements(unmarshalTextType):
|
|
|
|
return newUnmarshalTextDecoder(runtime.PtrTo(typ), structName, fieldName), nil
|
2021-08-12 19:03:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch typ.Kind() {
|
|
|
|
case reflect.Ptr:
|
2021-09-10 12:42:14 +00:00
|
|
|
return compilePtr(typ, structName, fieldName, structTypeToDecoder)
|
2021-08-12 19:03:24 +00:00
|
|
|
case reflect.Struct:
|
2021-09-10 12:42:14 +00:00
|
|
|
return compileStruct(typ, structName, fieldName, structTypeToDecoder)
|
2021-08-12 19:03:24 +00:00
|
|
|
case reflect.Slice:
|
|
|
|
elem := typ.Elem()
|
|
|
|
if elem.Kind() == reflect.Uint8 {
|
2021-09-10 12:42:14 +00:00
|
|
|
return compileBytes(elem, structName, fieldName)
|
2021-08-12 19:03:24 +00:00
|
|
|
}
|
2021-09-10 12:42:14 +00:00
|
|
|
return compileSlice(typ, structName, fieldName, structTypeToDecoder)
|
2021-08-12 19:03:24 +00:00
|
|
|
case reflect.Array:
|
2021-09-10 12:42:14 +00:00
|
|
|
return compileArray(typ, structName, fieldName, structTypeToDecoder)
|
2021-08-12 19:03:24 +00:00
|
|
|
case reflect.Map:
|
2021-09-10 12:42:14 +00:00
|
|
|
return compileMap(typ, structName, fieldName, structTypeToDecoder)
|
2021-08-12 19:03:24 +00:00
|
|
|
case reflect.Interface:
|
2021-09-10 12:42:14 +00:00
|
|
|
return compileInterface(typ, structName, fieldName)
|
2021-08-12 19:03:24 +00:00
|
|
|
case reflect.Uintptr:
|
2021-09-10 12:42:14 +00:00
|
|
|
return compileUint(typ, structName, fieldName)
|
2021-08-12 19:03:24 +00:00
|
|
|
case reflect.Int:
|
2021-09-10 12:42:14 +00:00
|
|
|
return compileInt(typ, structName, fieldName)
|
2021-08-12 19:03:24 +00:00
|
|
|
case reflect.Int8:
|
2021-09-10 12:42:14 +00:00
|
|
|
return compileInt8(typ, structName, fieldName)
|
2021-08-12 19:03:24 +00:00
|
|
|
case reflect.Int16:
|
2021-09-10 12:42:14 +00:00
|
|
|
return compileInt16(typ, structName, fieldName)
|
2021-08-12 19:03:24 +00:00
|
|
|
case reflect.Int32:
|
2021-09-10 12:42:14 +00:00
|
|
|
return compileInt32(typ, structName, fieldName)
|
2021-08-12 19:03:24 +00:00
|
|
|
case reflect.Int64:
|
2021-09-10 12:42:14 +00:00
|
|
|
return compileInt64(typ, structName, fieldName)
|
2021-08-12 19:03:24 +00:00
|
|
|
case reflect.Uint:
|
2021-09-10 12:42:14 +00:00
|
|
|
return compileUint(typ, structName, fieldName)
|
2021-08-12 19:03:24 +00:00
|
|
|
case reflect.Uint8:
|
2021-09-10 12:42:14 +00:00
|
|
|
return compileUint8(typ, structName, fieldName)
|
2021-08-12 19:03:24 +00:00
|
|
|
case reflect.Uint16:
|
2021-09-10 12:42:14 +00:00
|
|
|
return compileUint16(typ, structName, fieldName)
|
2021-08-12 19:03:24 +00:00
|
|
|
case reflect.Uint32:
|
2021-09-10 12:42:14 +00:00
|
|
|
return compileUint32(typ, structName, fieldName)
|
2021-08-12 19:03:24 +00:00
|
|
|
case reflect.Uint64:
|
2021-09-10 12:42:14 +00:00
|
|
|
return compileUint64(typ, structName, fieldName)
|
2021-08-12 19:03:24 +00:00
|
|
|
case reflect.String:
|
2021-09-10 12:42:14 +00:00
|
|
|
return compileString(typ, structName, fieldName)
|
2021-08-12 19:03:24 +00:00
|
|
|
case reflect.Bool:
|
2021-09-10 12:42:14 +00:00
|
|
|
return compileBool(structName, fieldName)
|
2021-08-12 19:03:24 +00:00
|
|
|
case reflect.Float32:
|
2021-09-10 12:42:14 +00:00
|
|
|
return compileFloat32(structName, fieldName)
|
2021-08-12 19:03:24 +00:00
|
|
|
case reflect.Float64:
|
2021-09-10 12:42:14 +00:00
|
|
|
return compileFloat64(structName, fieldName)
|
|
|
|
case reflect.Func:
|
|
|
|
return compileFunc(typ, structName, fieldName)
|
2021-08-12 19:03:24 +00:00
|
|
|
}
|
2021-09-10 12:42:14 +00:00
|
|
|
return nil, &errors.UnmarshalTypeError{
|
2021-08-12 19:03:24 +00:00
|
|
|
Value: "object",
|
2021-09-10 12:42:14 +00:00
|
|
|
Type: runtime.RType2Type(typ),
|
2021-08-12 19:03:24 +00:00
|
|
|
Offset: 0,
|
2021-09-10 12:42:14 +00:00
|
|
|
Struct: structName,
|
|
|
|
Field: fieldName,
|
2021-08-12 19:03:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func isStringTagSupportedType(typ *runtime.Type) bool {
|
2021-08-12 19:03:24 +00:00
|
|
|
switch {
|
2021-09-10 12:42:14 +00:00
|
|
|
case implementsUnmarshalJSONType(runtime.PtrTo(typ)):
|
2021-08-12 19:03:24 +00:00
|
|
|
return false
|
2021-09-10 12:42:14 +00:00
|
|
|
case runtime.PtrTo(typ).Implements(unmarshalTextType):
|
2021-08-12 19:03:24 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
switch typ.Kind() {
|
|
|
|
case reflect.Map:
|
|
|
|
return false
|
|
|
|
case reflect.Slice:
|
|
|
|
return false
|
|
|
|
case reflect.Array:
|
|
|
|
return false
|
|
|
|
case reflect.Struct:
|
|
|
|
return false
|
|
|
|
case reflect.Interface:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compileMapKey(typ *runtime.Type, structName, fieldName string, structTypeToDecoder map[uintptr]Decoder) (Decoder, error) {
|
|
|
|
if runtime.PtrTo(typ).Implements(unmarshalTextType) {
|
|
|
|
return newUnmarshalTextDecoder(runtime.PtrTo(typ), structName, fieldName), nil
|
2021-08-12 19:03:24 +00:00
|
|
|
}
|
2021-09-10 12:42:14 +00:00
|
|
|
dec, err := compile(typ, structName, fieldName, structTypeToDecoder)
|
2021-08-12 19:03:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
switch t := dec.(type) {
|
|
|
|
case *stringDecoder, *interfaceDecoder:
|
|
|
|
return dec, nil
|
|
|
|
case *boolDecoder, *intDecoder, *uintDecoder, *numberDecoder:
|
|
|
|
return newWrappedStringDecoder(typ, dec, structName, fieldName), nil
|
|
|
|
case *ptrDecoder:
|
|
|
|
dec = t.dec
|
|
|
|
default:
|
|
|
|
goto ERROR
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ERROR:
|
2021-09-10 12:42:14 +00:00
|
|
|
return nil, &errors.UnmarshalTypeError{
|
2021-08-12 19:03:24 +00:00
|
|
|
Value: "object",
|
2021-09-10 12:42:14 +00:00
|
|
|
Type: runtime.RType2Type(typ),
|
2021-08-12 19:03:24 +00:00
|
|
|
Offset: 0,
|
2021-09-10 12:42:14 +00:00
|
|
|
Struct: structName,
|
|
|
|
Field: fieldName,
|
2021-08-12 19:03:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compilePtr(typ *runtime.Type, structName, fieldName string, structTypeToDecoder map[uintptr]Decoder) (Decoder, error) {
|
|
|
|
dec, err := compile(typ.Elem(), structName, fieldName, structTypeToDecoder)
|
2021-08-12 19:03:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newPtrDecoder(dec, typ.Elem(), structName, fieldName), nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compileInt(typ *runtime.Type, structName, fieldName string) (Decoder, error) {
|
2021-08-12 19:03:24 +00:00
|
|
|
return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
|
|
|
|
*(*int)(p) = int(v)
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compileInt8(typ *runtime.Type, structName, fieldName string) (Decoder, error) {
|
2021-08-12 19:03:24 +00:00
|
|
|
return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
|
|
|
|
*(*int8)(p) = int8(v)
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compileInt16(typ *runtime.Type, structName, fieldName string) (Decoder, error) {
|
2021-08-12 19:03:24 +00:00
|
|
|
return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
|
|
|
|
*(*int16)(p) = int16(v)
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compileInt32(typ *runtime.Type, structName, fieldName string) (Decoder, error) {
|
2021-08-12 19:03:24 +00:00
|
|
|
return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
|
|
|
|
*(*int32)(p) = int32(v)
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compileInt64(typ *runtime.Type, structName, fieldName string) (Decoder, error) {
|
2021-08-12 19:03:24 +00:00
|
|
|
return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
|
|
|
|
*(*int64)(p) = v
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compileUint(typ *runtime.Type, structName, fieldName string) (Decoder, error) {
|
2021-08-12 19:03:24 +00:00
|
|
|
return newUintDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v uint64) {
|
|
|
|
*(*uint)(p) = uint(v)
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compileUint8(typ *runtime.Type, structName, fieldName string) (Decoder, error) {
|
2021-08-12 19:03:24 +00:00
|
|
|
return newUintDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v uint64) {
|
|
|
|
*(*uint8)(p) = uint8(v)
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compileUint16(typ *runtime.Type, structName, fieldName string) (Decoder, error) {
|
2021-08-12 19:03:24 +00:00
|
|
|
return newUintDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v uint64) {
|
|
|
|
*(*uint16)(p) = uint16(v)
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compileUint32(typ *runtime.Type, structName, fieldName string) (Decoder, error) {
|
2021-08-12 19:03:24 +00:00
|
|
|
return newUintDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v uint64) {
|
|
|
|
*(*uint32)(p) = uint32(v)
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compileUint64(typ *runtime.Type, structName, fieldName string) (Decoder, error) {
|
2021-08-12 19:03:24 +00:00
|
|
|
return newUintDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v uint64) {
|
|
|
|
*(*uint64)(p) = v
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compileFloat32(structName, fieldName string) (Decoder, error) {
|
2021-08-12 19:03:24 +00:00
|
|
|
return newFloatDecoder(structName, fieldName, func(p unsafe.Pointer, v float64) {
|
|
|
|
*(*float32)(p) = float32(v)
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compileFloat64(structName, fieldName string) (Decoder, error) {
|
2021-08-12 19:03:24 +00:00
|
|
|
return newFloatDecoder(structName, fieldName, func(p unsafe.Pointer, v float64) {
|
|
|
|
*(*float64)(p) = v
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compileString(typ *runtime.Type, structName, fieldName string) (Decoder, error) {
|
|
|
|
if typ == runtime.Type2RType(jsonNumberType) {
|
|
|
|
return newNumberDecoder(structName, fieldName, func(p unsafe.Pointer, v json.Number) {
|
|
|
|
*(*json.Number)(p) = v
|
2021-08-12 19:03:24 +00:00
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
return newStringDecoder(structName, fieldName), nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compileBool(structName, fieldName string) (Decoder, error) {
|
2021-08-12 19:03:24 +00:00
|
|
|
return newBoolDecoder(structName, fieldName), nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compileBytes(typ *runtime.Type, structName, fieldName string) (Decoder, error) {
|
2021-08-12 19:03:24 +00:00
|
|
|
return newBytesDecoder(typ, structName, fieldName), nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compileSlice(typ *runtime.Type, structName, fieldName string, structTypeToDecoder map[uintptr]Decoder) (Decoder, error) {
|
2021-08-12 19:03:24 +00:00
|
|
|
elem := typ.Elem()
|
2021-09-10 12:42:14 +00:00
|
|
|
decoder, err := compile(elem, structName, fieldName, structTypeToDecoder)
|
2021-08-12 19:03:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newSliceDecoder(decoder, elem, elem.Size(), structName, fieldName), nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compileArray(typ *runtime.Type, structName, fieldName string, structTypeToDecoder map[uintptr]Decoder) (Decoder, error) {
|
2021-08-12 19:03:24 +00:00
|
|
|
elem := typ.Elem()
|
2021-09-10 12:42:14 +00:00
|
|
|
decoder, err := compile(elem, structName, fieldName, structTypeToDecoder)
|
2021-08-12 19:03:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newArrayDecoder(decoder, elem, typ.Len(), structName, fieldName), nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compileMap(typ *runtime.Type, structName, fieldName string, structTypeToDecoder map[uintptr]Decoder) (Decoder, error) {
|
|
|
|
keyDec, err := compileMapKey(typ.Key(), structName, fieldName, structTypeToDecoder)
|
2021-08-12 19:03:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-10 12:42:14 +00:00
|
|
|
valueDec, err := compile(typ.Elem(), structName, fieldName, structTypeToDecoder)
|
2021-08-12 19:03:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newMapDecoder(typ, typ.Key(), keyDec, typ.Elem(), valueDec, structName, fieldName), nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compileInterface(typ *runtime.Type, structName, fieldName string) (Decoder, error) {
|
2021-08-12 19:03:24 +00:00
|
|
|
return newInterfaceDecoder(typ, structName, fieldName), nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compileFunc(typ *runtime.Type, strutName, fieldName string) (Decoder, error) {
|
|
|
|
return newFuncDecoder(typ, strutName, fieldName), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeConflictFields(fieldMap map[string]*structFieldSet, conflictedMap map[string]struct{}, dec *structDecoder, field reflect.StructField) {
|
2021-08-12 19:03:24 +00:00
|
|
|
for k, v := range dec.fieldMap {
|
|
|
|
if _, exists := conflictedMap[k]; exists {
|
|
|
|
// already conflicted key
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
set, exists := fieldMap[k]
|
|
|
|
if !exists {
|
|
|
|
fieldSet := &structFieldSet{
|
|
|
|
dec: v.dec,
|
|
|
|
offset: field.Offset + v.offset,
|
|
|
|
isTaggedKey: v.isTaggedKey,
|
|
|
|
key: k,
|
|
|
|
keyLen: int64(len(k)),
|
|
|
|
}
|
|
|
|
fieldMap[k] = fieldSet
|
|
|
|
lower := strings.ToLower(k)
|
|
|
|
if _, exists := fieldMap[lower]; !exists {
|
|
|
|
fieldMap[lower] = fieldSet
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if set.isTaggedKey {
|
|
|
|
if v.isTaggedKey {
|
|
|
|
// conflict tag key
|
|
|
|
delete(fieldMap, k)
|
|
|
|
delete(fieldMap, strings.ToLower(k))
|
|
|
|
conflictedMap[k] = struct{}{}
|
|
|
|
conflictedMap[strings.ToLower(k)] = struct{}{}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if v.isTaggedKey {
|
|
|
|
fieldSet := &structFieldSet{
|
|
|
|
dec: v.dec,
|
|
|
|
offset: field.Offset + v.offset,
|
|
|
|
isTaggedKey: v.isTaggedKey,
|
|
|
|
key: k,
|
|
|
|
keyLen: int64(len(k)),
|
|
|
|
}
|
|
|
|
fieldMap[k] = fieldSet
|
|
|
|
lower := strings.ToLower(k)
|
|
|
|
if _, exists := fieldMap[lower]; !exists {
|
|
|
|
fieldMap[lower] = fieldSet
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// conflict tag key
|
|
|
|
delete(fieldMap, k)
|
|
|
|
delete(fieldMap, strings.ToLower(k))
|
|
|
|
conflictedMap[k] = struct{}{}
|
|
|
|
conflictedMap[strings.ToLower(k)] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 12:42:14 +00:00
|
|
|
func compileStruct(typ *runtime.Type, structName, fieldName string, structTypeToDecoder map[uintptr]Decoder) (Decoder, error) {
|
2021-08-12 19:03:24 +00:00
|
|
|
fieldNum := typ.NumField()
|
|
|
|
conflictedMap := map[string]struct{}{}
|
|
|
|
fieldMap := map[string]*structFieldSet{}
|
|
|
|
typeptr := uintptr(unsafe.Pointer(typ))
|
|
|
|
if dec, exists := structTypeToDecoder[typeptr]; exists {
|
|
|
|
return dec, nil
|
|
|
|
}
|
|
|
|
structDec := newStructDecoder(structName, fieldName, fieldMap)
|
|
|
|
structTypeToDecoder[typeptr] = structDec
|
|
|
|
structName = typ.Name()
|
|
|
|
for i := 0; i < fieldNum; i++ {
|
|
|
|
field := typ.Field(i)
|
|
|
|
if runtime.IsIgnoredStructField(field) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
isUnexportedField := unicode.IsLower([]rune(field.Name)[0])
|
|
|
|
tag := runtime.StructTagFromField(field)
|
2021-09-10 12:42:14 +00:00
|
|
|
dec, err := compile(runtime.Type2RType(field.Type), structName, field.Name, structTypeToDecoder)
|
2021-08-12 19:03:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if field.Anonymous && !tag.IsTaggedKey {
|
|
|
|
if stDec, ok := dec.(*structDecoder); ok {
|
2021-09-10 12:42:14 +00:00
|
|
|
if runtime.Type2RType(field.Type) == typ {
|
2021-08-12 19:03:24 +00:00
|
|
|
// recursive definition
|
|
|
|
continue
|
|
|
|
}
|
2021-09-10 12:42:14 +00:00
|
|
|
removeConflictFields(fieldMap, conflictedMap, stDec, field)
|
2021-08-12 19:03:24 +00:00
|
|
|
} else if pdec, ok := dec.(*ptrDecoder); ok {
|
|
|
|
contentDec := pdec.contentDecoder()
|
|
|
|
if pdec.typ == typ {
|
|
|
|
// recursive definition
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var fieldSetErr error
|
|
|
|
if isUnexportedField {
|
|
|
|
fieldSetErr = fmt.Errorf(
|
|
|
|
"json: cannot set embedded pointer to unexported struct: %v",
|
|
|
|
field.Type.Elem(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if dec, ok := contentDec.(*structDecoder); ok {
|
|
|
|
for k, v := range dec.fieldMap {
|
|
|
|
if _, exists := conflictedMap[k]; exists {
|
|
|
|
// already conflicted key
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
set, exists := fieldMap[k]
|
|
|
|
if !exists {
|
|
|
|
fieldSet := &structFieldSet{
|
|
|
|
dec: newAnonymousFieldDecoder(pdec.typ, v.offset, v.dec),
|
|
|
|
offset: field.Offset,
|
|
|
|
isTaggedKey: v.isTaggedKey,
|
|
|
|
key: k,
|
|
|
|
keyLen: int64(len(k)),
|
|
|
|
err: fieldSetErr,
|
|
|
|
}
|
|
|
|
fieldMap[k] = fieldSet
|
|
|
|
lower := strings.ToLower(k)
|
|
|
|
if _, exists := fieldMap[lower]; !exists {
|
|
|
|
fieldMap[lower] = fieldSet
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if set.isTaggedKey {
|
|
|
|
if v.isTaggedKey {
|
|
|
|
// conflict tag key
|
|
|
|
delete(fieldMap, k)
|
|
|
|
delete(fieldMap, strings.ToLower(k))
|
|
|
|
conflictedMap[k] = struct{}{}
|
|
|
|
conflictedMap[strings.ToLower(k)] = struct{}{}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if v.isTaggedKey {
|
|
|
|
fieldSet := &structFieldSet{
|
|
|
|
dec: newAnonymousFieldDecoder(pdec.typ, v.offset, v.dec),
|
|
|
|
offset: field.Offset,
|
|
|
|
isTaggedKey: v.isTaggedKey,
|
|
|
|
key: k,
|
|
|
|
keyLen: int64(len(k)),
|
|
|
|
err: fieldSetErr,
|
|
|
|
}
|
|
|
|
fieldMap[k] = fieldSet
|
|
|
|
lower := strings.ToLower(k)
|
|
|
|
if _, exists := fieldMap[lower]; !exists {
|
|
|
|
fieldMap[lower] = fieldSet
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// conflict tag key
|
|
|
|
delete(fieldMap, k)
|
|
|
|
delete(fieldMap, strings.ToLower(k))
|
|
|
|
conflictedMap[k] = struct{}{}
|
|
|
|
conflictedMap[strings.ToLower(k)] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-09-10 12:42:14 +00:00
|
|
|
if tag.IsString && isStringTagSupportedType(runtime.Type2RType(field.Type)) {
|
|
|
|
dec = newWrappedStringDecoder(runtime.Type2RType(field.Type), dec, structName, field.Name)
|
2021-08-12 19:03:24 +00:00
|
|
|
}
|
|
|
|
var key string
|
|
|
|
if tag.Key != "" {
|
|
|
|
key = tag.Key
|
|
|
|
} else {
|
|
|
|
key = field.Name
|
|
|
|
}
|
|
|
|
fieldSet := &structFieldSet{
|
|
|
|
dec: dec,
|
|
|
|
offset: field.Offset,
|
|
|
|
isTaggedKey: tag.IsTaggedKey,
|
|
|
|
key: key,
|
|
|
|
keyLen: int64(len(key)),
|
|
|
|
}
|
|
|
|
fieldMap[key] = fieldSet
|
|
|
|
lower := strings.ToLower(key)
|
|
|
|
if _, exists := fieldMap[lower]; !exists {
|
|
|
|
fieldMap[lower] = fieldSet
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete(structTypeToDecoder, typeptr)
|
|
|
|
structDec.tryOptimize()
|
|
|
|
return structDec, nil
|
|
|
|
}
|
2021-09-10 12:42:14 +00:00
|
|
|
|
|
|
|
func implementsUnmarshalJSONType(typ *runtime.Type) bool {
|
|
|
|
return typ.Implements(unmarshalJSONType) || typ.Implements(unmarshalJSONContextType)
|
|
|
|
}
|