2018-02-14 08:56:04 +00:00
|
|
|
package jsoniter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-03-14 14:56:06 +00:00
|
|
|
"github.com/modern-go/reflect2"
|
2018-02-14 08:56:04 +00:00
|
|
|
"io"
|
|
|
|
"reflect"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2019-03-14 14:56:06 +00:00
|
|
|
func encoderOfStruct(ctx *ctx, typ reflect2.Type) ValEncoder {
|
2018-02-14 08:56:04 +00:00
|
|
|
type bindingTo struct {
|
|
|
|
binding *Binding
|
|
|
|
toName string
|
|
|
|
ignored bool
|
|
|
|
}
|
|
|
|
orderedBindings := []*bindingTo{}
|
2019-03-14 14:56:06 +00:00
|
|
|
structDescriptor := describeStruct(ctx, typ)
|
2018-02-14 08:56:04 +00:00
|
|
|
for _, binding := range structDescriptor.Fields {
|
|
|
|
for _, toName := range binding.ToNames {
|
|
|
|
new := &bindingTo{
|
|
|
|
binding: binding,
|
|
|
|
toName: toName,
|
|
|
|
}
|
|
|
|
for _, old := range orderedBindings {
|
|
|
|
if old.toName != toName {
|
|
|
|
continue
|
|
|
|
}
|
2019-03-14 14:56:06 +00:00
|
|
|
old.ignored, new.ignored = resolveConflictBinding(ctx.frozenConfig, old.binding, new.binding)
|
2018-02-14 08:56:04 +00:00
|
|
|
}
|
|
|
|
orderedBindings = append(orderedBindings, new)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(orderedBindings) == 0 {
|
|
|
|
return &emptyStructEncoder{}
|
|
|
|
}
|
|
|
|
finalOrderedFields := []structFieldTo{}
|
|
|
|
for _, bindingTo := range orderedBindings {
|
|
|
|
if !bindingTo.ignored {
|
|
|
|
finalOrderedFields = append(finalOrderedFields, structFieldTo{
|
|
|
|
encoder: bindingTo.binding.Encoder.(*structFieldEncoder),
|
|
|
|
toName: bindingTo.toName,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-03-14 14:56:06 +00:00
|
|
|
return &structEncoder{typ, finalOrderedFields}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createCheckIsEmpty(ctx *ctx, typ reflect2.Type) checkIsEmpty {
|
|
|
|
encoder := createEncoderOfNative(ctx, typ)
|
|
|
|
if encoder != nil {
|
|
|
|
return encoder
|
|
|
|
}
|
|
|
|
kind := typ.Kind()
|
|
|
|
switch kind {
|
|
|
|
case reflect.Interface:
|
|
|
|
return &dynamicEncoder{typ}
|
|
|
|
case reflect.Struct:
|
|
|
|
return &structEncoder{typ: typ}
|
|
|
|
case reflect.Array:
|
|
|
|
return &arrayEncoder{}
|
|
|
|
case reflect.Slice:
|
|
|
|
return &sliceEncoder{}
|
|
|
|
case reflect.Map:
|
|
|
|
return encoderOfMap(ctx, typ)
|
|
|
|
case reflect.Ptr:
|
|
|
|
return &OptionalEncoder{}
|
|
|
|
default:
|
|
|
|
return &lazyErrorEncoder{err: fmt.Errorf("unsupported type: %v", typ)}
|
|
|
|
}
|
2018-02-14 08:56:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func resolveConflictBinding(cfg *frozenConfig, old, new *Binding) (ignoreOld, ignoreNew bool) {
|
2019-03-14 14:56:06 +00:00
|
|
|
newTagged := new.Field.Tag().Get(cfg.getTagKey()) != ""
|
|
|
|
oldTagged := old.Field.Tag().Get(cfg.getTagKey()) != ""
|
2018-02-14 08:56:04 +00:00
|
|
|
if newTagged {
|
|
|
|
if oldTagged {
|
|
|
|
if len(old.levels) > len(new.levels) {
|
|
|
|
return true, false
|
|
|
|
} else if len(new.levels) > len(old.levels) {
|
|
|
|
return false, true
|
|
|
|
} else {
|
|
|
|
return true, true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return true, false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if oldTagged {
|
|
|
|
return true, false
|
|
|
|
}
|
|
|
|
if len(old.levels) > len(new.levels) {
|
|
|
|
return true, false
|
|
|
|
} else if len(new.levels) > len(old.levels) {
|
|
|
|
return false, true
|
|
|
|
} else {
|
|
|
|
return true, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type structFieldEncoder struct {
|
2019-03-14 14:56:06 +00:00
|
|
|
field reflect2.StructField
|
2018-02-14 08:56:04 +00:00
|
|
|
fieldEncoder ValEncoder
|
|
|
|
omitempty bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (encoder *structFieldEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
2019-03-14 14:56:06 +00:00
|
|
|
fieldPtr := encoder.field.UnsafeGet(ptr)
|
2018-02-14 08:56:04 +00:00
|
|
|
encoder.fieldEncoder.Encode(fieldPtr, stream)
|
|
|
|
if stream.Error != nil && stream.Error != io.EOF {
|
2019-03-14 14:56:06 +00:00
|
|
|
stream.Error = fmt.Errorf("%s: %s", encoder.field.Name(), stream.Error.Error())
|
2018-02-14 08:56:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (encoder *structFieldEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
2019-03-14 14:56:06 +00:00
|
|
|
fieldPtr := encoder.field.UnsafeGet(ptr)
|
2018-02-14 08:56:04 +00:00
|
|
|
return encoder.fieldEncoder.IsEmpty(fieldPtr)
|
|
|
|
}
|
|
|
|
|
2019-03-14 14:56:06 +00:00
|
|
|
func (encoder *structFieldEncoder) IsEmbeddedPtrNil(ptr unsafe.Pointer) bool {
|
|
|
|
isEmbeddedPtrNil, converted := encoder.fieldEncoder.(IsEmbeddedPtrNil)
|
|
|
|
if !converted {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
fieldPtr := encoder.field.UnsafeGet(ptr)
|
|
|
|
return isEmbeddedPtrNil.IsEmbeddedPtrNil(fieldPtr)
|
|
|
|
}
|
|
|
|
|
|
|
|
type IsEmbeddedPtrNil interface {
|
|
|
|
IsEmbeddedPtrNil(ptr unsafe.Pointer) bool
|
|
|
|
}
|
|
|
|
|
2018-02-14 08:56:04 +00:00
|
|
|
type structEncoder struct {
|
2019-03-14 14:56:06 +00:00
|
|
|
typ reflect2.Type
|
|
|
|
fields []structFieldTo
|
2018-02-14 08:56:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type structFieldTo struct {
|
|
|
|
encoder *structFieldEncoder
|
|
|
|
toName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (encoder *structEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
|
|
|
stream.WriteObjectStart()
|
|
|
|
isNotFirst := false
|
|
|
|
for _, field := range encoder.fields {
|
|
|
|
if field.encoder.omitempty && field.encoder.IsEmpty(ptr) {
|
|
|
|
continue
|
|
|
|
}
|
2019-03-14 14:56:06 +00:00
|
|
|
if field.encoder.IsEmbeddedPtrNil(ptr) {
|
|
|
|
continue
|
|
|
|
}
|
2018-02-14 08:56:04 +00:00
|
|
|
if isNotFirst {
|
|
|
|
stream.WriteMore()
|
|
|
|
}
|
|
|
|
stream.WriteObjectField(field.toName)
|
|
|
|
field.encoder.Encode(ptr, stream)
|
|
|
|
isNotFirst = true
|
|
|
|
}
|
|
|
|
stream.WriteObjectEnd()
|
|
|
|
if stream.Error != nil && stream.Error != io.EOF {
|
|
|
|
stream.Error = fmt.Errorf("%v.%s", encoder.typ, stream.Error.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (encoder *structEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
type emptyStructEncoder struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (encoder *emptyStructEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
|
|
|
stream.WriteEmptyObject()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (encoder *emptyStructEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
|
|
|
return false
|
|
|
|
}
|
2019-03-14 14:56:06 +00:00
|
|
|
|
|
|
|
type stringModeNumberEncoder struct {
|
|
|
|
elemEncoder ValEncoder
|
|
|
|
}
|
|
|
|
|
|
|
|
func (encoder *stringModeNumberEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
|
|
|
stream.writeByte('"')
|
|
|
|
encoder.elemEncoder.Encode(ptr, stream)
|
|
|
|
stream.writeByte('"')
|
|
|
|
}
|
|
|
|
|
|
|
|
func (encoder *stringModeNumberEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
|
|
|
return encoder.elemEncoder.IsEmpty(ptr)
|
|
|
|
}
|
|
|
|
|
|
|
|
type stringModeStringEncoder struct {
|
|
|
|
elemEncoder ValEncoder
|
|
|
|
cfg *frozenConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (encoder *stringModeStringEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
|
|
|
tempStream := encoder.cfg.BorrowStream(nil)
|
|
|
|
defer encoder.cfg.ReturnStream(tempStream)
|
|
|
|
encoder.elemEncoder.Encode(ptr, tempStream)
|
|
|
|
stream.WriteString(string(tempStream.Buffer()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (encoder *stringModeStringEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
|
|
|
return encoder.elemEncoder.IsEmpty(ptr)
|
|
|
|
}
|