mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-24 21:56:43 +00:00
rlp: rename ".." to "tail"
This commit is contained in:
parent
0b84e3224f
commit
99fe66973f
6 changed files with 76 additions and 41 deletions
|
|
@ -63,8 +63,9 @@ type Decoder interface {
|
||||||
// must contain an element for each decoded field. Decode returns an
|
// must contain an element for each decoded field. Decode returns an
|
||||||
// error if there are too few or too many elements.
|
// error if there are too few or too many elements.
|
||||||
//
|
//
|
||||||
// The decoding of struct fields honours two struct tags, "nil" and
|
// The decoding of struct fields honours two struct tags, "tail" and
|
||||||
// "..". The "nil" tag applies to pointer-typed fields and changes the
|
// "nil". For an explanation of "tail", see the example.
|
||||||
|
// The "nil" tag applies to pointer-typed fields and changes the
|
||||||
// decoding rules for the field such that input values of size zero
|
// decoding rules for the field such that input values of size zero
|
||||||
// decode as a nil pointer. This tag can be useful when decoding
|
// decode as a nil pointer. This tag can be useful when decoding
|
||||||
// recursive types.
|
// recursive types.
|
||||||
|
|
@ -283,8 +284,8 @@ func makeListDecoder(typ reflect.Type, tag tags) (decoder, error) {
|
||||||
dec = func(s *Stream, val reflect.Value) error {
|
dec = func(s *Stream, val reflect.Value) error {
|
||||||
return decodeListArray(s, val, etypeinfo.decoder)
|
return decodeListArray(s, val, etypeinfo.decoder)
|
||||||
}
|
}
|
||||||
case tag.dotdot:
|
case tag.tail:
|
||||||
// A slice with .. tag can occur as the last field
|
// A slice with "tail" tag can occur as the last field
|
||||||
// of a struct and is upposed to swallow all remaining
|
// of a struct and is upposed to swallow all remaining
|
||||||
// list elements. The struct decoder already called s.List,
|
// list elements. The struct decoder already called s.List,
|
||||||
// proceed directly to decoding the elements.
|
// proceed directly to decoding the elements.
|
||||||
|
|
|
||||||
|
|
@ -5,15 +5,15 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type structWithDotDot struct {
|
type structWithTail struct {
|
||||||
A, B uint
|
A, B uint
|
||||||
C []uint `rlp:".."`
|
C []uint `rlp:"tail"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleDecode_structTagDotDot() {
|
func ExampleDecode_structTagTail() {
|
||||||
// In this example, the ".." struct tag is used to decode lists of
|
// In this example, the "tail" struct tag is used to decode lists of
|
||||||
// differing length into a struct.
|
// differing length into a struct.
|
||||||
var val structWithDotDot
|
var val structWithTail
|
||||||
|
|
||||||
err := Decode(bytes.NewReader([]byte{0xC4, 0x01, 0x02, 0x03, 0x04}), &val)
|
err := Decode(bytes.NewReader([]byte{0xC4, 0x01, 0x02, 0x03, 0x04}), &val)
|
||||||
fmt.Printf("with 4 elements: err=%v val=%v\n", err, val)
|
fmt.Printf("with 4 elements: err=%v val=%v\n", err, val)
|
||||||
|
|
@ -29,5 +29,5 @@ func ExampleDecode_structTagDotDot() {
|
||||||
// Output:
|
// Output:
|
||||||
// with 4 elements: err=<nil> val={1 2 [3 4]}
|
// with 4 elements: err=<nil> val={1 2 [3 4]}
|
||||||
// with 6 elements: err=<nil> val={1 2 [3 4 5 6]}
|
// with 6 elements: err=<nil> val={1 2 [3 4 5 6]}
|
||||||
// with 1 element: err="rlp: too few elements for rlp.structWithDotDot"
|
// with 1 element: err="rlp: too few elements for rlp.structWithTail"
|
||||||
}
|
}
|
||||||
|
|
@ -312,19 +312,24 @@ type recstruct struct {
|
||||||
Child *recstruct `rlp:"nil"`
|
Child *recstruct `rlp:"nil"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type invalidDotDot1 struct {
|
type invalidTail1 struct {
|
||||||
A uint `rlp:".."`
|
A uint `rlp:"tail"`
|
||||||
B string
|
B string
|
||||||
}
|
}
|
||||||
|
|
||||||
type invalidDotDot2 struct {
|
type invalidTail2 struct {
|
||||||
A uint
|
A uint
|
||||||
B string `rlp:".."`
|
B string `rlp:"tail"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type dotdotRaw struct {
|
type tailRaw struct {
|
||||||
A uint
|
A uint
|
||||||
DotDot []RawValue `rlp:".."`
|
Tail []RawValue `rlp:"tail"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type tailUint struct {
|
||||||
|
A uint
|
||||||
|
Tail []uint `rlp:"tail"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -454,20 +459,35 @@ var decodeTests = []decodeTest{
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "C0",
|
input: "C0",
|
||||||
ptr: new(invalidDotDot1),
|
ptr: new(invalidTail1),
|
||||||
error: "rlp: invalid struct tag \"..\" for rlp.invalidDotDot1.A (must be on last field)",
|
error: "rlp: invalid struct tag \"tail\" for rlp.invalidTail1.A (must be on last field)",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "C0",
|
input: "C0",
|
||||||
ptr: new(invalidDotDot2),
|
ptr: new(invalidTail2),
|
||||||
error: "rlp: invalid struct tag \"..\" for rlp.invalidDotDot2.B (field type is not slice)",
|
error: "rlp: invalid struct tag \"tail\" for rlp.invalidTail2.B (field type is not slice)",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "C50102C20102",
|
||||||
|
ptr: new(tailUint),
|
||||||
|
error: "rlp: expected input string or byte for uint, decoding into (rlp.tailUint).Tail[1]",
|
||||||
},
|
},
|
||||||
|
|
||||||
// struct tag ".."
|
// struct tag "tail"
|
||||||
{
|
{
|
||||||
input: "C3010203",
|
input: "C3010203",
|
||||||
ptr: new(dotdotRaw),
|
ptr: new(tailRaw),
|
||||||
value: dotdotRaw{A: 1, DotDot: []RawValue{unhex("02"), unhex("03")}},
|
value: tailRaw{A: 1, Tail: []RawValue{unhex("02"), unhex("03")}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "C20102",
|
||||||
|
ptr: new(tailRaw),
|
||||||
|
value: tailRaw{A: 1, Tail: []RawValue{unhex("02")}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "C101",
|
||||||
|
ptr: new(tailRaw),
|
||||||
|
value: tailRaw{A: 1, Tail: []RawValue{}},
|
||||||
},
|
},
|
||||||
|
|
||||||
// RawValue
|
// RawValue
|
||||||
|
|
|
||||||
|
|
@ -513,7 +513,7 @@ func makeSliceWriter(typ reflect.Type, ts tags) (writer, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
writer := func(val reflect.Value, w *encbuf) error {
|
writer := func(val reflect.Value, w *encbuf) error {
|
||||||
if !ts.dotdot {
|
if !ts.tail {
|
||||||
defer w.listEnd(w.list())
|
defer w.listEnd(w.list())
|
||||||
}
|
}
|
||||||
vlen := val.Len()
|
vlen := val.Len()
|
||||||
|
|
|
||||||
|
|
@ -214,7 +214,10 @@ var encTests = []encTest{
|
||||||
{val: simplestruct{A: 3, B: "foo"}, output: "C50383666F6F"},
|
{val: simplestruct{A: 3, B: "foo"}, output: "C50383666F6F"},
|
||||||
{val: &recstruct{5, nil}, output: "C205C0"},
|
{val: &recstruct{5, nil}, output: "C205C0"},
|
||||||
{val: &recstruct{5, &recstruct{4, &recstruct{3, nil}}}, output: "C605C404C203C0"},
|
{val: &recstruct{5, &recstruct{4, &recstruct{3, nil}}}, output: "C605C404C203C0"},
|
||||||
{val: &dotdotRaw{A: 1, DotDot: []RawValue{unhex("02"), unhex("03")}}, output: "C3010203"},
|
{val: &tailRaw{A: 1, Tail: []RawValue{unhex("02"), unhex("03")}}, output: "C3010203"},
|
||||||
|
{val: &tailRaw{A: 1, Tail: []RawValue{unhex("02")}}, output: "C20102"},
|
||||||
|
{val: &tailRaw{A: 1, Tail: []RawValue{}}, output: "C101"},
|
||||||
|
{val: &tailRaw{A: 1, Tail: nil}, output: "C101"},
|
||||||
|
|
||||||
// nil
|
// nil
|
||||||
{val: (*uint)(nil), output: "80"},
|
{val: (*uint)(nil), output: "80"},
|
||||||
|
|
|
||||||
|
|
@ -38,10 +38,10 @@ type tags struct {
|
||||||
// rlp:"nil" controls whether empty input results in a nil pointer.
|
// rlp:"nil" controls whether empty input results in a nil pointer.
|
||||||
nilOK bool
|
nilOK bool
|
||||||
|
|
||||||
// rlp:".." controls whether this field swallows additional list
|
// rlp:"tail" controls whether this field swallows additional list
|
||||||
// elements. It can only be set for the last field, which must be
|
// elements. It can only be set for the last field, which must be
|
||||||
// a slice type.
|
// of slice type.
|
||||||
dotdot bool
|
tail bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type typekey struct {
|
type typekey struct {
|
||||||
|
|
@ -97,14 +97,9 @@ type field struct {
|
||||||
func structFields(typ reflect.Type) (fields []field, err error) {
|
func structFields(typ reflect.Type) (fields []field, err error) {
|
||||||
for i := 0; i < typ.NumField(); i++ {
|
for i := 0; i < typ.NumField(); i++ {
|
||||||
if f := typ.Field(i); f.PkgPath == "" { // exported
|
if f := typ.Field(i); f.PkgPath == "" { // exported
|
||||||
tags := parseStructTag(f.Tag.Get("rlp"))
|
tags, err := parseStructTag(typ, i)
|
||||||
if tags.dotdot {
|
if err != nil {
|
||||||
if i != typ.NumField()-1 {
|
return nil, err
|
||||||
return nil, fmt.Errorf(`rlp: invalid struct tag ".." for %v.%s (must be on last field)`, typ, f.Name)
|
|
||||||
}
|
|
||||||
if f.Type.Kind() != reflect.Slice {
|
|
||||||
return nil, fmt.Errorf(`rlp: invalid struct tag ".." for %v.%s (field type is not slice)`, typ, f.Name)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
info, err := cachedTypeInfo1(f.Type, tags)
|
info, err := cachedTypeInfo1(f.Type, tags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -116,11 +111,27 @@ func structFields(typ reflect.Type) (fields []field, err error) {
|
||||||
return fields, nil
|
return fields, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseStructTag(tag string) tags {
|
func parseStructTag(typ reflect.Type, fi int) (tags, error) {
|
||||||
return tags{
|
f := typ.Field(fi)
|
||||||
nilOK: strings.Contains(tag, "nil"),
|
var ts tags
|
||||||
dotdot: strings.Contains(tag, ".."),
|
for _, t := range strings.Split(f.Tag.Get("rlp"), ",") {
|
||||||
|
switch t = strings.TrimSpace(t); t {
|
||||||
|
case "":
|
||||||
|
case "nil":
|
||||||
|
ts.nilOK = true
|
||||||
|
case "tail":
|
||||||
|
ts.tail = true
|
||||||
|
if fi != typ.NumField()-1 {
|
||||||
|
return ts, fmt.Errorf(`rlp: invalid struct tag "tail" for %v.%s (must be on last field)`, typ, f.Name)
|
||||||
}
|
}
|
||||||
|
if f.Type.Kind() != reflect.Slice {
|
||||||
|
return ts, fmt.Errorf(`rlp: invalid struct tag "tail" for %v.%s (field type is not slice)`, typ, f.Name)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return ts, fmt.Errorf("rlp: unknown struct tag %q on %v.%s", t, typ, f.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func genTypeInfo(typ reflect.Type, tags tags) (info *typeinfo, err error) {
|
func genTypeInfo(typ reflect.Type, tags tags) (info *typeinfo, err error) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue