1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-24 08:22:14 +02:00
json-iterator/feature_reflect.go

572 lines
16 KiB
Go
Raw Normal View History

2016-12-04 05:06:38 +02:00
package jsoniter
import (
2017-06-06 17:27:00 +02:00
"encoding"
"encoding/json"
2016-12-04 06:27:34 +02:00
"fmt"
"reflect"
2017-06-20 01:39:38 +02:00
"time"
2017-06-20 01:39:54 +02:00
"unsafe"
2018-02-16 09:42:37 +02:00
"github.com/v2pro/plz/reflect2"
2016-12-04 05:06:38 +02:00
)
// ValDecoder is an internal type registered to cache as needed.
// Don't confuse jsoniter.ValDecoder with json.Decoder.
2017-06-11 10:28:31 +02:00
// For json.Decoder's adapter, refer to jsoniter.AdapterDecoder(todo link).
2017-06-11 10:30:31 +02:00
//
2017-06-11 10:28:31 +02:00
// Reflection on type to create decoders, which is then cached
// Reflection on value is avoided as we can, as the reflect.Value itself will allocate, with following exceptions
// 1. create instance of new value, for example *int will need a int to be allocated
// 2. append to slice, if the existing cap is not enough, allocate will be done using Reflect.New
// 3. assignment to map, both key and value will be reflect.Value
// For a simple struct binding, it will be reflect.Value free and allocation free
type ValDecoder interface {
2017-06-20 09:11:01 +02:00
Decode(ptr unsafe.Pointer, iter *Iterator)
2016-12-04 05:06:38 +02:00
}
2017-06-11 10:30:31 +02:00
// ValEncoder is an internal type registered to cache as needed.
// Don't confuse jsoniter.ValEncoder with json.Encoder.
2017-06-11 10:28:31 +02:00
// For json.Encoder's adapter, refer to jsoniter.AdapterEncoder(todo godoc link).
type ValEncoder interface {
2017-06-20 09:11:01 +02:00
IsEmpty(ptr unsafe.Pointer) bool
Encode(ptr unsafe.Pointer, stream *Stream)
2017-01-25 18:25:17 +02:00
}
type checkIsEmpty interface {
IsEmpty(ptr unsafe.Pointer) bool
}
2017-06-02 04:21:43 +02:00
var jsonNumberType reflect.Type
var jsoniterNumberType reflect.Type
2017-06-02 04:50:23 +02:00
var jsonRawMessageType reflect.Type
2017-06-19 17:10:20 +02:00
var jsoniterRawMessageType reflect.Type
2017-01-25 18:25:17 +02:00
var anyType reflect.Type
2017-05-24 08:34:00 +02:00
var marshalerType reflect.Type
2017-05-24 10:04:11 +02:00
var unmarshalerType reflect.Type
2017-06-28 18:14:55 +02:00
var textMarshalerType reflect.Type
2017-06-05 18:09:33 +02:00
var textUnmarshalerType reflect.Type
2016-12-04 14:50:55 +02:00
2017-01-07 01:49:50 +02:00
func init() {
2017-06-02 04:21:43 +02:00
jsonNumberType = reflect.TypeOf((*json.Number)(nil)).Elem()
jsoniterNumberType = reflect.TypeOf((*Number)(nil)).Elem()
2017-06-02 04:50:23 +02:00
jsonRawMessageType = reflect.TypeOf((*json.RawMessage)(nil)).Elem()
2017-06-19 17:10:20 +02:00
jsoniterRawMessageType = reflect.TypeOf((*RawMessage)(nil)).Elem()
2017-01-25 18:25:17 +02:00
anyType = reflect.TypeOf((*Any)(nil)).Elem()
2017-05-24 08:34:00 +02:00
marshalerType = reflect.TypeOf((*json.Marshaler)(nil)).Elem()
2017-05-24 10:04:11 +02:00
unmarshalerType = reflect.TypeOf((*json.Unmarshaler)(nil)).Elem()
2017-06-28 18:14:55 +02:00
textMarshalerType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
2017-06-05 18:09:33 +02:00
textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
2016-12-07 02:49:52 +02:00
}
2017-07-09 10:09:23 +02:00
// ReadVal copy the underlying JSON into go interface, same as json.Unmarshal
2017-01-09 11:47:21 +02:00
func (iter *Iterator) ReadVal(obj interface{}) {
typ := reflect.TypeOf(obj)
cacheKey := typ.Elem()
decoder := decoderOfType(iter.cfg, "", cacheKey)
2016-12-04 06:27:34 +02:00
e := (*emptyInterface)(unsafe.Pointer(&obj))
if e.word == nil {
iter.ReportError("ReadVal", "can not read into nil pointer")
return
}
2017-06-20 09:11:01 +02:00
decoder.Decode(e.word, iter)
2016-12-04 06:27:34 +02:00
}
2017-07-09 10:09:23 +02:00
// WriteVal copy the go interface into underlying JSON, same as json.Marshal
2017-01-09 11:47:21 +02:00
func (stream *Stream) WriteVal(val interface{}) {
2017-01-18 17:33:40 +02:00
if nil == val {
stream.WriteNil()
return
}
2017-01-09 11:47:21 +02:00
typ := reflect.TypeOf(val)
2018-02-14 09:04:23 +02:00
encoder := stream.cfg.EncoderOf(typ)
2018-02-18 16:57:01 +02:00
encoder.Encode(reflect2.PtrOf(val), stream)
2017-01-09 11:47:21 +02:00
}
func decoderOfType(cfg *frozenConfig, prefix string, typ reflect.Type) ValDecoder {
2017-05-05 10:51:05 +02:00
cacheKey := typ
decoder := cfg.getDecoderFromCache(cacheKey)
if decoder != nil {
return decoder
2017-05-05 10:51:05 +02:00
}
2017-11-22 18:09:35 +02:00
decoder = getTypeDecoderFromExtension(cfg, typ)
if decoder != nil {
cfg.addDecoderToCache(cacheKey, decoder)
return decoder
}
decoder = &placeholderDecoder{cfg: cfg, cacheKey: cacheKey}
cfg.addDecoderToCache(cacheKey, decoder)
decoder = createDecoderOfType(cfg, prefix, typ)
2017-06-20 10:36:22 +02:00
for _, extension := range extensions {
decoder = extension.DecorateDecoder(typ, decoder)
}
2017-11-22 18:09:35 +02:00
for _, extension := range cfg.extensions {
decoder = extension.DecorateDecoder(typ, decoder)
}
cfg.addDecoderToCache(cacheKey, decoder)
return decoder
2017-05-05 10:51:05 +02:00
}
func createDecoderOfType(cfg *frozenConfig, prefix string, typ reflect.Type) ValDecoder {
2017-06-26 03:57:45 +02:00
typeName := typ.String()
2017-06-26 08:25:56 +02:00
if typ == jsonRawMessageType {
return &jsonRawMessageCodec{}
2017-06-02 04:50:23 +02:00
}
2017-06-26 08:25:56 +02:00
if typ == jsoniterRawMessageType {
return &jsoniterRawMessageCodec{}
2017-06-19 17:10:20 +02:00
}
2017-06-02 09:43:58 +02:00
if typ.AssignableTo(jsonNumberType) {
return &jsonNumberCodec{}
2017-06-02 04:21:43 +02:00
}
if typ.AssignableTo(jsoniterNumberType) {
return &jsoniterNumberCodec{}
}
2017-06-28 18:14:55 +02:00
if typ.Implements(unmarshalerType) {
2017-05-24 10:04:11 +02:00
templateInterface := reflect.New(typ).Elem().Interface()
var decoder ValDecoder = &unmarshalerDecoder{extractInterface(templateInterface)}
if typ.Kind() == reflect.Ptr {
decoder = &OptionalDecoder{typ.Elem(), decoder}
}
return decoder
2017-05-24 10:04:11 +02:00
}
2017-06-28 18:14:55 +02:00
if reflect.PtrTo(typ).Implements(unmarshalerType) {
2017-06-28 17:55:32 +02:00
templateInterface := reflect.New(typ).Interface()
var decoder ValDecoder = &unmarshalerDecoder{extractInterface(templateInterface)}
return decoder
2017-06-28 17:55:32 +02:00
}
2017-06-28 18:14:55 +02:00
if typ.Implements(textUnmarshalerType) {
templateInterface := reflect.New(typ).Elem().Interface()
var decoder ValDecoder = &textUnmarshalerDecoder{extractInterface(templateInterface)}
if typ.Kind() == reflect.Ptr {
decoder = &OptionalDecoder{typ.Elem(), decoder}
2017-06-28 18:14:55 +02:00
}
return decoder
2017-06-28 18:14:55 +02:00
}
if reflect.PtrTo(typ).Implements(textUnmarshalerType) {
templateInterface := reflect.New(typ).Interface()
var decoder ValDecoder = &textUnmarshalerDecoder{extractInterface(templateInterface)}
return decoder
2017-06-28 18:14:55 +02:00
}
if typ.Kind() == reflect.Slice && typ.Elem().Kind() == reflect.Uint8 {
sliceDecoder := decoderOfSlice(cfg, prefix, typ)
return &base64Codec{sliceDecoder: sliceDecoder}
}
2018-02-18 16:57:01 +02:00
if typ == anyType {
return &directAnyCodec{}
}
2017-06-28 18:14:55 +02:00
if typ.Implements(anyType) {
return &anyCodec{}
2017-06-02 04:50:23 +02:00
}
switch typ.Kind() {
2016-12-04 05:06:38 +02:00
case reflect.String:
2017-06-26 03:57:45 +02:00
if typeName != "string" {
return decoderOfType(cfg, prefix, reflect.TypeOf((*string)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &stringCodec{}
2016-12-04 15:19:54 +02:00
case reflect.Int:
2017-06-26 03:57:45 +02:00
if typeName != "int" {
return decoderOfType(cfg, prefix, reflect.TypeOf((*int)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &intCodec{}
2016-12-04 15:32:43 +02:00
case reflect.Int8:
2017-06-26 03:57:45 +02:00
if typeName != "int8" {
return decoderOfType(cfg, prefix, reflect.TypeOf((*int8)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &int8Codec{}
2016-12-04 15:32:43 +02:00
case reflect.Int16:
2017-06-26 03:57:45 +02:00
if typeName != "int16" {
return decoderOfType(cfg, prefix, reflect.TypeOf((*int16)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &int16Codec{}
2016-12-04 15:32:43 +02:00
case reflect.Int32:
2017-06-26 03:57:45 +02:00
if typeName != "int32" {
return decoderOfType(cfg, prefix, reflect.TypeOf((*int32)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &int32Codec{}
2016-12-04 15:32:43 +02:00
case reflect.Int64:
2017-06-26 03:57:45 +02:00
if typeName != "int64" {
return decoderOfType(cfg, prefix, reflect.TypeOf((*int64)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &int64Codec{}
2016-12-04 15:32:43 +02:00
case reflect.Uint:
2017-06-26 03:57:45 +02:00
if typeName != "uint" {
return decoderOfType(cfg, prefix, reflect.TypeOf((*uint)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &uintCodec{}
2016-12-04 15:32:43 +02:00
case reflect.Uint8:
2017-06-26 03:57:45 +02:00
if typeName != "uint8" {
return decoderOfType(cfg, prefix, reflect.TypeOf((*uint8)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &uint8Codec{}
2016-12-04 15:32:43 +02:00
case reflect.Uint16:
2017-06-26 03:57:45 +02:00
if typeName != "uint16" {
return decoderOfType(cfg, prefix, reflect.TypeOf((*uint16)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &uint16Codec{}
2016-12-04 15:32:43 +02:00
case reflect.Uint32:
2017-06-26 03:57:45 +02:00
if typeName != "uint32" {
return decoderOfType(cfg, prefix, reflect.TypeOf((*uint32)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &uint32Codec{}
2017-07-02 05:03:13 +02:00
case reflect.Uintptr:
if typeName != "uintptr" {
return decoderOfType(cfg, prefix, reflect.TypeOf((*uintptr)(nil)).Elem())
2017-07-02 05:03:13 +02:00
}
return &uintptrCodec{}
2016-12-04 15:32:43 +02:00
case reflect.Uint64:
2017-06-26 03:57:45 +02:00
if typeName != "uint64" {
return decoderOfType(cfg, prefix, reflect.TypeOf((*uint64)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &uint64Codec{}
2016-12-04 16:15:12 +02:00
case reflect.Float32:
2017-06-26 03:57:45 +02:00
if typeName != "float32" {
return decoderOfType(cfg, prefix, reflect.TypeOf((*float32)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &float32Codec{}
2016-12-04 16:15:12 +02:00
case reflect.Float64:
2017-06-26 03:57:45 +02:00
if typeName != "float64" {
return decoderOfType(cfg, prefix, reflect.TypeOf((*float64)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &float64Codec{}
2016-12-04 16:15:12 +02:00
case reflect.Bool:
2017-06-26 03:57:45 +02:00
if typeName != "bool" {
return decoderOfType(cfg, prefix, reflect.TypeOf((*bool)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &boolCodec{}
2016-12-11 04:04:26 +02:00
case reflect.Interface:
2017-01-25 17:25:25 +02:00
if typ.NumMethod() == 0 {
return &emptyInterfaceCodec{}
2017-01-25 17:25:25 +02:00
}
return &nonEmptyInterfaceCodec{}
2016-12-04 06:27:34 +02:00
case reflect.Struct:
return decoderOfStruct(cfg, prefix, typ)
2017-06-19 17:43:28 +02:00
case reflect.Array:
return decoderOfArray(cfg, prefix, typ)
2016-12-04 07:13:38 +02:00
case reflect.Slice:
return decoderOfSlice(cfg, prefix, typ)
2016-12-10 18:38:07 +02:00
case reflect.Map:
return decoderOfMap(cfg, prefix, typ)
2016-12-04 07:13:38 +02:00
case reflect.Ptr:
return decoderOfOptional(cfg, prefix, typ)
2016-12-04 07:13:38 +02:00
default:
return &lazyErrorDecoder{err: fmt.Errorf("%s%s is unsupported type", prefix, typ.String())}
2016-12-04 07:13:38 +02:00
}
}
2018-02-14 09:04:23 +02:00
func (cfg *frozenConfig) EncoderOf(typ reflect.Type) ValEncoder {
2017-05-05 10:51:05 +02:00
cacheKey := typ
encoder := cfg.getEncoderFromCache(cacheKey)
if encoder != nil {
return encoder
2017-05-05 10:51:05 +02:00
}
2018-02-14 09:04:23 +02:00
encoder = encoderOfType(cfg, "", typ)
if shouldFixOnePtr(typ) {
encoder = &onePtrEncoder{encoder}
}
cfg.addEncoderToCache(cacheKey, encoder)
return encoder
}
type onePtrEncoder struct {
encoder ValEncoder
}
func (encoder *onePtrEncoder) IsEmpty(ptr unsafe.Pointer) bool {
return encoder.encoder.IsEmpty(unsafe.Pointer(&ptr))
}
func (encoder *onePtrEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
encoder.encoder.Encode(unsafe.Pointer(&ptr), stream)
}
func shouldFixOnePtr(typ reflect.Type) bool {
if isPtrKind(typ.Kind()) {
return true
}
if typ.Kind() == reflect.Struct {
if typ.NumField() != 1 {
return false
}
return shouldFixOnePtr(typ.Field(0).Type)
}
if typ.Kind() == reflect.Array {
if typ.Len() != 1 {
return false
}
return shouldFixOnePtr(typ.Elem())
}
return false
}
func isPtrKind(kind reflect.Kind) bool {
switch kind {
case reflect.Ptr, reflect.Map, reflect.Chan, reflect.Func:
return true
}
return false
}
func encoderOfType(cfg *frozenConfig, prefix string, typ reflect.Type) ValEncoder {
encoder := getTypeEncoderFromExtension(cfg, typ)
if encoder != nil {
return encoder
}
encoder = createEncoderOfType(cfg, prefix, typ)
2017-06-20 10:36:22 +02:00
for _, extension := range extensions {
encoder = extension.DecorateEncoder(typ, encoder)
}
2017-11-22 18:09:35 +02:00
for _, extension := range cfg.extensions {
encoder = extension.DecorateEncoder(typ, encoder)
}
return encoder
2017-05-05 10:51:05 +02:00
}
func createEncoderOfType(cfg *frozenConfig, prefix string, typ reflect.Type) ValEncoder {
2017-06-26 08:25:56 +02:00
if typ == jsonRawMessageType {
return &jsonRawMessageCodec{}
2017-06-02 04:50:23 +02:00
}
2017-06-26 08:25:56 +02:00
if typ == jsoniterRawMessageType {
return &jsoniterRawMessageCodec{}
2017-06-19 17:10:20 +02:00
}
2017-06-02 09:43:58 +02:00
if typ.AssignableTo(jsonNumberType) {
return &jsonNumberCodec{}
2017-06-02 04:21:43 +02:00
}
if typ.AssignableTo(jsoniterNumberType) {
return &jsoniterNumberCodec{}
}
2018-02-19 08:30:01 +02:00
encoder := createEncoderOfMarshaler(cfg, prefix, typ)
if encoder != nil {
return encoder
2017-06-28 18:14:55 +02:00
}
if typ.Kind() == reflect.Slice && typ.Elem().Kind() == reflect.Uint8 {
return &base64Codec{}
}
2018-02-18 16:57:01 +02:00
if typ == anyType {
return &directAnyCodec{}
}
2017-06-28 18:14:55 +02:00
if typ.Implements(anyType) {
2018-02-18 16:57:01 +02:00
return &anyCodec{
valType: reflect2.Type2(typ),
}
2017-06-02 04:50:23 +02:00
}
return createEncoderOfSimpleType(cfg, prefix, typ)
}
func createCheckIsEmpty(cfg *frozenConfig, typ reflect.Type) checkIsEmpty {
kind := typ.Kind()
switch kind {
case reflect.String:
return &stringCodec{}
case reflect.Int:
return &intCodec{}
case reflect.Int8:
return &int8Codec{}
case reflect.Int16:
return &int16Codec{}
case reflect.Int32:
return &int32Codec{}
case reflect.Int64:
return &int64Codec{}
case reflect.Uint:
return &uintCodec{}
case reflect.Uint8:
return &uint8Codec{}
case reflect.Uint16:
return &uint16Codec{}
case reflect.Uint32:
return &uint32Codec{}
case reflect.Uintptr:
return &uintptrCodec{}
case reflect.Uint64:
return &uint64Codec{}
case reflect.Float32:
return &float32Codec{}
case reflect.Float64:
return &float64Codec{}
case reflect.Bool:
return &boolCodec{}
case reflect.Interface:
return &dynamicEncoder{reflect2.Type2(typ)}
case reflect.Struct:
return &structEncoder{typ: typ}
case reflect.Array:
return &arrayEncoder{}
case reflect.Slice:
return &sliceEncoder{}
case reflect.Map:
return encoderOfMap(cfg, "", typ)
case reflect.Ptr:
return &OptionalEncoder{}
default:
return &lazyErrorEncoder{err: fmt.Errorf("unsupported type: %v", typ)}
}
}
func createEncoderOfSimpleType(cfg *frozenConfig, prefix string, typ reflect.Type) ValEncoder {
typeName := typ.String()
2017-05-26 18:36:21 +02:00
kind := typ.Kind()
switch kind {
2017-01-09 11:47:21 +02:00
case reflect.String:
2017-06-26 03:57:45 +02:00
if typeName != "string" {
return encoderOfType(cfg, prefix, reflect.TypeOf((*string)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &stringCodec{}
2017-01-09 11:47:21 +02:00
case reflect.Int:
2017-06-26 03:57:45 +02:00
if typeName != "int" {
return encoderOfType(cfg, prefix, reflect.TypeOf((*int)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &intCodec{}
2017-01-09 13:19:48 +02:00
case reflect.Int8:
2017-06-26 03:57:45 +02:00
if typeName != "int8" {
return encoderOfType(cfg, prefix, reflect.TypeOf((*int8)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &int8Codec{}
2017-01-09 13:19:48 +02:00
case reflect.Int16:
2017-06-26 03:57:45 +02:00
if typeName != "int16" {
return encoderOfType(cfg, prefix, reflect.TypeOf((*int16)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &int16Codec{}
2017-01-09 13:19:48 +02:00
case reflect.Int32:
2017-06-26 03:57:45 +02:00
if typeName != "int32" {
return encoderOfType(cfg, prefix, reflect.TypeOf((*int32)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &int32Codec{}
2017-01-09 13:19:48 +02:00
case reflect.Int64:
2017-06-26 03:57:45 +02:00
if typeName != "int64" {
return encoderOfType(cfg, prefix, reflect.TypeOf((*int64)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &int64Codec{}
2017-01-09 13:19:48 +02:00
case reflect.Uint:
2017-06-26 03:57:45 +02:00
if typeName != "uint" {
return encoderOfType(cfg, prefix, reflect.TypeOf((*uint)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &uintCodec{}
2017-01-09 13:19:48 +02:00
case reflect.Uint8:
2017-06-26 03:57:45 +02:00
if typeName != "uint8" {
return encoderOfType(cfg, prefix, reflect.TypeOf((*uint8)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &uint8Codec{}
2017-01-09 13:19:48 +02:00
case reflect.Uint16:
2017-06-26 03:57:45 +02:00
if typeName != "uint16" {
return encoderOfType(cfg, prefix, reflect.TypeOf((*uint16)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &uint16Codec{}
2017-01-09 13:19:48 +02:00
case reflect.Uint32:
2017-06-26 03:57:45 +02:00
if typeName != "uint32" {
return encoderOfType(cfg, prefix, reflect.TypeOf((*uint32)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &uint32Codec{}
2017-07-02 05:03:13 +02:00
case reflect.Uintptr:
if typeName != "uintptr" {
return encoderOfType(cfg, prefix, reflect.TypeOf((*uintptr)(nil)).Elem())
2017-07-02 05:03:13 +02:00
}
return &uintptrCodec{}
2017-01-09 13:19:48 +02:00
case reflect.Uint64:
2017-06-26 04:02:45 +02:00
if typeName != "uint64" {
return encoderOfType(cfg, prefix, reflect.TypeOf((*uint64)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &uint64Codec{}
2017-01-09 13:19:48 +02:00
case reflect.Float32:
2017-06-26 03:57:45 +02:00
if typeName != "float32" {
return encoderOfType(cfg, prefix, reflect.TypeOf((*float32)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &float32Codec{}
2017-01-09 13:19:48 +02:00
case reflect.Float64:
2017-06-26 03:57:45 +02:00
if typeName != "float64" {
return encoderOfType(cfg, prefix, reflect.TypeOf((*float64)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &float64Codec{}
2017-01-09 13:19:48 +02:00
case reflect.Bool:
2017-06-26 03:57:45 +02:00
if typeName != "bool" {
return encoderOfType(cfg, prefix, reflect.TypeOf((*bool)(nil)).Elem())
2017-06-26 03:57:45 +02:00
}
return &boolCodec{}
2017-01-09 15:00:01 +02:00
case reflect.Interface:
return &dynamicEncoder{reflect2.Type2(typ)}
2017-01-09 13:19:48 +02:00
case reflect.Struct:
return encoderOfStruct(cfg, prefix, typ)
2017-06-19 17:43:28 +02:00
case reflect.Array:
return encoderOfArray(cfg, prefix, typ)
2017-01-09 13:48:57 +02:00
case reflect.Slice:
return encoderOfSlice(cfg, prefix, typ)
2017-01-09 14:51:09 +02:00
case reflect.Map:
return encoderOfMap(cfg, prefix, typ)
2017-01-09 14:51:09 +02:00
case reflect.Ptr:
return encoderOfOptional(cfg, prefix, typ)
2017-01-09 11:47:21 +02:00
default:
return &lazyErrorEncoder{err: fmt.Errorf("%s%s is unsupported type", prefix, typ.String())}
}
}
type placeholderDecoder struct {
cfg *frozenConfig
cacheKey reflect.Type
}
func (decoder *placeholderDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
for i := 0; i < 500; i++ {
realDecoder := decoder.cfg.getDecoderFromCache(decoder.cacheKey)
_, isPlaceholder := realDecoder.(*placeholderDecoder)
if isPlaceholder {
time.Sleep(10 * time.Millisecond)
} else {
realDecoder.Decode(ptr, iter)
return
}
2016-12-04 05:06:38 +02:00
}
panic(fmt.Sprintf("real decoder not found for cache key: %v", decoder.cacheKey))
2017-01-09 14:51:09 +02:00
}
type lazyErrorDecoder struct {
err error
}
func (decoder *lazyErrorDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
if iter.WhatIsNext() != NilValue {
if iter.Error == nil {
iter.Error = decoder.err
}
} else {
iter.Skip()
2017-01-09 14:51:09 +02:00
}
}
type lazyErrorEncoder struct {
err error
}
func (encoder *lazyErrorEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
if ptr == nil {
stream.WriteNil()
} else if stream.Error == nil {
stream.Error = encoder.err
}
2016-12-04 05:06:38 +02:00
}
func (encoder *lazyErrorEncoder) IsEmpty(ptr unsafe.Pointer) bool {
return false
2017-05-24 08:34:00 +02:00
}
func extractInterface(val interface{}) emptyInterface {
return *((*emptyInterface)(unsafe.Pointer(&val)))
2016-12-10 18:38:07 +02:00
}
2017-01-09 14:51:09 +02:00
// emptyInterface is the header for an interface{} value.
type emptyInterface struct {
typ unsafe.Pointer
word unsafe.Pointer
}
// emptyInterface is the header for an interface with method (not interface{})
type nonEmptyInterface struct {
// see ../runtime/iface.go:/Itab
itab *struct {
ityp unsafe.Pointer // static interface type
typ unsafe.Pointer // dynamic concrete type
link unsafe.Pointer
bad int32
unused int32
fun [100000]unsafe.Pointer // method table
2017-06-16 10:46:30 +02:00
}
word unsafe.Pointer
2017-01-09 14:51:09 +02:00
}