@@ -5,8 +5,11 @@ import (
5
5
"reflect"
6
6
"unsafe"
7
7
"github.com/v2pro/plz/reflect2"
8
+ "strconv"
8
9
)
9
10
11
+ const ptrSize = 32 << uintptr (^ uintptr (0 )>> 63 )
12
+
10
13
func createEncoderOfNative (ctx * ctx , typ reflect2.Type ) ValEncoder {
11
14
if typ .Kind () == reflect .Slice && typ .(reflect2.SliceType ).Elem ().Kind () == reflect .Uint8 {
12
15
sliceDecoder := decoderOfSlice (ctx , typ )
@@ -24,7 +27,10 @@ func createEncoderOfNative(ctx *ctx, typ reflect2.Type) ValEncoder {
24
27
if typeName != "int" {
25
28
return encoderOfType (ctx , reflect2 .TypeOfPtr ((* int )(nil )).Elem ())
26
29
}
27
- return & intCodec {}
30
+ if strconv .IntSize == 32 {
31
+ return & int32Codec {}
32
+ }
33
+ return & int64Codec {}
28
34
case reflect .Int8 :
29
35
if typeName != "int8" {
30
36
return encoderOfType (ctx , reflect2 .TypeOfPtr ((* int8 )(nil )).Elem ())
@@ -49,7 +55,10 @@ func createEncoderOfNative(ctx *ctx, typ reflect2.Type) ValEncoder {
49
55
if typeName != "uint" {
50
56
return encoderOfType (ctx , reflect2 .TypeOfPtr ((* uint )(nil )).Elem ())
51
57
}
52
- return & uintCodec {}
58
+ if strconv .IntSize == 32 {
59
+ return & uint32Codec {}
60
+ }
61
+ return & uint64Codec {}
53
62
case reflect .Uint8 :
54
63
if typeName != "uint8" {
55
64
return encoderOfType (ctx , reflect2 .TypeOfPtr ((* uint8 )(nil )).Elem ())
@@ -69,7 +78,10 @@ func createEncoderOfNative(ctx *ctx, typ reflect2.Type) ValEncoder {
69
78
if typeName != "uintptr" {
70
79
return encoderOfType (ctx , reflect2 .TypeOfPtr ((* uintptr )(nil )).Elem ())
71
80
}
72
- return & uintptrCodec {}
81
+ if ptrSize == 32 {
82
+ return & uint32Codec {}
83
+ }
84
+ return & uint64Codec {}
73
85
case reflect .Uint64 :
74
86
if typeName != "uint64" {
75
87
return encoderOfType (ctx , reflect2 .TypeOfPtr ((* uint64 )(nil )).Elem ())
@@ -110,7 +122,10 @@ func createDecoderOfNative(ctx *ctx, typ reflect2.Type) ValDecoder {
110
122
if typeName != "int" {
111
123
return decoderOfType (ctx , reflect2 .TypeOfPtr ((* int )(nil )).Elem ())
112
124
}
113
- return & intCodec {}
125
+ if strconv .IntSize == 32 {
126
+ return & int32Codec {}
127
+ }
128
+ return & int64Codec {}
114
129
case reflect .Int8 :
115
130
if typeName != "int8" {
116
131
return decoderOfType (ctx , reflect2 .TypeOfPtr ((* int8 )(nil )).Elem ())
@@ -135,7 +150,10 @@ func createDecoderOfNative(ctx *ctx, typ reflect2.Type) ValDecoder {
135
150
if typeName != "uint" {
136
151
return decoderOfType (ctx , reflect2 .TypeOfPtr ((* uint )(nil )).Elem ())
137
152
}
138
- return & uintCodec {}
153
+ if strconv .IntSize == 32 {
154
+ return & uint32Codec {}
155
+ }
156
+ return & uint64Codec {}
139
157
case reflect .Uint8 :
140
158
if typeName != "uint8" {
141
159
return decoderOfType (ctx , reflect2 .TypeOfPtr ((* uint8 )(nil )).Elem ())
@@ -155,7 +173,10 @@ func createDecoderOfNative(ctx *ctx, typ reflect2.Type) ValDecoder {
155
173
if typeName != "uintptr" {
156
174
return decoderOfType (ctx , reflect2 .TypeOfPtr ((* uintptr )(nil )).Elem ())
157
175
}
158
- return & uintptrCodec {}
176
+ if ptrSize == 32 {
177
+ return & uint32Codec {}
178
+ }
179
+ return & uint64Codec {}
159
180
case reflect .Uint64 :
160
181
if typeName != "uint64" {
161
182
return decoderOfType (ctx , reflect2 .TypeOfPtr ((* uint64 )(nil )).Elem ())
@@ -196,40 +217,6 @@ func (codec *stringCodec) IsEmpty(ptr unsafe.Pointer) bool {
196
217
return * ((* string )(ptr )) == ""
197
218
}
198
219
199
- type intCodec struct {
200
- }
201
-
202
- func (codec * intCodec ) Decode (ptr unsafe.Pointer , iter * Iterator ) {
203
- if ! iter .ReadNil () {
204
- * ((* int )(ptr )) = iter .ReadInt ()
205
- }
206
- }
207
-
208
- func (codec * intCodec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
209
- stream .WriteInt (* ((* int )(ptr )))
210
- }
211
-
212
- func (codec * intCodec ) IsEmpty (ptr unsafe.Pointer ) bool {
213
- return * ((* int )(ptr )) == 0
214
- }
215
-
216
- type uintptrCodec struct {
217
- }
218
-
219
- func (codec * uintptrCodec ) Decode (ptr unsafe.Pointer , iter * Iterator ) {
220
- if ! iter .ReadNil () {
221
- * ((* uintptr )(ptr )) = uintptr (iter .ReadUint64 ())
222
- }
223
- }
224
-
225
- func (codec * uintptrCodec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
226
- stream .WriteUint64 (uint64 (* ((* uintptr )(ptr ))))
227
- }
228
-
229
- func (codec * uintptrCodec ) IsEmpty (ptr unsafe.Pointer ) bool {
230
- return * ((* uintptr )(ptr )) == 0
231
- }
232
-
233
220
type int8Codec struct {
234
221
}
235
222
@@ -298,24 +285,6 @@ func (codec *int64Codec) IsEmpty(ptr unsafe.Pointer) bool {
298
285
return * ((* int64 )(ptr )) == 0
299
286
}
300
287
301
- type uintCodec struct {
302
- }
303
-
304
- func (codec * uintCodec ) Decode (ptr unsafe.Pointer , iter * Iterator ) {
305
- if ! iter .ReadNil () {
306
- * ((* uint )(ptr )) = iter .ReadUint ()
307
- return
308
- }
309
- }
310
-
311
- func (codec * uintCodec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
312
- stream .WriteUint (* ((* uint )(ptr )))
313
- }
314
-
315
- func (codec * uintCodec ) IsEmpty (ptr unsafe.Pointer ) bool {
316
- return * ((* uint )(ptr )) == 0
317
- }
318
-
319
288
type uint8Codec struct {
320
289
}
321
290
0 commit comments