Support embedded struct

This commit is contained in:
Masaaki Goshima 2020-08-15 17:41:38 +09:00
parent 95cf762276
commit 025ac2a53b
6 changed files with 3650 additions and 1533 deletions

311
cmd/generator/main.go Normal file
View File

@ -0,0 +1,311 @@
package main
import (
"bytes"
"fmt"
"io/ioutil"
"path/filepath"
"runtime"
"strings"
"text/template"
)
type opType struct {
Op string
IndentOp string
Code string
}
type headType struct {
Head string
PtrHead string
AnonymousHead string
AnonymousPtrHead string
OmitEmptyHead string
OmitEmptyPtrHead string
}
type fieldType struct {
Field string
OmitEmptyField string
}
func _main() error {
tmpl, err := template.New("").Parse(`// Code generated by cmd/generator. DO NOT EDIT!
package json
type codeType int
const (
{{- range $index, $type := .CodeTypes }}
code{{ $type }} codeType = {{ $index }}
{{- end }}
)
type opType int
const (
{{- range $index, $type := .OpTypes }}
op{{ $type.Op }} opType = {{ $index }}
{{- end }}
)
func (t opType) String() string {
switch t {
{{- range $type := .OpTypes }}
case op{{ $type.Op }}:
return "{{ $type.Op }}"
{{- end }}
}
return ""
}
func (t opType) codeType() codeType {
switch t {
{{- range $type := .OpTypes }}
case op{{ $type.Op }}:
return code{{ $type.Code }}
{{- end }}
}
return codeOp
}
func (t opType) toIndent() opType {
switch t {
{{- range $type := .OpTypes }}
case op{{ $type.Op }}:
return op{{ $type.IndentOp }}
{{- end }}
}
return t
}
func (t opType) headToPtrHead() opType {
switch t {
{{- range $type := .HeadTypes }}
case op{{ $type.Head }}:
return op{{ $type.PtrHead }}
case op{{ $type.AnonymousHead }}:
return op{{ $type.AnonymousPtrHead }}
case op{{ $type.OmitEmptyHead }}:
return op{{ $type.OmitEmptyPtrHead }}
{{- end }}
}
return t
}
func (t opType) headToAnonymousHead() opType {
switch t {
{{- range $type := .HeadTypes }}
case op{{ $type.Head }}:
return op{{ $type.AnonymousHead }}
case op{{ $type.PtrHead }}:
return op{{ $type.AnonymousPtrHead }}
{{- end }}
}
return t
}
func (t opType) headToOmitEmptyHead() opType {
switch t {
{{- range $type := .HeadTypes }}
case op{{ $type.Head }}:
return op{{ $type.OmitEmptyHead }}
case op{{ $type.PtrHead }}:
return op{{ $type.OmitEmptyPtrHead }}
{{- end }}
}
return t
}
func (t opType) ptrHeadToHead() opType {
switch t {
{{- range $type := .HeadTypes }}
case op{{ $type.PtrHead }}:
return op{{ $type.Head }}
case op{{ $type.AnonymousPtrHead }}:
return op{{ $type.AnonymousHead }}
case op{{ $type.OmitEmptyPtrHead }}:
return op{{ $type.OmitEmptyHead }}
{{- end }}
}
return t
}
func (t opType) fieldToOmitEmptyField() opType {
switch t {
{{- range $type := .FieldTypes }}
case op{{ $type.Field }}:
return op{{ $type.OmitEmptyField }}
{{- end }}
}
return t
}
`)
if err != nil {
return err
}
codeTypes := []string{
"Op",
"ArrayHead",
"ArrayElem",
"SliceHead",
"SliceElem",
"MapHead",
"MapKey",
"MapValue",
"StructFieldRecursive",
"StructField",
}
primitiveTypes := []string{
"int", "int8", "int16", "int32", "int64",
"uint", "uint8", "uint16", "uint32", "uint64",
"float32", "float64", "bool", "string",
}
primitiveTypesUpper := []string{}
for _, typ := range primitiveTypes {
primitiveTypesUpper = append(primitiveTypesUpper, strings.ToUpper(string(typ[0]))+typ[1:])
}
opTypes := []opType{
{"End", "EndIndent", "Op"},
{"Interface", "InterfaceIndent", "Op"},
{"Ptr", "PtrIndent", "Op"},
{"MarshalJSON", "MarshalJSONIndent", "Op"},
{"MarshalText", "MarshalTextIndent", "Op"},
{"SliceHead", "SliceHeadIndent", "SliceHead"},
{"RootSliceHead", "RootSliceHeadIndent", "SliceHead"},
{"SliceElem", "SliceElemIndent", "SliceElem"},
{"RootSliceElem", "RootSliceElemIndent", "SliceElem"},
{"SliceEnd", "SliceEndIndent", "Op"},
{"ArrayHead", "ArrayHeadIndent", "ArrayHead"},
{"ArrayElem", "ArrayElemIndent", "ArrayElem"},
{"ArrayEnd", "ArrayEndIndent", "Op"},
{"MapHead", "MapHeadIndent", "MapHead"},
{"MapHeadLoad", "MapHeadLoadIndent", "MapHead"},
{"RootMapHead", "RootMapHeadIndent", "MapHead"},
{"MapKey", "MapKeyIndent", "MapKey"},
{"RootMapKey", "RootMapKeyIndent", "MapKey"},
{"MapValue", "MapValueIndent", "MapValue"},
{"MapEnd", "MapEndIndent", "Op"},
{"StructFieldHead", "StructFieldHeadIndent", "StructField"},
{"StructFieldHeadOmitEmpty", "StructFieldHeadOmitEmptyIndent", "StructField"},
{"StructFieldAnonymousHead", "StructFieldAnonymousHeadIndent", "StructField"},
{"StructFieldPtrHead", "StructFieldPtrHeadIndent", "StructField"},
{"StructFieldPtrHeadOmitEmpty", "StructFieldPtrHeadOmitEmptyIndent", "StructField"},
{"StructFieldPtrAnonymousHead", "StructFieldPtrAnonymousHeadIndent", "StructField"},
{"StructField", "StructFieldIndent", "StructField"},
{"StructFieldOmitEmpty", "StructFieldOmitEmptyIndent", "StructField"},
{"StructFieldRecursive", "StructFieldRecursiveIndent", "StructFieldRecursive"},
{"StructEnd", "StructEndIndent", "StructField"},
{"StructAnonymousEnd", "StructAnonymousEndIndent", "StructField"},
}
for _, typ := range primitiveTypesUpper {
opTypes = append(opTypes, opType{
Op: typ,
IndentOp: fmt.Sprintf("%sIndent", typ),
Code: "Op",
})
}
for _, prefix := range []string{
"StructFieldHead",
"StructFieldHeadOmitEmpty",
"StructFieldAnonymousHead",
"StructFieldPtrHead",
"StructFieldPtrHeadOmitEmpty",
"StructFieldPtrAnonymousHead",
"StructField",
"StructFieldOmitEmpty",
} {
for _, typ := range primitiveTypesUpper {
opTypes = append(opTypes, opType{
Op: fmt.Sprintf("%s%s", prefix, typ),
IndentOp: fmt.Sprintf("%s%sIndent", prefix, typ),
Code: "StructField",
})
}
}
for _, typ := range opTypes {
opTypes = append(opTypes, opType{
Op: typ.IndentOp,
IndentOp: typ.IndentOp,
Code: typ.Code,
})
}
base := headType{
Head: "StructFieldHead",
PtrHead: "StructFieldPtrHead",
AnonymousHead: "StructFieldAnonymousHead",
AnonymousPtrHead: "StructFieldPtrAnonymousHead",
OmitEmptyHead: "StructFieldHeadOmitEmpty",
OmitEmptyPtrHead: "StructFieldPtrHeadOmitEmpty",
}
headTypes := []headType{base}
for _, prim := range primitiveTypesUpper {
headTypes = append(headTypes, headType{
Head: fmt.Sprintf("%s%s", base.Head, prim),
PtrHead: fmt.Sprintf("%s%s", base.PtrHead, prim),
AnonymousHead: fmt.Sprintf("%s%s", base.AnonymousHead, prim),
AnonymousPtrHead: fmt.Sprintf("%s%s", base.AnonymousPtrHead, prim),
OmitEmptyHead: fmt.Sprintf("%s%s", base.OmitEmptyHead, prim),
OmitEmptyPtrHead: fmt.Sprintf("%s%s", base.OmitEmptyPtrHead, prim),
})
}
for _, typ := range headTypes {
headTypes = append(headTypes, headType{
Head: fmt.Sprintf("%sIndent", typ.Head),
PtrHead: fmt.Sprintf("%sIndent", typ.PtrHead),
AnonymousHead: fmt.Sprintf("%sIndent", typ.AnonymousHead),
AnonymousPtrHead: fmt.Sprintf("%sIndent", typ.AnonymousPtrHead),
OmitEmptyHead: fmt.Sprintf("%sIndent", typ.OmitEmptyHead),
OmitEmptyPtrHead: fmt.Sprintf("%sIndent", typ.OmitEmptyPtrHead),
})
}
baseField := fieldType{
Field: "StructField",
OmitEmptyField: "StructFieldOmitEmpty",
}
fieldTypes := []fieldType{baseField}
for _, prim := range primitiveTypesUpper {
fieldTypes = append(fieldTypes, fieldType{
Field: fmt.Sprintf("%s%s", baseField.Field, prim),
OmitEmptyField: fmt.Sprintf("%s%s", baseField.OmitEmptyField, prim),
})
}
for _, typ := range fieldTypes {
fieldTypes = append(fieldTypes, fieldType{
Field: fmt.Sprintf("%sIndent", typ.Field),
OmitEmptyField: fmt.Sprintf("%sIndent", typ.OmitEmptyField),
})
}
var b bytes.Buffer
if err := tmpl.Execute(&b, struct {
CodeTypes []string
OpTypes []opType
HeadTypes []headType
FieldTypes []fieldType
}{
CodeTypes: codeTypes,
OpTypes: opTypes,
HeadTypes: headTypes,
FieldTypes: fieldTypes,
}); err != nil {
return err
}
path := filepath.Join(repoRoot(), "encode_optype.go")
return ioutil.WriteFile(path, b.Bytes(), 0644)
}
func repoRoot() string {
_, file, _, _ := runtime.Caller(0)
relativePathFromRepoRoot := filepath.Join("cmd", "generator")
return strings.TrimSuffix(filepath.Dir(file), relativePathFromRepoRoot)
}
func main() {
if err := _main(); err != nil {
panic(err)
}
}

