forked from mirror/go-json
Add test case for MarshalText type
This commit is contained in:
parent
3c85aa1ba4
commit
6034aea48a
|
@ -281,7 +281,7 @@ func (t opType) fieldToStringTagField() opType {
|
|||
"array", "map", "mapLoad", "slice", "struct", "MarshalJSON", "MarshalText", "recursive",
|
||||
"intString", "uintString",
|
||||
"intPtr", "uintPtr", "float32Ptr", "float64Ptr", "boolPtr", "stringPtr", "bytesPtr",
|
||||
"arrayPtr", "slicePtr", "mapPtr", "marshalJSONPtr",
|
||||
"arrayPtr", "slicePtr", "mapPtr", "marshalJSONPtr", "marshalTextPtr",
|
||||
"intNPtr", "uintNPtr", "float32NPtr", "float64NPtr", "boolNPtr", "stringNPtr", "bytesNPtr",
|
||||
}
|
||||
primitiveTypesUpper := []string{}
|
||||
|
|
|
@ -2,7 +2,6 @@ package json_test
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/goccy/go-json"
|
||||
|
@ -3528,7 +3527,6 @@ func TestCoverMarshalJSON(t *testing.T) {
|
|||
for _, test := range tests {
|
||||
for _, indent := range []bool{false} {
|
||||
for _, htmlEscape := range []bool{false} {
|
||||
fmt.Println(test.name)
|
||||
var buf bytes.Buffer
|
||||
enc := json.NewEncoder(&buf)
|
||||
enc.SetEscapeHTML(htmlEscape)
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -61,9 +61,9 @@ func encodeCompileHead(ctx *encodeCompileContext) (*opcode, error) {
|
|||
return encodeCompileMarshalJSON(ctx)
|
||||
}
|
||||
case typ.Implements(marshalTextType):
|
||||
if typ.Kind() != reflect.Ptr || !typ.Elem().Implements(marshalTextType) {
|
||||
return encodeCompileMarshalText(ctx)
|
||||
case rtype_ptrTo(typ).Implements(marshalTextType):
|
||||
return encodeCompileMarshalTextPtr(ctx)
|
||||
}
|
||||
}
|
||||
isPtr := false
|
||||
orgType := typ
|
||||
|
@ -164,7 +164,8 @@ func encodeOptimizeStructEnd(c *opcode) {
|
|||
strings.Contains(prevOp, "Slice") ||
|
||||
strings.Contains(prevOp, "Array") ||
|
||||
strings.Contains(prevOp, "Map") ||
|
||||
strings.Contains(prevOp, "MarshalJSON") {
|
||||
strings.Contains(prevOp, "MarshalJSON") ||
|
||||
strings.Contains(prevOp, "MarshalText") {
|
||||
// not exists field
|
||||
code = code.next
|
||||
break
|
||||
|
@ -189,8 +190,6 @@ func encodeImplementsMarshaler(typ *rtype) bool {
|
|||
return true
|
||||
case typ.Implements(marshalTextType):
|
||||
return true
|
||||
case rtype_ptrTo(typ).Implements(marshalTextType):
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
@ -200,10 +199,8 @@ func encodeCompile(ctx *encodeCompileContext, isPtr bool) (*opcode, error) {
|
|||
switch {
|
||||
case typ.Implements(marshalJSONType) && (typ.Kind() != reflect.Ptr || !typ.Elem().Implements(marshalJSONType)):
|
||||
return encodeCompileMarshalJSON(ctx)
|
||||
case typ.Implements(marshalTextType):
|
||||
case typ.Implements(marshalTextType) && (typ.Kind() != reflect.Ptr || !typ.Elem().Implements(marshalTextType)):
|
||||
return encodeCompileMarshalText(ctx)
|
||||
case rtype_ptrTo(typ).Implements(marshalTextType):
|
||||
return encodeCompileMarshalTextPtr(ctx)
|
||||
}
|
||||
switch typ.Kind() {
|
||||
case reflect.Ptr:
|
||||
|
@ -753,6 +750,9 @@ func encodeTypeToHeaderType(ctx *encodeCompileContext, code *opcode) opType {
|
|||
return opStructFieldHeadMapPtr
|
||||
case opMarshalJSON:
|
||||
return opStructFieldHeadMarshalJSONPtr
|
||||
case opMarshalText:
|
||||
return opStructFieldHeadMarshalTextPtr
|
||||
|
||||
}
|
||||
}
|
||||
case opInt:
|
||||
|
@ -844,6 +844,8 @@ func encodeTypeToFieldType(ctx *encodeCompileContext, code *opcode) opType {
|
|||
return opStructFieldMapPtr
|
||||
case opMarshalJSON:
|
||||
return opStructFieldMarshalJSONPtr
|
||||
case opMarshalText:
|
||||
return opStructFieldMarshalTextPtr
|
||||
}
|
||||
}
|
||||
case opInt:
|
||||
|
@ -954,7 +956,10 @@ func encodeStructHeader(ctx *encodeCompileContext, fieldCode *opcode, valueCode
|
|||
return valueCode.beforeLastCode()
|
||||
case opStructFieldHeadMarshalJSONPtr,
|
||||
opStructFieldHeadOmitEmptyMarshalJSONPtr,
|
||||
opStructFieldHeadStringTagMarshalJSONPtr:
|
||||
opStructFieldHeadStringTagMarshalJSONPtr,
|
||||
opStructFieldHeadMarshalTextPtr,
|
||||
opStructFieldHeadOmitEmptyMarshalTextPtr,
|
||||
opStructFieldHeadStringTagMarshalTextPtr:
|
||||
ctx.decOpcodeIndex()
|
||||
fieldCode.typ = fieldCode.typ.Elem()
|
||||
return (*opcode)(unsafe.Pointer(fieldCode))
|
||||
|
@ -1250,6 +1255,15 @@ func encodeCompileStruct(ctx *encodeCompileContext, isPtr bool) (*opcode, error)
|
|||
nilcheck = false
|
||||
indirect = false
|
||||
disableIndirectConversion = true
|
||||
} else if i == 0 && fieldNum == 1 && isPtr && fieldType.Kind() != reflect.Ptr && rtype_ptrTo(fieldType).Implements(marshalTextType) && !fieldType.Implements(marshalTextType) {
|
||||
code, err := encodeCompileMarshalText(ctx.withType(rtype_ptrTo(fieldType)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
valueCode = code
|
||||
nilcheck = false
|
||||
indirect = false
|
||||
disableIndirectConversion = true
|
||||
} else if isPtr && fieldType.Kind() != reflect.Ptr && !fieldType.Implements(marshalJSONType) && rtype_ptrTo(fieldType).Implements(marshalJSONType) {
|
||||
code, err := encodeCompileMarshalJSON(ctx.withType(rtype_ptrTo(fieldType)))
|
||||
if err != nil {
|
||||
|
@ -1257,18 +1271,37 @@ func encodeCompileStruct(ctx *encodeCompileContext, isPtr bool) (*opcode, error)
|
|||
}
|
||||
nilcheck = false
|
||||
valueCode = code
|
||||
} else if isPtr && fieldType.Kind() != reflect.Ptr && !fieldType.Implements(marshalTextType) && rtype_ptrTo(fieldType).Implements(marshalTextType) {
|
||||
code, err := encodeCompileMarshalText(ctx.withType(rtype_ptrTo(fieldType)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nilcheck = false
|
||||
valueCode = code
|
||||
} else if fieldType.Implements(marshalJSONType) && fieldType.Kind() != reflect.Ptr {
|
||||
code, err := encodeCompileMarshalJSON(ctx.withType(fieldType))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
valueCode = code
|
||||
} else if fieldType.Implements(marshalTextType) && fieldType.Kind() != reflect.Ptr {
|
||||
code, err := encodeCompileMarshalText(ctx.withType(fieldType))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
valueCode = code
|
||||
} else if fieldType.Implements(marshalJSONType) && fieldType.Kind() == reflect.Ptr && !fieldType.Elem().Implements(marshalJSONType) {
|
||||
code, err := encodeCompileMarshalJSON(ctx.withType(fieldType))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
valueCode = code
|
||||
} else if fieldType.Implements(marshalTextType) && fieldType.Kind() == reflect.Ptr && !fieldType.Elem().Implements(marshalTextType) {
|
||||
code, err := encodeCompileMarshalText(ctx.withType(fieldType))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
valueCode = code
|
||||
} else {
|
||||
code, err := encodeCompile(ctx.withType(fieldType), isPtr)
|
||||
if err != nil {
|
||||
|
|
1802
encode_optype.go
1802
encode_optype.go
File diff suppressed because it is too large
Load Diff
486
encode_vm.go
486
encode_vm.go
|
@ -3245,8 +3245,7 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
if code.nilcheck && p == 0 {
|
||||
b = encodeNull(b)
|
||||
} else {
|
||||
iface := ptrToInterface(code, p)
|
||||
bb, err := encodeMarshalJSON(b, iface)
|
||||
bb, err := encodeMarshalJSON(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -3284,8 +3283,7 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
if code.nilcheck && p == 0 {
|
||||
b = encodeNull(b)
|
||||
} else {
|
||||
iface := ptrToInterface(code, p)
|
||||
bb, err := encodeMarshalJSON(b, iface)
|
||||
bb, err := encodeMarshalJSON(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -3323,7 +3321,7 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
code = code.nextField
|
||||
} else {
|
||||
b = append(b, code.key...)
|
||||
bb, err := encodeMarshalJSON(b, ptrToInterface(code, p+code.offset))
|
||||
bb, err := encodeMarshalJSON(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -3391,7 +3389,7 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
code = code.nextField
|
||||
} else {
|
||||
b = append(b, code.key...)
|
||||
bb, err := encodeMarshalJSON(b, ptrToInterface(code, p+code.offset))
|
||||
bb, err := encodeMarshalJSON(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -3424,7 +3422,7 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
if p == 0 && code.nilcheck {
|
||||
b = encodeNull(b)
|
||||
} else {
|
||||
bb, err := encodeMarshalJSON(b, ptrToInterface(code, p+code.offset))
|
||||
bb, err := encodeMarshalJSON(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -3457,7 +3455,7 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
if p == 0 && code.nilcheck {
|
||||
b = encodeNull(b)
|
||||
} else {
|
||||
bb, err := encodeMarshalJSON(b, ptrToInterface(code, p+code.offset))
|
||||
bb, err := encodeMarshalJSON(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -3519,7 +3517,7 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
code = code.nextField
|
||||
} else {
|
||||
b = append(b, code.key...)
|
||||
bb, err := encodeMarshalJSON(b, ptrToInterface(code, p+code.offset))
|
||||
bb, err := encodeMarshalJSON(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -3564,7 +3562,9 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
code = code.end.next
|
||||
break
|
||||
}
|
||||
if code.indirect {
|
||||
store(ctxptr, code.idx, ptrToPtr(p))
|
||||
}
|
||||
fallthrough
|
||||
case opStructFieldHeadMarshalText:
|
||||
p := load(ctxptr, code.idx)
|
||||
|
@ -3576,14 +3576,99 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
}
|
||||
b = append(b, '{')
|
||||
b = append(b, code.key...)
|
||||
bb, err := encodeMarshalText(b, ptrToInterface(code, p+code.offset))
|
||||
if code.typ.Kind() == reflect.Ptr {
|
||||
if code.indirect || code.op == opStructFieldPtrHeadMarshalText {
|
||||
p = ptrToPtr(p + code.offset)
|
||||
}
|
||||
}
|
||||
if code.nilcheck && p == 0 {
|
||||
b = encodeNull(b)
|
||||
} else {
|
||||
bb, err := encodeMarshalText(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b = bb
|
||||
}
|
||||
b = encodeComma(b)
|
||||
code = code.next
|
||||
case opStructFieldPtrHeadStringTagMarshalText:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 {
|
||||
b = encodeNull(b)
|
||||
b = encodeComma(b)
|
||||
code = code.end.next
|
||||
break
|
||||
}
|
||||
if code.indirect {
|
||||
store(ctxptr, code.idx, ptrToPtr(p))
|
||||
}
|
||||
fallthrough
|
||||
case opStructFieldHeadStringTagMarshalText:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 && code.indirect {
|
||||
b = encodeNull(b)
|
||||
b = encodeComma(b)
|
||||
code = code.end.next
|
||||
break
|
||||
}
|
||||
b = append(b, '{')
|
||||
b = append(b, code.key...)
|
||||
if code.typ.Kind() == reflect.Ptr {
|
||||
if code.indirect || code.op == opStructFieldPtrHeadStringTagMarshalText {
|
||||
p = ptrToPtr(p + code.offset)
|
||||
}
|
||||
}
|
||||
if code.nilcheck && p == 0 {
|
||||
b = encodeNull(b)
|
||||
} else {
|
||||
bb, err := encodeMarshalText(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b = bb
|
||||
}
|
||||
b = encodeComma(b)
|
||||
code = code.next
|
||||
case opStructFieldPtrHeadOmitEmptyMarshalText:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 {
|
||||
b = encodeNull(b)
|
||||
b = encodeComma(b)
|
||||
code = code.end.next
|
||||
break
|
||||
}
|
||||
if code.indirect {
|
||||
store(ctxptr, code.idx, ptrToPtr(p))
|
||||
}
|
||||
fallthrough
|
||||
case opStructFieldHeadOmitEmptyMarshalText:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 && code.indirect {
|
||||
b = encodeNull(b)
|
||||
b = encodeComma(b)
|
||||
code = code.end.next
|
||||
break
|
||||
}
|
||||
b = append(b, '{')
|
||||
if code.typ.Kind() == reflect.Ptr {
|
||||
if code.indirect || code.op == opStructFieldPtrHeadOmitEmptyMarshalText {
|
||||
p = ptrToPtr(p + code.offset)
|
||||
}
|
||||
}
|
||||
if p == 0 && code.nilcheck {
|
||||
code = code.nextField
|
||||
} else {
|
||||
b = append(b, code.key...)
|
||||
bb, err := encodeMarshalText(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b = bb
|
||||
b = encodeComma(b)
|
||||
code = code.next
|
||||
case opStructFieldPtrAnonymousHeadMarshalText:
|
||||
}
|
||||
case opStructFieldPtrHeadMarshalTextPtr, opStructFieldPtrHeadStringTagMarshalTextPtr:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 {
|
||||
b = encodeNull(b)
|
||||
|
@ -3593,6 +3678,74 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
}
|
||||
store(ctxptr, code.idx, ptrToPtr(p))
|
||||
fallthrough
|
||||
case opStructFieldHeadMarshalTextPtr, opStructFieldHeadStringTagMarshalTextPtr:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 && code.indirect {
|
||||
b = encodeNull(b)
|
||||
b = encodeComma(b)
|
||||
code = code.end.next
|
||||
break
|
||||
}
|
||||
b = append(b, '{')
|
||||
b = append(b, code.key...)
|
||||
if code.indirect {
|
||||
p = ptrToPtr(p + code.offset)
|
||||
}
|
||||
if p == 0 {
|
||||
b = encodeNull(b)
|
||||
} else {
|
||||
bb, err := encodeMarshalText(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b = bb
|
||||
}
|
||||
b = encodeComma(b)
|
||||
code = code.next
|
||||
case opStructFieldPtrHeadOmitEmptyMarshalTextPtr:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 {
|
||||
b = encodeNull(b)
|
||||
b = encodeComma(b)
|
||||
code = code.end.next
|
||||
break
|
||||
}
|
||||
store(ctxptr, code.idx, ptrToPtr(p))
|
||||
fallthrough
|
||||
case opStructFieldHeadOmitEmptyMarshalTextPtr:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 && code.indirect {
|
||||
b = encodeNull(b)
|
||||
b = encodeComma(b)
|
||||
code = code.end.next
|
||||
break
|
||||
}
|
||||
if code.indirect {
|
||||
p = ptrToPtr(p + code.offset)
|
||||
}
|
||||
b = append(b, '{')
|
||||
if p == 0 {
|
||||
code = code.nextField
|
||||
} else {
|
||||
b = append(b, code.key...)
|
||||
bb, err := encodeMarshalText(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b = bb
|
||||
b = encodeComma(b)
|
||||
code = code.next
|
||||
}
|
||||
case opStructFieldPtrAnonymousHeadMarshalText:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 {
|
||||
code = code.end.next
|
||||
break
|
||||
}
|
||||
if code.indirect {
|
||||
store(ctxptr, code.idx, ptrToPtr(p))
|
||||
}
|
||||
fallthrough
|
||||
case opStructFieldAnonymousHeadMarshalText:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 && code.indirect {
|
||||
|
@ -3600,127 +3753,143 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
break
|
||||
}
|
||||
b = append(b, code.key...)
|
||||
bb, err := encodeMarshalText(b, ptrToInterface(code, p+code.offset))
|
||||
if code.typ.Kind() == reflect.Ptr {
|
||||
if code.indirect || code.op == opStructFieldPtrAnonymousHeadMarshalText {
|
||||
p = ptrToPtr(p + code.offset)
|
||||
}
|
||||
}
|
||||
if p == 0 && code.nilcheck {
|
||||
b = encodeNull(b)
|
||||
} else {
|
||||
bb, err := encodeMarshalText(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b = bb
|
||||
}
|
||||
b = encodeComma(b)
|
||||
code = code.next
|
||||
case opStructFieldPtrAnonymousHeadStringTagMarshalText:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 {
|
||||
code = code.end.next
|
||||
break
|
||||
}
|
||||
if code.indirect {
|
||||
store(ctxptr, code.idx, ptrToPtr(p))
|
||||
}
|
||||
fallthrough
|
||||
case opStructFieldAnonymousHeadStringTagMarshalText:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 && code.indirect {
|
||||
code = code.end.next
|
||||
break
|
||||
}
|
||||
b = append(b, code.key...)
|
||||
if code.typ.Kind() == reflect.Ptr {
|
||||
if code.indirect || code.op == opStructFieldPtrAnonymousHeadStringTagMarshalText {
|
||||
p = ptrToPtr(p + code.offset)
|
||||
}
|
||||
}
|
||||
if p == 0 && code.nilcheck {
|
||||
b = encodeNull(b)
|
||||
} else {
|
||||
bb, err := encodeMarshalText(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b = bb
|
||||
}
|
||||
b = encodeComma(b)
|
||||
code = code.next
|
||||
case opStructFieldPtrAnonymousHeadMarshalTextPtr, opStructFieldPtrAnonymousHeadStringTagMarshalTextPtr:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 {
|
||||
code = code.end.next
|
||||
break
|
||||
}
|
||||
store(ctxptr, code.idx, ptrToPtr(p))
|
||||
fallthrough
|
||||
case opStructFieldAnonymousHeadMarshalTextPtr, opStructFieldAnonymousHeadStringTagMarshalTextPtr:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 && code.indirect {
|
||||
code = code.end.next
|
||||
break
|
||||
}
|
||||
b = append(b, code.key...)
|
||||
if code.indirect {
|
||||
p = ptrToPtr(p + code.offset)
|
||||
}
|
||||
if p == 0 {
|
||||
b = encodeNull(b)
|
||||
} else {
|
||||
bb, err := encodeMarshalText(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b = bb
|
||||
}
|
||||
b = encodeComma(b)
|
||||
code = code.next
|
||||
case opStructFieldPtrAnonymousHeadOmitEmptyMarshalText:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 {
|
||||
code = code.end.next
|
||||
break
|
||||
}
|
||||
if code.indirect {
|
||||
store(ctxptr, code.idx, ptrToPtr(p))
|
||||
}
|
||||
fallthrough
|
||||
case opStructFieldAnonymousHeadOmitEmptyMarshalText:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 && code.indirect {
|
||||
code = code.end.next
|
||||
break
|
||||
}
|
||||
if code.typ.Kind() == reflect.Ptr {
|
||||
if code.indirect || code.op == opStructFieldPtrAnonymousHeadOmitEmptyMarshalText {
|
||||
p = ptrToPtr(p + code.offset)
|
||||
}
|
||||
}
|
||||
if p == 0 && code.nilcheck {
|
||||
code = code.nextField
|
||||
} else {
|
||||
b = append(b, code.key...)
|
||||
bb, err := encodeMarshalText(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b = bb
|
||||
b = encodeComma(b)
|
||||
code = code.next
|
||||
case opStructFieldPtrHeadOmitEmptyMarshalText:
|
||||
ptr := load(ctxptr, code.idx)
|
||||
if ptr != 0 {
|
||||
store(ctxptr, code.idx, ptrToPtr(ptr))
|
||||
}
|
||||
fallthrough
|
||||
case opStructFieldHeadOmitEmptyMarshalText:
|
||||
ptr := load(ctxptr, code.idx)
|
||||
if ptr == 0 {
|
||||
b = encodeNull(b)
|
||||
b = encodeComma(b)
|
||||
case opStructFieldPtrAnonymousHeadOmitEmptyMarshalTextPtr:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 {
|
||||
code = code.end.next
|
||||
} else {
|
||||
b = append(b, '{')
|
||||
ptr += code.offset
|
||||
p := ptrToUnsafePtr(ptr)
|
||||
isPtr := code.typ.Kind() == reflect.Ptr
|
||||
if p == nil || (!isPtr && *(*unsafe.Pointer)(p) == nil) {
|
||||
break
|
||||
}
|
||||
store(ctxptr, code.idx, ptrToPtr(p))
|
||||
fallthrough
|
||||
case opStructFieldAnonymousHeadOmitEmptyMarshalTextPtr:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 && code.indirect {
|
||||
code = code.end.next
|
||||
break
|
||||
}
|
||||
if code.indirect {
|
||||
p = ptrToPtr(p + code.offset)
|
||||
}
|
||||
if p == 0 {
|
||||
code = code.nextField
|
||||
} else {
|
||||
v := *(*interface{})(unsafe.Pointer(&interfaceHeader{typ: code.typ, ptr: p}))
|
||||
bytes, err := v.(encoding.TextMarshaler).MarshalText()
|
||||
if err != nil {
|
||||
return nil, &MarshalerError{
|
||||
Type: rtype2type(code.typ),
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
b = append(b, code.key...)
|
||||
b = encodeNoEscapedString(b, *(*string)(unsafe.Pointer(&bytes)))
|
||||
b = encodeComma(b)
|
||||
code = code.next
|
||||
}
|
||||
}
|
||||
case opStructFieldPtrAnonymousHeadOmitEmptyMarshalText:
|
||||
ptr := load(ctxptr, code.idx)
|
||||
if ptr != 0 {
|
||||
store(ctxptr, code.idx, ptrToPtr(ptr))
|
||||
}
|
||||
fallthrough
|
||||
case opStructFieldAnonymousHeadOmitEmptyMarshalText:
|
||||
ptr := load(ctxptr, code.idx)
|
||||
if ptr == 0 {
|
||||
code = code.end.next
|
||||
} else {
|
||||
ptr += code.offset
|
||||
p := ptrToUnsafePtr(ptr)
|
||||
isPtr := code.typ.Kind() == reflect.Ptr
|
||||
if p == nil || (!isPtr && *(*unsafe.Pointer)(p) == nil) {
|
||||
code = code.nextField
|
||||
} else {
|
||||
v := *(*interface{})(unsafe.Pointer(&interfaceHeader{typ: code.typ, ptr: p}))
|
||||
bytes, err := v.(encoding.TextMarshaler).MarshalText()
|
||||
bb, err := encodeMarshalText(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, &MarshalerError{
|
||||
Type: rtype2type(code.typ),
|
||||
Err: err,
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
b = append(b, code.key...)
|
||||
b = encodeNoEscapedString(b, *(*string)(unsafe.Pointer(&bytes)))
|
||||
b = encodeComma(b)
|
||||
code = code.next
|
||||
}
|
||||
}
|
||||
case opStructFieldPtrHeadStringTagMarshalText:
|
||||
ptr := load(ctxptr, code.idx)
|
||||
if ptr != 0 {
|
||||
store(ctxptr, code.idx, ptrToPtr(ptr))
|
||||
}
|
||||
fallthrough
|
||||
case opStructFieldHeadStringTagMarshalText:
|
||||
ptr := load(ctxptr, code.idx)
|
||||
if ptr == 0 {
|
||||
b = encodeNull(b)
|
||||
b = encodeComma(b)
|
||||
code = code.end.next
|
||||
} else {
|
||||
b = append(b, '{')
|
||||
ptr += code.offset
|
||||
p := ptrToUnsafePtr(ptr)
|
||||
v := *(*interface{})(unsafe.Pointer(&interfaceHeader{typ: code.typ, ptr: p}))
|
||||
bytes, err := v.(encoding.TextMarshaler).MarshalText()
|
||||
if err != nil {
|
||||
return nil, &MarshalerError{
|
||||
Type: rtype2type(code.typ),
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
b = append(b, code.key...)
|
||||
b = encodeNoEscapedString(b, *(*string)(unsafe.Pointer(&bytes)))
|
||||
b = encodeComma(b)
|
||||
code = code.next
|
||||
}
|
||||
case opStructFieldPtrAnonymousHeadStringTagMarshalText:
|
||||
ptr := load(ctxptr, code.idx)
|
||||
if ptr != 0 {
|
||||
store(ctxptr, code.idx, ptrToPtr(ptr))
|
||||
}
|
||||
fallthrough
|
||||
case opStructFieldAnonymousHeadStringTagMarshalText:
|
||||
ptr := load(ctxptr, code.idx)
|
||||
if ptr == 0 {
|
||||
code = code.end.next
|
||||
} else {
|
||||
ptr += code.offset
|
||||
p := ptrToUnsafePtr(ptr)
|
||||
v := *(*interface{})(unsafe.Pointer(&interfaceHeader{typ: code.typ, ptr: p}))
|
||||
bytes, err := v.(encoding.TextMarshaler).MarshalText()
|
||||
if err != nil {
|
||||
return nil, errMarshaler(code, err)
|
||||
}
|
||||
b = append(b, code.key...)
|
||||
b = encodeNoEscapedString(b, *(*string)(unsafe.Pointer(&bytes)))
|
||||
b = bb
|
||||
b = encodeComma(b)
|
||||
code = code.next
|
||||
}
|
||||
|
@ -4187,8 +4356,7 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
if p == 0 && code.nilcheck {
|
||||
b = encodeNull(b)
|
||||
} else {
|
||||
v := ptrToInterface(code, p)
|
||||
bb, err := encodeMarshalJSON(b, v)
|
||||
bb, err := encodeMarshalJSON(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -4240,42 +4408,66 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
b = encodeComma(bb)
|
||||
}
|
||||
code = code.next
|
||||
case opStructFieldMarshalText:
|
||||
ptr := load(ctxptr, code.headIdx)
|
||||
case opStructFieldMarshalText, opStructFieldStringTagMarshalText:
|
||||
p := load(ctxptr, code.headIdx)
|
||||
b = append(b, code.key...)
|
||||
p := ptr + code.offset
|
||||
v := ptrToInterface(code, p)
|
||||
bytes, err := v.(encoding.TextMarshaler).MarshalText()
|
||||
if err != nil {
|
||||
return nil, errMarshaler(code, err)
|
||||
p += code.offset
|
||||
if code.typ.Kind() == reflect.Ptr {
|
||||
p = ptrToPtr(p)
|
||||
}
|
||||
b = encodeNoEscapedString(b, *(*string)(unsafe.Pointer(&bytes)))
|
||||
b = encodeComma(b)
|
||||
code = code.next
|
||||
case opStructFieldStringTagMarshalText:
|
||||
ptr := load(ctxptr, code.headIdx)
|
||||
p := ptr + code.offset
|
||||
v := ptrToInterface(code, p)
|
||||
bytes, err := v.(encoding.TextMarshaler).MarshalText()
|
||||
if p == 0 && code.nilcheck {
|
||||
b = encodeNull(b)
|
||||
} else {
|
||||
bb, err := encodeMarshalText(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, errMarshaler(code, err)
|
||||
return nil, err
|
||||
}
|
||||
b = bb
|
||||
}
|
||||
b = append(b, code.key...)
|
||||
b = encodeNoEscapedString(b, *(*string)(unsafe.Pointer(&bytes)))
|
||||
b = encodeComma(b)
|
||||
code = code.next
|
||||
case opStructFieldOmitEmptyMarshalText:
|
||||
ptr := load(ctxptr, code.headIdx)
|
||||
p := ptr + code.offset
|
||||
v := ptrToInterface(code, p)
|
||||
if v != nil {
|
||||
bytes, err := v.(encoding.TextMarshaler).MarshalText()
|
||||
if err != nil {
|
||||
return nil, errMarshaler(code, err)
|
||||
p := load(ctxptr, code.headIdx)
|
||||
p += code.offset
|
||||
if code.typ.Kind() == reflect.Ptr {
|
||||
p = ptrToPtr(p)
|
||||
}
|
||||
if p == 0 && code.nilcheck {
|
||||
code = code.nextField
|
||||
break
|
||||
}
|
||||
b = append(b, code.key...)
|
||||
b = encodeNoEscapedString(b, *(*string)(unsafe.Pointer(&bytes)))
|
||||
bb, err := encodeMarshalText(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b = encodeComma(bb)
|
||||
code = code.next
|
||||
case opStructFieldMarshalTextPtr, opStructFieldStringTagMarshalTextPtr:
|
||||
p := load(ctxptr, code.headIdx)
|
||||
b = append(b, code.key...)
|
||||
p = ptrToPtr(p + code.offset)
|
||||
if p == 0 {
|
||||
b = encodeNull(b)
|
||||
} else {
|
||||
bb, err := encodeMarshalText(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b = bb
|
||||
}
|
||||
b = encodeComma(b)
|
||||
code = code.next
|
||||
case opStructFieldOmitEmptyMarshalTextPtr:
|
||||
p := load(ctxptr, code.headIdx)
|
||||
p = ptrToPtr(p + code.offset)
|
||||
if p != 0 {
|
||||
b = append(b, code.key...)
|
||||
bb, err := encodeMarshalText(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b = encodeComma(bb)
|
||||
}
|
||||
code = code.next
|
||||
case opStructFieldArray, opStructFieldStringTagArray:
|
||||
|
|
Loading…
Reference in New Issue