Add AnonymousOmitEmpty type

This commit is contained in:
Masaaki Goshima 2020-08-15 18:17:48 +09:00
parent 025ac2a53b
commit 64cd28839f
4 changed files with 1348 additions and 354 deletions

View File

@ -17,12 +17,14 @@ type opType struct {
} }
type headType struct { type headType struct {
Head string Head string
PtrHead string PtrHead string
AnonymousHead string AnonymousHead string
AnonymousPtrHead string AnonymousPtrHead string
OmitEmptyHead string OmitEmptyHead string
OmitEmptyPtrHead string OmitEmptyPtrHead string
AnonymousOmitEmptyHead string
AnonymousOmitEmptyPtrHead string
} }
type fieldType struct { type fieldType struct {
@ -89,6 +91,8 @@ func (t opType) headToPtrHead() opType {
return op{{ $type.AnonymousPtrHead }} return op{{ $type.AnonymousPtrHead }}
case op{{ $type.OmitEmptyHead }}: case op{{ $type.OmitEmptyHead }}:
return op{{ $type.OmitEmptyPtrHead }} return op{{ $type.OmitEmptyPtrHead }}
case op{{ $type.AnonymousOmitEmptyHead }}:
return op{{ $type.AnonymousOmitEmptyPtrHead }}
{{- end }} {{- end }}
} }
return t return t
@ -101,6 +105,10 @@ func (t opType) headToAnonymousHead() opType {
return op{{ $type.AnonymousHead }} return op{{ $type.AnonymousHead }}
case op{{ $type.PtrHead }}: case op{{ $type.PtrHead }}:
return op{{ $type.AnonymousPtrHead }} return op{{ $type.AnonymousPtrHead }}
case op{{ $type.OmitEmptyHead }}:
return op{{ $type.AnonymousOmitEmptyHead }}
case op{{ $type.OmitEmptyPtrHead }}:
return op{{ $type.AnonymousOmitEmptyPtrHead }}
{{- end }} {{- end }}
} }
return t return t
@ -127,6 +135,8 @@ func (t opType) ptrHeadToHead() opType {
return op{{ $type.AnonymousHead }} return op{{ $type.AnonymousHead }}
case op{{ $type.OmitEmptyPtrHead }}: case op{{ $type.OmitEmptyPtrHead }}:
return op{{ $type.OmitEmptyHead }} return op{{ $type.OmitEmptyHead }}
case op{{ $type.AnonymousOmitEmptyPtrHead }}:
return op{{ $type.AnonymousOmitEmptyHead }}
{{- end }} {{- end }}
} }
return t return t
@ -191,6 +201,8 @@ func (t opType) fieldToOmitEmptyField() opType {
{"StructFieldHead", "StructFieldHeadIndent", "StructField"}, {"StructFieldHead", "StructFieldHeadIndent", "StructField"},
{"StructFieldHeadOmitEmpty", "StructFieldHeadOmitEmptyIndent", "StructField"}, {"StructFieldHeadOmitEmpty", "StructFieldHeadOmitEmptyIndent", "StructField"},
{"StructFieldAnonymousHead", "StructFieldAnonymousHeadIndent", "StructField"}, {"StructFieldAnonymousHead", "StructFieldAnonymousHeadIndent", "StructField"},
{"StructFieldAnonymousHeadOmitEmpty", "StructFieldAnonymousHeadOmitEmptyIndent", "StructField"},
{"StructFieldPtrAnonymousHeadOmitEmpty", "StructFieldPtrAnonymousHeadOmitEmptyIndent", "StructField"},
{"StructFieldPtrHead", "StructFieldPtrHeadIndent", "StructField"}, {"StructFieldPtrHead", "StructFieldPtrHeadIndent", "StructField"},
{"StructFieldPtrHeadOmitEmpty", "StructFieldPtrHeadOmitEmptyIndent", "StructField"}, {"StructFieldPtrHeadOmitEmpty", "StructFieldPtrHeadOmitEmptyIndent", "StructField"},
{"StructFieldPtrAnonymousHead", "StructFieldPtrAnonymousHeadIndent", "StructField"}, {"StructFieldPtrAnonymousHead", "StructFieldPtrAnonymousHeadIndent", "StructField"},
@ -211,9 +223,11 @@ func (t opType) fieldToOmitEmptyField() opType {
"StructFieldHead", "StructFieldHead",
"StructFieldHeadOmitEmpty", "StructFieldHeadOmitEmpty",
"StructFieldAnonymousHead", "StructFieldAnonymousHead",
"StructFieldAnonymousHeadOmitEmpty",
"StructFieldPtrHead", "StructFieldPtrHead",
"StructFieldPtrHeadOmitEmpty", "StructFieldPtrHeadOmitEmpty",
"StructFieldPtrAnonymousHead", "StructFieldPtrAnonymousHead",
"StructFieldPtrAnonymousHeadOmitEmpty",
"StructField", "StructField",
"StructFieldOmitEmpty", "StructFieldOmitEmpty",
} { } {
@ -233,32 +247,38 @@ func (t opType) fieldToOmitEmptyField() opType {
}) })
} }
base := headType{ base := headType{
Head: "StructFieldHead", Head: "StructFieldHead",
PtrHead: "StructFieldPtrHead", PtrHead: "StructFieldPtrHead",
AnonymousHead: "StructFieldAnonymousHead", AnonymousHead: "StructFieldAnonymousHead",
AnonymousPtrHead: "StructFieldPtrAnonymousHead", AnonymousPtrHead: "StructFieldPtrAnonymousHead",
OmitEmptyHead: "StructFieldHeadOmitEmpty", OmitEmptyHead: "StructFieldHeadOmitEmpty",
OmitEmptyPtrHead: "StructFieldPtrHeadOmitEmpty", OmitEmptyPtrHead: "StructFieldPtrHeadOmitEmpty",
AnonymousOmitEmptyHead: "StructFieldAnonymousHeadOmitEmpty",
AnonymousOmitEmptyPtrHead: "StructFieldPtrAnonymousHeadOmitEmpty",
} }
headTypes := []headType{base} headTypes := []headType{base}
for _, prim := range primitiveTypesUpper { for _, prim := range primitiveTypesUpper {
headTypes = append(headTypes, headType{ headTypes = append(headTypes, headType{
Head: fmt.Sprintf("%s%s", base.Head, prim), Head: fmt.Sprintf("%s%s", base.Head, prim),
PtrHead: fmt.Sprintf("%s%s", base.PtrHead, prim), PtrHead: fmt.Sprintf("%s%s", base.PtrHead, prim),
AnonymousHead: fmt.Sprintf("%s%s", base.AnonymousHead, prim), AnonymousHead: fmt.Sprintf("%s%s", base.AnonymousHead, prim),
AnonymousPtrHead: fmt.Sprintf("%s%s", base.AnonymousPtrHead, prim), AnonymousPtrHead: fmt.Sprintf("%s%s", base.AnonymousPtrHead, prim),
OmitEmptyHead: fmt.Sprintf("%s%s", base.OmitEmptyHead, prim), OmitEmptyHead: fmt.Sprintf("%s%s", base.OmitEmptyHead, prim),
OmitEmptyPtrHead: fmt.Sprintf("%s%s", base.OmitEmptyPtrHead, prim), OmitEmptyPtrHead: fmt.Sprintf("%s%s", base.OmitEmptyPtrHead, prim),
AnonymousOmitEmptyHead: fmt.Sprintf("%s%s", base.AnonymousOmitEmptyHead, prim),
AnonymousOmitEmptyPtrHead: fmt.Sprintf("%s%s", base.AnonymousOmitEmptyPtrHead, prim),
}) })
} }
for _, typ := range headTypes { for _, typ := range headTypes {
headTypes = append(headTypes, headType{ headTypes = append(headTypes, headType{
Head: fmt.Sprintf("%sIndent", typ.Head), Head: fmt.Sprintf("%sIndent", typ.Head),
PtrHead: fmt.Sprintf("%sIndent", typ.PtrHead), PtrHead: fmt.Sprintf("%sIndent", typ.PtrHead),
AnonymousHead: fmt.Sprintf("%sIndent", typ.AnonymousHead), AnonymousHead: fmt.Sprintf("%sIndent", typ.AnonymousHead),
AnonymousPtrHead: fmt.Sprintf("%sIndent", typ.AnonymousPtrHead), AnonymousPtrHead: fmt.Sprintf("%sIndent", typ.AnonymousPtrHead),
OmitEmptyHead: fmt.Sprintf("%sIndent", typ.OmitEmptyHead), OmitEmptyHead: fmt.Sprintf("%sIndent", typ.OmitEmptyHead),
OmitEmptyPtrHead: fmt.Sprintf("%sIndent", typ.OmitEmptyPtrHead), OmitEmptyPtrHead: fmt.Sprintf("%sIndent", typ.OmitEmptyPtrHead),
AnonymousOmitEmptyHead: fmt.Sprintf("%sIndent", typ.AnonymousOmitEmptyHead),
AnonymousOmitEmptyPtrHead: fmt.Sprintf("%sIndent", typ.AnonymousOmitEmptyPtrHead),
}) })
} }

