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

@ -18,6 +18,7 @@ type opcode struct {
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
anonymousHead bool // whether anonymous head or not
root bool // whether root root bool // whether root
indirect bool // whether indirect or not indirect bool // whether indirect or not
nilcheck bool // whether needs to nilcheck or not nilcheck bool // whether needs to nilcheck or not
@ -92,6 +93,7 @@ func (c *opcode) copy(codeMap map[uintptr]*opcode) *opcode {
rshiftNum: c.rshiftNum, rshiftNum: c.rshiftNum,
isTaggedKey: c.isTaggedKey, isTaggedKey: c.isTaggedKey,
anonymousKey: c.anonymousKey, anonymousKey: c.anonymousKey,
anonymousHead: c.anonymousHead,
root: c.root, root: c.root,
indirect: c.indirect, indirect: c.indirect,
nilcheck: c.nilcheck, nilcheck: c.nilcheck,

File diff suppressed because it is too large Load Diff