From 99fe66973fe598246c74678ba12a3fb054132ff2 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Fri, 19 Feb 2016 01:10:31 +0100 Subject: [PATCH] rlp: rename ".." to "tail" --- rlp/decode.go | 9 ++-- ...ode_dotdot_test.go => decode_tail_test.go} | 12 ++--- rlp/decode_test.go | 48 +++++++++++++------ rlp/encode.go | 2 +- rlp/encode_test.go | 5 +- rlp/typecache.go | 41 ++++++++++------ 6 files changed, 76 insertions(+), 41 deletions(-) rename rlp/{decode_dotdot_test.go => decode_tail_test.go} (78%) diff --git a/rlp/decode.go b/rlp/decode.go index 37ee544130..c4e5869cc9 100644 --- a/rlp/decode.go +++ b/rlp/decode.go @@ -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. diff --git a/rlp/decode_dotdot_test.go b/rlp/decode_tail_test.go similarity index 78% rename from rlp/decode_dotdot_test.go rename to rlp/decode_tail_test.go index 12a443822e..8853543905 100644 --- a/rlp/decode_dotdot_test.go +++ b/rlp/decode_tail_test.go @@ -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= val={1 2 [3 4]} // with 6 elements: err= 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" } diff --git a/rlp/decode_test.go b/rlp/decode_test.go index bf8b57e1c0..2d465b74dc 100644 --- a/rlp/decode_test.go +++ b/rlp/decode_test.go @@ -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 { - A uint - DotDot []RawValue `rlp:".."` +type tailRaw struct { + A uint + 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 diff --git a/rlp/encode.go b/rlp/encode.go index 408d1f8d65..17cfc6b663 100644 --- a/rlp/encode.go +++ b/rlp/encode.go @@ -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() diff --git a/rlp/encode_test.go b/rlp/encode_test.go index e634094d28..6f38294e46 100644 --- a/rlp/encode_test.go +++ b/rlp/encode_test.go @@ -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"}, diff --git a/rlp/typecache.go b/rlp/typecache.go index ac5bacfa48..a2f217c66f 100644 --- a/rlp/typecache.go +++ b/rlp/typecache.go @@ -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) {