2020-05-24 15:31:10 +03:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (d *Decoder) compileHead(typ *rtype) (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
|
|
|
}
|
2020-11-23 11:16:31 +03:00
|
|
|
return d.compile(typ.Elem(), "", "")
|
2020-05-24 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
2020-11-23 11:16:31 +03:00
|
|
|
func (d *Decoder) compile(typ *rtype, structName, fieldName string) (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:
|
2020-11-23 11:16:31 +03:00
|
|
|
return d.compilePtr(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Struct:
|
2020-11-23 11:16:31 +03:00
|
|
|
return d.compileStruct(typ, structName, fieldName)
|
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 {
|
2020-11-23 11:16:31 +03:00
|
|
|
return d.compileBytes(structName, fieldName)
|
2020-11-21 20:47:18 +03:00
|
|
|
}
|
2020-11-23 11:16:31 +03:00
|
|
|
return d.compileSlice(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Array:
|
2020-11-23 11:16:31 +03:00
|
|
|
return d.compileArray(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Map:
|
2020-11-23 11:16:31 +03:00
|
|
|
return d.compileMap(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Interface:
|
2020-11-23 11:16:31 +03:00
|
|
|
return d.compileInterface(typ, structName, fieldName)
|
2020-08-19 13:56:29 +03:00
|
|
|
case reflect.Uintptr:
|
2020-12-21 09:48:57 +03:00
|
|
|
return d.compileUint(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Int:
|
2020-12-15 06:29:19 +03:00
|
|
|
return d.compileInt(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Int8:
|
2020-12-15 06:29:19 +03:00
|
|
|
return d.compileInt8(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Int16:
|
2020-12-15 06:29:19 +03:00
|
|
|
return d.compileInt16(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Int32:
|
2020-12-15 06:29:19 +03:00
|
|
|
return d.compileInt32(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Int64:
|
2020-12-15 06:29:19 +03:00
|
|
|
return d.compileInt64(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Uint:
|
2020-12-21 09:48:57 +03:00
|
|
|
return d.compileUint(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Uint8:
|
2020-12-21 09:48:57 +03:00
|
|
|
return d.compileUint8(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Uint16:
|
2020-12-21 09:48:57 +03:00
|
|
|
return d.compileUint16(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Uint32:
|
2020-12-21 09:48:57 +03:00
|
|
|
return d.compileUint32(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Uint64:
|
2020-12-21 09:48:57 +03:00
|
|
|
return d.compileUint64(typ, structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.String:
|
2020-11-23 11:16:31 +03:00
|
|
|
return d.compileString(structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Bool:
|
2020-11-23 11:16:31 +03:00
|
|
|
return d.compileBool(structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Float32:
|
2020-11-23 11:16:31 +03:00
|
|
|
return d.compileFloat32(structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
case reflect.Float64:
|
2020-11-23 11:16:31 +03:00
|
|
|
return d.compileFloat64(structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
}
|
|
|
|
return nil, &UnsupportedTypeError{Type: rtype2type(typ)}
|
|
|
|
}
|
|
|
|
|
2020-12-12 12:09:46 +03:00
|
|
|
func (d *Decoder) compileMapKey(typ *rtype, structName, fieldName string) (decoder, error) {
|
|
|
|
dec, err := d.compile(typ, structName, fieldName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
switch t := dec.(type) {
|
|
|
|
case *stringDecoder, *interfaceDecoder, *unmarshalJSONDecoder, *unmarshalTextDecoder:
|
|
|
|
return dec, nil
|
|
|
|
case *boolDecoder, *intDecoder, *uintDecoder, *numberDecoder:
|
|
|
|
return newWrappedStringDecoder(dec, structName, fieldName), nil
|
|
|
|
case *ptrDecoder:
|
|
|
|
dec = t.dec
|
|
|
|
default:
|
|
|
|
goto ERROR
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ERROR:
|
|
|
|
return nil, &UnsupportedTypeError{Type: rtype2type(typ)}
|
|
|
|
}
|
|
|
|
|
2020-11-23 11:16:31 +03:00
|
|
|
func (d *Decoder) compilePtr(typ *rtype, structName, fieldName string) (decoder, error) {
|
|
|
|
dec, err := d.compile(typ.Elem(), structName, fieldName)
|
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
|
|
|
}
|
|
|
|
|
2020-12-15 06:29:19 +03:00
|
|
|
func (d *Decoder) compileInt(typ *rtype, structName, fieldName string) (decoder, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-15 06:29:19 +03:00
|
|
|
func (d *Decoder) compileInt8(typ *rtype, structName, fieldName string) (decoder, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-15 06:29:19 +03:00
|
|
|
func (d *Decoder) compileInt16(typ *rtype, structName, fieldName string) (decoder, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-15 06:29:19 +03:00
|
|
|
func (d *Decoder) compileInt32(typ *rtype, structName, fieldName string) (decoder, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-15 06:29:19 +03:00
|
|
|
func (d *Decoder) compileInt64(typ *rtype, structName, fieldName string) (decoder, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-21 09:48:57 +03:00
|
|
|
func (d *Decoder) compileUint(typ *rtype, structName, fieldName string) (decoder, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-21 09:48:57 +03:00
|
|
|
func (d *Decoder) compileUint8(typ *rtype, structName, fieldName string) (decoder, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-21 09:48:57 +03:00
|
|
|
func (d *Decoder) compileUint16(typ *rtype, structName, fieldName string) (decoder, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-21 09:48:57 +03:00
|
|
|
func (d *Decoder) compileUint32(typ *rtype, structName, fieldName string) (decoder, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-21 09:48:57 +03:00
|
|
|
func (d *Decoder) compileUint64(typ *rtype, structName, fieldName string) (decoder, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-23 11:16:31 +03:00
|
|
|
func (d *Decoder) compileFloat32(structName, fieldName string) (decoder, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-23 11:16:31 +03:00
|
|
|
func (d *Decoder) compileFloat64(structName, fieldName string) (decoder, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-23 11:16:31 +03:00
|
|
|
func (d *Decoder) compileString(structName, fieldName string) (decoder, error) {
|
|
|
|
return newStringDecoder(structName, fieldName), nil
|
2020-05-24 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
2020-11-23 11:16:31 +03:00
|
|
|
func (d *Decoder) compileBool(structName, fieldName string) (decoder, error) {
|
|
|
|
return newBoolDecoder(structName, fieldName), nil
|
2020-05-24 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
2020-11-23 11:16:31 +03:00
|
|
|
func (d *Decoder) compileBytes(structName, fieldName string) (decoder, error) {
|
|
|
|
return newBytesDecoder(structName, fieldName), nil
|
2020-11-21 20:47:18 +03:00
|
|
|
}
|
|
|
|
|
2020-11-23 11:16:31 +03:00
|
|
|
func (d *Decoder) compileSlice(typ *rtype, structName, fieldName string) (decoder, error) {
|
2020-05-24 15:31:10 +03:00
|
|
|
elem := typ.Elem()
|
2020-11-23 11:16:31 +03:00
|
|
|
decoder, err := d.compile(elem, structName, fieldName)
|
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
|
|
|
}
|
|
|
|
|
2020-11-23 11:16:31 +03:00
|
|
|
func (d *Decoder) compileArray(typ *rtype, structName, fieldName string) (decoder, error) {
|
2020-05-24 15:31:10 +03:00
|
|
|
elem := typ.Elem()
|
2020-11-23 11:16:31 +03:00
|
|
|
decoder, err := d.compile(elem, structName, fieldName)
|
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
|
|
|
}
|
|
|
|
|
2020-11-23 11:16:31 +03:00
|
|
|
func (d *Decoder) compileMap(typ *rtype, structName, fieldName string) (decoder, error) {
|
2020-12-12 12:09:46 +03:00
|
|
|
keyDec, err := d.compileMapKey(typ.Key(), structName, fieldName)
|
2020-05-24 15:31:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-23 11:16:31 +03:00
|
|
|
valueDec, err := d.compile(typ.Elem(), structName, fieldName)
|
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
|
|
|
}
|
|
|
|
|
2020-11-23 11:16:31 +03:00
|
|
|
func (d *Decoder) compileInterface(typ *rtype, structName, fieldName string) (decoder, error) {
|
|
|
|
return newInterfaceDecoder(typ, structName, fieldName), nil
|
2020-05-24 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
2020-12-22 15:55:59 +03:00
|
|
|
func (d *Decoder) removeConflictFields(fieldMap map[string]*structFieldSet, conflictedMap map[string]struct{}, dec *structDecoder, baseOffset uintptr) {
|
|
|
|
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: baseOffset + v.offset,
|
|
|
|
isTaggedKey: v.isTaggedKey,
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
conflictedMap[k] = struct{}{}
|
|
|
|
conflictedMap[strings.ToLower(k)] = struct{}{}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if v.isTaggedKey {
|
|
|
|
fieldSet := &structFieldSet{
|
|
|
|
dec: v.dec,
|
|
|
|
offset: baseOffset + v.offset,
|
|
|
|
isTaggedKey: v.isTaggedKey,
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
conflictedMap[k] = struct{}{}
|
|
|
|
conflictedMap[strings.ToLower(k)] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-23 11:16:31 +03:00
|
|
|
func (d *Decoder) compileStruct(typ *rtype, structName, fieldName string) (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))
|
|
|
|
if dec, exists := d.structTypeToDecoder[typeptr]; exists {
|
|
|
|
return dec, nil
|
|
|
|
}
|
2020-11-23 11:16:31 +03:00
|
|
|
structDec := newStructDecoder(structName, fieldName, fieldMap)
|
2020-08-25 11:18:37 +03:00
|
|
|
d.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
|
|
|
|
}
|
2020-08-20 06:38:50 +03:00
|
|
|
tag := structTagFromField(field)
|
2020-11-23 11:16:31 +03:00
|
|
|
dec, err := d.compile(type2rtype(field.Type), structName, field.Name)
|
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
|
|
|
|
}
|
|
|
|
d.removeConflictFields(fieldMap, conflictedMap, stDec, uintptr(field.Offset))
|
|
|
|
} else if pdec, ok := dec.(*ptrDecoder); ok {
|
|
|
|
contentDec := pdec.contentDecoder()
|
|
|
|
if pdec.typ == typ {
|
|
|
|
// recursive definition
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
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: uintptr(field.Offset),
|
|
|
|
isTaggedKey: v.isTaggedKey,
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
conflictedMap[k] = struct{}{}
|
|
|
|
conflictedMap[strings.ToLower(k)] = struct{}{}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if v.isTaggedKey {
|
|
|
|
fieldSet := &structFieldSet{
|
|
|
|
dec: newAnonymousFieldDecoder(pdec.typ, v.offset, v.dec),
|
|
|
|
offset: uintptr(field.Offset),
|
|
|
|
isTaggedKey: v.isTaggedKey,
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
conflictedMap[k] = struct{}{}
|
|
|
|
conflictedMap[strings.ToLower(k)] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if tag.isString {
|
|
|
|
dec = newWrappedStringDecoder(dec, structName, field.Name)
|
|
|
|
}
|
|
|
|
fieldSet := &structFieldSet{dec: dec, offset: field.Offset, isTaggedKey: tag.isTaggedKey}
|
|
|
|
if tag.key != "" {
|
|
|
|
fieldMap[tag.key] = fieldSet
|
2020-12-22 19:28:56 +03:00
|
|
|
lower := strings.ToLower(tag.key)
|
|
|
|
if _, exists := fieldMap[lower]; !exists {
|
|
|
|
fieldMap[lower] = fieldSet
|
|
|
|
}
|
2020-12-22 15:55:59 +03:00
|
|
|
} else {
|
|
|
|
fieldMap[field.Name] = fieldSet
|
2020-12-22 19:28:56 +03:00
|
|
|
lower := strings.ToLower(field.Name)
|
|
|
|
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
|
|
|
}
|
2020-08-25 11:18:37 +03:00
|
|
|
delete(d.structTypeToDecoder, typeptr)
|
|
|
|
return structDec, nil
|
2020-05-24 15:31:10 +03:00
|
|
|
}
|