2020-05-24 15:31:10 +03:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
2021-03-18 09:53:22 +03:00
|
|
|
"encoding/json"
|
2021-02-18 11:46:28 +03:00
|
|
|
"fmt"
|
2020-05-24 15:31:10 +03:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
2021-02-18 11:46:28 +03:00
|
|
|
"unicode"
|
2020-05-24 15:31:10 +03:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2021-03-18 09:53:22 +03:00
|
|
|
var (
|
|
|
|
jsonNumberType = reflect.TypeOf(json.Number(""))
|
|
|
|
)
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
func decodeCompileToGetDecoderSlowPath(typeptr uintptr, typ *rtype) (decoder, error) {
|
2021-02-04 12:00:08 +03:00
|
|
|
decoderMap := loadDecoderMap()
|
|
|
|
if dec, exists := decoderMap[typeptr]; exists {
|
|
|
|
return dec, nil
|
|
|
|
}
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
dec, err := decodeCompileHead(typ, map[uintptr]decoder{})
|
2021-02-04 12:00:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
storeDecoder(typeptr, dec, decoderMap)
|
|
|
|
return dec, nil
|
|
|
|
}
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
func decodeCompileHead(typ *rtype, structTypeToDecoder map[uintptr]decoder) (decoder, error) {
|
2020-08-20 06:38:50 +03:00
|
|
|
switch {
|
2020-11-21 20:47:18 +03:00
|
|
|
case rtype_ptrTo(typ).Implements(unmarshalJSONType):
|
2020-11-24 14:15:11 +03:00
|
|
|
return newUnmarshalJSONDecoder(rtype_ptrTo(typ), "", ""), nil
|
2020-08-20 06:38:50 +03:00
|
|
|
case rtype_ptrTo(typ).Implements(unmarshalTextType):
|
2020-11-24 14:15:11 +03:00
|
|
|
return newUnmarshalTextDecoder(rtype_ptrTo(typ), "", ""), nil
|
2020-05-24 15:31:10 +03:00
|
|
|
}
|
2021-02-10 18:33:54 +03:00
|
|
|
return decodeCompile(typ.Elem(), "", "", structTypeToDecoder)
|
2020-05-24 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
func decodeCompile(typ *rtype, structName, fieldName string, structTypeToDecoder map[uintptr]decoder) (decoder, error) {
|
2020-08-20 06:38:50 +03:00
|
|
|
switch {
|
2020-11-21 20:47:18 +03:00
|
|
|
case rtype_ptrTo(typ).Implements(unmarshalJSONType):
|
2020-11-24 14:15:11 +03:00
|
|
|
return newUnmarshalJSONDecoder(rtype_ptrTo(typ), structName, fieldName), nil
|
2020-08-20 06:38:50 +03:00
|
|
|
case rtype_ptrTo(typ).Implements(unmarshalTextType):
|
2020-11-24 14:15:11 +03:00
|
|
|
return newUnmarshalTextDecoder(rtype_ptrTo(typ), structName, fieldName), nil
|
2020-05-24 15:31:10 +03:00
|
|
|
}
|
2020-08-20 06:38:50 +03:00
|
|
|
|
2020-05-24 15:31:10 +03:00
|
|
|
switch typ.Kind() {
|
|
|
|
case reflect.Ptr:
|
2021-02-10 18:33:54 +03:00
|
|
|
return decodeCompilePtr(typ, structName, fieldName, structTypeToDecoder)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Struct:
|
2021-02-10 18:33:54 +03:00
|
|
|
return decodeCompileStruct(typ, structName, fieldName, structTypeToDecoder)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Slice:
|
2020-11-21 20:47:18 +03:00
|
|
|
elem := typ.Elem()
|
|
|
|
if elem.Kind() == reflect.Uint8 {
|
2021-02-10 18:33:54 +03:00
|
|
|
return decodeCompileBytes(elem, structName, fieldName)
|
2020-11-21 20:47:18 +03:00
|
|
|
}
|
2021-02-10 18:33:54 +03:00
|
|
|
return decodeCompileSlice(typ, structName, fieldName, structTypeToDecoder)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Array:
|
2021-02-10 18:33:54 +03:00
|
|
|
return decodeCompileArray(typ, structName, fieldName, structTypeToDecoder)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Map:
|
2021-02-10 18:33:54 +03:00
|
|
|
return decodeCompileMap(typ, structName, fieldName, structTypeToDecoder)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Interface:
|
2021-02-10 18:33:54 +03:00
|
|
|
return decodeCompileInterface(typ, structName, fieldName)
|
2020-08-19 13:56:29 +03:00
|
|
|
case reflect.Uintptr:
|
2021-02-10 18:33:54 +03:00
|
|
|
return decodeCompileUint(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Int:
|
2021-02-10 18:33:54 +03:00
|
|
|
return decodeCompileInt(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Int8:
|
2021-02-10 18:33:54 +03:00
|
|
|
return decodeCompileInt8(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Int16:
|
2021-02-10 18:33:54 +03:00
|
|
|
return decodeCompileInt16(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Int32:
|
2021-02-10 18:33:54 +03:00
|
|
|
return decodeCompileInt32(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Int64:
|
2021-02-10 18:33:54 +03:00
|
|
|
return decodeCompileInt64(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Uint:
|
2021-02-10 18:33:54 +03:00
|
|
|
return decodeCompileUint(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Uint8:
|
2021-02-10 18:33:54 +03:00
|
|
|
return decodeCompileUint8(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Uint16:
|
2021-02-10 18:33:54 +03:00
|
|
|
return decodeCompileUint16(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Uint32:
|
2021-02-10 18:33:54 +03:00
|
|
|
return decodeCompileUint32(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Uint64:
|
2021-02-10 18:33:54 +03:00
|
|
|
return decodeCompileUint64(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.String:
|
2021-03-11 13:29:35 +03:00
|
|
|
return decodeCompileString(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Bool:
|
2021-02-10 18:33:54 +03:00
|
|
|
return decodeCompileBool(structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Float32:
|
2021-02-10 18:33:54 +03:00
|
|
|
return decodeCompileFloat32(structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Float64:
|
2021-02-10 18:33:54 +03:00
|
|
|
return decodeCompileFloat64(structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
}
|
2020-12-24 08:26:18 +03:00
|
|
|
return nil, &UnmarshalTypeError{
|
|
|
|
Value: "object",
|
|
|
|
Type: rtype2type(typ),
|
|
|
|
Offset: 0,
|
|
|
|
}
|
2020-05-24 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
func decodeCompileMapKey(typ *rtype, structName, fieldName string, structTypeToDecoder map[uintptr]decoder) (decoder, error) {
|
2020-12-24 08:26:18 +03:00
|
|
|
if rtype_ptrTo(typ).Implements(unmarshalTextType) {
|
|
|
|
return newUnmarshalTextDecoder(rtype_ptrTo(typ), structName, fieldName), nil
|
|
|
|
}
|
2021-02-10 18:33:54 +03:00
|
|
|
dec, err := decodeCompile(typ, structName, fieldName, structTypeToDecoder)
|
2020-12-12 12:09:46 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
switch t := dec.(type) {
|
2020-12-24 08:26:18 +03:00
|
|
|
case *stringDecoder, *interfaceDecoder:
|
2020-12-12 12:09:46 +03:00
|
|
|
return dec, nil
|
|
|
|
case *boolDecoder, *intDecoder, *uintDecoder, *numberDecoder:
|
2021-02-16 05:46:00 +03:00
|
|
|
return newWrappedStringDecoder(typ, dec, structName, fieldName), nil
|
2020-12-12 12:09:46 +03:00
|
|
|
case *ptrDecoder:
|
|
|
|
dec = t.dec
|
|
|
|
default:
|
|
|
|
goto ERROR
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ERROR:
|
2020-12-24 08:26:18 +03:00
|
|
|
return nil, &UnmarshalTypeError{
|
|
|
|
Value: "object",
|
|
|
|
Type: rtype2type(typ),
|
|
|
|
Offset: 0,
|
|
|
|
}
|
2020-12-12 12:09:46 +03:00
|
|
|
}
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
func decodeCompilePtr(typ *rtype, structName, fieldName string, structTypeToDecoder map[uintptr]decoder) (decoder, error) {
|
|
|
|
dec, err := decodeCompile(typ.Elem(), structName, fieldName, structTypeToDecoder)
|
2020-05-24 15:31:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-23 11:16:31 +03:00
|
|
|
return newPtrDecoder(dec, typ.Elem(), structName, fieldName), nil
|
2020-05-24 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
func decodeCompileInt(typ *rtype, structName, fieldName string) (decoder, error) {
|
2020-12-15 06:29:19 +03:00
|
|
|
return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
|
2020-11-19 06:47:42 +03:00
|
|
|
*(*int)(p) = int(v)
|
2020-05-24 15:31:10 +03:00
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
func decodeCompileInt8(typ *rtype, structName, fieldName string) (decoder, error) {
|
2020-12-15 06:29:19 +03:00
|
|
|
return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
|
2020-11-19 06:47:42 +03:00
|
|
|
*(*int8)(p) = int8(v)
|
2020-05-24 15:31:10 +03:00
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
func decodeCompileInt16(typ *rtype, structName, fieldName string) (decoder, error) {
|
2020-12-15 06:29:19 +03:00
|
|
|
return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
|
2020-11-19 06:47:42 +03:00
|
|
|
*(*int16)(p) = int16(v)
|
2020-05-24 15:31:10 +03:00
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
func decodeCompileInt32(typ *rtype, structName, fieldName string) (decoder, error) {
|
2020-12-15 06:29:19 +03:00
|
|
|
return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
|
2020-11-19 06:47:42 +03:00
|
|
|
*(*int32)(p) = int32(v)
|
2020-05-24 15:31:10 +03:00
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
func decodeCompileInt64(typ *rtype, structName, fieldName string) (decoder, error) {
|
2020-12-15 06:29:19 +03:00
|
|
|
return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
|
2020-11-19 06:47:42 +03:00
|
|
|
*(*int64)(p) = v
|
2020-05-24 15:31:10 +03:00
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
func decodeCompileUint(typ *rtype, structName, fieldName string) (decoder, error) {
|
2020-12-21 09:48:57 +03:00
|
|
|
return newUintDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v uint64) {
|
2020-11-19 06:47:42 +03:00
|
|
|
*(*uint)(p) = uint(v)
|
2020-05-24 15:31:10 +03:00
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
func decodeCompileUint8(typ *rtype, structName, fieldName string) (decoder, error) {
|
2020-12-21 09:48:57 +03:00
|
|
|
return newUintDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v uint64) {
|
2020-11-19 06:47:42 +03:00
|
|
|
*(*uint8)(p) = uint8(v)
|
2020-05-24 15:31:10 +03:00
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
func decodeCompileUint16(typ *rtype, structName, fieldName string) (decoder, error) {
|
2020-12-21 09:48:57 +03:00
|
|
|
return newUintDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v uint64) {
|
2020-11-19 06:47:42 +03:00
|
|
|
*(*uint16)(p) = uint16(v)
|
2020-05-24 15:31:10 +03:00
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
func decodeCompileUint32(typ *rtype, structName, fieldName string) (decoder, error) {
|
2020-12-21 09:48:57 +03:00
|
|
|
return newUintDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v uint64) {
|
2020-11-19 06:47:42 +03:00
|
|
|
*(*uint32)(p) = uint32(v)
|
2020-05-24 15:31:10 +03:00
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
func decodeCompileUint64(typ *rtype, structName, fieldName string) (decoder, error) {
|
2020-12-21 09:48:57 +03:00
|
|
|
return newUintDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v uint64) {
|
2020-11-19 06:47:42 +03:00
|
|
|
*(*uint64)(p) = v
|
2020-05-24 15:31:10 +03:00
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
func decodeCompileFloat32(structName, fieldName string) (decoder, error) {
|
2020-11-23 11:16:31 +03:00
|
|
|
return newFloatDecoder(structName, fieldName, func(p unsafe.Pointer, v float64) {
|
2020-11-19 06:47:42 +03:00
|
|
|
*(*float32)(p) = float32(v)
|
2020-05-24 15:31:10 +03:00
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
func decodeCompileFloat64(structName, fieldName string) (decoder, error) {
|
2020-11-23 11:16:31 +03:00
|
|
|
return newFloatDecoder(structName, fieldName, func(p unsafe.Pointer, v float64) {
|
2020-11-19 06:47:42 +03:00
|
|
|
*(*float64)(p) = v
|
2020-05-24 15:31:10 +03:00
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2021-03-11 13:29:35 +03:00
|
|
|
func decodeCompileString(typ *rtype, structName, fieldName string) (decoder, error) {
|
|
|
|
if typ == type2rtype(jsonNumberType) {
|
|
|
|
return newNumberDecoder(structName, fieldName, func(p unsafe.Pointer, v Number) {
|
|
|
|
*(*Number)(p) = v
|
|
|
|
}), nil
|
|
|
|
}
|
2020-11-23 11:16:31 +03:00
|
|
|
return newStringDecoder(structName, fieldName), nil
|
2020-05-24 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
func decodeCompileBool(structName, fieldName string) (decoder, error) {
|
2020-11-23 11:16:31 +03:00
|
|
|
return newBoolDecoder(structName, fieldName), nil
|
2020-05-24 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
func decodeCompileBytes(typ *rtype, structName, fieldName string) (decoder, error) {
|
2020-12-24 08:26:18 +03:00
|
|
|
return newBytesDecoder(typ, structName, fieldName), nil
|
2020-11-21 20:47:18 +03:00
|
|
|
}
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
func decodeCompileSlice(typ *rtype, structName, fieldName string, structTypeToDecoder map[uintptr]decoder) (decoder, error) {
|
2020-05-24 15:31:10 +03:00
|
|
|
elem := typ.Elem()
|
2021-02-10 18:33:54 +03:00
|
|
|
decoder, err := decodeCompile(elem, structName, fieldName, structTypeToDecoder)
|
2020-05-24 15:31:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-23 11:16:31 +03:00
|
|
|
return newSliceDecoder(decoder, elem, elem.Size(), structName, fieldName), nil
|
2020-05-24 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
func decodeCompileArray(typ *rtype, structName, fieldName string, structTypeToDecoder map[uintptr]decoder) (decoder, error) {
|
2020-05-24 15:31:10 +03:00
|
|
|
elem := typ.Elem()
|
2021-02-10 18:33:54 +03:00
|
|
|
decoder, err := decodeCompile(elem, structName, fieldName, structTypeToDecoder)
|
2020-05-24 15:31:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-23 11:16:31 +03:00
|
|
|
return newArrayDecoder(decoder, elem, typ.Len(), structName, fieldName), nil
|
2020-05-24 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
func decodeCompileMap(typ *rtype, structName, fieldName string, structTypeToDecoder map[uintptr]decoder) (decoder, error) {
|
|
|
|
keyDec, err := decodeCompileMapKey(typ.Key(), structName, fieldName, structTypeToDecoder)
|
2020-05-24 15:31:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-10 18:33:54 +03:00
|
|
|
valueDec, err := decodeCompile(typ.Elem(), structName, fieldName, structTypeToDecoder)
|
2020-05-24 15:31:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-22 15:55:59 +03:00
|
|
|
return newMapDecoder(typ, typ.Key(), keyDec, typ.Elem(), valueDec, structName, fieldName), nil
|
2020-05-24 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
func decodeCompileInterface(typ *rtype, structName, fieldName string) (decoder, error) {
|
|
|
|
return newInterfaceDecoder(typ, structName, fieldName), nil
|
2020-05-24 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
2021-02-18 11:46:28 +03:00
|
|
|
func decodeRemoveConflictFields(fieldMap map[string]*structFieldSet, conflictedMap map[string]struct{}, dec *structDecoder, field reflect.StructField) {
|
2020-12-22 15:55:59 +03: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,
|
2021-02-18 11:46:28 +03:00
|
|
|
offset: field.Offset + v.offset,
|
2020-12-22 15:55:59 +03:00
|
|
|
isTaggedKey: v.isTaggedKey,
|
2021-02-06 14:07:01 +03:00
|
|
|
key: k,
|
|
|
|
keyLen: int64(len(k)),
|
2020-12-22 15:55:59 +03:00
|
|
|
}
|
|
|
|
fieldMap[k] = fieldSet
|
2020-12-22 19:28:56 +03:00
|
|
|
lower := strings.ToLower(k)
|
|
|
|
if _, exists := fieldMap[lower]; !exists {
|
|
|
|
fieldMap[lower] = fieldSet
|
|
|
|
}
|
2020-12-22 15:55:59 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if set.isTaggedKey {
|
|
|
|
if v.isTaggedKey {
|
|
|
|
// conflict tag key
|
|
|
|
delete(fieldMap, k)
|
2021-02-06 08:24:07 +03:00
|
|
|
delete(fieldMap, strings.ToLower(k))
|
2020-12-22 15:55:59 +03:00
|
|
|
conflictedMap[k] = struct{}{}
|
|
|
|
conflictedMap[strings.ToLower(k)] = struct{}{}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if v.isTaggedKey {
|
|
|
|
fieldSet := &structFieldSet{
|
|
|
|
dec: v.dec,
|
2021-02-18 11:46:28 +03:00
|
|
|
offset: field.Offset + v.offset,
|
2020-12-22 15:55:59 +03:00
|
|
|
isTaggedKey: v.isTaggedKey,
|
2021-02-06 14:07:01 +03:00
|
|
|
key: k,
|
|
|
|
keyLen: int64(len(k)),
|
2020-12-22 15:55:59 +03:00
|
|
|
}
|
|
|
|
fieldMap[k] = fieldSet
|
2020-12-22 19:28:56 +03:00
|
|
|
lower := strings.ToLower(k)
|
|
|
|
if _, exists := fieldMap[lower]; !exists {
|
|
|
|
fieldMap[lower] = fieldSet
|
|
|
|
}
|
2020-12-22 15:55:59 +03:00
|
|
|
} else {
|
|
|
|
// conflict tag key
|
|
|
|
delete(fieldMap, k)
|
2021-02-06 08:24:07 +03:00
|
|
|
delete(fieldMap, strings.ToLower(k))
|
2020-12-22 15:55:59 +03:00
|
|
|
conflictedMap[k] = struct{}{}
|
|
|
|
conflictedMap[strings.ToLower(k)] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 18:33:54 +03:00
|
|
|
func decodeCompileStruct(typ *rtype, structName, fieldName string, structTypeToDecoder map[uintptr]decoder) (decoder, error) {
|
2020-05-24 15:31:10 +03:00
|
|
|
fieldNum := typ.NumField()
|
2020-12-22 15:55:59 +03:00
|
|
|
conflictedMap := map[string]struct{}{}
|
2020-05-24 15:31:10 +03:00
|
|
|
fieldMap := map[string]*structFieldSet{}
|
2020-08-25 11:18:37 +03:00
|
|
|
typeptr := uintptr(unsafe.Pointer(typ))
|
2021-02-10 18:33:54 +03:00
|
|
|
if dec, exists := structTypeToDecoder[typeptr]; exists {
|
2020-08-25 11:18:37 +03:00
|
|
|
return dec, nil
|
|
|
|
}
|
2020-11-23 11:16:31 +03:00
|
|
|
structDec := newStructDecoder(structName, fieldName, fieldMap)
|
2021-02-10 18:33:54 +03:00
|
|
|
structTypeToDecoder[typeptr] = structDec
|
2020-11-23 11:16:31 +03:00
|
|
|
structName = typ.Name()
|
2020-05-24 15:31:10 +03:00
|
|
|
for i := 0; i < fieldNum; i++ {
|
|
|
|
field := typ.Field(i)
|
2020-08-20 06:38:50 +03:00
|
|
|
if isIgnoredStructField(field) {
|
2020-05-24 15:31:10 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-02-18 11:46:28 +03:00
|
|
|
isUnexportedField := unicode.IsLower([]rune(field.Name)[0])
|
2020-08-20 06:38:50 +03:00
|
|
|
tag := structTagFromField(field)
|
2021-02-10 18:33:54 +03:00
|
|
|
dec, err := decodeCompile(type2rtype(field.Type), structName, field.Name, structTypeToDecoder)
|
2020-05-24 15:31:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-22 15:55:59 +03:00
|
|
|
if field.Anonymous && !tag.isTaggedKey {
|
|
|
|
if stDec, ok := dec.(*structDecoder); ok {
|
|
|
|
if type2rtype(field.Type) == typ {
|
|
|
|
// recursive definition
|
|
|
|
continue
|
|
|
|
}
|
2021-02-18 11:46:28 +03:00
|
|
|
decodeRemoveConflictFields(fieldMap, conflictedMap, stDec, field)
|
2020-12-22 15:55:59 +03:00
|
|
|
} else if pdec, ok := dec.(*ptrDecoder); ok {
|
|
|
|
contentDec := pdec.contentDecoder()
|
|
|
|
if pdec.typ == typ {
|
|
|
|
// recursive definition
|
|
|
|
continue
|
|
|
|
}
|
2021-02-18 11:46:28 +03:00
|
|
|
var fieldSetErr error
|
|
|
|
if isUnexportedField {
|
|
|
|
fieldSetErr = fmt.Errorf(
|
|
|
|
"json: cannot set embedded pointer to unexported struct: %v",
|
|
|
|
field.Type.Elem(),
|
|
|
|
)
|
|
|
|
}
|
2020-12-22 15:55:59 +03:00
|
|
|
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),
|
2021-02-01 16:31:39 +03:00
|
|
|
offset: field.Offset,
|
2020-12-22 15:55:59 +03:00
|
|
|
isTaggedKey: v.isTaggedKey,
|
2021-02-06 14:07:01 +03:00
|
|
|
key: k,
|
|
|
|
keyLen: int64(len(k)),
|
2021-02-18 11:46:28 +03:00
|
|
|
err: fieldSetErr,
|
2020-12-22 15:55:59 +03:00
|
|
|
}
|
|
|
|
fieldMap[k] = fieldSet
|
2020-12-22 19:28:56 +03:00
|
|
|
lower := strings.ToLower(k)
|
|
|
|
if _, exists := fieldMap[lower]; !exists {
|
|
|
|
fieldMap[lower] = fieldSet
|
|
|
|
}
|
2020-12-22 15:55:59 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if set.isTaggedKey {
|
|
|
|
if v.isTaggedKey {
|
|
|
|
// conflict tag key
|
|
|
|
delete(fieldMap, k)
|
2021-02-06 08:24:07 +03:00
|
|
|
delete(fieldMap, strings.ToLower(k))
|
2020-12-22 15:55:59 +03:00
|
|
|
conflictedMap[k] = struct{}{}
|
|
|
|
conflictedMap[strings.ToLower(k)] = struct{}{}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if v.isTaggedKey {
|
|
|
|
fieldSet := &structFieldSet{
|
|
|
|
dec: newAnonymousFieldDecoder(pdec.typ, v.offset, v.dec),
|
2021-02-01 16:31:39 +03:00
|
|
|
offset: field.Offset,
|
2020-12-22 15:55:59 +03:00
|
|
|
isTaggedKey: v.isTaggedKey,
|
2021-02-06 14:07:01 +03:00
|
|
|
key: k,
|
|
|
|
keyLen: int64(len(k)),
|
2021-02-18 11:46:28 +03:00
|
|
|
err: fieldSetErr,
|
2020-12-22 15:55:59 +03:00
|
|
|
}
|
|
|
|
fieldMap[k] = fieldSet
|
2020-12-22 19:28:56 +03:00
|
|
|
lower := strings.ToLower(k)
|
|
|
|
if _, exists := fieldMap[lower]; !exists {
|
|
|
|
fieldMap[lower] = fieldSet
|
|
|
|
}
|
2020-12-22 15:55:59 +03:00
|
|
|
} else {
|
|
|
|
// conflict tag key
|
|
|
|
delete(fieldMap, k)
|
2021-02-06 08:24:07 +03:00
|
|
|
delete(fieldMap, strings.ToLower(k))
|
2020-12-22 15:55:59 +03:00
|
|
|
conflictedMap[k] = struct{}{}
|
|
|
|
conflictedMap[strings.ToLower(k)] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if tag.isString {
|
2021-02-16 05:46:00 +03:00
|
|
|
dec = newWrappedStringDecoder(type2rtype(field.Type), dec, structName, field.Name)
|
2020-12-22 15:55:59 +03:00
|
|
|
}
|
2021-02-06 14:07:01 +03:00
|
|
|
var key string
|
2020-12-22 15:55:59 +03:00
|
|
|
if tag.key != "" {
|
2021-02-06 14:07:01 +03:00
|
|
|
key = tag.key
|
2020-12-22 15:55:59 +03:00
|
|
|
} else {
|
2021-02-06 14:07:01 +03:00
|
|
|
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
|
2020-12-22 15:55:59 +03:00
|
|
|
}
|
2020-08-20 06:38:50 +03:00
|
|
|
}
|
2020-05-24 15:31:10 +03:00
|
|
|
}
|
2021-02-10 18:33:54 +03:00
|
|
|
delete(structTypeToDecoder, typeptr)
|
2021-02-04 14:58:58 +03:00
|
|
|
structDec.tryOptimize()
|
2020-08-25 11:18:37 +03:00
|
|
|
return structDec, nil
|
2020-05-24 15:31:10 +03:00
|
|
|
}
|