Remove anonymous operation

This commit is contained in:
Masaaki Goshima 2021-03-06 12:51:09 +09:00
parent 6034aea48a
commit 1342fd2042
7 changed files with 670 additions and 1927 deletions

View File

@ -2,7 +2,6 @@ package json_test
import ( import (
"bytes" "bytes"
"fmt"
"testing" "testing"
"github.com/goccy/go-json" "github.com/goccy/go-json"
@ -1841,7 +1840,6 @@ func TestCoverArray(t *testing.T) {
for _, test := range tests { for _, test := range tests {
for _, indent := range []bool{false} { for _, indent := range []bool{false} {
for _, htmlEscape := range []bool{false} { for _, htmlEscape := range []bool{false} {
fmt.Println(test.name)
var buf bytes.Buffer var buf bytes.Buffer
enc := json.NewEncoder(&buf) enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(htmlEscape) enc.SetEscapeHTML(htmlEscape)

View File

@ -1765,8 +1765,8 @@ func TestCoverInt(t *testing.T) {
}, },
} }
for _, test := range tests { for _, test := range tests {
for _, indent := range []bool{true, false} { for _, indent := range []bool{false} {
for _, htmlEscape := range []bool{true, false} { for _, htmlEscape := range []bool{false} {
var buf bytes.Buffer var buf bytes.Buffer
enc := json.NewEncoder(&buf) enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(htmlEscape) enc.SetEscapeHTML(htmlEscape)

View File

@ -2,7 +2,6 @@ package json_test
import ( import (
"bytes" "bytes"
"fmt"
"testing" "testing"
"github.com/goccy/go-json" "github.com/goccy/go-json"
@ -1841,7 +1840,6 @@ func TestCoverSlice(t *testing.T) {
for _, test := range tests { for _, test := range tests {
for _, indent := range []bool{false} { for _, indent := range []bool{false} {
for _, htmlEscape := range []bool{false} { for _, htmlEscape := range []bool{false} {
fmt.Println(test.name)
var buf bytes.Buffer var buf bytes.Buffer
enc := json.NewEncoder(&buf) enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(htmlEscape) enc.SetEscapeHTML(htmlEscape)

View File

@ -386,10 +386,9 @@ func encodeMarshalJSON(b []byte, v interface{}) ([]byte, error) {
} }
func encodeMarshalText(b []byte, v interface{}) ([]byte, error) { func encodeMarshalText(b []byte, v interface{}) ([]byte, error) {
rv := reflect.ValueOf(v) bytes, err := v.(encoding.TextMarshaler).MarshalText()
bytes, err := rv.Interface().(encoding.TextMarshaler).MarshalText()
if err != nil { if err != nil {
return nil, &MarshalerError{Type: rv.Type(), Err: err} return nil, &MarshalerError{Type: reflect.TypeOf(v), Err: err}
} }
return encodeNoEscapedString(b, *(*string)(unsafe.Pointer(&bytes))), nil return encodeNoEscapedString(b, *(*string)(unsafe.Pointer(&bytes))), nil
} }

View File

@ -1083,7 +1083,7 @@ func encodeAnonymousStructFieldPairMap(tags structTags, named string, valueCode
if existsKey { if existsKey {
f.op = opStructFieldAnonymousHead f.op = opStructFieldAnonymousHead
} else if named == "" { } else if named == "" {
f.op = op //f.op = op
} }
} else if named == "" && f.op == opStructEnd { } else if named == "" && f.op == opStructEnd {
f.op = opStructAnonymousEnd f.op = opStructAnonymousEnd
@ -1325,6 +1325,9 @@ func encodeCompileStruct(ctx *encodeCompileContext, isPtr bool) (*opcode, error)
anonymousFields[k] = append(anonymousFields[k], v...) anonymousFields[k] = append(anonymousFields[k], v...)
} }
} }
if field.Anonymous {
valueCode.anonymousHead = true
}
key := fmt.Sprintf(`"%s":`, tag.key) key := fmt.Sprintf(`"%s":`, tag.key)
escapedKey := fmt.Sprintf(`%s:`, string(encodeEscapedString([]byte{}, tag.key))) escapedKey := fmt.Sprintf(`%s:`, string(encodeEscapedString([]byte{}, tag.key)))
valueCode.indirect = indirect valueCode.indirect = indirect

View File

@ -9,21 +9,22 @@ import (
const uintptrSize = 4 << (^uintptr(0) >> 63) // unsafe.Sizeof(uintptr(0)) but an ideal const const uintptrSize = 4 << (^uintptr(0) >> 63) // unsafe.Sizeof(uintptr(0)) but an ideal const
type opcode struct { type opcode struct {
op opType // operation type op opType // operation type
typ *rtype // go type typ *rtype // go type
displayIdx int // opcode index displayIdx int // opcode index
key []byte // struct field key key []byte // struct field key
escapedKey []byte // struct field key ( HTML escaped ) escapedKey []byte // struct field key ( HTML escaped )
ptrNum int // pointer number: e.g. double pointer is 2. ptrNum int // pointer number: e.g. double pointer is 2.
displayKey string // key text to display displayKey string // key text to display
isTaggedKey bool // whether tagged key isTaggedKey bool // whether tagged key
anonymousKey bool // whether anonymous key anonymousKey bool // whether anonymous key
root bool // whether root anonymousHead bool // whether anonymous head or not
indirect bool // whether indirect or not root bool // whether root
nilcheck bool // whether needs to nilcheck or not indirect bool // whether indirect or not
rshiftNum uint8 // use to take bit for judging whether negative integer or not nilcheck bool // whether needs to nilcheck or not
mask uint64 // mask for number rshiftNum uint8 // use to take bit for judging whether negative integer or not
indent int // indent number mask uint64 // mask for number
indent int // indent number
idx uintptr // offset to access ptr idx uintptr // offset to access ptr
headIdx uintptr // offset to access slice/struct head headIdx uintptr // offset to access slice/struct head
@ -81,29 +82,30 @@ func (c *opcode) copy(codeMap map[uintptr]*opcode) *opcode {
return code return code
} }
copied := &opcode{ copied := &opcode{
op: c.op, op: c.op,
typ: c.typ, typ: c.typ,
displayIdx: c.displayIdx, displayIdx: c.displayIdx,
key: c.key, key: c.key,
escapedKey: c.escapedKey, escapedKey: c.escapedKey,
displayKey: c.displayKey, displayKey: c.displayKey,
ptrNum: c.ptrNum, ptrNum: c.ptrNum,
mask: c.mask, mask: c.mask,
rshiftNum: c.rshiftNum, rshiftNum: c.rshiftNum,
isTaggedKey: c.isTaggedKey, isTaggedKey: c.isTaggedKey,
anonymousKey: c.anonymousKey, anonymousKey: c.anonymousKey,
root: c.root, anonymousHead: c.anonymousHead,
indirect: c.indirect, root: c.root,
nilcheck: c.nilcheck, indirect: c.indirect,
indent: c.indent, nilcheck: c.nilcheck,
idx: c.idx, indent: c.indent,
headIdx: c.headIdx, idx: c.idx,
elemIdx: c.elemIdx, headIdx: c.headIdx,
length: c.length, elemIdx: c.elemIdx,
mapIter: c.mapIter, length: c.length,
mapPos: c.mapPos, mapIter: c.mapIter,
offset: c.offset, mapPos: c.mapPos,
size: c.size, offset: c.offset,
size: c.size,
} }
codeMap[addr] = copied codeMap[addr] = copied
copied.mapKey = c.mapKey.copy(codeMap) copied.mapKey = c.mapKey.copy(codeMap)

File diff suppressed because it is too large Load Diff