podman
453 строки · 10.9 Кб
1package jsoniter
2
3import (
4"encoding/base64"
5"reflect"
6"strconv"
7"unsafe"
8
9"github.com/modern-go/reflect2"
10)
11
12const ptrSize = 32 << uintptr(^uintptr(0)>>63)
13
14func createEncoderOfNative(ctx *ctx, typ reflect2.Type) ValEncoder {
15if typ.Kind() == reflect.Slice && typ.(reflect2.SliceType).Elem().Kind() == reflect.Uint8 {
16sliceDecoder := decoderOfSlice(ctx, typ)
17return &base64Codec{sliceDecoder: sliceDecoder}
18}
19typeName := typ.String()
20kind := typ.Kind()
21switch kind {
22case reflect.String:
23if typeName != "string" {
24return encoderOfType(ctx, reflect2.TypeOfPtr((*string)(nil)).Elem())
25}
26return &stringCodec{}
27case reflect.Int:
28if typeName != "int" {
29return encoderOfType(ctx, reflect2.TypeOfPtr((*int)(nil)).Elem())
30}
31if strconv.IntSize == 32 {
32return &int32Codec{}
33}
34return &int64Codec{}
35case reflect.Int8:
36if typeName != "int8" {
37return encoderOfType(ctx, reflect2.TypeOfPtr((*int8)(nil)).Elem())
38}
39return &int8Codec{}
40case reflect.Int16:
41if typeName != "int16" {
42return encoderOfType(ctx, reflect2.TypeOfPtr((*int16)(nil)).Elem())
43}
44return &int16Codec{}
45case reflect.Int32:
46if typeName != "int32" {
47return encoderOfType(ctx, reflect2.TypeOfPtr((*int32)(nil)).Elem())
48}
49return &int32Codec{}
50case reflect.Int64:
51if typeName != "int64" {
52return encoderOfType(ctx, reflect2.TypeOfPtr((*int64)(nil)).Elem())
53}
54return &int64Codec{}
55case reflect.Uint:
56if typeName != "uint" {
57return encoderOfType(ctx, reflect2.TypeOfPtr((*uint)(nil)).Elem())
58}
59if strconv.IntSize == 32 {
60return &uint32Codec{}
61}
62return &uint64Codec{}
63case reflect.Uint8:
64if typeName != "uint8" {
65return encoderOfType(ctx, reflect2.TypeOfPtr((*uint8)(nil)).Elem())
66}
67return &uint8Codec{}
68case reflect.Uint16:
69if typeName != "uint16" {
70return encoderOfType(ctx, reflect2.TypeOfPtr((*uint16)(nil)).Elem())
71}
72return &uint16Codec{}
73case reflect.Uint32:
74if typeName != "uint32" {
75return encoderOfType(ctx, reflect2.TypeOfPtr((*uint32)(nil)).Elem())
76}
77return &uint32Codec{}
78case reflect.Uintptr:
79if typeName != "uintptr" {
80return encoderOfType(ctx, reflect2.TypeOfPtr((*uintptr)(nil)).Elem())
81}
82if ptrSize == 32 {
83return &uint32Codec{}
84}
85return &uint64Codec{}
86case reflect.Uint64:
87if typeName != "uint64" {
88return encoderOfType(ctx, reflect2.TypeOfPtr((*uint64)(nil)).Elem())
89}
90return &uint64Codec{}
91case reflect.Float32:
92if typeName != "float32" {
93return encoderOfType(ctx, reflect2.TypeOfPtr((*float32)(nil)).Elem())
94}
95return &float32Codec{}
96case reflect.Float64:
97if typeName != "float64" {
98return encoderOfType(ctx, reflect2.TypeOfPtr((*float64)(nil)).Elem())
99}
100return &float64Codec{}
101case reflect.Bool:
102if typeName != "bool" {
103return encoderOfType(ctx, reflect2.TypeOfPtr((*bool)(nil)).Elem())
104}
105return &boolCodec{}
106}
107return nil
108}
109
110func createDecoderOfNative(ctx *ctx, typ reflect2.Type) ValDecoder {
111if typ.Kind() == reflect.Slice && typ.(reflect2.SliceType).Elem().Kind() == reflect.Uint8 {
112sliceDecoder := decoderOfSlice(ctx, typ)
113return &base64Codec{sliceDecoder: sliceDecoder}
114}
115typeName := typ.String()
116switch typ.Kind() {
117case reflect.String:
118if typeName != "string" {
119return decoderOfType(ctx, reflect2.TypeOfPtr((*string)(nil)).Elem())
120}
121return &stringCodec{}
122case reflect.Int:
123if typeName != "int" {
124return decoderOfType(ctx, reflect2.TypeOfPtr((*int)(nil)).Elem())
125}
126if strconv.IntSize == 32 {
127return &int32Codec{}
128}
129return &int64Codec{}
130case reflect.Int8:
131if typeName != "int8" {
132return decoderOfType(ctx, reflect2.TypeOfPtr((*int8)(nil)).Elem())
133}
134return &int8Codec{}
135case reflect.Int16:
136if typeName != "int16" {
137return decoderOfType(ctx, reflect2.TypeOfPtr((*int16)(nil)).Elem())
138}
139return &int16Codec{}
140case reflect.Int32:
141if typeName != "int32" {
142return decoderOfType(ctx, reflect2.TypeOfPtr((*int32)(nil)).Elem())
143}
144return &int32Codec{}
145case reflect.Int64:
146if typeName != "int64" {
147return decoderOfType(ctx, reflect2.TypeOfPtr((*int64)(nil)).Elem())
148}
149return &int64Codec{}
150case reflect.Uint:
151if typeName != "uint" {
152return decoderOfType(ctx, reflect2.TypeOfPtr((*uint)(nil)).Elem())
153}
154if strconv.IntSize == 32 {
155return &uint32Codec{}
156}
157return &uint64Codec{}
158case reflect.Uint8:
159if typeName != "uint8" {
160return decoderOfType(ctx, reflect2.TypeOfPtr((*uint8)(nil)).Elem())
161}
162return &uint8Codec{}
163case reflect.Uint16:
164if typeName != "uint16" {
165return decoderOfType(ctx, reflect2.TypeOfPtr((*uint16)(nil)).Elem())
166}
167return &uint16Codec{}
168case reflect.Uint32:
169if typeName != "uint32" {
170return decoderOfType(ctx, reflect2.TypeOfPtr((*uint32)(nil)).Elem())
171}
172return &uint32Codec{}
173case reflect.Uintptr:
174if typeName != "uintptr" {
175return decoderOfType(ctx, reflect2.TypeOfPtr((*uintptr)(nil)).Elem())
176}
177if ptrSize == 32 {
178return &uint32Codec{}
179}
180return &uint64Codec{}
181case reflect.Uint64:
182if typeName != "uint64" {
183return decoderOfType(ctx, reflect2.TypeOfPtr((*uint64)(nil)).Elem())
184}
185return &uint64Codec{}
186case reflect.Float32:
187if typeName != "float32" {
188return decoderOfType(ctx, reflect2.TypeOfPtr((*float32)(nil)).Elem())
189}
190return &float32Codec{}
191case reflect.Float64:
192if typeName != "float64" {
193return decoderOfType(ctx, reflect2.TypeOfPtr((*float64)(nil)).Elem())
194}
195return &float64Codec{}
196case reflect.Bool:
197if typeName != "bool" {
198return decoderOfType(ctx, reflect2.TypeOfPtr((*bool)(nil)).Elem())
199}
200return &boolCodec{}
201}
202return nil
203}
204
205type stringCodec struct {
206}
207
208func (codec *stringCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
209*((*string)(ptr)) = iter.ReadString()
210}
211
212func (codec *stringCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
213str := *((*string)(ptr))
214stream.WriteString(str)
215}
216
217func (codec *stringCodec) IsEmpty(ptr unsafe.Pointer) bool {
218return *((*string)(ptr)) == ""
219}
220
221type int8Codec struct {
222}
223
224func (codec *int8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
225if !iter.ReadNil() {
226*((*int8)(ptr)) = iter.ReadInt8()
227}
228}
229
230func (codec *int8Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
231stream.WriteInt8(*((*int8)(ptr)))
232}
233
234func (codec *int8Codec) IsEmpty(ptr unsafe.Pointer) bool {
235return *((*int8)(ptr)) == 0
236}
237
238type int16Codec struct {
239}
240
241func (codec *int16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
242if !iter.ReadNil() {
243*((*int16)(ptr)) = iter.ReadInt16()
244}
245}
246
247func (codec *int16Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
248stream.WriteInt16(*((*int16)(ptr)))
249}
250
251func (codec *int16Codec) IsEmpty(ptr unsafe.Pointer) bool {
252return *((*int16)(ptr)) == 0
253}
254
255type int32Codec struct {
256}
257
258func (codec *int32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
259if !iter.ReadNil() {
260*((*int32)(ptr)) = iter.ReadInt32()
261}
262}
263
264func (codec *int32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
265stream.WriteInt32(*((*int32)(ptr)))
266}
267
268func (codec *int32Codec) IsEmpty(ptr unsafe.Pointer) bool {
269return *((*int32)(ptr)) == 0
270}
271
272type int64Codec struct {
273}
274
275func (codec *int64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
276if !iter.ReadNil() {
277*((*int64)(ptr)) = iter.ReadInt64()
278}
279}
280
281func (codec *int64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
282stream.WriteInt64(*((*int64)(ptr)))
283}
284
285func (codec *int64Codec) IsEmpty(ptr unsafe.Pointer) bool {
286return *((*int64)(ptr)) == 0
287}
288
289type uint8Codec struct {
290}
291
292func (codec *uint8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
293if !iter.ReadNil() {
294*((*uint8)(ptr)) = iter.ReadUint8()
295}
296}
297
298func (codec *uint8Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
299stream.WriteUint8(*((*uint8)(ptr)))
300}
301
302func (codec *uint8Codec) IsEmpty(ptr unsafe.Pointer) bool {
303return *((*uint8)(ptr)) == 0
304}
305
306type uint16Codec struct {
307}
308
309func (codec *uint16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
310if !iter.ReadNil() {
311*((*uint16)(ptr)) = iter.ReadUint16()
312}
313}
314
315func (codec *uint16Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
316stream.WriteUint16(*((*uint16)(ptr)))
317}
318
319func (codec *uint16Codec) IsEmpty(ptr unsafe.Pointer) bool {
320return *((*uint16)(ptr)) == 0
321}
322
323type uint32Codec struct {
324}
325
326func (codec *uint32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
327if !iter.ReadNil() {
328*((*uint32)(ptr)) = iter.ReadUint32()
329}
330}
331
332func (codec *uint32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
333stream.WriteUint32(*((*uint32)(ptr)))
334}
335
336func (codec *uint32Codec) IsEmpty(ptr unsafe.Pointer) bool {
337return *((*uint32)(ptr)) == 0
338}
339
340type uint64Codec struct {
341}
342
343func (codec *uint64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
344if !iter.ReadNil() {
345*((*uint64)(ptr)) = iter.ReadUint64()
346}
347}
348
349func (codec *uint64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
350stream.WriteUint64(*((*uint64)(ptr)))
351}
352
353func (codec *uint64Codec) IsEmpty(ptr unsafe.Pointer) bool {
354return *((*uint64)(ptr)) == 0
355}
356
357type float32Codec struct {
358}
359
360func (codec *float32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
361if !iter.ReadNil() {
362*((*float32)(ptr)) = iter.ReadFloat32()
363}
364}
365
366func (codec *float32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
367stream.WriteFloat32(*((*float32)(ptr)))
368}
369
370func (codec *float32Codec) IsEmpty(ptr unsafe.Pointer) bool {
371return *((*float32)(ptr)) == 0
372}
373
374type float64Codec struct {
375}
376
377func (codec *float64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
378if !iter.ReadNil() {
379*((*float64)(ptr)) = iter.ReadFloat64()
380}
381}
382
383func (codec *float64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
384stream.WriteFloat64(*((*float64)(ptr)))
385}
386
387func (codec *float64Codec) IsEmpty(ptr unsafe.Pointer) bool {
388return *((*float64)(ptr)) == 0
389}
390
391type boolCodec struct {
392}
393
394func (codec *boolCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
395if !iter.ReadNil() {
396*((*bool)(ptr)) = iter.ReadBool()
397}
398}
399
400func (codec *boolCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
401stream.WriteBool(*((*bool)(ptr)))
402}
403
404func (codec *boolCodec) IsEmpty(ptr unsafe.Pointer) bool {
405return !(*((*bool)(ptr)))
406}
407
408type base64Codec struct {
409sliceType *reflect2.UnsafeSliceType
410sliceDecoder ValDecoder
411}
412
413func (codec *base64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
414if iter.ReadNil() {
415codec.sliceType.UnsafeSetNil(ptr)
416return
417}
418switch iter.WhatIsNext() {
419case StringValue:
420src := iter.ReadString()
421dst, err := base64.StdEncoding.DecodeString(src)
422if err != nil {
423iter.ReportError("decode base64", err.Error())
424} else {
425codec.sliceType.UnsafeSet(ptr, unsafe.Pointer(&dst))
426}
427case ArrayValue:
428codec.sliceDecoder.Decode(ptr, iter)
429default:
430iter.ReportError("base64Codec", "invalid input")
431}
432}
433
434func (codec *base64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
435if codec.sliceType.UnsafeIsNil(ptr) {
436stream.WriteNil()
437return
438}
439src := *((*[]byte)(ptr))
440encoding := base64.StdEncoding
441stream.writeByte('"')
442if len(src) != 0 {
443size := encoding.EncodedLen(len(src))
444buf := make([]byte, size)
445encoding.Encode(buf, src)
446stream.buf = append(stream.buf, buf...)
447}
448stream.writeByte('"')
449}
450
451func (codec *base64Codec) IsEmpty(ptr unsafe.Pointer) bool {
452return len(*((*[]byte)(ptr))) == 0
453}
454