Skip to content

Commit

Permalink
rlp: add support for "-" struct tag
Browse files Browse the repository at this point in the history
  • Loading branch information
fjl committed Mar 7, 2017
1 parent 667cd51 commit eee96a5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
16 changes: 10 additions & 6 deletions rlp/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,16 @@ 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, "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.
// The decoding of struct fields honours certain struct tags, "tail",
// "nil" and "-".
//
// The "-" tag ignores fields.
//
// 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.
//
// type StructWithEmptyOK struct {
// Foo *[20]byte `rlp:"nil"`
Expand Down
13 changes: 13 additions & 0 deletions rlp/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,12 @@ var (
)
)

type hasIgnoredField struct {
A uint
B uint `rlp:"-"`
C uint
}

var decodeTests = []decodeTest{
// booleans
{input: "01", ptr: new(bool), value: true},
Expand Down Expand Up @@ -490,6 +496,13 @@ var decodeTests = []decodeTest{
value: tailRaw{A: 1, Tail: []RawValue{}},
},

// struct tag "-"
{
input: "C20102",
ptr: new(hasIgnoredField),
value: hasIgnoredField{A: 1, C: 2},
},

// RawValue
{input: "01", ptr: new(RawValue), value: RawValue(unhex("01"))},
{input: "82FFFF", ptr: new(RawValue), value: RawValue(unhex("82FFFF"))},
Expand Down
1 change: 1 addition & 0 deletions rlp/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ var encTests = []encTest{
{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"},
{val: &hasIgnoredField{A: 1, B: 2, C: 3}, output: "C20103"},

// nil
{val: (*uint)(nil), output: "80"},
Expand Down
8 changes: 7 additions & 1 deletion rlp/typecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ type typeinfo struct {
type tags struct {
// rlp:"nil" controls whether empty input results in a nil pointer.
nilOK bool

// rlp:"tail" controls whether this field swallows additional list
// elements. It can only be set for the last field, which must be
// of slice type.
tail bool
// rlp:"-" ignores fields.
ignored bool
}

type typekey struct {
Expand Down Expand Up @@ -101,6 +102,9 @@ func structFields(typ reflect.Type) (fields []field, err error) {
if err != nil {
return nil, err
}
if tags.ignored {
continue
}
info, err := cachedTypeInfo1(f.Type, tags)
if err != nil {
return nil, err
Expand All @@ -117,6 +121,8 @@ func parseStructTag(typ reflect.Type, fi int) (tags, error) {
for _, t := range strings.Split(f.Tag.Get("rlp"), ",") {
switch t = strings.TrimSpace(t); t {
case "":
case "-":
ts.ignored = true
case "nil":
ts.nilOK = true
case "tail":
Expand Down

0 comments on commit eee96a5

Please sign in to comment.