File diff suppressed because it is too large Load Diff

View File

@ -140,6 +140,13 @@ func Test_Marshal(t *testing.T) {
*T *T
B string `json:"b"` B string `json:"b"`
} }
type T2 struct {
A string `json:"a,omitempty"`
}
type U2 struct {
*T2
B string `json:"b,omitempty"`
}
t.Run("exists field", func(t *testing.T) { t.Run("exists field", func(t *testing.T) {
bytes, err := json.Marshal(&U{ bytes, err := json.Marshal(&U{
T: &T{ T: &T{
@ -149,6 +156,16 @@ func Test_Marshal(t *testing.T) {
}) })
assertErr(t, err) assertErr(t, err)
assertEq(t, "embedded", `{"a":"aaa","b":"bbb"}`, string(bytes)) assertEq(t, "embedded", `{"a":"aaa","b":"bbb"}`, string(bytes))
t.Run("omitempty", func(t *testing.T) {
bytes, err := json.Marshal(&U2{
T2: &T2{
A: "aaa",
},
B: "bbb",
})
assertErr(t, err)
assertEq(t, "embedded", `{"a":"aaa","b":"bbb"}`, string(bytes))
})
}) })
t.Run("none field", func(t *testing.T) { t.Run("none field", func(t *testing.T) {
bytes, err := json.Marshal(&U{ bytes, err := json.Marshal(&U{
@ -156,6 +173,13 @@ func Test_Marshal(t *testing.T) {
}) })
assertErr(t, err) assertErr(t, err)
assertEq(t, "embedded", `{"b":"bbb"}`, string(bytes)) assertEq(t, "embedded", `{"b":"bbb"}`, string(bytes))
t.Run("omitempty", func(t *testing.T) {
bytes, err := json.Marshal(&U2{
B: "bbb",
})
assertErr(t, err)
assertEq(t, "embedded", `{"b":"bbb"}`, string(bytes))
})
}) })
}) })
t.Run("omitempty", func(t *testing.T) { t.Run("omitempty", func(t *testing.T) {

View File

@ -1237,19 +1237,38 @@ func (e *Encoder) run(code *opcode) error {
e.encodeNull() e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent)
e.encodeByte('{') e.encodeByte('{')
p := ptr + field.offset p := ptr + field.offset
if p == 0 || *(*uintptr)(unsafe.Pointer(p)) == 0 { if p == 0 || *(*uintptr)(unsafe.Pointer(p)) == 0 {
code = field.nextField code = field.nextField
} else { } else {
e.encodeIndent(code.indent + 1)
e.encodeBytes(field.key) e.encodeBytes(field.key)
code = field.next code = field.next
code.ptr = p code.ptr = p
} }
field.nextField.ptr = field.ptr field.nextField.ptr = field.ptr
} }
case opStructFieldPtrAnonymousHeadOmitEmpty:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldAnonymousHeadOmitEmpty:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end.next
} else {
p := ptr + field.offset
if p == 0 || *(*uintptr)(unsafe.Pointer(p)) == 0 {
code = field.nextField
} else {
e.encodeBytes(field.key)
code = field.next
code.ptr = p
}
field.nextField.ptr = ptr
}
case opStructFieldPtrHeadOmitEmptyInt: case opStructFieldPtrHeadOmitEmptyInt:
if code.ptr != 0 { if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr) code.ptr = e.ptrToPtr(code.ptr)
@ -1262,19 +1281,38 @@ func (e *Encoder) run(code *opcode) error {
e.encodeNull() e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent)
e.encodeByte('{') e.encodeByte('{')
v := e.ptrToInt(ptr + field.offset) v := e.ptrToInt(ptr + field.offset)
if v == 0 { if v == 0 {
code = field.nextField code = field.nextField
} else { } else {
e.encodeIndent(code.indent + 1)
e.encodeBytes(field.key) e.encodeBytes(field.key)
e.encodeInt(v) e.encodeInt(v)
code = field.next code = field.next
} }
field.nextField.ptr = field.ptr field.nextField.ptr = field.ptr
} }
case opStructFieldPtrAnonymousHeadOmitEmptyInt:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldAnonymousHeadOmitEmptyInt:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end.next
} else {
v := e.ptrToInt(ptr + field.offset)
if v == 0 {
code = field.nextField
} else {
e.encodeBytes(field.key)
e.encodeInt(v)
code = field.next
}
field.nextField.ptr = ptr
}
case opStructFieldPtrHeadOmitEmptyInt8: case opStructFieldPtrHeadOmitEmptyInt8:
if code.ptr != 0 { if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr) code.ptr = e.ptrToPtr(code.ptr)
@ -1287,18 +1325,37 @@ func (e *Encoder) run(code *opcode) error {
e.encodeNull() e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent)
e.encodeByte('{') e.encodeByte('{')
v := e.ptrToInt8(ptr + field.offset) v := e.ptrToInt8(ptr + field.offset)
if v == 0 { if v == 0 {
code = field.nextField code = field.nextField
} else { } else {
e.encodeIndent(code.indent + 1)
e.encodeBytes(field.key) e.encodeBytes(field.key)
e.encodeInt8(v) e.encodeInt8(v)
code = field.next code = field.next
} }
field.nextField.ptr = field.ptr field.nextField.ptr = ptr
}
case opStructFieldPtrAnonymousHeadOmitEmptyInt8:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldAnonymousHeadOmitEmptyInt8:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end.next
} else {
v := e.ptrToInt8(ptr + field.offset)
if v == 0 {
code = field.nextField
} else {
e.encodeBytes(field.key)
e.encodeInt8(v)
code = field.next
}
field.nextField.ptr = ptr
} }
case opStructFieldPtrHeadOmitEmptyInt16: case opStructFieldPtrHeadOmitEmptyInt16:
if code.ptr != 0 { if code.ptr != 0 {
@ -1312,19 +1369,38 @@ func (e *Encoder) run(code *opcode) error {
e.encodeNull() e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent)
e.encodeByte('{') e.encodeByte('{')
v := e.ptrToInt16(ptr + field.offset) v := e.ptrToInt16(ptr + field.offset)
if v == 0 { if v == 0 {
code = field.nextField code = field.nextField
} else { } else {
e.encodeIndent(code.indent + 1)
e.encodeBytes(field.key) e.encodeBytes(field.key)
e.encodeInt16(v) e.encodeInt16(v)
code = field.next code = field.next
} }
field.nextField.ptr = field.ptr field.nextField.ptr = field.ptr
} }
case opStructFieldPtrAnonymousHeadOmitEmptyInt16:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldAnonymousHeadOmitEmptyInt16:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end.next
} else {
v := e.ptrToInt16(ptr + field.offset)
if v == 0 {
code = field.nextField
} else {
e.encodeBytes(field.key)
e.encodeInt16(v)
code = field.next
}
field.nextField.ptr = ptr
}
case opStructFieldPtrHeadOmitEmptyInt32: case opStructFieldPtrHeadOmitEmptyInt32:
if code.ptr != 0 { if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr) code.ptr = e.ptrToPtr(code.ptr)
@ -1337,19 +1413,38 @@ func (e *Encoder) run(code *opcode) error {
e.encodeNull() e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent)
e.encodeByte('{') e.encodeByte('{')
v := e.ptrToInt32(ptr + field.offset) v := e.ptrToInt32(ptr + field.offset)
if v == 0 { if v == 0 {
code = field.nextField code = field.nextField
} else { } else {
e.encodeIndent(code.indent + 1)
e.encodeBytes(field.key) e.encodeBytes(field.key)
e.encodeInt32(v) e.encodeInt32(v)
code = field.next code = field.next
} }
field.nextField.ptr = field.ptr field.nextField.ptr = field.ptr
} }
case opStructFieldPtrAnonymousHeadOmitEmptyInt32:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldAnonymousHeadOmitEmptyInt32:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end.next
} else {
v := e.ptrToInt32(ptr + field.offset)
if v == 0 {
code = field.nextField
} else {
e.encodeBytes(field.key)
e.encodeInt32(v)
code = field.next
}
field.nextField.ptr = ptr
}
case opStructFieldPtrHeadOmitEmptyInt64: case opStructFieldPtrHeadOmitEmptyInt64:
if code.ptr != 0 { if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr) code.ptr = e.ptrToPtr(code.ptr)
@ -1362,18 +1457,37 @@ func (e *Encoder) run(code *opcode) error {
e.encodeNull() e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent)
e.encodeByte('{') e.encodeByte('{')
v := e.ptrToInt64(ptr + field.offset) v := e.ptrToInt64(ptr + field.offset)
if v == 0 { if v == 0 {
code = field.nextField code = field.nextField
} else { } else {
e.encodeIndent(code.indent + 1)
e.encodeBytes(field.key) e.encodeBytes(field.key)
e.encodeInt64(v) e.encodeInt64(v)
code = field.next code = field.next
} }
field.nextField.ptr = field.ptr field.nextField.ptr = ptr
}
case opStructFieldPtrAnonymousHeadOmitEmptyInt64:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldAnonymousHeadOmitEmptyInt64:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end.next
} else {
v := e.ptrToInt64(ptr + field.offset)
if v == 0 {
code = field.nextField
} else {
e.encodeBytes(field.key)
e.encodeInt64(v)
code = field.next
}
field.nextField.ptr = ptr
} }
case opStructFieldPtrHeadOmitEmptyUint: case opStructFieldPtrHeadOmitEmptyUint:
if code.ptr != 0 { if code.ptr != 0 {
@ -1387,18 +1501,37 @@ func (e *Encoder) run(code *opcode) error {
e.encodeNull() e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent)
e.encodeByte('{') e.encodeByte('{')
v := e.ptrToUint(ptr + field.offset) v := e.ptrToUint(ptr + field.offset)
if v == 0 { if v == 0 {
code = field.nextField code = field.nextField
} else { } else {
e.encodeIndent(code.indent + 1)
e.encodeBytes(field.key) e.encodeBytes(field.key)
e.encodeUint(v) e.encodeUint(v)
code = field.next code = field.next
} }
field.nextField.ptr = field.ptr field.nextField.ptr = ptr
}
case opStructFieldPtrAnonymousHeadOmitEmptyUint:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldAnonymousHeadOmitEmptyUint:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end.next
} else {
v := e.ptrToUint(ptr + field.offset)
if v == 0 {
code = field.nextField
} else {
e.encodeBytes(field.key)
e.encodeUint(v)
code = field.next
}
field.nextField.ptr = ptr
} }
case opStructFieldPtrHeadOmitEmptyUint8: case opStructFieldPtrHeadOmitEmptyUint8:
if code.ptr != 0 { if code.ptr != 0 {
@ -1412,18 +1545,37 @@ func (e *Encoder) run(code *opcode) error {
e.encodeNull() e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent)
e.encodeByte('{') e.encodeByte('{')
v := e.ptrToUint8(ptr + field.offset) v := e.ptrToUint8(ptr + field.offset)
if v == 0 { if v == 0 {
code = field.nextField code = field.nextField
} else { } else {
e.encodeIndent(code.indent + 1)
e.encodeBytes(field.key) e.encodeBytes(field.key)
e.encodeUint8(v) e.encodeUint8(v)
code = field.next code = field.next
} }
field.nextField.ptr = field.ptr field.nextField.ptr = ptr
}
case opStructFieldPtrAnonymousHeadOmitEmptyUint8:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldAnonymousHeadOmitEmptyUint8:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end.next
} else {
v := e.ptrToUint8(ptr + field.offset)
if v == 0 {
code = field.nextField
} else {
e.encodeBytes(field.key)
e.encodeUint8(v)
code = field.next
}
field.nextField.ptr = ptr
} }
case opStructFieldPtrHeadOmitEmptyUint16: case opStructFieldPtrHeadOmitEmptyUint16:
if code.ptr != 0 { if code.ptr != 0 {
@ -1437,19 +1589,38 @@ func (e *Encoder) run(code *opcode) error {
e.encodeNull() e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent)
e.encodeByte('{') e.encodeByte('{')
v := e.ptrToUint16(ptr + field.offset) v := e.ptrToUint16(ptr + field.offset)
if v == 0 { if v == 0 {
code = field.nextField code = field.nextField
} else { } else {
e.encodeIndent(code.indent + 1)
e.encodeBytes(field.key) e.encodeBytes(field.key)
e.encodeUint16(v) e.encodeUint16(v)
code = field.next code = field.next
} }
field.nextField.ptr = field.ptr field.nextField.ptr = field.ptr
} }
case opStructFieldPtrAnonymousHeadOmitEmptyUint16:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldAnonymousHeadOmitEmptyUint16:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end.next
} else {
v := e.ptrToUint16(ptr + field.offset)
if v == 0 {
code = field.nextField
} else {
e.encodeBytes(field.key)
e.encodeUint16(v)
code = field.next
}
field.nextField.ptr = ptr
}
case opStructFieldPtrHeadOmitEmptyUint32: case opStructFieldPtrHeadOmitEmptyUint32:
if code.ptr != 0 { if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr) code.ptr = e.ptrToPtr(code.ptr)
@ -1462,18 +1633,37 @@ func (e *Encoder) run(code *opcode) error {
e.encodeNull() e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent)
e.encodeByte('{') e.encodeByte('{')
v := e.ptrToUint32(ptr + field.offset) v := e.ptrToUint32(ptr + field.offset)
if v == 0 { if v == 0 {
code = field.nextField code = field.nextField
} else { } else {
e.encodeIndent(code.indent + 1)
e.encodeBytes(field.key) e.encodeBytes(field.key)
e.encodeUint32(v) e.encodeUint32(v)
code = field.next code = field.next
} }
field.nextField.ptr = field.ptr field.nextField.ptr = ptr
}
case opStructFieldPtrAnonymousHeadOmitEmptyUint32:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldAnonymousHeadOmitEmptyUint32:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end.next
} else {
v := e.ptrToUint32(ptr + field.offset)
if v == 0 {
code = field.nextField
} else {
e.encodeBytes(field.key)
e.encodeUint32(v)
code = field.next
}
field.nextField.ptr = ptr
} }
case opStructFieldPtrHeadOmitEmptyUint64: case opStructFieldPtrHeadOmitEmptyUint64:
if code.ptr != 0 { if code.ptr != 0 {
@ -1487,19 +1677,38 @@ func (e *Encoder) run(code *opcode) error {
e.encodeNull() e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent)
e.encodeByte('{') e.encodeByte('{')
v := e.ptrToUint64(ptr + field.offset) v := e.ptrToUint64(ptr + field.offset)
if v == 0 { if v == 0 {
code = field.nextField code = field.nextField
} else { } else {
e.encodeIndent(code.indent + 1)
e.encodeBytes(field.key) e.encodeBytes(field.key)
e.encodeUint64(v) e.encodeUint64(v)
code = field.next code = field.next
} }
field.nextField.ptr = field.ptr field.nextField.ptr = field.ptr
} }
case opStructFieldPtrAnonymousHeadOmitEmptyUint64:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldAnonymousHeadOmitEmptyUint64:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end.next
} else {
v := e.ptrToUint64(ptr + field.offset)
if v == 0 {
code = field.nextField
} else {
e.encodeBytes(field.key)
e.encodeUint64(v)
code = field.next
}
field.nextField.ptr = ptr
}
case opStructFieldPtrHeadOmitEmptyFloat32: case opStructFieldPtrHeadOmitEmptyFloat32:
if code.ptr != 0 { if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr) code.ptr = e.ptrToPtr(code.ptr)
@ -1512,18 +1721,37 @@ func (e *Encoder) run(code *opcode) error {
e.encodeNull() e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent)
e.encodeByte('{') e.encodeByte('{')
v := e.ptrToFloat32(ptr + field.offset) v := e.ptrToFloat32(ptr + field.offset)
if v == 0 { if v == 0 {
code = field.nextField code = field.nextField
} else { } else {
e.encodeIndent(code.indent + 1)
e.encodeBytes(field.key) e.encodeBytes(field.key)
e.encodeFloat32(v) e.encodeFloat32(v)
code = field.next code = field.next
} }
field.nextField.ptr = field.ptr field.nextField.ptr = ptr
}
case opStructFieldPtrAnonymousHeadOmitEmptyFloat32:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldAnonymousHeadOmitEmptyFloat32:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end.next
} else {
v := e.ptrToFloat32(ptr + field.offset)
if v == 0 {
code = field.nextField
} else {
e.encodeBytes(field.key)
e.encodeFloat32(v)
code = field.next
}
field.nextField.ptr = ptr
} }
case opStructFieldPtrHeadOmitEmptyFloat64: case opStructFieldPtrHeadOmitEmptyFloat64:
if code.ptr != 0 { if code.ptr != 0 {
@ -1537,7 +1765,6 @@ func (e *Encoder) run(code *opcode) error {
e.encodeNull() e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent)
e.encodeByte('{') e.encodeByte('{')
v := e.ptrToFloat64(ptr + field.offset) v := e.ptrToFloat64(ptr + field.offset)
if v == 0 { if v == 0 {
@ -1549,13 +1776,39 @@ func (e *Encoder) run(code *opcode) error {
Str: strconv.FormatFloat(v, 'g', -1, 64), Str: strconv.FormatFloat(v, 'g', -1, 64),
} }
} }
e.encodeIndent(code.indent + 1)
e.encodeBytes(field.key) e.encodeBytes(field.key)
e.encodeFloat64(v) e.encodeFloat64(v)
code = field.next code = field.next
} }
field.nextField.ptr = field.ptr field.nextField.ptr = field.ptr
} }
case opStructFieldPtrAnonymousHeadOmitEmptyFloat64:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldAnonymousHeadOmitEmptyFloat64:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end.next
} else {
v := e.ptrToFloat64(ptr + field.offset)
if v == 0 {
code = field.nextField
} else {
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)
code = field.next
}
field.nextField.ptr = ptr
}
case opStructFieldPtrHeadOmitEmptyString: case opStructFieldPtrHeadOmitEmptyString:
if code.ptr != 0 { if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr) code.ptr = e.ptrToPtr(code.ptr)
@ -1568,18 +1821,37 @@ func (e *Encoder) run(code *opcode) error {
e.encodeNull() e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent)
e.encodeByte('{') e.encodeByte('{')
v := e.ptrToString(ptr + field.offset) v := e.ptrToString(ptr + field.offset)
if v == "" { if v == "" {
code = field.nextField code = field.nextField
} else { } else {
e.encodeIndent(code.indent + 1)
e.encodeBytes(field.key) e.encodeBytes(field.key)
e.encodeString(v) e.encodeString(v)
code = field.next code = field.next
} }
field.nextField.ptr = field.ptr field.nextField.ptr = ptr
}
case opStructFieldPtrAnonymousHeadOmitEmptyString:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldAnonymousHeadOmitEmptyString:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end.next
} else {
v := e.ptrToString(ptr + field.offset)
if v == "" {
code = field.nextField
} else {
e.encodeBytes(field.key)
e.encodeString(v)
code = field.next
}
field.nextField.ptr = ptr
} }
case opStructFieldPtrHeadOmitEmptyBool: case opStructFieldPtrHeadOmitEmptyBool:
if code.ptr != 0 { if code.ptr != 0 {
@ -1593,20 +1865,38 @@ func (e *Encoder) run(code *opcode) error {
e.encodeNull() e.encodeNull()
code = field.end.next code = field.end.next
} else { } else {
e.encodeIndent(code.indent)
e.encodeByte('{') e.encodeByte('{')
v := e.ptrToBool(ptr + field.offset) v := e.ptrToBool(ptr + field.offset)
if !v { if !v {
code = field.nextField code = field.nextField
} else { } else {
e.encodeIndent(code.indent + 1)
e.encodeBytes(field.key) e.encodeBytes(field.key)
e.encodeBool(v) e.encodeBool(v)
code = field.next code = field.next
} }
field.nextField.ptr = field.ptr field.nextField.ptr = ptr
}
case opStructFieldPtrAnonymousHeadOmitEmptyBool:
if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr)
}
fallthrough
case opStructFieldAnonymousHeadOmitEmptyBool:
field := code.toStructFieldCode()
ptr := field.ptr
if ptr == 0 {
code = field.end.next
} else {
v := e.ptrToBool(ptr + field.offset)
if !v {
code = field.nextField
} else {
e.encodeBytes(field.key)
e.encodeBool(v)
code = field.next
}
field.nextField.ptr = ptr
} }
case opStructFieldPtrHeadOmitEmptyIndent: case opStructFieldPtrHeadOmitEmptyIndent:
if code.ptr != 0 { if code.ptr != 0 {
code.ptr = e.ptrToPtr(code.ptr) code.ptr = e.ptrToPtr(code.ptr)