@@ -32,11 +32,9 @@ type intCodec struct {
3232}
3333
3434func (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 ()
3837 }
39- * ((* int )(ptr )) = iter .ReadInt ()
4038}
4139
4240func (codec * intCodec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -55,11 +53,9 @@ type uintptrCodec struct {
5553}
5654
5755func (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 ())
6158 }
62- * ((* uintptr )(ptr )) = uintptr (iter .ReadUint64 ())
6359}
6460
6561func (codec * uintptrCodec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -78,11 +74,9 @@ type int8Codec struct {
7874}
7975
8076func (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 ()
8479 }
85- * ((* int8 )(ptr )) = iter .ReadInt8 ()
8680}
8781
8882func (codec * int8Codec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -101,11 +95,9 @@ type int16Codec struct {
10195}
10296
10397func (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 ()
107100 }
108- * ((* int16 )(ptr )) = iter .ReadInt16 ()
109101}
110102
111103func (codec * int16Codec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -124,11 +116,9 @@ type int32Codec struct {
124116}
125117
126118func (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 ()
130121 }
131- * ((* int32 )(ptr )) = iter .ReadInt32 ()
132122}
133123
134124func (codec * int32Codec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -147,11 +137,9 @@ type int64Codec struct {
147137}
148138
149139func (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 ()
153142 }
154- * ((* int64 )(ptr )) = iter .ReadInt64 ()
155143}
156144
157145func (codec * int64Codec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -170,11 +158,10 @@ type uintCodec struct {
170158}
171159
172160func (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 ()
175163 return
176164 }
177- * ((* uint )(ptr )) = iter .ReadUint ()
178165}
179166
180167func (codec * uintCodec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -193,11 +180,9 @@ type uint8Codec struct {
193180}
194181
195182func (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 ()
199185 }
200- * ((* uint8 )(ptr )) = iter .ReadUint8 ()
201186}
202187
203188func (codec * uint8Codec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -216,11 +201,9 @@ type uint16Codec struct {
216201}
217202
218203func (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 ()
222206 }
223- * ((* uint16 )(ptr )) = iter .ReadUint16 ()
224207}
225208
226209func (codec * uint16Codec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -239,11 +222,9 @@ type uint32Codec struct {
239222}
240223
241224func (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 ()
245227 }
246- * ((* uint32 )(ptr )) = iter .ReadUint32 ()
247228}
248229
249230func (codec * uint32Codec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -262,11 +243,9 @@ type uint64Codec struct {
262243}
263244
264245func (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 ()
268248 }
269- * ((* uint64 )(ptr )) = iter .ReadUint64 ()
270249}
271250
272251func (codec * uint64Codec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -285,11 +264,9 @@ type float32Codec struct {
285264}
286265
287266func (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 ()
291269 }
292- * ((* float32 )(ptr )) = iter .ReadFloat32 ()
293270}
294271
295272func (codec * float32Codec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -308,11 +285,9 @@ type float64Codec struct {
308285}
309286
310287func (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 ()
314290 }
315- * ((* float64 )(ptr )) = iter .ReadFloat64 ()
316291}
317292
318293func (codec * float64Codec ) Encode (ptr unsafe.Pointer , stream * Stream ) {
@@ -352,13 +327,39 @@ type emptyInterfaceCodec struct {
352327}
353328
354329func (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+
357358 return
358359 }
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
362363 } else {
363364 * ((* interface {})(ptr )) = iter .Read ()
364365 }
0 commit comments