rlp: rename ".." to "tail"

This commit is contained in:
Felix Lange 2016-02-19 01:10:31 +01:00
parent 0b84e3224f
commit 99fe66973f
6 changed files with 76 additions and 41 deletions

View file

@ -63,8 +63,9 @@ type Decoder interface {
// must contain an element for each decoded field. Decode returns an
// error if there are too few or too many elements.
//
// The decoding of struct fields honours two struct tags, "nil" and
// "..". The "nil" tag applies to pointer-typed fields and changes the
// The decoding of struct fields honours two struct tags, "tail" and
// "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
// decode as a nil pointer. This tag can be useful when decoding
// recursive types.
@ -283,8 +284,8 @@ func makeListDecoder(typ reflect.Type, tag tags) (decoder, error) {
dec = func(s *Stream, val reflect.Value) error {
return decodeListArray(s, val, etypeinfo.decoder)
}
case tag.dotdot:
// A slice with .. tag can occur as the last field
case tag.tail:
// A slice with "tail" tag can occur as the last field
// of a struct and is upposed to swallow all remaining
// list elements. The struct decoder already called s.List,
// proceed directly to decoding the elements.

View file

@ -5,15 +5,15 @@ import (
"fmt"
)
type structWithDotDot struct {
type structWithTail struct {
A, B uint
C []uint `rlp:".."`
C []uint `rlp:"tail"`
}
func ExampleDecode_structTagDotDot() {
// In this example, the ".." struct tag is used to decode lists of
func ExampleDecode_structTagTail() {
// In this example, the "tail" struct tag is used to decode lists of
// differing length into a struct.
var val structWithDotDot
var val structWithTail
err := Decode(bytes.NewReader([]byte{0xC4, 0x01, 0x02, 0x03, 0x04}), &val)
fmt.Printf("with 4 elements: err=%v val=%v\n", err, val)
@ -29,5 +29,5 @@ func ExampleDecode_structTagDotDot() {
// Output:
// with 4 elements: err=<nil> val={1 2 [3 4]}
// 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"
}

View file

@ -312,19 +312,24 @@ type recstruct struct {
Child *recstruct `rlp:"nil"`
}
type invalidDotDot1 struct {
A uint `rlp:".."`
type invalidTail1 struct {
A uint `rlp:"tail"`
B string
}
type invalidDotDot2 struct {
type invalidTail2 struct {
A uint
B string `rlp:".."`
B string `rlp:"tail"`
}
type dotdotRaw struct {
type tailRaw struct {
A uint
DotDot []RawValue `rlp:".."`
Tail []RawValue `rlp:"tail"`
}
type tailUint struct {
A uint
Tail []uint `rlp:"tail"`
}
var (
@ -454,20 +459,35 @@ var decodeTests = []decodeTest{
},
{
input: "C0",
ptr: new(invalidDotDot1),
error: "rlp: invalid struct tag \"..\" for rlp.invalidDotDot1.A (must be on last field)",
ptr: new(invalidTail1),
error: "rlp: invalid struct tag \"tail\" for rlp.invalidTail1.A (must be on last field)",
},
{
input: "C0",
ptr: new(invalidDotDot2),
error: "rlp: invalid struct tag \"..\" for rlp.invalidDotDot2.B (field type is not slice)",
ptr: new(invalidTail2),
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",
ptr: new(dotdotRaw),
value: dotdotRaw{A: 1, DotDot: []RawValue{unhex("02"), unhex("03")}},
ptr: new(tailRaw),
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

View file

@ -513,7 +513,7 @@ func makeSliceWriter(typ reflect.Type, ts tags) (writer, error) {
return nil, err
}
writer := func(val reflect.Value, w *encbuf) error {
if !ts.dotdot {
if !ts.tail {
defer w.listEnd(w.list())
}
vlen := val.Len()

View file

@ -214,7 +214,10 @@ var encTests = []encTest{
{val: simplestruct{A: 3, B: "foo"}, output: "C50383666F6F"},
{val: &recstruct{5, nil}, output: "C205C0"},
{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
{val: (*uint)(nil), output: "80"},

View file

@ -38,10 +38,10 @@ type tags struct {
// rlp:"nil" controls whether empty input results in a nil pointer.
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
// a slice type.
dotdot bool
// of slice type.
tail bool
}
type typekey struct {
@ -97,14 +97,9 @@ type field struct {
func structFields(typ reflect.Type) (fields []field, err error) {
for i := 0; i < typ.NumField(); i++ {
if f := typ.Field(i); f.PkgPath == "" { // exported
tags := parseStructTag(f.Tag.Get("rlp"))
if tags.dotdot {
if i != typ.NumField()-1 {
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)
}
tags, err := parseStructTag(typ, i)
if err != nil {
return nil, err
}
info, err := cachedTypeInfo1(f.Type, tags)
if err != nil {
@ -116,11 +111,27 @@ func structFields(typ reflect.Type) (fields []field, err error) {
return fields, nil
}
func parseStructTag(tag string) tags {
return tags{
nilOK: strings.Contains(tag, "nil"),
dotdot: strings.Contains(tag, ".."),
func parseStructTag(typ reflect.Type, fi int) (tags, error) {
f := typ.Field(fi)
var ts tags
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) {