go-json/encode_compile.go

931 lines
24 KiB
Go
Raw Normal View History

2020-04-29 18:31:50 +03:00
package json
import (
"fmt"
"reflect"
"unsafe"
)
2020-05-04 12:39:17 +03:00
func (e *Encoder) compileHead(typ *rtype, withIndent bool) (*opcode, error) {
root := true
2020-08-18 07:36:36 +03:00
switch {
case typ.Implements(marshalJSONType):
2020-05-04 12:39:17 +03:00
return newOpCode(opMarshalJSON, typ, e.indent, newEndOp(e.indent)), nil
2020-08-18 07:36:36 +03:00
case rtype_ptrTo(typ).Implements(marshalJSONType):
return newOpCode(opMarshalJSON, rtype_ptrTo(typ), e.indent, newEndOp(e.indent)), nil
case typ.Implements(marshalTextType):
2020-05-04 12:39:17 +03:00
return newOpCode(opMarshalText, typ, e.indent, newEndOp(e.indent)), nil
2020-08-18 07:36:36 +03:00
case rtype_ptrTo(typ).Implements(marshalTextType):
return newOpCode(opMarshalText, rtype_ptrTo(typ), e.indent, newEndOp(e.indent)), nil
2020-05-04 12:39:17 +03:00
}
isPtr := false
2020-05-04 12:39:17 +03:00
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
isPtr = true
2020-05-04 12:39:17 +03:00
}
if typ.Kind() == reflect.Map {
return e.compileMap(typ, isPtr, root, withIndent)
} else if typ.Kind() == reflect.Struct {
return e.compileStruct(typ, isPtr, root, withIndent)
}
2020-08-12 10:54:15 +03:00
return e.compile(typ, root, withIndent)
2020-05-04 12:39:17 +03:00
}
func (e *Encoder) implementsMarshaler(typ *rtype) bool {
switch {
case typ.Implements(marshalJSONType):
return true
case rtype_ptrTo(typ).Implements(marshalJSONType):
return true
case typ.Implements(marshalTextType):
return true
case rtype_ptrTo(typ).Implements(marshalTextType):
return true
}
return false
}
2020-08-12 10:54:15 +03:00
func (e *Encoder) compile(typ *rtype, root, withIndent bool) (*opcode, error) {
2020-08-18 07:36:36 +03:00
switch {
case typ.Implements(marshalJSONType):
2020-05-04 12:39:17 +03:00
return newOpCode(opMarshalJSON, typ, e.indent, newEndOp(e.indent)), nil
2020-08-18 07:36:36 +03:00
case rtype_ptrTo(typ).Implements(marshalJSONType):
return newOpCode(opMarshalJSON, rtype_ptrTo(typ), e.indent, newEndOp(e.indent)), nil
case typ.Implements(marshalTextType):
2020-05-04 12:39:17 +03:00
return newOpCode(opMarshalText, typ, e.indent, newEndOp(e.indent)), nil
2020-08-18 07:36:36 +03:00
case rtype_ptrTo(typ).Implements(marshalTextType):
return newOpCode(opMarshalText, rtype_ptrTo(typ), e.indent, newEndOp(e.indent)), nil
2020-05-04 12:39:17 +03:00
}
2020-04-29 18:31:50 +03:00
switch typ.Kind() {
case reflect.Ptr:
2020-08-12 10:54:15 +03:00
return e.compilePtr(typ, root, withIndent)
2020-04-29 18:31:50 +03:00
case reflect.Slice:
elem := typ.Elem()
if !e.implementsMarshaler(elem) && elem.Kind() == reflect.Uint8 {
2020-08-19 04:34:11 +03:00
return e.compileBytes(typ)
}
2020-08-12 10:54:15 +03:00
return e.compileSlice(typ, root, withIndent)
case reflect.Array:
2020-08-12 10:54:15 +03:00
return e.compileArray(typ, root, withIndent)
case reflect.Map:
2020-08-12 10:54:15 +03:00
return e.compileMap(typ, true, root, withIndent)
2020-04-29 18:31:50 +03:00
case reflect.Struct:
return e.compileStruct(typ, false, root, withIndent)
2020-08-12 10:54:15 +03:00
case reflect.Interface:
return e.compileInterface(typ, root)
2020-04-29 18:31:50 +03:00
case reflect.Int:
2020-04-30 07:52:24 +03:00
return e.compileInt(typ)
2020-04-29 18:31:50 +03:00
case reflect.Int8:
2020-04-30 07:52:24 +03:00
return e.compileInt8(typ)
2020-04-29 18:31:50 +03:00
case reflect.Int16:
2020-04-30 07:52:24 +03:00
return e.compileInt16(typ)
2020-04-29 18:31:50 +03:00
case reflect.Int32:
2020-04-30 07:52:24 +03:00
return e.compileInt32(typ)
2020-04-29 18:31:50 +03:00
case reflect.Int64:
2020-04-30 07:52:24 +03:00
return e.compileInt64(typ)
2020-04-29 18:31:50 +03:00
case reflect.Uint:
2020-04-30 07:52:24 +03:00
return e.compileUint(typ)
2020-04-29 18:31:50 +03:00
case reflect.Uint8:
2020-04-30 07:52:24 +03:00
return e.compileUint8(typ)
2020-04-29 18:31:50 +03:00
case reflect.Uint16:
2020-04-30 07:52:24 +03:00
return e.compileUint16(typ)
2020-04-29 18:31:50 +03:00
case reflect.Uint32:
2020-04-30 07:52:24 +03:00
return e.compileUint32(typ)
2020-04-29 18:31:50 +03:00
case reflect.Uint64:
2020-04-30 07:52:24 +03:00
return e.compileUint64(typ)
2020-04-29 18:31:50 +03:00
case reflect.Uintptr:
2020-04-30 07:52:24 +03:00
return e.compileUint(typ)
2020-04-29 18:31:50 +03:00
case reflect.Float32:
2020-04-30 07:52:24 +03:00
return e.compileFloat32(typ)
2020-04-29 18:31:50 +03:00
case reflect.Float64:
2020-04-30 07:52:24 +03:00
return e.compileFloat64(typ)
2020-04-29 18:31:50 +03:00
case reflect.String:
2020-04-30 07:52:24 +03:00
return e.compileString(typ)
2020-04-29 18:31:50 +03:00
case reflect.Bool:
2020-04-30 07:52:24 +03:00
return e.compileBool(typ)
2020-04-29 18:31:50 +03:00
}
return nil, &UnsupportedTypeError{Type: rtype2type(typ)}
2020-04-29 18:31:50 +03:00
}
2020-08-20 11:47:38 +03:00
func (e *Encoder) compileKey(typ *rtype, root, withIndent bool) (*opcode, error) {
switch {
case typ.Implements(marshalJSONType):
return newOpCode(opMarshalJSON, typ, e.indent, newEndOp(e.indent)), nil
case rtype_ptrTo(typ).Implements(marshalJSONType):
return newOpCode(opMarshalJSON, rtype_ptrTo(typ), e.indent, newEndOp(e.indent)), nil
case typ.Implements(marshalTextType):
return newOpCode(opMarshalText, typ, e.indent, newEndOp(e.indent)), nil
case rtype_ptrTo(typ).Implements(marshalTextType):
return newOpCode(opMarshalText, rtype_ptrTo(typ), e.indent, newEndOp(e.indent)), nil
}
switch typ.Kind() {
case reflect.Ptr:
return e.compilePtr(typ, root, withIndent)
case reflect.Interface:
return e.compileInterface(typ, root)
case reflect.Int:
return e.compileInt(typ)
case reflect.Int8:
return e.compileInt8(typ)
case reflect.Int16:
return e.compileInt16(typ)
case reflect.Int32:
return e.compileInt32(typ)
case reflect.Int64:
return e.compileInt64(typ)
case reflect.Uint:
return e.compileUint(typ)
case reflect.Uint8:
return e.compileUint8(typ)
case reflect.Uint16:
return e.compileUint16(typ)
case reflect.Uint32:
return e.compileUint32(typ)
case reflect.Uint64:
return e.compileUint64(typ)
case reflect.Uintptr:
return e.compileUint(typ)
case reflect.Float32:
return e.compileFloat32(typ)
case reflect.Float64:
return e.compileFloat64(typ)
case reflect.String:
return e.compileString(typ)
case reflect.Bool:
return e.compileBool(typ)
}
return nil, &UnsupportedTypeError{Type: rtype2type(typ)}
}
2020-04-30 05:56:56 +03:00
func (e *Encoder) optimizeStructFieldPtrHead(typ *rtype, code *opcode) *opcode {
2020-08-15 11:41:38 +03:00
ptrHeadOp := code.op.headToPtrHead()
if code.op != ptrHeadOp {
code.op = ptrHeadOp
return code
2020-04-30 05:56:56 +03:00
}
2020-08-15 11:41:38 +03:00
return newOpCode(opPtr, typ, e.indent, code)
2020-04-30 05:56:56 +03:00
}
2020-08-12 10:54:15 +03:00
func (e *Encoder) compilePtr(typ *rtype, root, withIndent bool) (*opcode, error) {
code, err := e.compile(typ.Elem(), root, withIndent)
2020-04-30 05:56:56 +03:00
if err != nil {
return nil, err
2020-04-29 19:44:48 +03:00
}
2020-04-30 05:56:56 +03:00
return e.optimizeStructFieldPtrHead(typ, code), nil
2020-04-29 18:31:50 +03:00
}
2020-04-30 07:52:24 +03:00
func (e *Encoder) compileInt(typ *rtype) (*opcode, error) {
2020-05-02 17:35:41 +03:00
return newOpCode(opInt, typ, e.indent, newEndOp(e.indent)), nil
2020-04-29 18:31:50 +03:00
}
2020-04-30 07:52:24 +03:00
func (e *Encoder) compileInt8(typ *rtype) (*opcode, error) {
2020-05-02 17:35:41 +03:00
return newOpCode(opInt8, typ, e.indent, newEndOp(e.indent)), nil
2020-04-29 18:31:50 +03:00
}
2020-04-30 07:52:24 +03:00
func (e *Encoder) compileInt16(typ *rtype) (*opcode, error) {
2020-05-02 17:35:41 +03:00
return newOpCode(opInt16, typ, e.indent, newEndOp(e.indent)), nil
2020-04-29 18:31:50 +03:00
}
2020-04-30 07:52:24 +03:00
func (e *Encoder) compileInt32(typ *rtype) (*opcode, error) {
2020-05-02 17:35:41 +03:00
return newOpCode(opInt32, typ, e.indent, newEndOp(e.indent)), nil
2020-04-29 18:31:50 +03:00
}
2020-04-30 07:52:24 +03:00
func (e *Encoder) compileInt64(typ *rtype) (*opcode, error) {
2020-05-02 17:35:41 +03:00
return newOpCode(opInt64, typ, e.indent, newEndOp(e.indent)), nil
2020-04-29 18:31:50 +03:00
}
2020-04-30 07:52:24 +03:00
func (e *Encoder) compileUint(typ *rtype) (*opcode, error) {
2020-05-02 17:35:41 +03:00
return newOpCode(opUint, typ, e.indent, newEndOp(e.indent)), nil
2020-04-29 18:31:50 +03:00
}
2020-04-30 07:52:24 +03:00
func (e *Encoder) compileUint8(typ *rtype) (*opcode, error) {
2020-05-02 17:35:41 +03:00
return newOpCode(opUint8, typ, e.indent, newEndOp(e.indent)), nil
2020-04-29 18:31:50 +03:00
}
2020-04-30 07:52:24 +03:00
func (e *Encoder) compileUint16(typ *rtype) (*opcode, error) {
2020-05-02 17:35:41 +03:00
return newOpCode(opUint16, typ, e.indent, newEndOp(e.indent)), nil
2020-04-29 18:31:50 +03:00
}
2020-04-30 07:52:24 +03:00
func (e *Encoder) compileUint32(typ *rtype) (*opcode, error) {
2020-05-02 17:35:41 +03:00
return newOpCode(opUint32, typ, e.indent, newEndOp(e.indent)), nil
2020-04-29 18:31:50 +03:00
}
2020-04-30 07:52:24 +03:00
func (e *Encoder) compileUint64(typ *rtype) (*opcode, error) {
2020-05-02 17:35:41 +03:00
return newOpCode(opUint64, typ, e.indent, newEndOp(e.indent)), nil
2020-04-29 18:31:50 +03:00
}
2020-04-30 07:52:24 +03:00
func (e *Encoder) compileFloat32(typ *rtype) (*opcode, error) {
2020-05-02 17:35:41 +03:00
return newOpCode(opFloat32, typ, e.indent, newEndOp(e.indent)), nil
2020-04-29 18:31:50 +03:00
}
2020-04-30 07:52:24 +03:00
func (e *Encoder) compileFloat64(typ *rtype) (*opcode, error) {
2020-05-02 17:35:41 +03:00
return newOpCode(opFloat64, typ, e.indent, newEndOp(e.indent)), nil
2020-04-29 18:31:50 +03:00
}
2020-04-30 07:52:24 +03:00
func (e *Encoder) compileString(typ *rtype) (*opcode, error) {
2020-05-02 17:35:41 +03:00
return newOpCode(opString, typ, e.indent, newEndOp(e.indent)), nil
2020-04-29 18:31:50 +03:00
}
2020-04-30 07:52:24 +03:00
func (e *Encoder) compileBool(typ *rtype) (*opcode, error) {
2020-05-02 17:35:41 +03:00
return newOpCode(opBool, typ, e.indent, newEndOp(e.indent)), nil
2020-04-29 18:31:50 +03:00
}
2020-08-19 04:34:11 +03:00
func (e *Encoder) compileBytes(typ *rtype) (*opcode, error) {
return newOpCode(opBytes, typ, e.indent, newEndOp(e.indent)), nil
}
2020-08-12 10:54:15 +03:00
func (e *Encoder) compileInterface(typ *rtype, root bool) (*opcode, error) {
return (*opcode)(unsafe.Pointer(&interfaceCode{
opcodeHeader: &opcodeHeader{
op: opInterface,
typ: typ,
indent: e.indent,
next: newEndOp(e.indent),
},
root: root,
})), nil
}
2020-08-12 10:54:15 +03:00
func (e *Encoder) compileSlice(typ *rtype, root, withIndent bool) (*opcode, error) {
2020-04-29 18:31:50 +03:00
elem := typ.Elem()
size := elem.Size()
2020-05-02 17:35:41 +03:00
e.indent++
2020-08-12 10:54:15 +03:00
code, err := e.compile(elem, false, withIndent)
2020-05-02 17:35:41 +03:00
e.indent--
2020-04-29 18:31:50 +03:00
if err != nil {
return nil, err
}
2020-04-29 19:44:48 +03:00
// header => opcode => elem => end
// ^ |
// |________|
2020-04-29 18:31:50 +03:00
2020-05-02 17:35:41 +03:00
header := newSliceHeaderCode(e.indent)
elemCode := &sliceElemCode{
opcodeHeader: &opcodeHeader{
op: opSliceElem,
indent: e.indent,
},
size: size,
}
end := newOpCode(opSliceEnd, nil, e.indent, newEndOp(e.indent))
if withIndent {
2020-08-12 10:54:15 +03:00
if root {
header.op = opRootSliceHeadIndent
elemCode.op = opRootSliceElemIndent
} else {
header.op = opSliceHeadIndent
elemCode.op = opSliceElemIndent
}
2020-05-02 17:35:41 +03:00
end.op = opSliceEndIndent
}
header.elem = elemCode
header.end = end
header.next = code
code.beforeLastCode().next = (*opcode)(unsafe.Pointer(elemCode))
elemCode.next = code
elemCode.end = end
return (*opcode)(unsafe.Pointer(header)), nil
}
2020-08-12 10:54:15 +03:00
func (e *Encoder) compileArray(typ *rtype, root, withIndent bool) (*opcode, error) {
elem := typ.Elem()
alen := typ.Len()
size := elem.Size()
2020-05-02 17:35:41 +03:00
e.indent++
2020-08-12 10:54:15 +03:00
code, err := e.compile(elem, false, withIndent)
2020-05-02 17:35:41 +03:00
e.indent--
if err != nil {
return nil, err
}
// header => opcode => elem => end
// ^ |
// |________|
2020-05-02 17:35:41 +03:00
header := newArrayHeaderCode(e.indent, alen)
elemCode := &arrayElemCode{
opcodeHeader: &opcodeHeader{
op: opArrayElem,
},
len: uintptr(alen),
size: size,
}
2020-05-02 17:35:41 +03:00
end := newOpCode(opArrayEnd, nil, e.indent, newEndOp(e.indent))
if withIndent {
header.op = opArrayHeadIndent
elemCode.op = opArrayElemIndent
end.op = opArrayEndIndent
}
2020-04-29 18:31:50 +03:00
2020-04-29 19:44:48 +03:00
header.elem = elemCode
header.end = end
header.next = code
2020-04-29 18:31:50 +03:00
code.beforeLastCode().next = (*opcode)(unsafe.Pointer(elemCode))
elemCode.next = code
elemCode.end = end
return (*opcode)(unsafe.Pointer(header)), nil
}
2020-04-30 07:52:24 +03:00
//go:linkname mapiterinit reflect.mapiterinit
//go:noescape
func mapiterinit(mapType *rtype, m unsafe.Pointer) unsafe.Pointer
//go:linkname mapiterkey reflect.mapiterkey
//go:noescape
func mapiterkey(it unsafe.Pointer) unsafe.Pointer
//go:linkname mapiternext reflect.mapiternext
//go:noescape
func mapiternext(it unsafe.Pointer)
//go:linkname maplen reflect.maplen
//go:noescape
func maplen(m unsafe.Pointer) int
2020-08-12 10:54:15 +03:00
func (e *Encoder) compileMap(typ *rtype, withLoad, root, withIndent bool) (*opcode, error) {
// header => code => value => code => key => code => value => code => end
// ^ |
// |_______________________|
2020-05-02 17:35:41 +03:00
e.indent++
keyType := typ.Key()
2020-08-20 11:47:38 +03:00
keyCode, err := e.compileKey(keyType, false, withIndent)
if err != nil {
return nil, err
}
valueType := typ.Elem()
2020-08-12 10:54:15 +03:00
valueCode, err := e.compile(valueType, false, withIndent)
if err != nil {
return nil, err
}
2020-05-02 17:35:41 +03:00
key := newMapKeyCode(e.indent)
value := newMapValueCode(e.indent)
e.indent--
header := newMapHeaderCode(typ, withLoad, e.indent)
header.key = key
header.value = value
2020-05-02 17:35:41 +03:00
end := newOpCode(opMapEnd, nil, e.indent, newEndOp(e.indent))
if withIndent {
if header.op == opMapHead {
2020-08-12 10:54:15 +03:00
if root {
header.op = opRootMapHeadIndent
} else {
header.op = opMapHeadIndent
}
} else {
header.op = opMapHeadLoadIndent
}
2020-08-12 10:54:15 +03:00
if root {
key.op = opRootMapKeyIndent
} else {
key.op = opMapKeyIndent
}
2020-05-02 17:35:41 +03:00
value.op = opMapValueIndent
end.op = opMapEndIndent
}
header.next = keyCode
keyCode.beforeLastCode().next = (*opcode)(unsafe.Pointer(value))
value.next = valueCode
valueCode.beforeLastCode().next = (*opcode)(unsafe.Pointer(key))
key.next = keyCode
header.end = end
key.end = end
2020-04-29 18:31:50 +03:00
return (*opcode)(unsafe.Pointer(header)), nil
}
2020-08-15 11:41:38 +03:00
func (e *Encoder) typeToHeaderType(op opType) opType {
2020-05-02 17:35:41 +03:00
switch op {
case opInt:
return opStructFieldHeadInt
case opInt8:
return opStructFieldHeadInt8
case opInt16:
return opStructFieldHeadInt16
case opInt32:
return opStructFieldHeadInt32
case opInt64:
return opStructFieldHeadInt64
case opUint:
return opStructFieldHeadUint
case opUint8:
return opStructFieldHeadUint8
case opUint16:
return opStructFieldHeadUint16
case opUint32:
return opStructFieldHeadUint32
case opUint64:
return opStructFieldHeadUint64
case opFloat32:
return opStructFieldHeadFloat32
case opFloat64:
return opStructFieldHeadFloat64
case opString:
return opStructFieldHeadString
case opBool:
return opStructFieldHeadBool
case opMapHead:
return opStructFieldHeadMap
case opMapHeadLoad:
return opStructFieldHeadMapLoad
case opMapHeadIndent:
return opStructFieldHeadMapIndent
case opMapHeadLoadIndent:
return opStructFieldHeadMapLoadIndent
case opArrayHead:
return opStructFieldHeadArray
case opArrayHeadIndent:
return opStructFieldHeadArrayIndent
case opSliceHead:
return opStructFieldHeadSlice
case opSliceHeadIndent:
return opStructFieldHeadSliceIndent
case opStructFieldHead:
return opStructFieldHeadStruct
case opStructFieldHeadIndent:
return opStructFieldHeadStructIndent
case opMarshalJSON:
return opStructFieldHeadMarshalJSON
case opMarshalText:
return opStructFieldHeadMarshalText
2020-05-02 17:35:41 +03:00
}
return opStructFieldHead
}
2020-08-15 11:41:38 +03:00
func (e *Encoder) typeToFieldType(op opType) opType {
2020-05-02 17:35:41 +03:00
switch op {
case opInt:
return opStructFieldInt
case opInt8:
return opStructFieldInt8
case opInt16:
return opStructFieldInt16
case opInt32:
return opStructFieldInt32
case opInt64:
return opStructFieldInt64
case opUint:
return opStructFieldUint
case opUint8:
return opStructFieldUint8
case opUint16:
return opStructFieldUint16
case opUint32:
return opStructFieldUint32
case opUint64:
return opStructFieldUint64
case opFloat32:
return opStructFieldFloat32
case opFloat64:
return opStructFieldFloat64
case opString:
return opStructFieldString
case opBool:
return opStructFieldBool
case opMapHead:
return opStructFieldMap
case opMapHeadLoad:
return opStructFieldMapLoad
case opMapHeadIndent:
return opStructFieldMapIndent
case opMapHeadLoadIndent:
return opStructFieldMapLoadIndent
case opArrayHead:
return opStructFieldArray
case opArrayHeadIndent:
return opStructFieldArrayIndent
case opSliceHead:
return opStructFieldSlice
case opSliceHeadIndent:
return opStructFieldSliceIndent
case opStructFieldHead:
return opStructFieldStruct
case opStructFieldHeadIndent:
return opStructFieldStructIndent
case opMarshalJSON:
return opStructFieldMarshalJSON
case opMarshalText:
return opStructFieldMarshalText
2020-05-02 17:35:41 +03:00
}
return opStructField
}
2020-08-19 13:56:02 +03:00
func (e *Encoder) optimizeStructHeader(op opType, tag *structTag, withIndent bool) opType {
2020-08-15 11:41:38 +03:00
headType := e.typeToHeaderType(op)
2020-08-19 13:56:02 +03:00
switch {
case tag.isOmitEmpty:
2020-08-15 11:41:38 +03:00
headType = headType.headToOmitEmptyHead()
2020-08-19 13:56:02 +03:00
case tag.isString:
headType = headType.headToStringTagHead()
2020-08-15 11:41:38 +03:00
}
if withIndent {
return headType.toIndent()
}
return headType
}
2020-08-19 13:56:02 +03:00
func (e *Encoder) optimizeStructField(op opType, tag *structTag, withIndent bool) opType {
2020-08-15 11:41:38 +03:00
fieldType := e.typeToFieldType(op)
2020-08-19 13:56:02 +03:00
switch {
case tag.isOmitEmpty:
2020-08-15 11:41:38 +03:00
fieldType = fieldType.fieldToOmitEmptyField()
2020-08-19 13:56:02 +03:00
case tag.isString:
fieldType = fieldType.fieldToStringTagField()
2020-08-15 11:41:38 +03:00
}
if withIndent {
return fieldType.toIndent()
}
return fieldType
}
2020-08-14 16:44:09 +03:00
func (e *Encoder) recursiveCode(typ *rtype, code *compiledCode) *opcode {
return (*opcode)(unsafe.Pointer(&recursiveCode{
opcodeHeader: &opcodeHeader{
op: opStructFieldRecursive,
typ: typ,
indent: e.indent,
next: newEndOp(e.indent),
},
jmp: code,
}))
}
func (e *Encoder) compiledCode(typ *rtype, withIndent bool) *opcode {
2020-08-12 12:42:29 +03:00
typeptr := uintptr(unsafe.Pointer(typ))
if withIndent {
2020-08-14 16:44:09 +03:00
if compiledCode, exists := e.structTypeToCompiledIndentCode[typeptr]; exists {
return e.recursiveCode(typ, compiledCode)
2020-08-12 12:42:29 +03:00
}
} else {
2020-08-14 16:44:09 +03:00
if compiledCode, exists := e.structTypeToCompiledCode[typeptr]; exists {
return e.recursiveCode(typ, compiledCode)
}
}
return nil
}
2020-08-19 13:56:02 +03:00
func (e *Encoder) structHeader(fieldCode *structFieldCode, valueCode *opcode, tag *structTag, withIndent bool) *opcode {
2020-08-14 16:44:09 +03:00
fieldCode.indent--
2020-08-19 13:56:02 +03:00
op := e.optimizeStructHeader(valueCode.op, tag, withIndent)
2020-08-14 16:44:09 +03:00
fieldCode.op = op
switch op {
case opStructFieldHead,
opStructFieldHeadSlice,
opStructFieldHeadArray,
opStructFieldHeadMap,
opStructFieldHeadMapLoad,
opStructFieldHeadStruct,
2020-08-14 16:44:09 +03:00
opStructFieldHeadOmitEmpty,
opStructFieldHeadOmitEmptySlice,
opStructFieldHeadOmitEmptyArray,
opStructFieldHeadOmitEmptyMap,
opStructFieldHeadOmitEmptyMapLoad,
opStructFieldHeadOmitEmptyStruct,
opStructFieldHeadStringTag,
2020-08-14 16:44:09 +03:00
opStructFieldHeadIndent,
opStructFieldHeadSliceIndent,
opStructFieldHeadArrayIndent,
opStructFieldHeadMapIndent,
opStructFieldHeadMapLoadIndent,
opStructFieldHeadStructIndent,
opStructFieldHeadOmitEmptyIndent,
opStructFieldHeadOmitEmptySliceIndent,
opStructFieldHeadOmitEmptyArrayIndent,
opStructFieldHeadOmitEmptyMapIndent,
opStructFieldHeadOmitEmptyMapLoadIndent,
opStructFieldHeadOmitEmptyStructIndent,
opStructFieldHeadStringTagIndent:
2020-08-14 16:44:09 +03:00
return valueCode.beforeLastCode()
}
return (*opcode)(unsafe.Pointer(fieldCode))
}
2020-08-19 13:56:02 +03:00
func (e *Encoder) structField(fieldCode *structFieldCode, valueCode *opcode, tag *structTag, withIndent bool) *opcode {
2020-08-14 16:44:09 +03:00
code := (*opcode)(unsafe.Pointer(fieldCode))
2020-08-19 13:56:02 +03:00
op := e.optimizeStructField(valueCode.op, tag, withIndent)
2020-08-14 16:44:09 +03:00
fieldCode.op = op
switch op {
case opStructField,
opStructFieldSlice,
opStructFieldArray,
opStructFieldMap,
opStructFieldMapLoad,
opStructFieldStruct,
2020-08-14 16:44:09 +03:00
opStructFieldOmitEmpty,
opStructFieldOmitEmptySlice,
opStructFieldOmitEmptyArray,
opStructFieldOmitEmptyMap,
opStructFieldOmitEmptyMapLoad,
opStructFieldOmitEmptyStruct,
opStructFieldStringTag,
2020-08-14 16:44:09 +03:00
opStructFieldIndent,
opStructFieldSliceIndent,
opStructFieldArrayIndent,
opStructFieldMapIndent,
opStructFieldMapLoadIndent,
opStructFieldStructIndent,
opStructFieldOmitEmptyIndent,
opStructFieldOmitEmptySliceIndent,
opStructFieldOmitEmptyArrayIndent,
opStructFieldOmitEmptyMapIndent,
opStructFieldOmitEmptyMapLoadIndent,
opStructFieldOmitEmptyStructIndent,
opStructFieldStringTagIndent:
2020-08-14 16:44:09 +03:00
return valueCode.beforeLastCode()
}
return code
}
2020-08-22 06:58:34 +03:00
func (e *Encoder) isNotExistsField(head *structFieldCode) bool {
if head == nil {
return false
}
if head.op != opStructFieldAnonymousHead {
return false
}
if head.next == nil {
return false
}
if head.nextField == nil {
return false
}
if head.nextField.op != opStructAnonymousEnd {
return false
}
if head.next.op == opStructAnonymousEnd {
return true
}
if head.next.op.codeType() != codeStructField {
return false
}
return e.isNotExistsField(head.next.toStructFieldCode())
}
func (e *Encoder) optimizeAnonymousFields(head *structFieldCode) {
code := head
var prev *structFieldCode
for {
if code.op == opStructEnd || code.op == opStructEndIndent {
break
}
if code.op == opStructField || code.op == opStructFieldIndent {
codeType := code.next.op.codeType()
if codeType == codeStructField {
if e.isNotExistsField(code.next.toStructFieldCode()) {
code.next = code.nextField
linkPrevToNextField(prev, code)
code = prev
}
}
}
prev = code
code = code.nextField.toStructFieldCode()
}
}
type structFieldPair struct {
2020-08-22 09:40:18 +03:00
prevField *structFieldCode
curField *structFieldCode
isTaggedKey bool
linked bool
}
func (e *Encoder) anonymousStructFieldPairMap(typ *rtype, tags structTags, valueCode *structFieldCode) map[string][]structFieldPair {
anonymousFields := map[string][]structFieldPair{}
f := valueCode
var prevAnonymousField *structFieldCode
for {
existsKey := tags.existsKey(f.displayKey)
op := f.op.headToAnonymousHead()
if op != f.op {
if existsKey {
f.op = opStructFieldAnonymousHead
} else {
f.op = op
}
} else if f.op == opStructEnd {
f.op = opStructAnonymousEnd
} else if existsKey {
linkPrevToNextField(prevAnonymousField, f)
}
if f.displayKey == "" {
if f.nextField == nil {
break
}
prevAnonymousField = f
f = f.nextField.toStructFieldCode()
continue
}
anonymousFields[f.displayKey] = append(anonymousFields[f.displayKey], structFieldPair{
prevField: prevAnonymousField,
curField: f,
isTaggedKey: f.isTaggedKey,
})
if f.next != nil && f.nextField != f.next && f.next.op.codeType() == codeStructField {
for k, v := range e.anonymousStructFieldPairMap(typ, tags, f.next.toStructFieldCode()) {
anonymousFields[k] = append(anonymousFields[k], v...)
}
}
if f.nextField == nil {
break
}
prevAnonymousField = f
f = f.nextField.toStructFieldCode()
}
return anonymousFields
}
func (e *Encoder) optimizeConflictAnonymousFields(anonymousFields map[string][]structFieldPair) {
for _, fieldPairs := range anonymousFields {
if len(fieldPairs) == 1 {
continue
}
// conflict anonymous fields
taggedPairs := []structFieldPair{}
for _, fieldPair := range fieldPairs {
if fieldPair.isTaggedKey {
taggedPairs = append(taggedPairs, fieldPair)
} else {
if !fieldPair.linked {
if fieldPair.prevField == nil {
// head operation
fieldPair.curField.op = opStructFieldAnonymousHead
} else {
linkPrevToNextField(fieldPair.prevField, fieldPair.curField)
}
fieldPair.linked = true
}
}
}
if len(taggedPairs) > 1 {
for _, fieldPair := range taggedPairs {
if !fieldPair.linked {
if fieldPair.prevField == nil {
// head operation
fieldPair.curField.op = opStructFieldAnonymousHead
} else {
linkPrevToNextField(fieldPair.prevField, fieldPair.curField)
}
fieldPair.linked = true
}
}
} else {
for _, fieldPair := range taggedPairs {
fieldPair.curField.isTaggedKey = false
}
}
}
2020-08-22 06:58:34 +03:00
}
func (e *Encoder) compileStruct(typ *rtype, isPtr, root, withIndent bool) (*opcode, error) {
2020-08-14 16:44:09 +03:00
if code := e.compiledCode(typ, withIndent); code != nil {
return code, nil
}
typeptr := uintptr(unsafe.Pointer(typ))
2020-08-12 12:42:29 +03:00
compiled := &compiledCode{}
if withIndent {
e.structTypeToCompiledIndentCode[typeptr] = compiled
2020-08-13 09:26:35 +03:00
} else {
e.structTypeToCompiledCode[typeptr] = compiled
2020-08-12 12:42:29 +03:00
}
2020-04-30 05:56:56 +03:00
// header => code => structField => code => end
// ^ |
// |__________|
2020-04-29 18:31:50 +03:00
fieldNum := typ.NumField()
fieldIdx := 0
2020-04-29 19:44:48 +03:00
var (
head *structFieldCode
code *opcode
prevField *structFieldCode
)
2020-05-02 17:35:41 +03:00
e.indent++
2020-08-22 06:58:34 +03:00
tags := structTags{}
2020-08-22 09:40:18 +03:00
anonymousFields := map[string][]structFieldPair{}
2020-04-29 18:31:50 +03:00
for i := 0; i < fieldNum; i++ {
field := typ.Field(i)
2020-08-20 06:38:50 +03:00
if isIgnoredStructField(field) {
2020-04-29 18:31:50 +03:00
continue
}
2020-08-22 06:58:34 +03:00
tags = append(tags, structTagFromField(field))
}
for i, tag := range tags {
field := tag.field
2020-04-29 18:31:50 +03:00
fieldType := type2rtype(field.Type)
if isPtr && i == 0 {
// head field of pointer structure at top level
// if field type is pointer and implements MarshalJSON or MarshalText,
// it need to operation of dereference of pointer.
if field.Type.Kind() == reflect.Ptr &&
(field.Type.Implements(marshalJSONType) || field.Type.Implements(marshalTextType)) {
fieldType = rtype_ptrTo(fieldType)
}
}
2020-08-12 10:54:15 +03:00
valueCode, err := e.compile(fieldType, false, withIndent)
2020-04-29 18:31:50 +03:00
if err != nil {
return nil, err
}
2020-08-15 11:41:38 +03:00
if field.Anonymous {
2020-08-22 09:40:18 +03:00
for k, v := range e.anonymousStructFieldPairMap(typ, tags, valueCode.toStructFieldCode()) {
anonymousFields[k] = append(anonymousFields[k], v...)
2020-08-15 11:41:38 +03:00
}
}
if fieldNum == 1 && valueCode.op == opPtr {
// if field number is one and primitive pointer type,
// it should encode as **not** pointer .
switch valueCode.next.op {
case opInt, opInt8, opInt16, opInt32, opInt64,
opUint, opUint8, opUint16, opUint32, opUint64,
opFloat32, opFloat64, opBool, opString, opBytes:
valueCode = valueCode.next
}
}
2020-08-19 13:56:02 +03:00
key := fmt.Sprintf(`"%s":`, tag.key)
2020-04-30 05:56:56 +03:00
fieldCode := &structFieldCode{
opcodeHeader: &opcodeHeader{
typ: valueCode.typ,
2020-05-02 17:35:41 +03:00
next: valueCode,
indent: e.indent,
2020-04-30 05:56:56 +03:00
},
2020-08-15 11:41:38 +03:00
anonymousKey: field.Anonymous,
key: []byte(key),
2020-08-22 09:40:18 +03:00
isTaggedKey: tag.isTaggedKey,
2020-08-22 06:58:34 +03:00
displayKey: tag.key,
2020-08-15 11:41:38 +03:00
offset: field.Offset,
2020-04-30 05:56:56 +03:00
}
2020-04-29 18:31:50 +03:00
if fieldIdx == 0 {
2020-08-19 13:56:02 +03:00
code = e.structHeader(fieldCode, valueCode, tag, withIndent)
2020-04-29 19:44:48 +03:00
head = fieldCode
2020-04-29 18:31:50 +03:00
prevField = fieldCode
} else {
2020-08-14 16:44:09 +03:00
fcode := (*opcode)(unsafe.Pointer(fieldCode))
code.next = fcode
2020-08-19 13:56:02 +03:00
code = e.structField(fieldCode, valueCode, tag, withIndent)
2020-08-14 16:44:09 +03:00
prevField.nextField = fcode
2020-04-29 18:31:50 +03:00
prevField = fieldCode
}
fieldIdx++
}
2020-05-02 17:35:41 +03:00
e.indent--
2020-05-01 07:12:01 +03:00
2020-08-15 11:41:38 +03:00
structEndCode := (*opcode)(unsafe.Pointer(&structFieldCode{
opcodeHeader: &opcodeHeader{
op: opStructEnd,
typ: nil,
indent: e.indent,
},
}))
2020-08-14 16:44:09 +03:00
structEndCode.next = newEndOp(e.indent)
2020-05-02 17:35:41 +03:00
if withIndent {
structEndCode.op = opStructEndIndent
}
2020-05-01 07:12:01 +03:00
if prevField != nil && prevField.nextField == nil {
prevField.nextField = structEndCode
}
// no struct field
if head == nil {
head = &structFieldCode{
opcodeHeader: &opcodeHeader{
2020-05-02 17:35:41 +03:00
op: opStructFieldHead,
typ: typ,
indent: e.indent,
2020-05-01 07:12:01 +03:00
},
nextField: structEndCode,
}
2020-05-02 17:35:41 +03:00
if withIndent {
head.op = opStructFieldHeadIndent
}
2020-05-01 07:12:01 +03:00
code = (*opcode)(unsafe.Pointer(head))
}
2020-04-29 19:44:48 +03:00
head.end = structEndCode
2020-04-29 18:31:50 +03:00
code.next = structEndCode
2020-08-22 06:58:34 +03:00
2020-08-22 09:40:18 +03:00
e.optimizeConflictAnonymousFields(anonymousFields)
2020-08-22 06:58:34 +03:00
e.optimizeAnonymousFields(head)
2020-08-12 12:42:29 +03:00
ret := (*opcode)(unsafe.Pointer(head))
compiled.code = ret
2020-08-15 15:13:08 +03:00
if withIndent {
delete(e.structTypeToCompiledIndentCode, typeptr)
} else {
delete(e.structTypeToCompiledCode, typeptr)
}
2020-08-12 12:42:29 +03:00
return ret, nil
2020-04-29 18:31:50 +03:00
}