View File

@ -77,132 +77,12 @@ func (e *Encoder) compile(typ *rtype, root, withIndent bool) (*opcode, error) {
}
func (e *Encoder) optimizeStructFieldPtrHead(typ *rtype, code *opcode) *opcode {
switch code.op {
case opStructFieldHead:
code.op = opStructFieldPtrHead
case opStructFieldHeadInt:
code.op = opStructFieldPtrHeadInt
case opStructFieldHeadInt8:
code.op = opStructFieldPtrHeadInt8
case opStructFieldHeadInt16:
code.op = opStructFieldPtrHeadInt16
case opStructFieldHeadInt32:
code.op = opStructFieldPtrHeadInt32
case opStructFieldHeadInt64:
code.op = opStructFieldPtrHeadInt64
case opStructFieldHeadUint:
code.op = opStructFieldPtrHeadUint
case opStructFieldHeadUint8:
code.op = opStructFieldPtrHeadUint8
case opStructFieldHeadUint16:
code.op = opStructFieldPtrHeadUint16
case opStructFieldHeadUint32:
code.op = opStructFieldPtrHeadUint32
case opStructFieldHeadUint64:
code.op = opStructFieldPtrHeadUint64
case opStructFieldHeadFloat32:
code.op = opStructFieldPtrHeadFloat32
case opStructFieldHeadFloat64:
code.op = opStructFieldPtrHeadFloat64
case opStructFieldHeadString:
code.op = opStructFieldPtrHeadString
case opStructFieldHeadBool:
code.op = opStructFieldPtrHeadBool
case opStructFieldHeadOmitEmpty:
code.op = opStructFieldPtrHeadOmitEmpty
case opStructFieldHeadIntOmitEmpty:
code.op = opStructFieldPtrHeadIntOmitEmpty
case opStructFieldHeadInt8OmitEmpty:
code.op = opStructFieldPtrHeadInt8OmitEmpty
case opStructFieldHeadInt16OmitEmpty:
code.op = opStructFieldPtrHeadInt16OmitEmpty
case opStructFieldHeadInt32OmitEmpty:
code.op = opStructFieldPtrHeadInt32OmitEmpty
case opStructFieldHeadInt64OmitEmpty:
code.op = opStructFieldPtrHeadInt64OmitEmpty
case opStructFieldHeadUintOmitEmpty:
code.op = opStructFieldPtrHeadUintOmitEmpty
case opStructFieldHeadUint8OmitEmpty:
code.op = opStructFieldPtrHeadUint8OmitEmpty
case opStructFieldHeadUint16OmitEmpty:
code.op = opStructFieldPtrHeadUint16OmitEmpty
case opStructFieldHeadUint32OmitEmpty:
code.op = opStructFieldPtrHeadUint32OmitEmpty
case opStructFieldHeadUint64OmitEmpty:
code.op = opStructFieldPtrHeadUint64OmitEmpty
case opStructFieldHeadFloat32OmitEmpty:
code.op = opStructFieldPtrHeadFloat32OmitEmpty
case opStructFieldHeadFloat64OmitEmpty:
code.op = opStructFieldPtrHeadFloat64OmitEmpty
case opStructFieldHeadStringOmitEmpty:
code.op = opStructFieldPtrHeadStringOmitEmpty
case opStructFieldHeadBoolOmitEmpty:
code.op = opStructFieldPtrHeadBoolOmitEmpty
case opStructFieldHeadIndent:
code.op = opStructFieldPtrHeadIndent
case opStructFieldHeadIntIndent:
code.op = opStructFieldPtrHeadIntIndent
case opStructFieldHeadInt8Indent:
code.op = opStructFieldPtrHeadInt8Indent
case opStructFieldHeadInt16Indent:
code.op = opStructFieldPtrHeadInt16Indent
case opStructFieldHeadInt32Indent:
code.op = opStructFieldPtrHeadInt32Indent
case opStructFieldHeadInt64Indent:
code.op = opStructFieldPtrHeadInt64Indent
case opStructFieldHeadUintIndent:
code.op = opStructFieldPtrHeadUintIndent
case opStructFieldHeadUint8Indent:
code.op = opStructFieldPtrHeadUint8Indent
case opStructFieldHeadUint16Indent:
code.op = opStructFieldPtrHeadUint16Indent
case opStructFieldHeadUint32Indent:
code.op = opStructFieldPtrHeadUint32Indent
case opStructFieldHeadUint64Indent:
code.op = opStructFieldPtrHeadUint64Indent
case opStructFieldHeadFloat32Indent:
code.op = opStructFieldPtrHeadFloat32Indent
case opStructFieldHeadFloat64Indent:
code.op = opStructFieldPtrHeadFloat64Indent
case opStructFieldHeadStringIndent:
code.op = opStructFieldPtrHeadStringIndent
case opStructFieldHeadBoolIndent:
code.op = opStructFieldPtrHeadBoolIndent
case opStructFieldHeadOmitEmptyIndent:
code.op = opStructFieldPtrHeadOmitEmptyIndent
case opStructFieldHeadIntOmitEmptyIndent:
code.op = opStructFieldPtrHeadIntOmitEmptyIndent
case opStructFieldHeadInt8OmitEmptyIndent:
code.op = opStructFieldPtrHeadInt8OmitEmptyIndent
case opStructFieldHeadInt16OmitEmptyIndent:
code.op = opStructFieldPtrHeadInt16OmitEmptyIndent
case opStructFieldHeadInt32OmitEmptyIndent:
code.op = opStructFieldPtrHeadInt32OmitEmptyIndent
case opStructFieldHeadInt64OmitEmptyIndent:
code.op = opStructFieldPtrHeadInt64OmitEmptyIndent
case opStructFieldHeadUintOmitEmptyIndent:
code.op = opStructFieldPtrHeadUintOmitEmptyIndent
case opStructFieldHeadUint8OmitEmptyIndent:
code.op = opStructFieldPtrHeadUint8OmitEmptyIndent
case opStructFieldHeadUint16OmitEmptyIndent:
code.op = opStructFieldPtrHeadUint16OmitEmptyIndent
case opStructFieldHeadUint32OmitEmptyIndent:
code.op = opStructFieldPtrHeadUint32OmitEmptyIndent
case opStructFieldHeadUint64OmitEmptyIndent:
code.op = opStructFieldPtrHeadUint64OmitEmptyIndent
case opStructFieldHeadFloat32OmitEmptyIndent:
code.op = opStructFieldPtrHeadFloat32OmitEmptyIndent
case opStructFieldHeadFloat64OmitEmptyIndent:
code.op = opStructFieldPtrHeadFloat64OmitEmptyIndent
case opStructFieldHeadStringOmitEmptyIndent:
code.op = opStructFieldPtrHeadStringOmitEmptyIndent
case opStructFieldHeadBoolOmitEmptyIndent:
code.op = opStructFieldPtrHeadBoolOmitEmptyIndent
default:
return newOpCode(opPtr, typ, e.indent, code)
ptrHeadOp := code.op.headToPtrHead()
if code.op != ptrHeadOp {
code.op = ptrHeadOp
return code
}
return code
return newOpCode(opPtr, typ, e.indent, code)
}
func (e *Encoder) compilePtr(typ *rtype, root, withIndent bool) (*opcode, error) {
@ -456,118 +336,7 @@ func (e *Encoder) isIgnoredStructField(field reflect.StructField) bool {
return false
}
func (e *Encoder) optimizeStructHeaderOmitEmptyIndent(op opType) opType {
switch op {
case opInt:
return opStructFieldHeadIntOmitEmptyIndent
case opInt8:
return opStructFieldHeadInt8OmitEmptyIndent
case opInt16:
return opStructFieldHeadInt16OmitEmptyIndent
case opInt32:
return opStructFieldHeadInt32OmitEmptyIndent
case opInt64:
return opStructFieldHeadInt64OmitEmptyIndent
case opUint:
return opStructFieldHeadUintOmitEmptyIndent
case opUint8:
return opStructFieldHeadUint8OmitEmptyIndent
case opUint16:
return opStructFieldHeadUint16OmitEmptyIndent
case opUint32:
return opStructFieldHeadUint32OmitEmptyIndent
case opUint64:
return opStructFieldHeadUint64OmitEmptyIndent
case opFloat32:
return opStructFieldHeadFloat32OmitEmptyIndent
case opFloat64:
return opStructFieldHeadFloat64OmitEmptyIndent
case opString:
return opStructFieldHeadStringOmitEmptyIndent
case opBool:
return opStructFieldHeadBoolOmitEmptyIndent
}
return opStructFieldHeadOmitEmptyIndent
}
func (e *Encoder) optimizeStructHeaderIndent(op opType, isOmitEmpty bool) opType {
if isOmitEmpty {
return e.optimizeStructHeaderOmitEmptyIndent(op)
}
switch op {
case opInt:
return opStructFieldHeadIntIndent
case opInt8:
return opStructFieldHeadInt8Indent
case opInt16:
return opStructFieldHeadInt16Indent
case opInt32:
return opStructFieldHeadInt32Indent
case opInt64:
return opStructFieldHeadInt64Indent
case opUint:
return opStructFieldHeadUintIndent
case opUint8:
return opStructFieldHeadUint8Indent
case opUint16:
return opStructFieldHeadUint16Indent
case opUint32:
return opStructFieldHeadUint32Indent
case opUint64:
return opStructFieldHeadUint64Indent
case opFloat32:
return opStructFieldHeadFloat32Indent
case opFloat64:
return opStructFieldHeadFloat64Indent
case opString:
return opStructFieldHeadStringIndent
case opBool:
return opStructFieldHeadBoolIndent
}
return opStructFieldHeadIndent
}
func (e *Encoder) optimizeStructHeaderOmitEmpty(op opType) opType {
switch op {
case opInt:
return opStructFieldHeadIntOmitEmpty
case opInt8:
return opStructFieldHeadInt8OmitEmpty
case opInt16:
return opStructFieldHeadInt16OmitEmpty
case opInt32:
return opStructFieldHeadInt32OmitEmpty
case opInt64:
return opStructFieldHeadInt64OmitEmpty
case opUint:
return opStructFieldHeadUintOmitEmpty
case opUint8:
return opStructFieldHeadUint8OmitEmpty
case opUint16:
return opStructFieldHeadUint16OmitEmpty
case opUint32:
return opStructFieldHeadUint32OmitEmpty
case opUint64:
return opStructFieldHeadUint64OmitEmpty
case opFloat32:
return opStructFieldHeadFloat32OmitEmpty
case opFloat64:
return opStructFieldHeadFloat64OmitEmpty
case opString:
return opStructFieldHeadStringOmitEmpty
case opBool:
return opStructFieldHeadBoolOmitEmpty
}
return opStructFieldHeadOmitEmpty
}
func (e *Encoder) optimizeStructHeader(op opType, isOmitEmpty, withIndent bool) opType {
if withIndent {
return e.optimizeStructHeaderIndent(op, isOmitEmpty)
}
if isOmitEmpty {
return e.optimizeStructHeaderOmitEmpty(op)
}
func (e *Encoder) typeToHeaderType(op opType) opType {
switch op {
case opInt:
return opStructFieldHeadInt
@ -601,118 +370,7 @@ func (e *Encoder) optimizeStructHeader(op opType, isOmitEmpty, withIndent bool)
return opStructFieldHead
}
func (e *Encoder) optimizeStructFieldOmitEmptyIndent(op opType) opType {
switch op {
case opInt:
return opStructFieldIntOmitEmptyIndent
case opInt8:
return opStructFieldInt8OmitEmptyIndent
case opInt16:
return opStructFieldInt16OmitEmptyIndent
case opInt32:
return opStructFieldInt32OmitEmptyIndent
case opInt64:
return opStructFieldInt64OmitEmptyIndent
case opUint:
return opStructFieldUintOmitEmptyIndent
case opUint8:
return opStructFieldUint8OmitEmptyIndent
case opUint16:
return opStructFieldUint16OmitEmptyIndent
case opUint32:
return opStructFieldUint32OmitEmptyIndent
case opUint64:
return opStructFieldUint64OmitEmptyIndent
case opFloat32:
return opStructFieldFloat32OmitEmptyIndent
case opFloat64:
return opStructFieldFloat64OmitEmptyIndent
case opString:
return opStructFieldStringOmitEmptyIndent
case opBool:
return opStructFieldBoolOmitEmptyIndent
}
return opStructFieldOmitEmptyIndent
}
func (e *Encoder) optimizeStructFieldIndent(op opType, isOmitEmpty bool) opType {
if isOmitEmpty {
return e.optimizeStructFieldOmitEmptyIndent(op)
}
switch op {
case opInt:
return opStructFieldIntIndent
case opInt8:
return opStructFieldInt8Indent
case opInt16:
return opStructFieldInt16Indent
case opInt32:
return opStructFieldInt32Indent
case opInt64:
return opStructFieldInt64Indent
case opUint:
return opStructFieldUintIndent
case opUint8:
return opStructFieldUint8Indent
case opUint16:
return opStructFieldUint16Indent
case opUint32:
return opStructFieldUint32Indent
case opUint64:
return opStructFieldUint64Indent
case opFloat32:
return opStructFieldFloat32Indent
case opFloat64:
return opStructFieldFloat64Indent
case opString:
return opStructFieldStringIndent
case opBool:
return opStructFieldBoolIndent
}
return opStructFieldIndent
}
func (e *Encoder) optimizeStructFieldOmitEmpty(op opType) opType {
switch op {
case opInt:
return opStructFieldIntOmitEmpty
case opInt8:
return opStructFieldInt8OmitEmpty
case opInt16:
return opStructFieldInt16OmitEmpty
case opInt32:
return opStructFieldInt32OmitEmpty
case opInt64:
return opStructFieldInt64OmitEmpty
case opUint:
return opStructFieldUintOmitEmpty
case opUint8:
return opStructFieldUint8OmitEmpty
case opUint16:
return opStructFieldUint16OmitEmpty
case opUint32:
return opStructFieldUint32OmitEmpty
case opUint64:
return opStructFieldUint64OmitEmpty
case opFloat32:
return opStructFieldFloat32OmitEmpty
case opFloat64:
return opStructFieldFloat64OmitEmpty
case opString:
return opStructFieldStringOmitEmpty
case opBool:
return opStructFieldBoolOmitEmpty
}
return opStructFieldOmitEmpty
}
func (e *Encoder) optimizeStructField(op opType, isOmitEmpty, withIndent bool) opType {
if withIndent {
return e.optimizeStructFieldIndent(op, isOmitEmpty)
}
if isOmitEmpty {
return e.optimizeStructFieldOmitEmpty(op)
}
func (e *Encoder) typeToFieldType(op opType) opType {
switch op {
case opInt:
return opStructFieldInt
@ -746,6 +404,28 @@ func (e *Encoder) optimizeStructField(op opType, isOmitEmpty, withIndent bool) o
return opStructField
}
func (e *Encoder) optimizeStructHeader(op opType, isOmitEmpty, withIndent bool) opType {
headType := e.typeToHeaderType(op)
if isOmitEmpty {
headType = headType.headToOmitEmptyHead()
}
if withIndent {
return headType.toIndent()
}
return headType
}
func (e *Encoder) optimizeStructField(op opType, isOmitEmpty, withIndent bool) opType {
fieldType := e.typeToFieldType(op)
if isOmitEmpty {
fieldType = fieldType.fieldToOmitEmptyField()
}
if withIndent {
return fieldType.toIndent()
}
return fieldType
}
func (e *Encoder) recursiveCode(typ *rtype, code *compiledCode) *opcode {
return (*opcode)(unsafe.Pointer(&recursiveCode{
opcodeHeader: &opcodeHeader{
@ -848,6 +528,19 @@ func (e *Encoder) compileStruct(typ *rtype, root, withIndent bool) (*opcode, err
if err != nil {
return nil, err
}
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()
}
}
key := fmt.Sprintf(`"%s":`, keyName)
fieldCode := &structFieldCode{
opcodeHeader: &opcodeHeader{
@ -855,8 +548,9 @@ func (e *Encoder) compileStruct(typ *rtype, root, withIndent bool) (*opcode, err
next: valueCode,
indent: e.indent,
},
key: []byte(key),
offset: field.Offset,
anonymousKey: field.Anonymous,
key: []byte(key),
offset: field.Offset,
}
if fieldIdx == 0 {
code = e.structHeader(fieldCode, valueCode, isOmitEmpty, withIndent)
@ -873,7 +567,13 @@ func (e *Encoder) compileStruct(typ *rtype, root, withIndent bool) (*opcode, err
}
e.indent--
structEndCode := newOpCode(opStructEnd, nil, e.indent, nil)
structEndCode := (*opcode)(unsafe.Pointer(&structFieldCode{
opcodeHeader: &opcodeHeader{
op: opStructEnd,
typ: nil,
indent: e.indent,
},
}))
structEndCode.next = newEndOp(e.indent)
if withIndent {
structEndCode.op = opStructEndIndent

File diff suppressed because it is too large Load Diff

2929
encode_optype.go Normal file

File diff suppressed because it is too large Load Diff

View File

@ -140,16 +140,23 @@ func Test_Marshal(t *testing.T) {
*T
B string `json:"b"`
}
v := &U{
T: &T{
A: "aaa",
},
B: "bbb",
}
fmt.Printf("v.T = %p v.T.A = %p\n", v.T, &v.T.A)
bytes, err := json.Marshal(v)
assertErr(t, err)
assertEq(t, "embedded", `{"a":"aaa","b":"bbb"}`, string(bytes))
t.Run("exists field", func(t *testing.T) {
bytes, err := json.Marshal(&U{
T: &T{
A: "aaa",
},
B: "bbb",
})
assertErr(t, err)
assertEq(t, "embedded", `{"a":"aaa","b":"bbb"}`, string(bytes))
})
t.Run("none field", func(t *testing.T) {
bytes, err := json.Marshal(&U{
B: "bbb",
})
assertErr(t, err)
assertEq(t, "embedded", `{"b":"bbb"}`, string(bytes))
})
})
t.Run("omitempty", func(t *testing.T) {
type T struct {

View File

@ -477,7 +477,9 @@ func (e *Encoder) run(code *opcode) error {
code = field.end.next
} else {
e.encodeByte('{')
e.encodeBytes(field.key)
if !field.anonymousKey {
e.encodeBytes(field.key)
}
code = field.next
code.ptr = field.ptr + field.offset
field.nextField.ptr = field.ptr
@ -494,8 +496,22 @@ func (e *Encoder) run(code *opcode) error {
} else {
e.encodeByte('{')
e.encodeBytes(field.key)
e.encodeInt(e.ptrToInt(field.ptr + field.offset))
field.nextField.ptr = field.ptr
e.encodeInt(e.ptrToInt(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrAnonymousHeadInt:
code.ptr = e.ptrToPtr(code.ptr)
fallthrough
case opStructFieldAnonymousHeadInt:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end
} else {
e.encodeBytes(field.key)
e.encodeInt(e.ptrToInt(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrHeadInt8:
@ -510,8 +526,22 @@ func (e *Encoder) run(code *opcode) error {
} else {
e.encodeByte('{')
e.encodeBytes(field.key)
e.encodeInt8(e.ptrToInt8(field.ptr + field.offset))
field.nextField.ptr = field.ptr
e.encodeInt8(e.ptrToInt8(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrAnonymousHeadInt8:
code.ptr = e.ptrToPtr(code.ptr)
fallthrough
case opStructFieldAnonymousHeadInt8:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end
} else {
e.encodeBytes(field.key)
e.encodeInt8(e.ptrToInt8(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrHeadInt16:
@ -526,8 +556,22 @@ func (e *Encoder) run(code *opcode) error {
} else {
e.encodeByte('{')
e.encodeBytes(field.key)
e.encodeInt16(e.ptrToInt16(field.ptr + field.offset))
field.nextField.ptr = field.ptr
e.encodeInt16(e.ptrToInt16(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrAnonymousHeadInt16:
code.ptr = e.ptrToPtr(code.ptr)
fallthrough
case opStructFieldAnonymousHeadInt16:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end
} else {
e.encodeBytes(field.key)
e.encodeInt16(e.ptrToInt16(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrHeadInt32:
@ -542,8 +586,22 @@ func (e *Encoder) run(code *opcode) error {
} else {
e.encodeByte('{')
e.encodeBytes(field.key)
e.encodeInt32(e.ptrToInt32(field.ptr + field.offset))
field.nextField.ptr = field.ptr
e.encodeInt32(e.ptrToInt32(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrAnonymousHeadInt32:
code.ptr = e.ptrToPtr(code.ptr)
fallthrough
case opStructFieldAnonymousHeadInt32:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end
} else {
e.encodeBytes(field.key)
e.encodeInt32(e.ptrToInt32(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrHeadInt64:
@ -558,8 +616,22 @@ func (e *Encoder) run(code *opcode) error {
} else {
e.encodeByte('{')
e.encodeBytes(field.key)
e.encodeInt64(e.ptrToInt64(field.ptr + field.offset))
field.nextField.ptr = field.ptr
e.encodeInt64(e.ptrToInt64(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrAnonymousHeadInt64:
code.ptr = e.ptrToPtr(code.ptr)
fallthrough
case opStructFieldAnonymousHeadInt64:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end
} else {
e.encodeBytes(field.key)
e.encodeInt64(e.ptrToInt64(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrHeadUint:
@ -574,8 +646,22 @@ func (e *Encoder) run(code *opcode) error {
} else {
e.encodeByte('{')
e.encodeBytes(field.key)
e.encodeUint(e.ptrToUint(field.ptr + field.offset))
field.nextField.ptr = field.ptr
e.encodeUint(e.ptrToUint(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrAnonymousHeadUint:
code.ptr = e.ptrToPtr(code.ptr)
fallthrough
case opStructFieldAnonymousHeadUint:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end
} else {
e.encodeBytes(field.key)
e.encodeUint(e.ptrToUint(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrHeadUint8:
@ -590,8 +676,22 @@ func (e *Encoder) run(code *opcode) error {
} else {
e.encodeByte('{')
e.encodeBytes(field.key)
e.encodeUint8(e.ptrToUint8(field.ptr + field.offset))
field.nextField.ptr = field.ptr
e.encodeUint8(e.ptrToUint8(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrAnonymousHeadUint8:
code.ptr = e.ptrToPtr(code.ptr)
fallthrough
case opStructFieldAnonymousHeadUint8:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end
} else {
e.encodeBytes(field.key)
e.encodeUint8(e.ptrToUint8(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrHeadUint16:
@ -606,8 +706,22 @@ func (e *Encoder) run(code *opcode) error {
} else {
e.encodeByte('{')
e.encodeBytes(field.key)
e.encodeUint16(e.ptrToUint16(field.ptr + field.offset))
field.nextField.ptr = field.ptr
e.encodeUint16(e.ptrToUint16(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrAnonymousHeadUint16:
code.ptr = e.ptrToPtr(code.ptr)
fallthrough
case opStructFieldAnonymousHeadUint16:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end
} else {
e.encodeBytes(field.key)
e.encodeUint16(e.ptrToUint16(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrHeadUint32:
@ -622,8 +736,22 @@ func (e *Encoder) run(code *opcode) error {
} else {
e.encodeByte('{')
e.encodeBytes(field.key)
e.encodeUint32(e.ptrToUint32(field.ptr + field.offset))
field.nextField.ptr = field.ptr
e.encodeUint32(e.ptrToUint32(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrAnonymousHeadUint32:
code.ptr = e.ptrToPtr(code.ptr)
fallthrough
case opStructFieldAnonymousHeadUint32:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end
} else {
e.encodeBytes(field.key)
e.encodeUint32(e.ptrToUint32(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrHeadUint64:
@ -638,8 +766,22 @@ func (e *Encoder) run(code *opcode) error {
} else {
e.encodeByte('{')
e.encodeBytes(field.key)
e.encodeUint64(e.ptrToUint64(field.ptr + field.offset))
field.nextField.ptr = field.ptr
e.encodeUint64(e.ptrToUint64(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrAnonymousHeadUint64:
code.ptr = e.ptrToPtr(code.ptr)
fallthrough
case opStructFieldAnonymousHeadUint64:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end
} else {
e.encodeBytes(field.key)
e.encodeUint64(e.ptrToUint64(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrHeadFloat32:
@ -654,8 +796,22 @@ func (e *Encoder) run(code *opcode) error {
} else {
e.encodeByte('{')
e.encodeBytes(field.key)
e.encodeFloat32(e.ptrToFloat32(field.ptr + field.offset))
field.nextField.ptr = field.ptr
e.encodeFloat32(e.ptrToFloat32(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrAnonymousHeadFloat32:
code.ptr = e.ptrToPtr(code.ptr)
fallthrough
case opStructFieldAnonymousHeadFloat32:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end
} else {
e.encodeBytes(field.key)
e.encodeFloat32(e.ptrToFloat32(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrHeadFloat64:
@ -668,7 +824,7 @@ func (e *Encoder) run(code *opcode) error {
e.encodeNull()
code = field.end
} else {
v := e.ptrToFloat64(field.ptr + field.offset)
v := e.ptrToFloat64(ptr + field.offset)
if math.IsInf(v, 0) || math.IsNaN(v) {
return &UnsupportedValueError{
Value: reflect.ValueOf(v),
@ -678,7 +834,28 @@ func (e *Encoder) run(code *opcode) error {
e.encodeByte('{')
e.encodeBytes(field.key)
e.encodeFloat64(v)
field.nextField.ptr = field.ptr
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrAnonymousHeadFloat64:
code.ptr = e.ptrToPtr(code.ptr)
fallthrough
case opStructFieldAnonymousHeadFloat64:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end
} else {
v := e.ptrToFloat64(ptr + field.offset)
if math.IsInf(v, 0) || math.IsNaN(v) {
return &UnsupportedValueError{
Value: reflect.ValueOf(v),
Str: strconv.FormatFloat(v, 'g', -1, 64),
}
}
e.encodeBytes(field.key)
e.encodeFloat64(v)
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrHeadString:
@ -693,8 +870,22 @@ func (e *Encoder) run(code *opcode) error {
} else {
e.encodeByte('{')
e.encodeBytes(field.key)
e.encodeString(e.ptrToString(field.ptr + field.offset))
field.nextField.ptr = field.ptr
e.encodeString(e.ptrToString(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrAnonymousHeadString:
code.ptr = e.ptrToPtr(code.ptr)
fallthrough
case opStructFieldAnonymousHeadString:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end
} else {
e.encodeBytes(field.key)
e.encodeString(e.ptrToString(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrHeadBool:
@ -709,11 +900,24 @@ func (e *Encoder) run(code *opcode) error {
} else {
e.encodeByte('{')
e.encodeBytes(field.key)
e.encodeBool(e.ptrToBool(field.ptr + field.offset))
field.nextField.ptr = field.ptr
e.encodeBool(e.ptrToBool(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrAnonymousHeadBool:
code.ptr = e.ptrToPtr(code.ptr)
fallthrough
case opStructFieldAnonymousHeadBool:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end
} else {
e.encodeBytes(field.key)
e.encodeBool(e.ptrToBool(ptr + field.offset))
field.nextField.ptr = ptr
code = field.next
}
case opStructFieldPtrHeadIndent:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
@ -1046,12 +1250,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadIntOmitEmpty:
case opStructFieldPtrHeadOmitEmptyInt:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadIntOmitEmpty:
case opStructFieldHeadOmitEmptyInt:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1071,12 +1275,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadInt8OmitEmpty:
case opStructFieldPtrHeadOmitEmptyInt8:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadInt8OmitEmpty:
case opStructFieldHeadOmitEmptyInt8:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1096,12 +1300,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadInt16OmitEmpty:
case opStructFieldPtrHeadOmitEmptyInt16:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadInt16OmitEmpty:
case opStructFieldHeadOmitEmptyInt16:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1121,12 +1325,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadInt32OmitEmpty:
case opStructFieldPtrHeadOmitEmptyInt32:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadInt32OmitEmpty:
case opStructFieldHeadOmitEmptyInt32:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1146,12 +1350,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadInt64OmitEmpty:
case opStructFieldPtrHeadOmitEmptyInt64:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadInt64OmitEmpty:
case opStructFieldHeadOmitEmptyInt64:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1171,12 +1375,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadUintOmitEmpty:
case opStructFieldPtrHeadOmitEmptyUint:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadUintOmitEmpty:
case opStructFieldHeadOmitEmptyUint:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1196,12 +1400,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadUint8OmitEmpty:
case opStructFieldPtrHeadOmitEmptyUint8:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadUint8OmitEmpty:
case opStructFieldHeadOmitEmptyUint8:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1221,12 +1425,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadUint16OmitEmpty:
case opStructFieldPtrHeadOmitEmptyUint16:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadUint16OmitEmpty:
case opStructFieldHeadOmitEmptyUint16:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1246,12 +1450,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadUint32OmitEmpty:
case opStructFieldPtrHeadOmitEmptyUint32:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadUint32OmitEmpty:
case opStructFieldHeadOmitEmptyUint32:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1271,12 +1475,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadUint64OmitEmpty:
case opStructFieldPtrHeadOmitEmptyUint64:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadUint64OmitEmpty:
case opStructFieldHeadOmitEmptyUint64:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1296,12 +1500,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadFloat32OmitEmpty:
case opStructFieldPtrHeadOmitEmptyFloat32:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadFloat32OmitEmpty:
case opStructFieldHeadOmitEmptyFloat32:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1321,12 +1525,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadFloat64OmitEmpty:
case opStructFieldPtrHeadOmitEmptyFloat64:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadFloat64OmitEmpty:
case opStructFieldHeadOmitEmptyFloat64:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1352,12 +1556,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadStringOmitEmpty:
case opStructFieldPtrHeadOmitEmptyString:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadStringOmitEmpty:
case opStructFieldHeadOmitEmptyString:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1377,12 +1581,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadBoolOmitEmpty:
case opStructFieldPtrHeadOmitEmptyBool:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadBoolOmitEmpty:
case opStructFieldHeadOmitEmptyBool:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1430,12 +1634,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadIntOmitEmptyIndent:
case opStructFieldPtrHeadOmitEmptyIntIndent:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadIntOmitEmptyIndent:
case opStructFieldHeadOmitEmptyIntIndent:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1457,12 +1661,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadInt8OmitEmptyIndent:
case opStructFieldPtrHeadOmitEmptyInt8Indent:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadInt8OmitEmptyIndent:
case opStructFieldHeadOmitEmptyInt8Indent:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1484,12 +1688,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadInt16OmitEmptyIndent:
case opStructFieldPtrHeadOmitEmptyInt16Indent:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadInt16OmitEmptyIndent:
case opStructFieldHeadOmitEmptyInt16Indent:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1511,12 +1715,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadInt32OmitEmptyIndent:
case opStructFieldPtrHeadOmitEmptyInt32Indent:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadInt32OmitEmptyIndent:
case opStructFieldHeadOmitEmptyInt32Indent:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1538,12 +1742,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadInt64OmitEmptyIndent:
case opStructFieldPtrHeadOmitEmptyInt64Indent:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadInt64OmitEmptyIndent:
case opStructFieldHeadOmitEmptyInt64Indent:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1565,12 +1769,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadUintOmitEmptyIndent:
case opStructFieldPtrHeadOmitEmptyUintIndent:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadUintOmitEmptyIndent:
case opStructFieldHeadOmitEmptyUintIndent:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1592,12 +1796,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadUint8OmitEmptyIndent:
case opStructFieldPtrHeadOmitEmptyUint8Indent:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadUint8OmitEmptyIndent:
case opStructFieldHeadOmitEmptyUint8Indent:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1619,12 +1823,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadUint16OmitEmptyIndent:
case opStructFieldPtrHeadOmitEmptyUint16Indent:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadUint16OmitEmptyIndent:
case opStructFieldHeadOmitEmptyUint16Indent:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1646,12 +1850,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadUint32OmitEmptyIndent:
case opStructFieldPtrHeadOmitEmptyUint32Indent:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadUint32OmitEmptyIndent:
case opStructFieldHeadOmitEmptyUint32Indent:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1673,12 +1877,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadUint64OmitEmptyIndent:
case opStructFieldPtrHeadOmitEmptyUint64Indent:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadUint64OmitEmptyIndent:
case opStructFieldHeadOmitEmptyUint64Indent:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1700,12 +1904,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadFloat32OmitEmptyIndent:
case opStructFieldPtrHeadOmitEmptyFloat32Indent:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadFloat32OmitEmptyIndent:
case opStructFieldHeadOmitEmptyFloat32Indent:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1727,12 +1931,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadFloat64OmitEmptyIndent:
case opStructFieldPtrHeadOmitEmptyFloat64Indent:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadFloat64OmitEmptyIndent:
case opStructFieldHeadOmitEmptyFloat64Indent:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1760,12 +1964,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadStringOmitEmptyIndent:
case opStructFieldPtrHeadOmitEmptyStringIndent:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadStringOmitEmptyIndent:
case opStructFieldHeadOmitEmptyStringIndent:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -1787,12 +1991,12 @@ func (e *Encoder) run(code *opcode) error {
}
field.nextField.ptr = field.ptr
}
case opStructFieldPtrHeadBoolOmitEmptyIndent:
case opStructFieldPtrHeadOmitEmptyBoolIndent:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldHeadBoolOmitEmptyIndent:
case opStructFieldHeadOmitEmptyBoolIndent:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
@ -2143,7 +2347,7 @@ func (e *Encoder) run(code *opcode) error {
code.ptr = p
}
c.nextField.ptr = c.ptr
case opStructFieldIntOmitEmpty:
case opStructFieldOmitEmptyInt:
c := code.toStructFieldCode()
v := e.ptrToInt(c.ptr + c.offset)
if v != 0 {
@ -2155,7 +2359,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldInt8OmitEmpty:
case opStructFieldOmitEmptyInt8:
c := code.toStructFieldCode()
v := e.ptrToInt8(c.ptr + c.offset)
if v != 0 {
@ -2167,7 +2371,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldInt16OmitEmpty:
case opStructFieldOmitEmptyInt16:
c := code.toStructFieldCode()
v := e.ptrToInt16(c.ptr + c.offset)
if v != 0 {
@ -2179,7 +2383,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldInt32OmitEmpty:
case opStructFieldOmitEmptyInt32:
c := code.toStructFieldCode()
v := e.ptrToInt32(c.ptr + c.offset)
if v != 0 {
@ -2191,7 +2395,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldInt64OmitEmpty:
case opStructFieldOmitEmptyInt64:
c := code.toStructFieldCode()
v := e.ptrToInt64(c.ptr + c.offset)
if v != 0 {
@ -2203,7 +2407,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldUintOmitEmpty:
case opStructFieldOmitEmptyUint:
c := code.toStructFieldCode()
v := e.ptrToUint(c.ptr + c.offset)
if v != 0 {
@ -2215,7 +2419,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldUint8OmitEmpty:
case opStructFieldOmitEmptyUint8:
c := code.toStructFieldCode()
v := e.ptrToUint8(c.ptr + c.offset)
if v != 0 {
@ -2227,7 +2431,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldUint16OmitEmpty:
case opStructFieldOmitEmptyUint16:
c := code.toStructFieldCode()
v := e.ptrToUint16(c.ptr + c.offset)
if v != 0 {
@ -2239,7 +2443,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldUint32OmitEmpty:
case opStructFieldOmitEmptyUint32:
c := code.toStructFieldCode()
v := e.ptrToUint32(c.ptr + c.offset)
if v != 0 {
@ -2251,7 +2455,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldUint64OmitEmpty:
case opStructFieldOmitEmptyUint64:
c := code.toStructFieldCode()
v := e.ptrToUint64(c.ptr + c.offset)
if v != 0 {
@ -2263,7 +2467,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldFloat32OmitEmpty:
case opStructFieldOmitEmptyFloat32:
c := code.toStructFieldCode()
v := e.ptrToFloat32(c.ptr + c.offset)
if v != 0 {
@ -2275,7 +2479,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldFloat64OmitEmpty:
case opStructFieldOmitEmptyFloat64:
c := code.toStructFieldCode()
v := e.ptrToFloat64(c.ptr + c.offset)
if v != 0 {
@ -2293,7 +2497,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldStringOmitEmpty:
case opStructFieldOmitEmptyString:
c := code.toStructFieldCode()
v := e.ptrToString(c.ptr + c.offset)
if v != "" {
@ -2305,7 +2509,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldBoolOmitEmpty:
case opStructFieldOmitEmptyBool:
c := code.toStructFieldCode()
v := e.ptrToBool(c.ptr + c.offset)
if v {
@ -2334,7 +2538,7 @@ func (e *Encoder) run(code *opcode) error {
code.ptr = p
}
c.nextField.ptr = c.ptr
case opStructFieldIntOmitEmptyIndent:
case opStructFieldOmitEmptyIntIndent:
c := code.toStructFieldCode()
v := e.ptrToInt(c.ptr + c.offset)
if v != 0 {
@ -2348,7 +2552,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldInt8OmitEmptyIndent:
case opStructFieldOmitEmptyInt8Indent:
c := code.toStructFieldCode()
v := e.ptrToInt8(c.ptr + c.offset)
if v != 0 {
@ -2362,7 +2566,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldInt16OmitEmptyIndent:
case opStructFieldOmitEmptyInt16Indent:
c := code.toStructFieldCode()
v := e.ptrToInt16(c.ptr + c.offset)
if v != 0 {
@ -2376,7 +2580,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldInt32OmitEmptyIndent:
case opStructFieldOmitEmptyInt32Indent:
c := code.toStructFieldCode()
v := e.ptrToInt32(c.ptr + c.offset)
if v != 0 {
@ -2390,7 +2594,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldInt64OmitEmptyIndent:
case opStructFieldOmitEmptyInt64Indent:
c := code.toStructFieldCode()
v := e.ptrToInt64(c.ptr + c.offset)
if v != 0 {
@ -2404,7 +2608,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldUintOmitEmptyIndent:
case opStructFieldOmitEmptyUintIndent:
c := code.toStructFieldCode()
v := e.ptrToUint(c.ptr + c.offset)
if v != 0 {
@ -2418,7 +2622,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldUint8OmitEmptyIndent:
case opStructFieldOmitEmptyUint8Indent:
c := code.toStructFieldCode()
v := e.ptrToUint8(c.ptr + c.offset)
if v != 0 {
@ -2432,7 +2636,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldUint16OmitEmptyIndent:
case opStructFieldOmitEmptyUint16Indent:
c := code.toStructFieldCode()
v := e.ptrToUint16(c.ptr + c.offset)
if v != 0 {
@ -2446,7 +2650,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldUint32OmitEmptyIndent:
case opStructFieldOmitEmptyUint32Indent:
c := code.toStructFieldCode()
v := e.ptrToUint32(c.ptr + c.offset)
if v != 0 {
@ -2460,7 +2664,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldUint64OmitEmptyIndent:
case opStructFieldOmitEmptyUint64Indent:
c := code.toStructFieldCode()
v := e.ptrToUint64(c.ptr + c.offset)
if v != 0 {
@ -2474,7 +2678,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldFloat32OmitEmptyIndent:
case opStructFieldOmitEmptyFloat32Indent:
c := code.toStructFieldCode()
v := e.ptrToFloat32(c.ptr + c.offset)
if v != 0 {
@ -2488,7 +2692,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldFloat64OmitEmptyIndent:
case opStructFieldOmitEmptyFloat64Indent:
c := code.toStructFieldCode()
v := e.ptrToFloat64(c.ptr + c.offset)
if v != 0 {
@ -2508,7 +2712,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldStringOmitEmptyIndent:
case opStructFieldOmitEmptyStringIndent:
c := code.toStructFieldCode()
v := e.ptrToString(c.ptr + c.offset)
if v != "" {
@ -2522,7 +2726,7 @@ func (e *Encoder) run(code *opcode) error {
}
code = code.next
code.ptr = c.ptr
case opStructFieldBoolOmitEmptyIndent:
case opStructFieldOmitEmptyBoolIndent:
c := code.toStructFieldCode()
v := e.ptrToBool(c.ptr + c.offset)
if v {
@ -2539,6 +2743,8 @@ func (e *Encoder) run(code *opcode) error {
case opStructEnd:
e.encodeByte('}')
code = code.next
case opStructAnonymousEnd:
code = code.next
case opStructEndIndent:
e.encodeByte('\n')
e.encodeIndent(code.indent)