9 "github.com/modern-go/reflect2"
12 const ptrSize = 32 << uintptr(^uintptr(0)>>63)
14 func createEncoderOfNative(ctx *ctx, typ reflect2.Type) ValEncoder {
15 if typ.Kind() == reflect.Slice && typ.(reflect2.SliceType).Elem().Kind() == reflect.Uint8 {
16 sliceDecoder := decoderOfSlice(ctx, typ)
17 return &base64Codec{sliceDecoder: sliceDecoder}
19 typeName := typ.String()
23 if typeName != "string" {
24 return encoderOfType(ctx, reflect2.TypeOfPtr((*string)(nil)).Elem())
28 if typeName != "int" {
29 return encoderOfType(ctx, reflect2.TypeOfPtr((*int)(nil)).Elem())
31 if strconv.IntSize == 32 {
36 if typeName != "int8" {
37 return encoderOfType(ctx, reflect2.TypeOfPtr((*int8)(nil)).Elem())
41 if typeName != "int16" {
42 return encoderOfType(ctx, reflect2.TypeOfPtr((*int16)(nil)).Elem())
46 if typeName != "int32" {
47 return encoderOfType(ctx, reflect2.TypeOfPtr((*int32)(nil)).Elem())
51 if typeName != "int64" {
52 return encoderOfType(ctx, reflect2.TypeOfPtr((*int64)(nil)).Elem())
56 if typeName != "uint" {
57 return encoderOfType(ctx, reflect2.TypeOfPtr((*uint)(nil)).Elem())
59 if strconv.IntSize == 32 {
64 if typeName != "uint8" {
65 return encoderOfType(ctx, reflect2.TypeOfPtr((*uint8)(nil)).Elem())
69 if typeName != "uint16" {
70 return encoderOfType(ctx, reflect2.TypeOfPtr((*uint16)(nil)).Elem())
74 if typeName != "uint32" {
75 return encoderOfType(ctx, reflect2.TypeOfPtr((*uint32)(nil)).Elem())
79 if typeName != "uintptr" {
80 return encoderOfType(ctx, reflect2.TypeOfPtr((*uintptr)(nil)).Elem())
87 if typeName != "uint64" {
88 return encoderOfType(ctx, reflect2.TypeOfPtr((*uint64)(nil)).Elem())
92 if typeName != "float32" {
93 return encoderOfType(ctx, reflect2.TypeOfPtr((*float32)(nil)).Elem())
95 return &float32Codec{}
97 if typeName != "float64" {
98 return encoderOfType(ctx, reflect2.TypeOfPtr((*float64)(nil)).Elem())
100 return &float64Codec{}
102 if typeName != "bool" {
103 return encoderOfType(ctx, reflect2.TypeOfPtr((*bool)(nil)).Elem())
110 func createDecoderOfNative(ctx *ctx, typ reflect2.Type) ValDecoder {
111 if typ.Kind() == reflect.Slice && typ.(reflect2.SliceType).Elem().Kind() == reflect.Uint8 {
112 sliceDecoder := decoderOfSlice(ctx, typ)
113 return &base64Codec{sliceDecoder: sliceDecoder}
115 typeName := typ.String()
118 if typeName != "string" {
119 return decoderOfType(ctx, reflect2.TypeOfPtr((*string)(nil)).Elem())
121 return &stringCodec{}
123 if typeName != "int" {
124 return decoderOfType(ctx, reflect2.TypeOfPtr((*int)(nil)).Elem())
126 if strconv.IntSize == 32 {
131 if typeName != "int8" {
132 return decoderOfType(ctx, reflect2.TypeOfPtr((*int8)(nil)).Elem())
136 if typeName != "int16" {
137 return decoderOfType(ctx, reflect2.TypeOfPtr((*int16)(nil)).Elem())
141 if typeName != "int32" {
142 return decoderOfType(ctx, reflect2.TypeOfPtr((*int32)(nil)).Elem())
146 if typeName != "int64" {
147 return decoderOfType(ctx, reflect2.TypeOfPtr((*int64)(nil)).Elem())
151 if typeName != "uint" {
152 return decoderOfType(ctx, reflect2.TypeOfPtr((*uint)(nil)).Elem())
154 if strconv.IntSize == 32 {
155 return &uint32Codec{}
157 return &uint64Codec{}
159 if typeName != "uint8" {
160 return decoderOfType(ctx, reflect2.TypeOfPtr((*uint8)(nil)).Elem())
164 if typeName != "uint16" {
165 return decoderOfType(ctx, reflect2.TypeOfPtr((*uint16)(nil)).Elem())
167 return &uint16Codec{}
169 if typeName != "uint32" {
170 return decoderOfType(ctx, reflect2.TypeOfPtr((*uint32)(nil)).Elem())
172 return &uint32Codec{}
173 case reflect.Uintptr:
174 if typeName != "uintptr" {
175 return decoderOfType(ctx, reflect2.TypeOfPtr((*uintptr)(nil)).Elem())
178 return &uint32Codec{}
180 return &uint64Codec{}
182 if typeName != "uint64" {
183 return decoderOfType(ctx, reflect2.TypeOfPtr((*uint64)(nil)).Elem())
185 return &uint64Codec{}
186 case reflect.Float32:
187 if typeName != "float32" {
188 return decoderOfType(ctx, reflect2.TypeOfPtr((*float32)(nil)).Elem())
190 return &float32Codec{}
191 case reflect.Float64:
192 if typeName != "float64" {
193 return decoderOfType(ctx, reflect2.TypeOfPtr((*float64)(nil)).Elem())
195 return &float64Codec{}
197 if typeName != "bool" {
198 return decoderOfType(ctx, reflect2.TypeOfPtr((*bool)(nil)).Elem())
205 type stringCodec struct {
208 func (codec *stringCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
209 *((*string)(ptr)) = iter.ReadString()
212 func (codec *stringCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
213 str := *((*string)(ptr))
214 stream.WriteString(str)
217 func (codec *stringCodec) IsEmpty(ptr unsafe.Pointer) bool {
218 return *((*string)(ptr)) == ""
221 type int8Codec struct {
224 func (codec *int8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
226 *((*int8)(ptr)) = iter.ReadInt8()
230 func (codec *int8Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
231 stream.WriteInt8(*((*int8)(ptr)))
234 func (codec *int8Codec) IsEmpty(ptr unsafe.Pointer) bool {
235 return *((*int8)(ptr)) == 0
238 type int16Codec struct {
241 func (codec *int16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
243 *((*int16)(ptr)) = iter.ReadInt16()
247 func (codec *int16Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
248 stream.WriteInt16(*((*int16)(ptr)))
251 func (codec *int16Codec) IsEmpty(ptr unsafe.Pointer) bool {
252 return *((*int16)(ptr)) == 0
255 type int32Codec struct {
258 func (codec *int32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
260 *((*int32)(ptr)) = iter.ReadInt32()
264 func (codec *int32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
265 stream.WriteInt32(*((*int32)(ptr)))
268 func (codec *int32Codec) IsEmpty(ptr unsafe.Pointer) bool {
269 return *((*int32)(ptr)) == 0
272 type int64Codec struct {
275 func (codec *int64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
277 *((*int64)(ptr)) = iter.ReadInt64()
281 func (codec *int64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
282 stream.WriteInt64(*((*int64)(ptr)))
285 func (codec *int64Codec) IsEmpty(ptr unsafe.Pointer) bool {
286 return *((*int64)(ptr)) == 0
289 type uint8Codec struct {
292 func (codec *uint8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
294 *((*uint8)(ptr)) = iter.ReadUint8()
298 func (codec *uint8Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
299 stream.WriteUint8(*((*uint8)(ptr)))
302 func (codec *uint8Codec) IsEmpty(ptr unsafe.Pointer) bool {
303 return *((*uint8)(ptr)) == 0
306 type uint16Codec struct {
309 func (codec *uint16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
311 *((*uint16)(ptr)) = iter.ReadUint16()
315 func (codec *uint16Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
316 stream.WriteUint16(*((*uint16)(ptr)))
319 func (codec *uint16Codec) IsEmpty(ptr unsafe.Pointer) bool {
320 return *((*uint16)(ptr)) == 0
323 type uint32Codec struct {
326 func (codec *uint32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
328 *((*uint32)(ptr)) = iter.ReadUint32()
332 func (codec *uint32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
333 stream.WriteUint32(*((*uint32)(ptr)))
336 func (codec *uint32Codec) IsEmpty(ptr unsafe.Pointer) bool {
337 return *((*uint32)(ptr)) == 0
340 type uint64Codec struct {
343 func (codec *uint64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
345 *((*uint64)(ptr)) = iter.ReadUint64()
349 func (codec *uint64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
350 stream.WriteUint64(*((*uint64)(ptr)))
353 func (codec *uint64Codec) IsEmpty(ptr unsafe.Pointer) bool {
354 return *((*uint64)(ptr)) == 0
357 type float32Codec struct {
360 func (codec *float32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
362 *((*float32)(ptr)) = iter.ReadFloat32()
366 func (codec *float32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
367 stream.WriteFloat32(*((*float32)(ptr)))
370 func (codec *float32Codec) IsEmpty(ptr unsafe.Pointer) bool {
371 return *((*float32)(ptr)) == 0
374 type float64Codec struct {
377 func (codec *float64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
379 *((*float64)(ptr)) = iter.ReadFloat64()
383 func (codec *float64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
384 stream.WriteFloat64(*((*float64)(ptr)))
387 func (codec *float64Codec) IsEmpty(ptr unsafe.Pointer) bool {
388 return *((*float64)(ptr)) == 0
391 type boolCodec struct {
394 func (codec *boolCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
396 *((*bool)(ptr)) = iter.ReadBool()
400 func (codec *boolCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
401 stream.WriteBool(*((*bool)(ptr)))
404 func (codec *boolCodec) IsEmpty(ptr unsafe.Pointer) bool {
405 return !(*((*bool)(ptr)))
408 type base64Codec struct {
409 sliceType *reflect2.UnsafeSliceType
410 sliceDecoder ValDecoder
413 func (codec *base64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
415 codec.sliceType.UnsafeSetNil(ptr)
418 switch iter.WhatIsNext() {
420 src := iter.ReadString()
421 dst, err := base64.StdEncoding.DecodeString(src)
423 iter.ReportError("decode base64", err.Error())
425 codec.sliceType.UnsafeSet(ptr, unsafe.Pointer(&dst))
428 codec.sliceDecoder.Decode(ptr, iter)
430 iter.ReportError("base64Codec", "invalid input")
434 func (codec *base64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
435 src := *((*[]byte)(ptr))
440 encoding := base64.StdEncoding
441 stream.writeByte('"')
442 size := encoding.EncodedLen(len(src))
443 buf := make([]byte, size)
444 encoding.Encode(buf, src)
445 stream.buf = append(stream.buf, buf...)
446 stream.writeByte('"')
449 func (codec *base64Codec) IsEmpty(ptr unsafe.Pointer) bool {
450 return len(*((*[]byte)(ptr))) == 0