Skip to content

Commit 24bb68e

Browse files
committed
rlp: add RawValue
1 parent bc17dba commit 24bb68e

File tree

5 files changed

+39
-1
lines changed

5 files changed

+39
-1
lines changed

rlp/decode.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ var (
173173
func makeDecoder(typ reflect.Type, tags tags) (dec decoder, err error) {
174174
kind := typ.Kind()
175175
switch {
176+
case typ == rawValueType:
177+
return decodeRawValue, nil
176178
case typ.Implements(decoderInterface):
177179
return decodeDecoder, nil
178180
case kind != reflect.Ptr && reflect.PtrTo(typ).Implements(decoderInterface):
@@ -203,6 +205,15 @@ func makeDecoder(typ reflect.Type, tags tags) (dec decoder, err error) {
203205
}
204206
}
205207

208+
func decodeRawValue(s *Stream, val reflect.Value) error {
209+
r, err := s.Raw()
210+
if err != nil {
211+
return err
212+
}
213+
val.SetBytes(r)
214+
return nil
215+
}
216+
206217
func decodeUint(s *Stream, val reflect.Value) error {
207218
typ := val.Type()
208219
num, err := s.uint(typ.Bits())

rlp/decode_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,11 @@ var decodeTests = []decodeTest{
438438
error: "rlp: expected input string or byte for uint, decoding into (rlp.recstruct).Child.I",
439439
},
440440

441+
// RawValue
442+
{input: "01", ptr: new(RawValue), value: RawValue(unhex("01"))},
443+
{input: "82FFFF", ptr: new(RawValue), value: RawValue(unhex("82FFFF"))},
444+
{input: "C20102", ptr: new([]RawValue), value: []RawValue{unhex("01"), unhex("02")}},
445+
441446
// pointers
442447
{input: "00", ptr: new(*[]byte), value: &[]byte{0}},
443448
{input: "80", ptr: new(*uint), value: uintp(0)},

rlp/encode.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ var (
354354
func makeWriter(typ reflect.Type) (writer, error) {
355355
kind := typ.Kind()
356356
switch {
357+
case typ == rawValueType:
358+
return writeRawValue, nil
357359
case typ.Implements(encoderInterface):
358360
return writeEncoder, nil
359361
case kind != reflect.Ptr && reflect.PtrTo(typ).Implements(encoderInterface):
@@ -389,6 +391,11 @@ func isByte(typ reflect.Type) bool {
389391
return typ.Kind() == reflect.Uint8 && !typ.Implements(encoderInterface)
390392
}
391393

394+
func writeRawValue(val reflect.Value, w *encbuf) error {
395+
w.str = append(w.str, val.Bytes()...)
396+
return nil
397+
}
398+
392399
func writeUint(val reflect.Value, w *encbuf) error {
393400
i := val.Uint()
394401
if i == 0 {

rlp/encode_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ var encTests = []encTest{
204204
output: "F90200CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376",
205205
},
206206

207+
// RawValue
208+
{val: RawValue(unhex("01")), output: "01"},
209+
{val: RawValue(unhex("82FFFF")), output: "82FFFF"},
210+
{val: []RawValue{unhex("01"), unhex("02")}, output: "C20102"},
211+
207212
// structs
208213
{val: simplestruct{}, output: "C28080"},
209214
{val: simplestruct{A: 3, B: "foo"}, output: "C50383666F6F"},

rlp/raw.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,17 @@
1616

1717
package rlp
1818

19-
import "io"
19+
import (
20+
"io"
21+
"reflect"
22+
)
23+
24+
// RawValue represents an encoded RLP value and can be used to delay
25+
// RLP decoding or precompute an encoding. Note that the decoder does
26+
// not verify whether the content of RawValues is valid RLP.
27+
type RawValue []byte
28+
29+
var rawValueType = reflect.TypeOf(RawValue{})
2030

2131
// Split returns the content of first RLP value and any
2232
// bytes after the value as subslices of b.

0 commit comments

Comments
 (0)