@@ -55,81 +55,23 @@ var (
55
55
}
56
56
)
57
57
58
- // Decoder is implemented by types that require custom RLP
59
- // decoding rules or need to decode into private fields.
58
+ // Decoder is implemented by types that require custom RLP decoding rules or need to decode
59
+ // into private fields.
60
60
//
61
- // The DecodeRLP method should read one value from the given
62
- // Stream. It is not forbidden to read less or more, but it might
63
- // be confusing.
61
+ // The DecodeRLP method should read one value from the given Stream. It is not forbidden to
62
+ // read less or more, but it might be confusing.
64
63
type Decoder interface {
65
64
DecodeRLP (* Stream ) error
66
65
}
67
66
68
- // Decode parses RLP-encoded data from r and stores the result in the
69
- // value pointed to by val . Val must be a non-nil pointer. If r does
70
- // not implement ByteReader, Decode will do its own buffering .
67
+ // Decode parses RLP-encoded data from r and stores the result in the value pointed to by
68
+ // val. Please see package-level documentation for the decoding rules . Val must be a
69
+ // non-nil pointer .
71
70
//
72
- // Decode uses the following type-dependent decoding rules:
71
+ // If r does not implement ByteReader, Decode will do its own buffering.
73
72
//
74
- // If the type implements the Decoder interface, decode calls
75
- // DecodeRLP.
76
- //
77
- // To decode into a pointer, Decode will decode into the value pointed
78
- // to. If the pointer is nil, a new value of the pointer's element
79
- // type is allocated. If the pointer is non-nil, the existing value
80
- // will be reused.
81
- //
82
- // To decode into a struct, Decode expects the input to be an RLP
83
- // list. The decoded elements of the list are assigned to each public
84
- // field in the order given by the struct's definition. The input list
85
- // must contain an element for each decoded field. Decode returns an
86
- // error if there are too few or too many elements.
87
- //
88
- // The decoding of struct fields honours certain struct tags, "tail",
89
- // "nil" and "-".
90
- //
91
- // The "-" tag ignores fields.
92
- //
93
- // For an explanation of "tail", see the example.
94
- //
95
- // The "nil" tag applies to pointer-typed fields and changes the decoding
96
- // rules for the field such that input values of size zero decode as a nil
97
- // pointer. This tag can be useful when decoding recursive types.
98
- //
99
- // type StructWithEmptyOK struct {
100
- // Foo *[20]byte `rlp:"nil"`
101
- // }
102
- //
103
- // To decode into a slice, the input must be a list and the resulting
104
- // slice will contain the input elements in order. For byte slices,
105
- // the input must be an RLP string. Array types decode similarly, with
106
- // the additional restriction that the number of input elements (or
107
- // bytes) must match the array's length.
108
- //
109
- // To decode into a Go string, the input must be an RLP string. The
110
- // input bytes are taken as-is and will not necessarily be valid UTF-8.
111
- //
112
- // To decode into an unsigned integer type, the input must also be an RLP
113
- // string. The bytes are interpreted as a big endian representation of
114
- // the integer. If the RLP string is larger than the bit size of the
115
- // type, Decode will return an error. Decode also supports *big.Int.
116
- // There is no size limit for big integers.
117
- //
118
- // To decode into a boolean, the input must contain an unsigned integer
119
- // of value zero (false) or one (true).
120
- //
121
- // To decode into an interface value, Decode stores one of these
122
- // in the value:
123
- //
124
- // []interface{}, for RLP lists
125
- // []byte, for RLP strings
126
- //
127
- // Non-empty interface types are not supported, nor are signed integers,
128
- // floating point numbers, maps, channels and functions.
129
- //
130
- // Note that Decode does not set an input limit for all readers
131
- // and may be vulnerable to panics cause by huge value sizes. If
132
- // you need an input limit, use
73
+ // Note that Decode does not set an input limit for all readers and may be vulnerable to
74
+ // panics cause by huge value sizes. If you need an input limit, use
133
75
//
134
76
// NewStream(r, limit).Decode(val)
135
77
func Decode (r io.Reader , val interface {}) error {
@@ -140,9 +82,8 @@ func Decode(r io.Reader, val interface{}) error {
140
82
return stream .Decode (val )
141
83
}
142
84
143
- // DecodeBytes parses RLP data from b into val.
144
- // Please see the documentation of Decode for the decoding rules.
145
- // The input must contain exactly one value and no trailing data.
85
+ // DecodeBytes parses RLP data from b into val. Please see package-level documentation for
86
+ // the decoding rules. The input must contain exactly one value and no trailing data.
146
87
func DecodeBytes (b []byte , val interface {}) error {
147
88
r := bytes .NewReader (b )
148
89
@@ -211,14 +152,15 @@ func makeDecoder(typ reflect.Type, tags tags) (dec decoder, err error) {
211
152
switch {
212
153
case typ == rawValueType :
213
154
return decodeRawValue , nil
214
- case typ .Implements (decoderInterface ):
215
155
return decodeDecoder , nil
216
- case kind != reflect .Ptr && reflect .PtrTo (typ ).Implements (decoderInterface ):
217
- return decodeDecoderNoPtr , nil
218
156
case typ .AssignableTo (reflect .PtrTo (bigInt )):
219
157
return decodeBigInt , nil
220
158
case typ .AssignableTo (bigInt ):
221
159
return decodeBigIntNoPtr , nil
160
+ case kind == reflect .Ptr :
161
+ return makePtrDecoder (typ , tags )
162
+ case reflect .PtrTo (typ ).Implements (decoderInterface ):
163
+ return decodeDecoder , nil
222
164
case isUint (kind ):
223
165
return decodeUint , nil
224
166
case kind == reflect .Bool :
@@ -229,11 +171,6 @@ func makeDecoder(typ reflect.Type, tags tags) (dec decoder, err error) {
229
171
return makeListDecoder (typ , tags )
230
172
case kind == reflect .Struct :
231
173
return makeStructDecoder (typ )
232
- case kind == reflect .Ptr :
233
- if tags .nilOK {
234
- return makeOptionalPtrDecoder (typ )
235
- }
236
- return makePtrDecoder (typ )
237
174
case kind == reflect .Interface :
238
175
return decodeInterface , nil
239
176
default :
@@ -448,6 +385,11 @@ func makeStructDecoder(typ reflect.Type) (decoder, error) {
448
385
if err != nil {
449
386
return nil , err
450
387
}
388
+ for _ , f := range fields {
389
+ if f .info .decoderErr != nil {
390
+ return nil , structFieldError {typ , f .index , f .info .decoderErr }
391
+ }
392
+ }
451
393
dec := func (s * Stream , val reflect.Value ) (err error ) {
452
394
if _ , err := s .List (); err != nil {
453
395
return wrapStreamError (err , typ )
@@ -465,15 +407,22 @@ func makeStructDecoder(typ reflect.Type) (decoder, error) {
465
407
return dec , nil
466
408
}
467
409
468
- // makePtrDecoder creates a decoder that decodes into
469
- // the pointer's element type.
470
- func makePtrDecoder (typ reflect.Type ) (decoder , error ) {
410
+ // makePtrDecoder creates a decoder that decodes into the pointer's element type.
411
+ func makePtrDecoder (typ reflect.Type , tag tags ) (decoder , error ) {
471
412
etype := typ .Elem ()
472
413
etypeinfo := cachedTypeInfo1 (etype , tags {})
473
- if etypeinfo .decoderErr != nil {
414
+ switch {
415
+ case etypeinfo .decoderErr != nil :
474
416
return nil , etypeinfo .decoderErr
417
+ case ! tag .nilOK :
418
+ return makeSimplePtrDecoder (etype , etypeinfo ), nil
419
+ default :
420
+ return makeNilPtrDecoder (etype , etypeinfo , tag .nilKind ), nil
475
421
}
476
- dec := func (s * Stream , val reflect.Value ) (err error ) {
422
+ }
423
+
424
+ func makeSimplePtrDecoder (etype reflect.Type , etypeinfo * typeinfo ) decoder {
425
+ return func (s * Stream , val reflect.Value ) (err error ) {
477
426
newval := val
478
427
if val .IsNil () {
479
428
newval = reflect .New (etype )
@@ -483,30 +432,35 @@ func makePtrDecoder(typ reflect.Type) (decoder, error) {
483
432
}
484
433
return err
485
434
}
486
- return dec , nil
487
435
}
488
436
489
- // makeOptionalPtrDecoder creates a decoder that decodes empty values
490
- // as nil. Non-empty values are decoded into a value of the element type,
491
- // just like makePtrDecoder does.
437
+ // makeNilPtrDecoder creates a decoder that decodes empty values as nil. Non-empty
438
+ // values are decoded into a value of the element type, just like makePtrDecoder does.
492
439
//
493
440
// This decoder is used for pointer-typed struct fields with struct tag "nil".
494
- func makeOptionalPtrDecoder (typ reflect.Type ) (decoder , error ) {
495
- etype := typ .Elem ()
496
- etypeinfo := cachedTypeInfo1 (etype , tags {})
497
- if etypeinfo .decoderErr != nil {
498
- return nil , etypeinfo .decoderErr
499
- }
500
- dec := func (s * Stream , val reflect.Value ) (err error ) {
441
+ func makeNilPtrDecoder (etype reflect.Type , etypeinfo * typeinfo , nilKind Kind ) decoder {
442
+ typ := reflect .PtrTo (etype )
443
+ nilPtr := reflect .Zero (typ )
444
+ return func (s * Stream , val reflect.Value ) (err error ) {
501
445
kind , size , err := s .Kind ()
502
- if err != nil || size == 0 && kind != Byte {
446
+ if err != nil {
447
+ val .Set (nilPtr )
448
+ return wrapStreamError (err , typ )
449
+ }
450
+ // Handle empty values as a nil pointer.
451
+ if kind != Byte && size == 0 {
452
+ if kind != nilKind {
453
+ return & decodeError {
454
+ msg : fmt .Sprintf ("wrong kind of empty value (got %v, want %v)" , kind , nilKind ),
455
+ typ : typ ,
456
+ }
457
+ }
503
458
// rearm s.Kind. This is important because the input
504
459
// position must advance to the next value even though
505
460
// we don't read anything.
506
461
s .kind = - 1
507
- // set the pointer to nil.
508
- val .Set (reflect .Zero (typ ))
509
- return err
462
+ val .Set (nilPtr )
463
+ return nil
510
464
}
511
465
newval := val
512
466
if val .IsNil () {
@@ -517,7 +471,6 @@ func makeOptionalPtrDecoder(typ reflect.Type) (decoder, error) {
517
471
}
518
472
return err
519
473
}
520
- return dec , nil
521
474
}
522
475
523
476
var ifsliceType = reflect .TypeOf ([]interface {}{})
@@ -546,21 +499,8 @@ func decodeInterface(s *Stream, val reflect.Value) error {
546
499
return nil
547
500
}
548
501
549
- // This decoder is used for non-pointer values of types
550
- // that implement the Decoder interface using a pointer receiver.
551
- func decodeDecoderNoPtr (s * Stream , val reflect.Value ) error {
552
- return val .Addr ().Interface ().(Decoder ).DecodeRLP (s )
553
- }
554
-
555
502
func decodeDecoder (s * Stream , val reflect.Value ) error {
556
- // Decoder instances are not handled using the pointer rule if the type
557
- // implements Decoder with pointer receiver (i.e. always)
558
- // because it might handle empty values specially.
559
- // We need to allocate one here in this case, like makePtrDecoder does.
560
- if val .Kind () == reflect .Ptr && val .IsNil () {
561
- val .Set (reflect .New (val .Type ().Elem ()))
562
- }
563
- return val .Interface ().(Decoder ).DecodeRLP (s )
503
+ return val .Addr ().Interface ().(Decoder ).DecodeRLP (s )
564
504
}
565
505
566
506
// Kind represents the kind of value contained in an RLP stream.
0 commit comments