ipbl/vendor/github.com/goccy/go-json/internal/encoder/compiler.go

929 lines
26 KiB
Go
Raw Normal View History

2021-11-07 20:44:48 +01:00
package encoder
import (
"context"
"encoding"
"encoding/json"
"reflect"
"sync/atomic"
"unsafe"
"github.com/goccy/go-json/internal/errors"
"github.com/goccy/go-json/internal/runtime"
)
type marshalerContext interface {
MarshalJSON(context.Context) ([]byte, error)
}
var (
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
)
func init() {
typeAddr = runtime.AnalyzeTypeAddr()
if typeAddr == nil {
typeAddr = &runtime.TypeAddr{}
}
2022-07-16 11:42:03 +02:00
cachedOpcodeSets = make([]*OpcodeSet, typeAddr.AddrRange>>typeAddr.AddrShift+1)
2021-11-07 20:44:48 +01: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-12-12 17:33:40 +01:00
codeSet, err := newCompiler().compile(typeptr)
if err != nil {
return nil, err
}
storeOpcodeSet(typeptr, codeSet, opcodeMap)
return codeSet, nil
}
2021-11-07 20:44:48 +01:00
2022-02-22 14:43:07 +01:00
func getFilteredCodeSetIfNeeded(ctx *RuntimeContext, codeSet *OpcodeSet) (*OpcodeSet, error) {
if (ctx.Option.Flag & ContextOption) == 0 {
return codeSet, nil
}
query := FieldQueryFromContext(ctx.Option.Context)
if query == nil {
return codeSet, nil
}
ctx.Option.Flag |= FieldQueryOption
cacheCodeSet := codeSet.getQueryCache(query.Hash())
if cacheCodeSet != nil {
return cacheCodeSet, nil
}
queryCodeSet, err := newCompiler().codeToOpcodeSet(codeSet.Type, codeSet.Code.Filter(query))
if err != nil {
return nil, err
}
codeSet.setQueryCache(query.Hash(), queryCodeSet)
return queryCodeSet, nil
}
2021-12-12 17:33:40 +01:00
type Compiler struct {
structTypeToCode map[uintptr]*StructCode
}
2021-11-07 20:44:48 +01:00
2021-12-12 17:33:40 +01: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-11-07 20:44:48 +01:00
if err != nil {
return nil, err
}
2022-02-22 14:43:07 +01:00
return c.codeToOpcodeSet(typ, code)
}
func (c *Compiler) codeToOpcodeSet(typ *runtime.Type, code Code) (*OpcodeSet, error) {
2021-12-12 17:33:40 +01:00
noescapeKeyCode := c.codeToOpcode(&compileContext{
structTypeToCodes: map[uintptr]Opcodes{},
recursiveCodes: &Opcodes{},
}, typ, code)
if err := noescapeKeyCode.Validate(); err != nil {
2021-11-07 20:44:48 +01:00
return nil, err
}
2021-12-12 17:33:40 +01:00
escapeKeyCode := c.codeToOpcode(&compileContext{
structTypeToCodes: map[uintptr]Opcodes{},
recursiveCodes: &Opcodes{},
escapeKey: true,
}, typ, code)
2021-11-07 20:44:48 +01:00
noescapeKeyCode = copyOpcode(noescapeKeyCode)
escapeKeyCode = copyOpcode(escapeKeyCode)
setTotalLengthToInterfaceOp(noescapeKeyCode)
setTotalLengthToInterfaceOp(escapeKeyCode)
interfaceNoescapeKeyCode := copyToInterfaceOpcode(noescapeKeyCode)
interfaceEscapeKeyCode := copyToInterfaceOpcode(escapeKeyCode)
codeLength := noescapeKeyCode.TotalLength()
2021-12-12 17:33:40 +01:00
return &OpcodeSet{
Type: typ,
2021-11-07 20:44:48 +01:00
NoescapeKeyCode: noescapeKeyCode,
EscapeKeyCode: escapeKeyCode,
InterfaceNoescapeKeyCode: interfaceNoescapeKeyCode,
InterfaceEscapeKeyCode: interfaceEscapeKeyCode,
CodeLength: codeLength,
EndCode: ToEndCode(interfaceNoescapeKeyCode),
2022-02-22 14:43:07 +01:00
Code: code,
QueryCache: map[string]*OpcodeSet{},
2021-12-12 17:33:40 +01:00
}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
func (c *Compiler) typeToCode(typ *runtime.Type) (Code, error) {
2021-11-07 20:44:48 +01:00
switch {
2021-12-12 17:33:40 +01:00
case c.implementsMarshalJSON(typ):
return c.marshalJSONCode(typ)
case c.implementsMarshalText(typ):
return c.marshalTextCode(typ)
2021-11-07 20:44:48 +01:00
}
isPtr := false
orgType := typ
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
isPtr = true
}
switch {
2021-12-12 17:33:40 +01:00
case c.implementsMarshalJSON(typ):
return c.marshalJSONCode(orgType)
case c.implementsMarshalText(typ):
return c.marshalTextCode(orgType)
2021-11-07 20:44:48 +01:00
}
switch typ.Kind() {
case reflect.Slice:
elem := typ.Elem()
if elem.Kind() == reflect.Uint8 {
p := runtime.PtrTo(elem)
2021-12-12 17:33:40 +01:00
if !c.implementsMarshalJSONType(p) && !p.Implements(marshalTextType) {
return c.bytesCode(typ, isPtr)
2021-11-07 20:44:48 +01:00
}
}
2021-12-12 17:33:40 +01:00
return c.sliceCode(typ)
2021-11-07 20:44:48 +01:00
case reflect.Map:
if isPtr {
2021-12-12 17:33:40 +01:00
return c.ptrCode(runtime.PtrTo(typ))
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
return c.mapCode(typ)
2021-11-07 20:44:48 +01:00
case reflect.Struct:
2021-12-12 17:33:40 +01:00
return c.structCode(typ, isPtr)
2021-11-07 20:44:48 +01:00
case reflect.Int:
2021-12-12 17:33:40 +01:00
return c.intCode(typ, isPtr)
2021-11-07 20:44:48 +01:00
case reflect.Int8:
2021-12-12 17:33:40 +01:00
return c.int8Code(typ, isPtr)
2021-11-07 20:44:48 +01:00
case reflect.Int16:
2021-12-12 17:33:40 +01:00
return c.int16Code(typ, isPtr)
2021-11-07 20:44:48 +01:00
case reflect.Int32:
2021-12-12 17:33:40 +01:00
return c.int32Code(typ, isPtr)
2021-11-07 20:44:48 +01:00
case reflect.Int64:
2021-12-12 17:33:40 +01:00
return c.int64Code(typ, isPtr)
2021-11-07 20:44:48 +01:00
case reflect.Uint, reflect.Uintptr:
2021-12-12 17:33:40 +01:00
return c.uintCode(typ, isPtr)
2021-11-07 20:44:48 +01:00
case reflect.Uint8:
2021-12-12 17:33:40 +01:00
return c.uint8Code(typ, isPtr)
2021-11-07 20:44:48 +01:00
case reflect.Uint16:
2021-12-12 17:33:40 +01:00
return c.uint16Code(typ, isPtr)
2021-11-07 20:44:48 +01:00
case reflect.Uint32:
2021-12-12 17:33:40 +01:00
return c.uint32Code(typ, isPtr)
2021-11-07 20:44:48 +01:00
case reflect.Uint64:
2021-12-12 17:33:40 +01:00
return c.uint64Code(typ, isPtr)
2021-11-07 20:44:48 +01:00
case reflect.Float32:
2021-12-12 17:33:40 +01:00
return c.float32Code(typ, isPtr)
2021-11-07 20:44:48 +01:00
case reflect.Float64:
2021-12-12 17:33:40 +01:00
return c.float64Code(typ, isPtr)
2021-11-07 20:44:48 +01:00
case reflect.String:
2021-12-12 17:33:40 +01:00
return c.stringCode(typ, isPtr)
2021-11-07 20:44:48 +01:00
case reflect.Bool:
2021-12-12 17:33:40 +01:00
return c.boolCode(typ, isPtr)
2021-11-07 20:44:48 +01:00
case reflect.Interface:
2021-12-12 17:33:40 +01:00
return c.interfaceCode(typ, isPtr)
2021-11-07 20:44:48 +01:00
default:
if isPtr && typ.Implements(marshalTextType) {
typ = orgType
}
2021-12-12 17:33:40 +01:00
return c.typeToCodeWithPtr(typ, isPtr)
2021-11-07 20:44:48 +01:00
}
}
2021-12-12 17:33:40 +01:00
func (c *Compiler) typeToCodeWithPtr(typ *runtime.Type, isPtr bool) (Code, error) {
2021-11-07 20:44:48 +01:00
switch {
2021-12-12 17:33:40 +01:00
case c.implementsMarshalJSON(typ):
return c.marshalJSONCode(typ)
case c.implementsMarshalText(typ):
return c.marshalTextCode(typ)
2021-11-07 20:44:48 +01:00
}
switch typ.Kind() {
case reflect.Ptr:
2021-12-12 17:33:40 +01:00
return c.ptrCode(typ)
2021-11-07 20:44:48 +01:00
case reflect.Slice:
elem := typ.Elem()
if elem.Kind() == reflect.Uint8 {
p := runtime.PtrTo(elem)
2021-12-12 17:33:40 +01:00
if !c.implementsMarshalJSONType(p) && !p.Implements(marshalTextType) {
return c.bytesCode(typ, false)
2021-11-07 20:44:48 +01:00
}
}
2021-12-12 17:33:40 +01:00
return c.sliceCode(typ)
2021-11-07 20:44:48 +01:00
case reflect.Array:
2021-12-12 17:33:40 +01:00
return c.arrayCode(typ)
2021-11-07 20:44:48 +01:00
case reflect.Map:
2021-12-12 17:33:40 +01:00
return c.mapCode(typ)
2021-11-07 20:44:48 +01:00
case reflect.Struct:
2021-12-12 17:33:40 +01:00
return c.structCode(typ, isPtr)
2021-11-07 20:44:48 +01:00
case reflect.Interface:
2021-12-12 17:33:40 +01:00
return c.interfaceCode(typ, false)
2021-11-07 20:44:48 +01:00
case reflect.Int:
2021-12-12 17:33:40 +01:00
return c.intCode(typ, false)
2021-11-07 20:44:48 +01:00
case reflect.Int8:
2021-12-12 17:33:40 +01:00
return c.int8Code(typ, false)
2021-11-07 20:44:48 +01:00
case reflect.Int16:
2021-12-12 17:33:40 +01:00
return c.int16Code(typ, false)
2021-11-07 20:44:48 +01:00
case reflect.Int32:
2021-12-12 17:33:40 +01:00
return c.int32Code(typ, false)
2021-11-07 20:44:48 +01:00
case reflect.Int64:
2021-12-12 17:33:40 +01:00
return c.int64Code(typ, false)
2021-11-07 20:44:48 +01:00
case reflect.Uint:
2021-12-12 17:33:40 +01:00
return c.uintCode(typ, false)
2021-11-07 20:44:48 +01:00
case reflect.Uint8:
2021-12-12 17:33:40 +01:00
return c.uint8Code(typ, false)
2021-11-07 20:44:48 +01:00
case reflect.Uint16:
2021-12-12 17:33:40 +01:00
return c.uint16Code(typ, false)
2021-11-07 20:44:48 +01:00
case reflect.Uint32:
2021-12-12 17:33:40 +01:00
return c.uint32Code(typ, false)
2021-11-07 20:44:48 +01:00
case reflect.Uint64:
2021-12-12 17:33:40 +01:00
return c.uint64Code(typ, false)
2021-11-07 20:44:48 +01:00
case reflect.Uintptr:
2021-12-12 17:33:40 +01:00
return c.uintCode(typ, false)
2021-11-07 20:44:48 +01:00
case reflect.Float32:
2021-12-12 17:33:40 +01:00
return c.float32Code(typ, false)
2021-11-07 20:44:48 +01:00
case reflect.Float64:
2021-12-12 17:33:40 +01:00
return c.float64Code(typ, false)
2021-11-07 20:44:48 +01:00
case reflect.String:
2021-12-12 17:33:40 +01:00
return c.stringCode(typ, false)
2021-11-07 20:44:48 +01:00
case reflect.Bool:
2021-12-12 17:33:40 +01:00
return c.boolCode(typ, false)
2021-11-07 20:44:48 +01:00
}
return nil, &errors.UnsupportedTypeError{Type: runtime.RType2Type(typ)}
}
const intSize = 32 << (^uint(0) >> 63)
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) intCode(typ *runtime.Type, isPtr bool) (*IntCode, error) {
return &IntCode{typ: typ, bitSize: intSize, isPtr: isPtr}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) int8Code(typ *runtime.Type, isPtr bool) (*IntCode, error) {
return &IntCode{typ: typ, bitSize: 8, isPtr: isPtr}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) int16Code(typ *runtime.Type, isPtr bool) (*IntCode, error) {
return &IntCode{typ: typ, bitSize: 16, isPtr: isPtr}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) int32Code(typ *runtime.Type, isPtr bool) (*IntCode, error) {
return &IntCode{typ: typ, bitSize: 32, isPtr: isPtr}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) int64Code(typ *runtime.Type, isPtr bool) (*IntCode, error) {
return &IntCode{typ: typ, bitSize: 64, isPtr: isPtr}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) uintCode(typ *runtime.Type, isPtr bool) (*UintCode, error) {
return &UintCode{typ: typ, bitSize: intSize, isPtr: isPtr}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) uint8Code(typ *runtime.Type, isPtr bool) (*UintCode, error) {
return &UintCode{typ: typ, bitSize: 8, isPtr: isPtr}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) uint16Code(typ *runtime.Type, isPtr bool) (*UintCode, error) {
return &UintCode{typ: typ, bitSize: 16, isPtr: isPtr}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) uint32Code(typ *runtime.Type, isPtr bool) (*UintCode, error) {
return &UintCode{typ: typ, bitSize: 32, isPtr: isPtr}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) uint64Code(typ *runtime.Type, isPtr bool) (*UintCode, error) {
return &UintCode{typ: typ, bitSize: 64, isPtr: isPtr}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) float32Code(typ *runtime.Type, isPtr bool) (*FloatCode, error) {
return &FloatCode{typ: typ, bitSize: 32, isPtr: isPtr}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) float64Code(typ *runtime.Type, isPtr bool) (*FloatCode, error) {
return &FloatCode{typ: typ, bitSize: 64, isPtr: isPtr}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) stringCode(typ *runtime.Type, isPtr bool) (*StringCode, error) {
return &StringCode{typ: typ, isPtr: isPtr}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) boolCode(typ *runtime.Type, isPtr bool) (*BoolCode, error) {
return &BoolCode{typ: typ, isPtr: isPtr}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) intStringCode(typ *runtime.Type) (*IntCode, error) {
return &IntCode{typ: typ, bitSize: intSize, isString: true}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) int8StringCode(typ *runtime.Type) (*IntCode, error) {
return &IntCode{typ: typ, bitSize: 8, isString: true}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) int16StringCode(typ *runtime.Type) (*IntCode, error) {
return &IntCode{typ: typ, bitSize: 16, isString: true}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) int32StringCode(typ *runtime.Type) (*IntCode, error) {
return &IntCode{typ: typ, bitSize: 32, isString: true}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) int64StringCode(typ *runtime.Type) (*IntCode, error) {
return &IntCode{typ: typ, bitSize: 64, isString: true}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) uintStringCode(typ *runtime.Type) (*UintCode, error) {
return &UintCode{typ: typ, bitSize: intSize, isString: true}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) uint8StringCode(typ *runtime.Type) (*UintCode, error) {
return &UintCode{typ: typ, bitSize: 8, isString: true}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) uint16StringCode(typ *runtime.Type) (*UintCode, error) {
return &UintCode{typ: typ, bitSize: 16, isString: true}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) uint32StringCode(typ *runtime.Type) (*UintCode, error) {
return &UintCode{typ: typ, bitSize: 32, isString: true}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) uint64StringCode(typ *runtime.Type) (*UintCode, error) {
return &UintCode{typ: typ, bitSize: 64, isString: true}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) bytesCode(typ *runtime.Type, isPtr bool) (*BytesCode, error) {
return &BytesCode{typ: typ, isPtr: isPtr}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) interfaceCode(typ *runtime.Type, isPtr bool) (*InterfaceCode, error) {
return &InterfaceCode{typ: typ, isPtr: isPtr}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//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
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
//nolint:unparam
func (c *Compiler) marshalTextCode(typ *runtime.Type) (*MarshalTextCode, error) {
return &MarshalTextCode{
typ: typ,
isAddrForMarshaler: c.isPtrMarshalTextType(typ),
isNilableType: c.isNilableType(typ),
}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
func (c *Compiler) ptrCode(typ *runtime.Type) (*PtrCode, error) {
code, err := c.typeToCodeWithPtr(typ.Elem(), true)
2021-11-07 20:44:48 +01:00
if err != nil {
return nil, err
}
2021-12-12 17:33:40 +01:00
ptr, ok := code.(*PtrCode)
if ok {
return &PtrCode{typ: typ, value: ptr.value, ptrNum: ptr.ptrNum + 1}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
return &PtrCode{typ: typ, value: code, ptrNum: 1}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
func (c *Compiler) sliceCode(typ *runtime.Type) (*SliceCode, error) {
elem := typ.Elem()
code, err := c.listElemCode(elem)
2021-11-07 20:44:48 +01:00
if err != nil {
return nil, err
}
2021-12-12 17:33:40 +01:00
if code.Kind() == CodeKindStruct {
structCode := code.(*StructCode)
structCode.enableIndirect()
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
return &SliceCode{typ: typ, value: code}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
func (c *Compiler) arrayCode(typ *runtime.Type) (*ArrayCode, error) {
elem := typ.Elem()
code, err := c.listElemCode(elem)
2021-11-07 20:44:48 +01:00
if err != nil {
return nil, err
}
2021-12-12 17:33:40 +01:00
if code.Kind() == CodeKindStruct {
structCode := code.(*StructCode)
structCode.enableIndirect()
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
return &ArrayCode{typ: typ, value: code}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
func (c *Compiler) mapCode(typ *runtime.Type) (*MapCode, error) {
keyCode, err := c.mapKeyCode(typ.Key())
2021-11-07 20:44:48 +01:00
if err != nil {
return nil, err
}
2021-12-12 17:33:40 +01:00
valueCode, err := c.mapValueCode(typ.Elem())
2021-11-07 20:44:48 +01:00
if err != nil {
return nil, err
}
2021-12-12 17:33:40 +01:00
if valueCode.Kind() == CodeKindStruct {
structCode := valueCode.(*StructCode)
structCode.enableIndirect()
}
return &MapCode{typ: typ, key: keyCode, value: valueCode}, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
func (c *Compiler) listElemCode(typ *runtime.Type) (Code, error) {
2021-11-07 20:44:48 +01:00
switch {
2021-12-12 17:33:40 +01:00
case c.isPtrMarshalJSONType(typ):
return c.marshalJSONCode(typ)
2021-11-07 20:44:48 +01:00
case !typ.Implements(marshalTextType) && runtime.PtrTo(typ).Implements(marshalTextType):
2021-12-12 17:33:40 +01:00
return c.marshalTextCode(typ)
2021-11-07 20:44:48 +01:00
case typ.Kind() == reflect.Map:
2021-12-12 17:33:40 +01:00
return c.ptrCode(runtime.PtrTo(typ))
2021-11-07 20:44:48 +01:00
default:
2022-07-16 11:42:03 +02:00
// isPtr was originally used to indicate whether the type of top level is pointer.
// However, since the slice/array element is a specification that can get the pointer address, explicitly set isPtr to true.
// See here for related issues: https://github.com/goccy/go-json/issues/370
code, err := c.typeToCodeWithPtr(typ, true)
2021-11-07 20:44:48 +01:00
if err != nil {
return nil, err
}
2021-12-12 17:33:40 +01:00
ptr, ok := code.(*PtrCode)
if ok {
if ptr.value.Kind() == CodeKindMap {
ptr.ptrNum++
}
2021-11-07 20:44:48 +01:00
}
return code, nil
}
}
2021-12-12 17:33:40 +01:00
func (c *Compiler) mapKeyCode(typ *runtime.Type) (Code, error) {
switch {
case c.implementsMarshalText(typ):
return c.marshalTextCode(typ)
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01: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-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
return nil, &errors.UnsupportedTypeError{Type: runtime.RType2Type(typ)}
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
func (c *Compiler) mapValueCode(typ *runtime.Type) (Code, error) {
switch typ.Kind() {
2021-11-07 20:44:48 +01:00
case reflect.Map:
2021-12-12 17:33:40 +01:00
return c.ptrCode(runtime.PtrTo(typ))
2021-11-07 20:44:48 +01:00
default:
2021-12-12 17:33:40 +01:00
code, err := c.typeToCodeWithPtr(typ, false)
2021-11-07 20:44:48 +01:00
if err != nil {
return nil, err
}
2021-12-12 17:33:40 +01:00
ptr, ok := code.(*PtrCode)
if ok {
if ptr.value.Kind() == CodeKindMap {
ptr.ptrNum++
}
2021-11-07 20:44:48 +01:00
}
return code, nil
}
}
2021-12-12 17:33:40 +01: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-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
indirect := runtime.IfaceIndir(typ)
code := &StructCode{typ: typ, isPtr: isPtr, isIndirect: indirect}
c.structTypeToCode[typeptr] = code
2021-11-07 20:44:48 +01:00
2021-12-12 17:33:40 +01: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-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
fieldMap := c.getFieldMap(fields)
duplicatedFieldMap := c.getDuplicatedFieldMap(fieldMap)
code.fields = c.filteredDuplicatedFields(fields, duplicatedFieldMap)
if !code.disableIndirectConversion && !indirect && isPtr {
code.enableIndirect()
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
delete(c.structTypeToCode, typeptr)
return code, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01: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-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01: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-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
return fieldCode, nil
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
func (c *Compiler) isAssignableIndirect(fieldCode *StructFieldCode, isPtr bool) bool {
if isPtr {
2021-11-07 20:44:48 +01:00
return false
}
2021-12-12 17:33:40 +01:00
codeType := fieldCode.value.Kind()
if codeType == CodeKindMarshalJSON {
2021-11-07 20:44:48 +01:00
return false
}
2021-12-12 17:33:40 +01:00
if codeType == CodeKindMarshalText {
2021-11-07 20:44:48 +01:00
return false
}
2021-12-12 17:33:40 +01:00
return true
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01: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-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
continue
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
fieldMap[field.key] = append(fieldMap[field.key], field)
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
return fieldMap
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01: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
}
for k, v := range c.getFieldMapFromAnonymousParent(structCode.fields) {
fieldMap[k] = append(fieldMap[k], v...)
}
return fieldMap
}
2021-11-07 20:44:48 +01:00
2021-12-12 17:33:40 +01:00
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
}
fieldMap[k] = append(fieldMap[k], v...)
2021-11-07 20:44:48 +01:00
}
continue
}
2021-12-12 17:33:40 +01:00
fieldMap[field.key] = append(fieldMap[field.key], field)
}
return fieldMap
}
2021-11-07 20:44:48 +01:00
2021-12-12 17:33:40 +01:00
func (c *Compiler) getDuplicatedFieldMap(fieldMap map[string][]*StructFieldCode) map[*StructFieldCode]struct{} {
duplicatedFieldMap := map[*StructFieldCode]struct{}{}
for _, fields := range fieldMap {
if len(fields) == 1 {
continue
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
if c.isTaggedKeyOnly(fields) {
for _, field := range fields {
if field.isTaggedKey {
continue
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
duplicatedFieldMap[field] = struct{}{}
}
} else {
for _, field := range fields {
duplicatedFieldMap[field] = struct{}{}
2021-11-07 20:44:48 +01:00
}
}
}
2021-12-12 17:33:40 +01:00
return duplicatedFieldMap
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01: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)
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
continue
2021-11-07 20:44:48 +01:00
}
}
2021-12-12 17:33:40 +01:00
if _, exists := duplicatedFieldMap[field]; exists {
continue
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
filteredFields = append(filteredFields, field)
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
return filteredFields
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
func (c *Compiler) isTaggedKeyOnly(fields []*StructFieldCode) bool {
var taggedKeyFieldCount int
for _, field := range fields {
if field.isTaggedKey {
taggedKeyFieldCount++
}
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
return taggedKeyFieldCount == 1
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
func (c *Compiler) typeToStructTags(typ *runtime.Type) runtime.StructTags {
2021-11-07 20:44:48 +01:00
tags := runtime.StructTags{}
2021-12-12 17:33:40 +01:00
fieldNum := typ.NumField()
2021-11-07 20:44:48 +01:00
for i := 0; i < fieldNum; i++ {
field := typ.Field(i)
if runtime.IsIgnoredStructField(field) {
continue
}
tags = append(tags, runtime.StructTagFromField(field))
}
2021-12-12 17:33:40 +01:00
return tags
}
2021-11-07 20:44:48 +01:00
2021-12-12 17:33:40 +01: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-11-07 20:44:48 +01:00
2021-12-12 17:33:40 +01: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-11-07 20:44:48 +01:00
2021-12-12 17:33:40 +01:00
func (c *Compiler) implementsMarshalJSON(typ *runtime.Type) bool {
if !c.implementsMarshalJSONType(typ) {
return false
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
if typ.Kind() != reflect.Ptr {
return true
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
// type kind is reflect.Ptr
if !c.implementsMarshalJSONType(typ.Elem()) {
return true
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
// needs to dereference
return false
}
2021-11-07 20:44:48 +01:00
2021-12-12 17:33:40 +01:00
func (c *Compiler) implementsMarshalText(typ *runtime.Type) bool {
if !typ.Implements(marshalTextType) {
return false
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
if typ.Kind() != reflect.Ptr {
return true
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
// type kind is reflect.Ptr
if !typ.Elem().Implements(marshalTextType) {
return true
}
// needs to dereference
return false
}
2021-11-07 20:44:48 +01:00
2021-12-12 17:33:40 +01:00
func (c *Compiler) isNilableType(typ *runtime.Type) bool {
2022-07-01 10:25:48 +02:00
if !runtime.IfaceIndir(typ) {
return true
}
2021-12-12 17:33:40 +01:00
switch typ.Kind() {
case reflect.Ptr:
return true
case reflect.Map:
return true
case reflect.Func:
return true
default:
return false
}
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
func (c *Compiler) implementsMarshalJSONType(typ *runtime.Type) bool {
2021-11-07 20:44:48 +01:00
return typ.Implements(marshalJSONType) || typ.Implements(marshalJSONContextType)
}
2021-12-12 17:33:40 +01:00
func (c *Compiler) isPtrMarshalJSONType(typ *runtime.Type) bool {
return !c.implementsMarshalJSONType(typ) && c.implementsMarshalJSONType(runtime.PtrTo(typ))
2021-11-07 20:44:48 +01:00
}
2021-12-12 17:33:40 +01:00
func (c *Compiler) isPtrMarshalTextType(typ *runtime.Type) bool {
2021-11-07 20:44:48 +01:00
return !typ.Implements(marshalTextType) && runtime.PtrTo(typ).Implements(marshalTextType)
}
2021-12-12 17:33:40 +01: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) {
2022-07-01 10:25:48 +02:00
recursiveCodes := map[uintptr]*CompiledCode{}
2021-12-12 17:33:40 +01:00
for _, recursive := range *ctx.recursiveCodes {
typeptr := uintptr(unsafe.Pointer(recursive.Type))
codes := ctx.structTypeToCodes[typeptr]
2022-07-01 10:25:48 +02:00
if recursiveCode, ok := recursiveCodes[typeptr]; ok {
*recursive.Jmp = *recursiveCode
continue
}
code := copyOpcode(codes.First())
2021-12-12 17:33:40 +01:00
code.Op = code.Op.PtrHeadToHead()
2022-07-01 10:25:48 +02:00
lastCode := newEndOp(&compileContext{}, recursive.Type)
lastCode.Op = OpRecursiveEnd
2021-12-12 17:33:40 +01:00
2022-07-01 10:25:48 +02:00
// OpRecursiveEnd must set before call TotalLength
code.End.Next = lastCode
2021-12-12 17:33:40 +01:00
totalLength := code.TotalLength()
2022-07-01 10:25:48 +02:00
// Idx, ElemIdx, Length must set after call TotalLength
2021-12-12 17:33:40 +01:00
lastCode.Idx = uint32((totalLength + 1) * uintptrSize)
lastCode.ElemIdx = lastCode.Idx + uintptrSize
lastCode.Length = lastCode.Idx + 2*uintptrSize
// extend length to alloc slot for elemIdx + length
curTotalLength := uintptr(recursive.TotalLength()) + 3
nextTotalLength := uintptr(totalLength) + 3
2022-07-01 10:25:48 +02:00
compiled := recursive.Jmp
compiled.Code = code
2021-12-12 17:33:40 +01:00
compiled.CurLen = curTotalLength
compiled.NextLen = nextTotalLength
compiled.Linked = true
2022-07-01 10:25:48 +02:00
recursiveCodes[typeptr] = compiled
2021-12-12 17:33:40 +01:00
}
}