Skip to content

Commit eee96a5

Browse files
committed
rlp: add support for "-" struct tag
1 parent 667cd51 commit eee96a5

4 files changed

Lines changed: 31 additions & 7 deletions

File tree

‎rlp/decode.go‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,16 @@ type Decoder interface {
6363
// must contain an element for each decoded field. Decode returns an
6464
// error if there are too few or too many elements.
6565
//
66-
// The decoding of struct fields honours two struct tags, "tail" and
67-
// "nil". For an explanation of "tail", see the example.
68-
// The "nil" tag applies to pointer-typed fields and changes the
69-
// decoding rules for the field such that input values of size zero
70-
// decode as a nil pointer. This tag can be useful when decoding
71-
// recursive types.
66+
// The decoding of struct fields honours certain struct tags, "tail",
67+
// "nil" and "-".
68+
//
69+
// The "-" tag ignores fields.
70+
//
71+
// For an explanation of "tail", see the example.
72+
//
73+
// The "nil" tag applies to pointer-typed fields and changes the decoding
74+
// rules for the field such that input values of size zero decode as a nil
75+
// pointer. This tag can be useful when decoding recursive types.
7276
//
7377
// type StructWithEmptyOK struct {
7478
// Foo *[20]byte `rlp:"nil"`

‎rlp/decode_test.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,12 @@ var (
339339
)
340340
)
341341

342+
type hasIgnoredField struct {
343+
A uint
344+
B uint `rlp:"-"`
345+
C uint
346+
}
347+
342348
var decodeTests = []decodeTest{
343349
// booleans
344350
{input: "01", ptr: new(bool), value: true},
@@ -490,6 +496,13 @@ var decodeTests = []decodeTest{
490496
value: tailRaw{A: 1, Tail: []RawValue{}},
491497
},
492498

499+
// struct tag "-"
500+
{
501+
input: "C20102",
502+
ptr: new(hasIgnoredField),
503+
value: hasIgnoredField{A: 1, C: 2},
504+
},
505+
493506
// RawValue
494507
{input: "01", ptr: new(RawValue), value: RawValue(unhex("01"))},
495508
{input: "82FFFF", ptr: new(RawValue), value: RawValue(unhex("82FFFF"))},

‎rlp/encode_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ var encTests = []encTest{
218218
{val: &tailRaw{A: 1, Tail: []RawValue{unhex("02")}}, output: "C20102"},
219219
{val: &tailRaw{A: 1, Tail: []RawValue{}}, output: "C101"},
220220
{val: &tailRaw{A: 1, Tail: nil}, output: "C101"},
221+
{val: &hasIgnoredField{A: 1, B: 2, C: 3}, output: "C20103"},
221222

222223
// nil
223224
{val: (*uint)(nil), output: "80"},

‎rlp/typecache.go‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ type typeinfo struct {
3737
type tags struct {
3838
// rlp:"nil" controls whether empty input results in a nil pointer.
3939
nilOK bool
40-
4140
// rlp:"tail" controls whether this field swallows additional list
4241
// elements. It can only be set for the last field, which must be
4342
// of slice type.
4443
tail bool
44+
// rlp:"-" ignores fields.
45+
ignored bool
4546
}
4647

4748
type typekey struct {
@@ -101,6 +102,9 @@ func structFields(typ reflect.Type) (fields []field, err error) {
101102
if err != nil {
102103
return nil, err
103104
}
105+
if tags.ignored {
106+
continue
107+
}
104108
info, err := cachedTypeInfo1(f.Type, tags)
105109
if err != nil {
106110
return nil, err
@@ -117,6 +121,8 @@ func parseStructTag(typ reflect.Type, fi int) (tags, error) {
117121
for _, t := range strings.Split(f.Tag.Get("rlp"), ",") {
118122
switch t = strings.TrimSpace(t); t {
119123
case "":
124+
case "-":
125+
ts.ignored = true
120126
case "nil":
121127
ts.nilOK = true
122128
case "tail":

0 commit comments

Comments
 (0)