2021-03-16 13:44:32 +03:00
|
|
|
package encoder
|
|
|
|
|
|
|
|
import (
|
2021-06-12 11:06:26 +03:00
|
|
|
"context"
|
2021-03-16 13:44:32 +03:00
|
|
|
"encoding"
|
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
|
|
|
"sync/atomic"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/goccy/go-json/internal/errors"
|
|
|
|
"github.com/goccy/go-json/internal/runtime"
|
|
|
|
)
|
|
|
|
|
2021-06-12 11:06:26 +03:00
|
|
|
type marshalerContext interface {
|
|
|
|
MarshalJSON(context.Context) ([]byte, error)
|
|
|
|
}
|
|
|
|
|
2021-03-16 13:44:32 +03:00
|
|
|
var (
|
2021-06-12 11:06:26 +03:00
|
|
|
marshalJSONType = reflect.TypeOf((*json.Marshaler)(nil)).Elem()
|
|
|
|
marshalJSONContextType = reflect.TypeOf((*marshalerContext)(nil)).Elem()
|
|
|
|
marshalTextType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
|
|
|
|
jsonNumberType = reflect.TypeOf(json.Number(""))
|
|
|
|
cachedOpcodeSets []*OpcodeSet
|
|
|
|
cachedOpcodeMap unsafe.Pointer // map[uintptr]*OpcodeSet
|
|
|
|
typeAddr *runtime.TypeAddr
|
2021-03-16 13:44:32 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
typeAddr = runtime.AnalyzeTypeAddr()
|
2021-03-20 12:24:13 +03:00
|
|
|
if typeAddr == nil {
|
|
|
|
typeAddr = &runtime.TypeAddr{}
|
|
|
|
}
|
2021-05-04 03:00:10 +03:00
|
|
|
cachedOpcodeSets = make([]*OpcodeSet, typeAddr.AddrRange>>typeAddr.AddrShift)
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func loadOpcodeMap() map[uintptr]*OpcodeSet {
|
|
|
|
p := atomic.LoadPointer(&cachedOpcodeMap)
|
|
|
|
return *(*map[uintptr]*OpcodeSet)(unsafe.Pointer(&p))
|
|
|
|
}
|
|
|
|
|
|
|
|
func storeOpcodeSet(typ uintptr, set *OpcodeSet, m map[uintptr]*OpcodeSet) {
|
|
|
|
newOpcodeMap := make(map[uintptr]*OpcodeSet, len(m)+1)
|
|
|
|
newOpcodeMap[typ] = set
|
|
|
|
|
|
|
|
for k, v := range m {
|
|
|
|
newOpcodeMap[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
atomic.StorePointer(&cachedOpcodeMap, *(*unsafe.Pointer)(unsafe.Pointer(&newOpcodeMap)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func compileToGetCodeSetSlowPath(typeptr uintptr) (*OpcodeSet, error) {
|
|
|
|
opcodeMap := loadOpcodeMap()
|
|
|
|
if codeSet, exists := opcodeMap[typeptr]; exists {
|
|
|
|
return codeSet, nil
|
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
codeSet, err := newCompiler().compile(typeptr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
storeOpcodeSet(typeptr, codeSet, opcodeMap)
|
|
|
|
return codeSet, nil
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
type Compiler struct {
|
|
|
|
structTypeToCode map[uintptr]*StructCode
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
func newCompiler() *Compiler {
|
|
|
|
return &Compiler{
|
|
|
|
structTypeToCode: map[uintptr]*StructCode{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Compiler) compile(typeptr uintptr) (*OpcodeSet, error) {
|
|
|
|
// noescape trick for header.typ ( reflect.*rtype )
|
|
|
|
typ := *(**runtime.Type)(unsafe.Pointer(&typeptr))
|
|
|
|
code, err := c.typeToCode(typ)
|
2021-03-16 13:44:32 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
noescapeKeyCode := c.codeToOpcode(&compileContext{
|
|
|
|
structTypeToCodes: map[uintptr]Opcodes{},
|
|
|
|
recursiveCodes: &Opcodes{},
|
|
|
|
}, typ, code)
|
|
|
|
if err := noescapeKeyCode.Validate(); err != nil {
|
2021-05-29 08:47:18 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
escapeKeyCode := c.codeToOpcode(&compileContext{
|
|
|
|
structTypeToCodes: map[uintptr]Opcodes{},
|
|
|
|
recursiveCodes: &Opcodes{},
|
|
|
|
escapeKey: true,
|
|
|
|
}, typ, code)
|
2021-05-29 08:47:18 +03:00
|
|
|
noescapeKeyCode = copyOpcode(noescapeKeyCode)
|
|
|
|
escapeKeyCode = copyOpcode(escapeKeyCode)
|
2021-06-25 14:17:12 +03:00
|
|
|
setTotalLengthToInterfaceOp(noescapeKeyCode)
|
|
|
|
setTotalLengthToInterfaceOp(escapeKeyCode)
|
|
|
|
interfaceNoescapeKeyCode := copyToInterfaceOpcode(noescapeKeyCode)
|
|
|
|
interfaceEscapeKeyCode := copyToInterfaceOpcode(escapeKeyCode)
|
2021-05-29 08:47:18 +03:00
|
|
|
codeLength := noescapeKeyCode.TotalLength()
|
2021-11-27 20:14:57 +03:00
|
|
|
return &OpcodeSet{
|
|
|
|
Type: typ,
|
2021-06-25 14:17:12 +03:00
|
|
|
NoescapeKeyCode: noescapeKeyCode,
|
|
|
|
EscapeKeyCode: escapeKeyCode,
|
|
|
|
InterfaceNoescapeKeyCode: interfaceNoescapeKeyCode,
|
|
|
|
InterfaceEscapeKeyCode: interfaceEscapeKeyCode,
|
|
|
|
CodeLength: codeLength,
|
|
|
|
EndCode: ToEndCode(interfaceNoescapeKeyCode),
|
2021-11-27 20:14:57 +03:00
|
|
|
}, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) typeToCode(typ *runtime.Type) (Code, error) {
|
2021-03-16 13:44:32 +03:00
|
|
|
switch {
|
2021-11-27 20:14:57 +03:00
|
|
|
case c.implementsMarshalJSON(typ):
|
|
|
|
return c.marshalJSONCode(typ)
|
|
|
|
case c.implementsMarshalText(typ):
|
|
|
|
return c.marshalTextCode(typ)
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
isPtr := false
|
|
|
|
orgType := typ
|
|
|
|
if typ.Kind() == reflect.Ptr {
|
|
|
|
typ = typ.Elem()
|
|
|
|
isPtr = true
|
|
|
|
}
|
|
|
|
switch {
|
2021-11-27 20:14:57 +03:00
|
|
|
case c.implementsMarshalJSON(typ):
|
|
|
|
return c.marshalJSONCode(orgType)
|
|
|
|
case c.implementsMarshalText(typ):
|
|
|
|
return c.marshalTextCode(orgType)
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
2021-03-19 17:31:29 +03:00
|
|
|
switch typ.Kind() {
|
|
|
|
case reflect.Slice:
|
|
|
|
elem := typ.Elem()
|
|
|
|
if elem.Kind() == reflect.Uint8 {
|
|
|
|
p := runtime.PtrTo(elem)
|
2021-11-27 20:14:57 +03:00
|
|
|
if !c.implementsMarshalJSONType(p) && !p.Implements(marshalTextType) {
|
|
|
|
return c.bytesCode(typ, isPtr)
|
2021-03-19 17:31:29 +03:00
|
|
|
}
|
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.sliceCode(typ)
|
2021-03-19 17:31:29 +03:00
|
|
|
case reflect.Map:
|
2021-03-16 13:44:32 +03:00
|
|
|
if isPtr {
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.ptrCode(runtime.PtrTo(typ))
|
2021-04-02 08:03:00 +03:00
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.mapCode(typ)
|
2021-03-19 17:31:29 +03:00
|
|
|
case reflect.Struct:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.structCode(typ, isPtr)
|
2021-03-19 17:31:29 +03:00
|
|
|
case reflect.Int:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.intCode(typ, isPtr)
|
2021-03-19 17:31:29 +03:00
|
|
|
case reflect.Int8:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.int8Code(typ, isPtr)
|
2021-03-19 17:31:29 +03:00
|
|
|
case reflect.Int16:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.int16Code(typ, isPtr)
|
2021-03-19 17:31:29 +03:00
|
|
|
case reflect.Int32:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.int32Code(typ, isPtr)
|
2021-03-19 17:31:29 +03:00
|
|
|
case reflect.Int64:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.int64Code(typ, isPtr)
|
2021-03-19 17:31:29 +03:00
|
|
|
case reflect.Uint, reflect.Uintptr:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.uintCode(typ, isPtr)
|
2021-03-19 17:31:29 +03:00
|
|
|
case reflect.Uint8:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.uint8Code(typ, isPtr)
|
2021-03-19 17:31:29 +03:00
|
|
|
case reflect.Uint16:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.uint16Code(typ, isPtr)
|
2021-03-19 17:31:29 +03:00
|
|
|
case reflect.Uint32:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.uint32Code(typ, isPtr)
|
2021-03-19 17:31:29 +03:00
|
|
|
case reflect.Uint64:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.uint64Code(typ, isPtr)
|
2021-03-19 17:31:29 +03:00
|
|
|
case reflect.Float32:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.float32Code(typ, isPtr)
|
2021-03-19 17:31:29 +03:00
|
|
|
case reflect.Float64:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.float64Code(typ, isPtr)
|
2021-03-19 17:31:29 +03:00
|
|
|
case reflect.String:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.stringCode(typ, isPtr)
|
2021-03-19 17:31:29 +03:00
|
|
|
case reflect.Bool:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.boolCode(typ, isPtr)
|
2021-03-19 17:55:01 +03:00
|
|
|
case reflect.Interface:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.interfaceCode(typ, isPtr)
|
2021-03-19 17:31:29 +03:00
|
|
|
default:
|
|
|
|
if isPtr && typ.Implements(marshalTextType) {
|
|
|
|
typ = orgType
|
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.typeToCodeWithPtr(typ, isPtr)
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) typeToCodeWithPtr(typ *runtime.Type, isPtr bool) (Code, error) {
|
2021-03-16 13:44:32 +03:00
|
|
|
switch {
|
2021-11-27 20:14:57 +03:00
|
|
|
case c.implementsMarshalJSON(typ):
|
|
|
|
return c.marshalJSONCode(typ)
|
|
|
|
case c.implementsMarshalText(typ):
|
|
|
|
return c.marshalTextCode(typ)
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
switch typ.Kind() {
|
|
|
|
case reflect.Ptr:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.ptrCode(typ)
|
2021-03-16 13:44:32 +03:00
|
|
|
case reflect.Slice:
|
|
|
|
elem := typ.Elem()
|
|
|
|
if elem.Kind() == reflect.Uint8 {
|
|
|
|
p := runtime.PtrTo(elem)
|
2021-11-27 20:14:57 +03:00
|
|
|
if !c.implementsMarshalJSONType(p) && !p.Implements(marshalTextType) {
|
|
|
|
return c.bytesCode(typ, false)
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.sliceCode(typ)
|
2021-03-16 13:44:32 +03:00
|
|
|
case reflect.Array:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.arrayCode(typ)
|
2021-03-16 13:44:32 +03:00
|
|
|
case reflect.Map:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.mapCode(typ)
|
2021-03-16 13:44:32 +03:00
|
|
|
case reflect.Struct:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.structCode(typ, isPtr)
|
2021-03-16 13:44:32 +03:00
|
|
|
case reflect.Interface:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.interfaceCode(typ, false)
|
2021-03-16 13:44:32 +03:00
|
|
|
case reflect.Int:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.intCode(typ, false)
|
2021-03-16 13:44:32 +03:00
|
|
|
case reflect.Int8:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.int8Code(typ, false)
|
2021-03-16 13:44:32 +03:00
|
|
|
case reflect.Int16:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.int16Code(typ, false)
|
2021-03-16 13:44:32 +03:00
|
|
|
case reflect.Int32:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.int32Code(typ, false)
|
2021-03-16 13:44:32 +03:00
|
|
|
case reflect.Int64:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.int64Code(typ, false)
|
2021-03-16 13:44:32 +03:00
|
|
|
case reflect.Uint:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.uintCode(typ, false)
|
2021-03-16 13:44:32 +03:00
|
|
|
case reflect.Uint8:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.uint8Code(typ, false)
|
2021-03-16 13:44:32 +03:00
|
|
|
case reflect.Uint16:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.uint16Code(typ, false)
|
2021-03-16 13:44:32 +03:00
|
|
|
case reflect.Uint32:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.uint32Code(typ, false)
|
2021-03-16 13:44:32 +03:00
|
|
|
case reflect.Uint64:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.uint64Code(typ, false)
|
2021-03-16 13:44:32 +03:00
|
|
|
case reflect.Uintptr:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.uintCode(typ, false)
|
2021-03-16 13:44:32 +03:00
|
|
|
case reflect.Float32:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.float32Code(typ, false)
|
2021-03-16 13:44:32 +03:00
|
|
|
case reflect.Float64:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.float64Code(typ, false)
|
2021-03-16 13:44:32 +03:00
|
|
|
case reflect.String:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.stringCode(typ, false)
|
2021-03-16 13:44:32 +03:00
|
|
|
case reflect.Bool:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.boolCode(typ, false)
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
return nil, &errors.UnsupportedTypeError{Type: runtime.RType2Type(typ)}
|
|
|
|
}
|
|
|
|
|
|
|
|
const intSize = 32 << (^uint(0) >> 63)
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) intCode(typ *runtime.Type, isPtr bool) (*IntCode, error) {
|
|
|
|
return &IntCode{typ: typ, bitSize: intSize, isPtr: isPtr}, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) int8Code(typ *runtime.Type, isPtr bool) (*IntCode, error) {
|
|
|
|
return &IntCode{typ: typ, bitSize: 8, isPtr: isPtr}, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) int16Code(typ *runtime.Type, isPtr bool) (*IntCode, error) {
|
|
|
|
return &IntCode{typ: typ, bitSize: 16, isPtr: isPtr}, nil
|
2021-03-19 17:31:29 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) int32Code(typ *runtime.Type, isPtr bool) (*IntCode, error) {
|
|
|
|
return &IntCode{typ: typ, bitSize: 32, isPtr: isPtr}, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) int64Code(typ *runtime.Type, isPtr bool) (*IntCode, error) {
|
|
|
|
return &IntCode{typ: typ, bitSize: 64, isPtr: isPtr}, nil
|
2021-03-19 17:31:29 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) uintCode(typ *runtime.Type, isPtr bool) (*UintCode, error) {
|
|
|
|
return &UintCode{typ: typ, bitSize: intSize, isPtr: isPtr}, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) uint8Code(typ *runtime.Type, isPtr bool) (*UintCode, error) {
|
|
|
|
return &UintCode{typ: typ, bitSize: 8, isPtr: isPtr}, nil
|
2021-03-19 17:31:29 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) uint16Code(typ *runtime.Type, isPtr bool) (*UintCode, error) {
|
|
|
|
return &UintCode{typ: typ, bitSize: 16, isPtr: isPtr}, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) uint32Code(typ *runtime.Type, isPtr bool) (*UintCode, error) {
|
|
|
|
return &UintCode{typ: typ, bitSize: 32, isPtr: isPtr}, nil
|
2021-03-19 17:31:29 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) uint64Code(typ *runtime.Type, isPtr bool) (*UintCode, error) {
|
|
|
|
return &UintCode{typ: typ, bitSize: 64, isPtr: isPtr}, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) float32Code(typ *runtime.Type, isPtr bool) (*FloatCode, error) {
|
|
|
|
return &FloatCode{typ: typ, bitSize: 32, isPtr: isPtr}, nil
|
2021-03-19 17:31:29 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) float64Code(typ *runtime.Type, isPtr bool) (*FloatCode, error) {
|
|
|
|
return &FloatCode{typ: typ, bitSize: 64, isPtr: isPtr}, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) stringCode(typ *runtime.Type, isPtr bool) (*StringCode, error) {
|
|
|
|
return &StringCode{typ: typ, isPtr: isPtr}, nil
|
2021-03-19 17:31:29 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) boolCode(typ *runtime.Type, isPtr bool) (*BoolCode, error) {
|
|
|
|
return &BoolCode{typ: typ, isPtr: isPtr}, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) intStringCode(typ *runtime.Type) (*IntCode, error) {
|
|
|
|
return &IntCode{typ: typ, bitSize: intSize, isString: true}, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) int8StringCode(typ *runtime.Type) (*IntCode, error) {
|
|
|
|
return &IntCode{typ: typ, bitSize: 8, isString: true}, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) int16StringCode(typ *runtime.Type) (*IntCode, error) {
|
|
|
|
return &IntCode{typ: typ, bitSize: 16, isString: true}, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) int32StringCode(typ *runtime.Type) (*IntCode, error) {
|
|
|
|
return &IntCode{typ: typ, bitSize: 32, isString: true}, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) int64StringCode(typ *runtime.Type) (*IntCode, error) {
|
|
|
|
return &IntCode{typ: typ, bitSize: 64, isString: true}, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) uintStringCode(typ *runtime.Type) (*UintCode, error) {
|
|
|
|
return &UintCode{typ: typ, bitSize: intSize, isString: true}, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) uint8StringCode(typ *runtime.Type) (*UintCode, error) {
|
|
|
|
return &UintCode{typ: typ, bitSize: 8, isString: true}, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) uint16StringCode(typ *runtime.Type) (*UintCode, error) {
|
|
|
|
return &UintCode{typ: typ, bitSize: 16, isString: true}, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) uint32StringCode(typ *runtime.Type) (*UintCode, error) {
|
|
|
|
return &UintCode{typ: typ, bitSize: 32, isString: true}, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) uint64StringCode(typ *runtime.Type) (*UintCode, error) {
|
|
|
|
return &UintCode{typ: typ, bitSize: 64, isString: true}, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:48:01 +03:00
|
|
|
//nolint:unparam
|
|
|
|
func (c *Compiler) bytesCode(typ *runtime.Type, isPtr bool) (*BytesCode, error) {
|
|
|
|
return &BytesCode{typ: typ, isPtr: isPtr}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//nolint:unparam
|
|
|
|
func (c *Compiler) interfaceCode(typ *runtime.Type, isPtr bool) (*InterfaceCode, error) {
|
|
|
|
return &InterfaceCode{typ: typ, isPtr: isPtr}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//nolint:unparam
|
|
|
|
func (c *Compiler) marshalJSONCode(typ *runtime.Type) (*MarshalJSONCode, error) {
|
|
|
|
return &MarshalJSONCode{
|
|
|
|
typ: typ,
|
|
|
|
isAddrForMarshaler: c.isPtrMarshalJSONType(typ),
|
|
|
|
isNilableType: c.isNilableType(typ),
|
|
|
|
isMarshalerContext: typ.Implements(marshalJSONContextType) || runtime.PtrTo(typ).Implements(marshalJSONContextType),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//nolint:unparam
|
|
|
|
func (c *Compiler) marshalTextCode(typ *runtime.Type) (*MarshalTextCode, error) {
|
|
|
|
return &MarshalTextCode{
|
|
|
|
typ: typ,
|
|
|
|
isAddrForMarshaler: c.isPtrMarshalTextType(typ),
|
|
|
|
isNilableType: c.isNilableType(typ),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Compiler) ptrCode(typ *runtime.Type) (*PtrCode, error) {
|
|
|
|
code, err := c.typeToCodeWithPtr(typ.Elem(), true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ptr, ok := code.(*PtrCode)
|
|
|
|
if ok {
|
|
|
|
return &PtrCode{typ: typ, value: ptr.value, ptrNum: ptr.ptrNum + 1}, nil
|
|
|
|
}
|
|
|
|
return &PtrCode{typ: typ, value: code, ptrNum: 1}, nil
|
|
|
|
}
|
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) sliceCode(typ *runtime.Type) (*SliceCode, error) {
|
|
|
|
elem := typ.Elem()
|
|
|
|
code, err := c.listElemCode(elem)
|
2021-03-19 17:31:29 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
if code.Kind() == CodeKindStruct {
|
|
|
|
structCode := code.(*StructCode)
|
|
|
|
structCode.enableIndirect()
|
|
|
|
}
|
|
|
|
return &SliceCode{typ: typ, value: code}, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) arrayCode(typ *runtime.Type) (*ArrayCode, error) {
|
|
|
|
elem := typ.Elem()
|
|
|
|
code, err := c.listElemCode(elem)
|
2021-03-19 17:31:29 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
if code.Kind() == CodeKindStruct {
|
|
|
|
structCode := code.(*StructCode)
|
|
|
|
structCode.enableIndirect()
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
return &ArrayCode{typ: typ, value: code}, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) mapCode(typ *runtime.Type) (*MapCode, error) {
|
|
|
|
keyCode, err := c.mapKeyCode(typ.Key())
|
2021-03-19 17:31:29 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
valueCode, err := c.mapValueCode(typ.Elem())
|
2021-03-19 17:31:29 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
if valueCode.Kind() == CodeKindStruct {
|
|
|
|
structCode := valueCode.(*StructCode)
|
|
|
|
structCode.enableIndirect()
|
|
|
|
}
|
|
|
|
return &MapCode{typ: typ, key: keyCode, value: valueCode}, nil
|
2021-03-19 17:31:29 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) listElemCode(typ *runtime.Type) (Code, error) {
|
2021-03-16 13:44:32 +03:00
|
|
|
switch {
|
2021-11-27 20:14:57 +03:00
|
|
|
case c.isPtrMarshalJSONType(typ):
|
|
|
|
return c.marshalJSONCode(typ)
|
2021-03-16 13:44:32 +03:00
|
|
|
case !typ.Implements(marshalTextType) && runtime.PtrTo(typ).Implements(marshalTextType):
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.marshalTextCode(typ)
|
2021-03-20 19:33:13 +03:00
|
|
|
case typ.Kind() == reflect.Map:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.ptrCode(runtime.PtrTo(typ))
|
2021-03-16 13:44:32 +03:00
|
|
|
default:
|
2021-11-27 20:14:57 +03:00
|
|
|
code, err := c.typeToCodeWithPtr(typ, false)
|
2021-03-20 19:33:13 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
ptr, ok := code.(*PtrCode)
|
|
|
|
if ok {
|
|
|
|
if ptr.value.Kind() == CodeKindMap {
|
|
|
|
ptr.ptrNum++
|
|
|
|
}
|
2021-03-20 19:33:13 +03:00
|
|
|
}
|
|
|
|
return code, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) mapKeyCode(typ *runtime.Type) (Code, error) {
|
|
|
|
switch {
|
|
|
|
case c.implementsMarshalJSON(typ):
|
|
|
|
return c.marshalJSONCode(typ)
|
|
|
|
case c.implementsMarshalText(typ):
|
|
|
|
return c.marshalTextCode(typ)
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
switch typ.Kind() {
|
|
|
|
case reflect.Ptr:
|
|
|
|
return c.ptrCode(typ)
|
|
|
|
case reflect.String:
|
|
|
|
return c.stringCode(typ, false)
|
|
|
|
case reflect.Int:
|
|
|
|
return c.intStringCode(typ)
|
|
|
|
case reflect.Int8:
|
|
|
|
return c.int8StringCode(typ)
|
|
|
|
case reflect.Int16:
|
|
|
|
return c.int16StringCode(typ)
|
|
|
|
case reflect.Int32:
|
|
|
|
return c.int32StringCode(typ)
|
|
|
|
case reflect.Int64:
|
|
|
|
return c.int64StringCode(typ)
|
|
|
|
case reflect.Uint:
|
|
|
|
return c.uintStringCode(typ)
|
|
|
|
case reflect.Uint8:
|
|
|
|
return c.uint8StringCode(typ)
|
|
|
|
case reflect.Uint16:
|
|
|
|
return c.uint16StringCode(typ)
|
|
|
|
case reflect.Uint32:
|
|
|
|
return c.uint32StringCode(typ)
|
|
|
|
case reflect.Uint64:
|
|
|
|
return c.uint64StringCode(typ)
|
|
|
|
case reflect.Uintptr:
|
|
|
|
return c.uintStringCode(typ)
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
return nil, &errors.UnsupportedTypeError{Type: runtime.RType2Type(typ)}
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) mapValueCode(typ *runtime.Type) (Code, error) {
|
|
|
|
switch typ.Kind() {
|
2021-03-16 13:44:32 +03:00
|
|
|
case reflect.Map:
|
2021-11-27 20:14:57 +03:00
|
|
|
return c.ptrCode(runtime.PtrTo(typ))
|
2021-03-16 13:44:32 +03:00
|
|
|
default:
|
2021-11-27 20:14:57 +03:00
|
|
|
code, err := c.typeToCodeWithPtr(typ, false)
|
2021-03-20 19:33:13 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
ptr, ok := code.(*PtrCode)
|
|
|
|
if ok {
|
|
|
|
if ptr.value.Kind() == CodeKindMap {
|
|
|
|
ptr.ptrNum++
|
|
|
|
}
|
2021-03-20 19:33:13 +03:00
|
|
|
}
|
|
|
|
return code, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) structCode(typ *runtime.Type, isPtr bool) (*StructCode, error) {
|
|
|
|
typeptr := uintptr(unsafe.Pointer(typ))
|
|
|
|
if code, exists := c.structTypeToCode[typeptr]; exists {
|
|
|
|
derefCode := *code
|
|
|
|
derefCode.isRecursive = true
|
|
|
|
return &derefCode, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
indirect := runtime.IfaceIndir(typ)
|
|
|
|
code := &StructCode{typ: typ, isPtr: isPtr, isIndirect: indirect}
|
|
|
|
c.structTypeToCode[typeptr] = code
|
2021-03-16 13:44:32 +03:00
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
fieldNum := typ.NumField()
|
|
|
|
tags := c.typeToStructTags(typ)
|
|
|
|
fields := []*StructFieldCode{}
|
|
|
|
for i, tag := range tags {
|
|
|
|
isOnlyOneFirstField := i == 0 && fieldNum == 1
|
|
|
|
field, err := c.structFieldCode(code, tag, isPtr, isOnlyOneFirstField)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if field.isAnonymous {
|
|
|
|
structCode := field.getAnonymousStruct()
|
|
|
|
if structCode != nil {
|
|
|
|
structCode.removeFieldsByTags(tags)
|
|
|
|
if c.isAssignableIndirect(field, isPtr) {
|
|
|
|
if indirect {
|
|
|
|
structCode.isIndirect = true
|
|
|
|
} else {
|
|
|
|
structCode.isIndirect = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
structCode := field.getStruct()
|
|
|
|
if structCode != nil {
|
|
|
|
if indirect {
|
|
|
|
// if parent is indirect type, set child indirect property to true
|
|
|
|
structCode.isIndirect = true
|
|
|
|
} else {
|
|
|
|
// if parent is not indirect type, set child indirect property to false.
|
|
|
|
// but if parent's indirect is false and isPtr is true, then indirect must be true.
|
|
|
|
// Do this only if indirectConversion is enabled at the end of compileStruct.
|
|
|
|
structCode.isIndirect = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fields = append(fields, field)
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
fieldMap := c.getFieldMap(fields)
|
|
|
|
duplicatedFieldMap := c.getDuplicatedFieldMap(fieldMap)
|
|
|
|
code.fields = c.filteredDuplicatedFields(fields, duplicatedFieldMap)
|
|
|
|
if !code.disableIndirectConversion && !indirect && isPtr {
|
|
|
|
code.enableIndirect()
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
delete(c.structTypeToCode, typeptr)
|
|
|
|
return code, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) structFieldCode(structCode *StructCode, tag *runtime.StructTag, isPtr, isOnlyOneFirstField bool) (*StructFieldCode, error) {
|
|
|
|
field := tag.Field
|
|
|
|
fieldType := runtime.Type2RType(field.Type)
|
|
|
|
isIndirectSpecialCase := isPtr && isOnlyOneFirstField
|
|
|
|
fieldCode := &StructFieldCode{
|
|
|
|
typ: fieldType,
|
|
|
|
key: tag.Key,
|
|
|
|
tag: tag,
|
|
|
|
offset: field.Offset,
|
|
|
|
isAnonymous: field.Anonymous && !tag.IsTaggedKey,
|
|
|
|
isTaggedKey: tag.IsTaggedKey,
|
|
|
|
isNilableType: c.isNilableType(fieldType),
|
|
|
|
isNilCheck: true,
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
switch {
|
|
|
|
case c.isMovePointerPositionFromHeadToFirstMarshalJSONFieldCase(fieldType, isIndirectSpecialCase):
|
|
|
|
code, err := c.marshalJSONCode(fieldType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fieldCode.value = code
|
|
|
|
fieldCode.isAddrForMarshaler = true
|
|
|
|
fieldCode.isNilCheck = false
|
|
|
|
structCode.isIndirect = false
|
|
|
|
structCode.disableIndirectConversion = true
|
|
|
|
case c.isMovePointerPositionFromHeadToFirstMarshalTextFieldCase(fieldType, isIndirectSpecialCase):
|
|
|
|
code, err := c.marshalTextCode(fieldType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fieldCode.value = code
|
|
|
|
fieldCode.isAddrForMarshaler = true
|
|
|
|
fieldCode.isNilCheck = false
|
|
|
|
structCode.isIndirect = false
|
|
|
|
structCode.disableIndirectConversion = true
|
|
|
|
case isPtr && c.isPtrMarshalJSONType(fieldType):
|
|
|
|
// *struct{ field T }
|
|
|
|
// func (*T) MarshalJSON() ([]byte, error)
|
|
|
|
code, err := c.marshalJSONCode(fieldType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fieldCode.value = code
|
|
|
|
fieldCode.isAddrForMarshaler = true
|
|
|
|
fieldCode.isNilCheck = false
|
|
|
|
case isPtr && c.isPtrMarshalTextType(fieldType):
|
|
|
|
// *struct{ field T }
|
|
|
|
// func (*T) MarshalText() ([]byte, error)
|
|
|
|
code, err := c.marshalTextCode(fieldType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fieldCode.value = code
|
|
|
|
fieldCode.isAddrForMarshaler = true
|
|
|
|
fieldCode.isNilCheck = false
|
|
|
|
default:
|
|
|
|
code, err := c.typeToCodeWithPtr(fieldType, isPtr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch code.Kind() {
|
|
|
|
case CodeKindPtr, CodeKindInterface:
|
|
|
|
fieldCode.isNextOpPtrType = true
|
|
|
|
}
|
|
|
|
fieldCode.value = code
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
return fieldCode, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) isAssignableIndirect(fieldCode *StructFieldCode, isPtr bool) bool {
|
|
|
|
if isPtr {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
codeType := fieldCode.value.Kind()
|
|
|
|
if codeType == CodeKindMarshalJSON {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if codeType == CodeKindMarshalText {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) getFieldMap(fields []*StructFieldCode) map[string][]*StructFieldCode {
|
|
|
|
fieldMap := map[string][]*StructFieldCode{}
|
|
|
|
for _, field := range fields {
|
|
|
|
if field.isAnonymous {
|
|
|
|
for k, v := range c.getAnonymousFieldMap(field) {
|
|
|
|
fieldMap[k] = append(fieldMap[k], v...)
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
fieldMap[field.key] = append(fieldMap[field.key], field)
|
|
|
|
}
|
|
|
|
return fieldMap
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) getAnonymousFieldMap(field *StructFieldCode) map[string][]*StructFieldCode {
|
|
|
|
fieldMap := map[string][]*StructFieldCode{}
|
|
|
|
structCode := field.getAnonymousStruct()
|
|
|
|
if structCode == nil || structCode.isRecursive {
|
|
|
|
fieldMap[field.key] = append(fieldMap[field.key], field)
|
|
|
|
return fieldMap
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
for k, v := range c.getFieldMapFromAnonymousParent(structCode.fields) {
|
|
|
|
fieldMap[k] = append(fieldMap[k], v...)
|
|
|
|
}
|
|
|
|
return fieldMap
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Compiler) getFieldMapFromAnonymousParent(fields []*StructFieldCode) map[string][]*StructFieldCode {
|
|
|
|
fieldMap := map[string][]*StructFieldCode{}
|
|
|
|
for _, field := range fields {
|
|
|
|
if field.isAnonymous {
|
|
|
|
for k, v := range c.getAnonymousFieldMap(field) {
|
|
|
|
// Do not handle tagged key when embedding more than once
|
|
|
|
for _, vv := range v {
|
|
|
|
vv.isTaggedKey = false
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
fieldMap[k] = append(fieldMap[k], v...)
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
continue
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
fieldMap[field.key] = append(fieldMap[field.key], field)
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
return fieldMap
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) getDuplicatedFieldMap(fieldMap map[string][]*StructFieldCode) map[*StructFieldCode]struct{} {
|
|
|
|
duplicatedFieldMap := map[*StructFieldCode]struct{}{}
|
|
|
|
for _, fields := range fieldMap {
|
|
|
|
if len(fields) == 1 {
|
2021-03-16 13:44:32 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
if c.isTaggedKeyOnly(fields) {
|
|
|
|
for _, field := range fields {
|
|
|
|
if field.isTaggedKey {
|
|
|
|
continue
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
duplicatedFieldMap[field] = struct{}{}
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
} else {
|
2021-11-27 20:14:57 +03:00
|
|
|
for _, field := range fields {
|
|
|
|
duplicatedFieldMap[field] = struct{}{}
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
return duplicatedFieldMap
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) filteredDuplicatedFields(fields []*StructFieldCode, duplicatedFieldMap map[*StructFieldCode]struct{}) []*StructFieldCode {
|
|
|
|
filteredFields := make([]*StructFieldCode, 0, len(fields))
|
|
|
|
for _, field := range fields {
|
|
|
|
if field.isAnonymous {
|
|
|
|
structCode := field.getAnonymousStruct()
|
|
|
|
if structCode != nil && !structCode.isRecursive {
|
|
|
|
structCode.fields = c.filteredDuplicatedFields(structCode.fields, duplicatedFieldMap)
|
|
|
|
if len(structCode.fields) > 0 {
|
|
|
|
filteredFields = append(filteredFields, field)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, exists := duplicatedFieldMap[field]; exists {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
filteredFields = append(filteredFields, field)
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
return filteredFields
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) isTaggedKeyOnly(fields []*StructFieldCode) bool {
|
|
|
|
var taggedKeyFieldCount int
|
|
|
|
for _, field := range fields {
|
|
|
|
if field.isTaggedKey {
|
|
|
|
taggedKeyFieldCount++
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
return taggedKeyFieldCount == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Compiler) typeToStructTags(typ *runtime.Type) runtime.StructTags {
|
2021-03-16 13:44:32 +03:00
|
|
|
tags := runtime.StructTags{}
|
2021-11-27 20:14:57 +03:00
|
|
|
fieldNum := typ.NumField()
|
2021-03-16 13:44:32 +03:00
|
|
|
for i := 0; i < fieldNum; i++ {
|
|
|
|
field := typ.Field(i)
|
|
|
|
if runtime.IsIgnoredStructField(field) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tags = append(tags, runtime.StructTagFromField(field))
|
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
return tags
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
// *struct{ field T } => struct { field *T }
|
|
|
|
// func (*T) MarshalJSON() ([]byte, error)
|
|
|
|
func (c *Compiler) isMovePointerPositionFromHeadToFirstMarshalJSONFieldCase(typ *runtime.Type, isIndirectSpecialCase bool) bool {
|
|
|
|
return isIndirectSpecialCase && !c.isNilableType(typ) && c.isPtrMarshalJSONType(typ)
|
|
|
|
}
|
2021-07-22 07:54:11 +03:00
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
// *struct{ field T } => struct { field *T }
|
|
|
|
// func (*T) MarshalText() ([]byte, error)
|
|
|
|
func (c *Compiler) isMovePointerPositionFromHeadToFirstMarshalTextFieldCase(typ *runtime.Type, isIndirectSpecialCase bool) bool {
|
|
|
|
return isIndirectSpecialCase && !c.isNilableType(typ) && c.isPtrMarshalTextType(typ)
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) implementsMarshalJSON(typ *runtime.Type) bool {
|
|
|
|
if !c.implementsMarshalJSONType(typ) {
|
|
|
|
return false
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
if typ.Kind() != reflect.Ptr {
|
|
|
|
return true
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
// type kind is reflect.Ptr
|
|
|
|
if !c.implementsMarshalJSONType(typ.Elem()) {
|
|
|
|
return true
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
// needs to dereference
|
|
|
|
return false
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) implementsMarshalText(typ *runtime.Type) bool {
|
|
|
|
if !typ.Implements(marshalTextType) {
|
|
|
|
return false
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
if typ.Kind() != reflect.Ptr {
|
|
|
|
return true
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
// type kind is reflect.Ptr
|
|
|
|
if !typ.Elem().Implements(marshalTextType) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// needs to dereference
|
|
|
|
return false
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) isNilableType(typ *runtime.Type) bool {
|
|
|
|
switch typ.Kind() {
|
|
|
|
case reflect.Ptr:
|
|
|
|
return true
|
|
|
|
case reflect.Map:
|
|
|
|
return true
|
|
|
|
case reflect.Func:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) implementsMarshalJSONType(typ *runtime.Type) bool {
|
2021-06-12 11:06:26 +03:00
|
|
|
return typ.Implements(marshalJSONType) || typ.Implements(marshalJSONContextType)
|
|
|
|
}
|
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) isPtrMarshalJSONType(typ *runtime.Type) bool {
|
|
|
|
return !c.implementsMarshalJSONType(typ) && c.implementsMarshalJSONType(runtime.PtrTo(typ))
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
func (c *Compiler) isPtrMarshalTextType(typ *runtime.Type) bool {
|
2021-03-16 13:44:32 +03:00
|
|
|
return !typ.Implements(marshalTextType) && runtime.PtrTo(typ).Implements(marshalTextType)
|
|
|
|
}
|
2021-11-27 20:14:57 +03:00
|
|
|
|
|
|
|
func (c *Compiler) codeToOpcode(ctx *compileContext, typ *runtime.Type, code Code) *Opcode {
|
|
|
|
codes := code.ToOpcode(ctx)
|
|
|
|
codes.Last().Next = newEndOp(ctx, typ)
|
|
|
|
c.linkRecursiveCode(ctx)
|
|
|
|
return codes.First()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Compiler) linkRecursiveCode(ctx *compileContext) {
|
|
|
|
for _, recursive := range *ctx.recursiveCodes {
|
|
|
|
typeptr := uintptr(unsafe.Pointer(recursive.Type))
|
|
|
|
codes := ctx.structTypeToCodes[typeptr]
|
|
|
|
compiled := recursive.Jmp
|
|
|
|
compiled.Code = copyOpcode(codes.First())
|
|
|
|
code := compiled.Code
|
|
|
|
code.End.Next = newEndOp(&compileContext{}, recursive.Type)
|
|
|
|
code.Op = code.Op.PtrHeadToHead()
|
|
|
|
|
|
|
|
beforeLastCode := code.End
|
|
|
|
lastCode := beforeLastCode.Next
|
|
|
|
|
|
|
|
totalLength := code.TotalLength()
|
|
|
|
lastCode.Idx = uint32((totalLength + 1) * uintptrSize)
|
|
|
|
lastCode.ElemIdx = lastCode.Idx + uintptrSize
|
|
|
|
lastCode.Length = lastCode.Idx + 2*uintptrSize
|
|
|
|
code.End.Next.Op = OpRecursiveEnd
|
|
|
|
|
|
|
|
// extend length to alloc slot for elemIdx + length
|
|
|
|
curTotalLength := uintptr(recursive.TotalLength()) + 3
|
|
|
|
nextTotalLength := uintptr(totalLength) + 3
|
|
|
|
compiled.CurLen = curTotalLength
|
|
|
|
compiled.NextLen = nextTotalLength
|
|
|
|
compiled.Linked = true
|
|
|
|
}
|
|
|
|
}
|