@@ -32,11 +32,9 @@ type intCodec struct {
32
32
}
33
33
34
34
func (codec * intCodec ) Decode (ptr unsafe.Pointer , iter * Iterator ) {
35
- if iter .ReadNil () {
36
- * ((* int )(ptr )) = 0
37
- return
35
+ if ! iter .ReadNil () {
36
+ * ((* int )(ptr )) = iter .ReadInt ()
38
37
}
39
- * ((* int )(ptr )) = iter .ReadInt ()
40
38
}
41
39
42
40
func (codec * intCodec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -55,11 +53,9 @@ type uintptrCodec struct {
55
53
}
56
54
57
55
func (codec * uintptrCodec ) Decode (ptr unsafe.Pointer , iter * Iterator ) {
58
- if iter .ReadNil () {
59
- * ((* uintptr )(ptr )) = 0
60
- return
56
+ if ! iter .ReadNil () {
57
+ * ((* uintptr )(ptr )) = uintptr (iter .ReadUint64 ())
61
58
}
62
- * ((* uintptr )(ptr )) = uintptr (iter .ReadUint64 ())
63
59
}
64
60
65
61
func (codec * uintptrCodec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -78,11 +74,9 @@ type int8Codec struct {
78
74
}
79
75
80
76
func (codec * int8Codec ) Decode (ptr unsafe.Pointer , iter * Iterator ) {
81
- if iter .ReadNil () {
82
- * ((* uint8 )(ptr )) = 0
83
- return
77
+ if ! iter .ReadNil () {
78
+ * ((* int8 )(ptr )) = iter .ReadInt8 ()
84
79
}
85
- * ((* int8 )(ptr )) = iter .ReadInt8 ()
86
80
}
87
81
88
82
func (codec * int8Codec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -101,11 +95,9 @@ type int16Codec struct {
101
95
}
102
96
103
97
func (codec * int16Codec ) Decode (ptr unsafe.Pointer , iter * Iterator ) {
104
- if iter .ReadNil () {
105
- * ((* int16 )(ptr )) = 0
106
- return
98
+ if ! iter .ReadNil () {
99
+ * ((* int16 )(ptr )) = iter .ReadInt16 ()
107
100
}
108
- * ((* int16 )(ptr )) = iter .ReadInt16 ()
109
101
}
110
102
111
103
func (codec * int16Codec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -124,11 +116,9 @@ type int32Codec struct {
124
116
}
125
117
126
118
func (codec * int32Codec ) Decode (ptr unsafe.Pointer , iter * Iterator ) {
127
- if iter .ReadNil () {
128
- * ((* int32 )(ptr )) = 0
129
- return
119
+ if ! iter .ReadNil () {
120
+ * ((* int32 )(ptr )) = iter .ReadInt32 ()
130
121
}
131
- * ((* int32 )(ptr )) = iter .ReadInt32 ()
132
122
}
133
123
134
124
func (codec * int32Codec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -147,11 +137,9 @@ type int64Codec struct {
147
137
}
148
138
149
139
func (codec * int64Codec ) Decode (ptr unsafe.Pointer , iter * Iterator ) {
150
- if iter .ReadNil () {
151
- * ((* int64 )(ptr )) = 0
152
- return
140
+ if ! iter .ReadNil () {
141
+ * ((* int64 )(ptr )) = iter .ReadInt64 ()
153
142
}
154
- * ((* int64 )(ptr )) = iter .ReadInt64 ()
155
143
}
156
144
157
145
func (codec * int64Codec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -170,11 +158,10 @@ type uintCodec struct {
170
158
}
171
159
172
160
func (codec * uintCodec ) Decode (ptr unsafe.Pointer , iter * Iterator ) {
173
- if iter .ReadNil () {
174
- * ((* uint )(ptr )) = 0
161
+ if ! iter .ReadNil () {
162
+ * ((* uint )(ptr )) = iter . ReadUint ()
175
163
return
176
164
}
177
- * ((* uint )(ptr )) = iter .ReadUint ()
178
165
}
179
166
180
167
func (codec * uintCodec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -193,11 +180,9 @@ type uint8Codec struct {
193
180
}
194
181
195
182
func (codec * uint8Codec ) Decode (ptr unsafe.Pointer , iter * Iterator ) {
196
- if iter .ReadNil () {
197
- * ((* uint8 )(ptr )) = 0
198
- return
183
+ if ! iter .ReadNil () {
184
+ * ((* uint8 )(ptr )) = iter .ReadUint8 ()
199
185
}
200
- * ((* uint8 )(ptr )) = iter .ReadUint8 ()
201
186
}
202
187
203
188
func (codec * uint8Codec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -216,11 +201,9 @@ type uint16Codec struct {
216
201
}
217
202
218
203
func (codec * uint16Codec ) Decode (ptr unsafe.Pointer , iter * Iterator ) {
219
- if iter .ReadNil () {
220
- * ((* uint16 )(ptr )) = 0
221
- return
204
+ if ! iter .ReadNil () {
205
+ * ((* uint16 )(ptr )) = iter .ReadUint16 ()
222
206
}
223
- * ((* uint16 )(ptr )) = iter .ReadUint16 ()
224
207
}
225
208
226
209
func (codec * uint16Codec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -239,11 +222,9 @@ type uint32Codec struct {
239
222
}
240
223
241
224
func (codec * uint32Codec ) Decode (ptr unsafe.Pointer , iter * Iterator ) {
242
- if iter .ReadNil () {
243
- * ((* uint32 )(ptr )) = 0
244
- return
225
+ if ! iter .ReadNil () {
226
+ * ((* uint32 )(ptr )) = iter .ReadUint32 ()
245
227
}
246
- * ((* uint32 )(ptr )) = iter .ReadUint32 ()
247
228
}
248
229
249
230
func (codec * uint32Codec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -262,11 +243,9 @@ type uint64Codec struct {
262
243
}
263
244
264
245
func (codec * uint64Codec ) Decode (ptr unsafe.Pointer , iter * Iterator ) {
265
- if iter .ReadNil () {
266
- * ((* uint64 )(ptr )) = 0
267
- return
246
+ if ! iter .ReadNil () {
247
+ * ((* uint64 )(ptr )) = iter .ReadUint64 ()
268
248
}
269
- * ((* uint64 )(ptr )) = iter .ReadUint64 ()
270
249
}
271
250
272
251
func (codec * uint64Codec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -285,11 +264,9 @@ type float32Codec struct {
285
264
}
286
265
287
266
func (codec * float32Codec ) Decode (ptr unsafe.Pointer , iter * Iterator ) {
288
- if iter .ReadNil () {
289
- * ((* float32 )(ptr )) = 0
290
- return
267
+ if ! iter .ReadNil () {
268
+ * ((* float32 )(ptr )) = iter .ReadFloat32 ()
291
269
}
292
- * ((* float32 )(ptr )) = iter .ReadFloat32 ()
293
270
}
294
271
295
272
func (codec * float32Codec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -308,11 +285,9 @@ type float64Codec struct {
308
285
}
309
286
310
287
func (codec * float64Codec ) Decode (ptr unsafe.Pointer , iter * Iterator ) {
311
- if iter .ReadNil () {
312
- * ((* float64 )(ptr )) = 0
313
- return
288
+ if ! iter .ReadNil () {
289
+ * ((* float64 )(ptr )) = iter .ReadFloat64 ()
314
290
}
315
- * ((* float64 )(ptr )) = iter .ReadFloat64 ()
316
291
}
317
292
318
293
func (codec * float64Codec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -352,13 +327,39 @@ type emptyInterfaceCodec struct {
352
327
}
353
328
354
329
func (codec * emptyInterfaceCodec ) Decode (ptr unsafe.Pointer , iter * Iterator ) {
355
- if iter .ReadNil () {
356
- * ((* interface {})(ptr )) = nil
330
+ existing := * ((* interface {})(ptr ))
331
+
332
+ // Checking for both typed and untyped nil pointers.
333
+ if existing != nil &&
334
+ reflect .TypeOf (existing ).Kind () == reflect .Ptr &&
335
+ ! reflect .ValueOf (existing ).IsNil () {
336
+
337
+ var ptrToExisting interface {}
338
+ for {
339
+ elem := reflect .ValueOf (existing ).Elem ()
340
+ if elem .Kind () != reflect .Ptr || elem .IsNil () {
341
+ break
342
+ }
343
+ ptrToExisting = existing
344
+ existing = elem .Interface ()
345
+ }
346
+
347
+ if iter .ReadNil () {
348
+ if ptrToExisting != nil {
349
+ nilPtr := reflect .Zero (reflect .TypeOf (ptrToExisting ).Elem ())
350
+ reflect .ValueOf (ptrToExisting ).Elem ().Set (nilPtr )
351
+ } else {
352
+ * ((* interface {})(ptr )) = nil
353
+ }
354
+ } else {
355
+ iter .ReadVal (existing )
356
+ }
357
+
357
358
return
358
359
}
359
- existing := * (( * interface {})( ptr ))
360
- if existing != nil && reflect . TypeOf ( existing ). Kind () == reflect . Ptr {
361
- iter . ReadVal ( existing )
360
+
361
+ if iter . ReadNil () {
362
+ * (( * interface {})( ptr )) = nil
362
363
} else {
363
364
* ((* interface {})(ptr )) = iter .Read ()
364
365
}
0 commit comments