go-json/encode_compile.go

665 lines
17 KiB
Go
Raw Normal View History

2020-04-29 18:31:50 +03:00
package json
import (
"fmt"
"reflect"
"strings"
"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
}
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:
2020-08-19 04:34:11 +03:00
if typ.Elem().Kind() == reflect.Uint8 {
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-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-12 10:54:15 +03:00
keyCode, err := e.compile(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-04-30 07:52:24 +03:00
func (e *Encoder) getTag(field reflect.StructField) string {
return field.Tag.Get("json")
}
func (e *Encoder) isIgnoredStructField(field reflect.StructField) bool {
if field.PkgPath != "" && !field.Anonymous {
// private field
return true
}
tag := e.getTag(field)
if tag == "-" {
return true
}
return false
}
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 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 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
type structTag struct {
key string
isOmitEmpty bool
isString bool
}
func (e *Encoder) structTagFromField(field reflect.StructField) *structTag {
2020-08-14 16:44:09 +03:00
keyName := field.Name
tag := e.getTag(field)
opts := strings.Split(tag, ",")
if len(opts) > 0 {
if opts[0] != "" {
keyName = opts[0]
2020-08-12 12:42:29 +03:00
}
}
2020-08-19 13:56:02 +03:00
st := &structTag{key: keyName}
2020-08-14 16:44:09 +03:00
if len(opts) > 1 {
2020-08-19 13:56:02 +03:00
st.isOmitEmpty = opts[1] == "omitempty"
st.isString = opts[1] == "string"
2020-08-14 16:44:09 +03:00
}
2020-08-19 13:56:02 +03:00
return st
2020-08-14 16:44:09 +03:00
}
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,
opStructFieldHeadOmitEmpty,
opStructFieldHeadIndent,
opStructFieldHeadOmitEmptyIndent:
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,
opStructFieldOmitEmpty,
opStructFieldIndent,
opStructFieldOmitEmptyIndent:
return valueCode.beforeLastCode()
}
return code
}
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-04-29 18:31:50 +03:00
for i := 0; i < fieldNum; i++ {
field := typ.Field(i)
if e.isIgnoredStructField(field) {
continue
}
2020-08-19 13:56:02 +03:00
tag := e.structTagFromField(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 {
f := valueCode.toStructFieldCode()
for {
f.op = f.op.headToAnonymousHead()
if f.op == opStructEnd {
f.op = opStructAnonymousEnd
}
if f.nextField == nil {
break
}
f = f.nextField.toStructFieldCode()
}
}
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),
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-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
}