-
Notifications
You must be signed in to change notification settings - Fork 20.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rlp: add support for optional struct fields #22832
Conversation
Suggestion: diff --git a/rlp/decode_test.go b/rlp/decode_test.go
index af4cd0acc4..c9191949fa 100644
--- a/rlp/decode_test.go
+++ b/rlp/decode_test.go
@@ -979,12 +979,17 @@ func TestInvalidOptionalField(t *testing.T) {
for _, test := range tests {
err := DecodeBytes(unhex("C20102"), test.v)
if err == nil {
- t.Errorf("no error for %T", test.v)
+ t.Errorf("decode: no error for %T", test.v)
} else if err.Error() != test.err {
- t.Errorf("wrong error for %T: %v", test.v, err.Error())
+ t.Errorf("decode: wrong error for %T: %v", test.v, err.Error())
+ }
+ _, err = EncodeToBytes(test.v)
+ if err == nil {
+ t.Errorf("encode: no error for %T", test.v)
+ } else if err.Error() != test.err {
+ t.Errorf("encode: wrong error for %T: %v", test.v, err.Error())
}
}
-
}
func ExampleDecode() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested it a bit, it seems to work exactly as expected. I expected it to fail some of my tests, e..g
type x struct {
A uint64
B uint `rlp:"optional"`
C uint `rlp:"optional"`
}
When doing EncodeToBytes(&x{0xaa, 0, 0xcc})
it handles it fine (writes all data without complaining about optional), and with EncodeToBytes(&x{0xaa, 0, 0})
it ignores the two last elements. So that's good!
Regarding the suggestion, I think it's not necessary because the test is specifically for the struct tags validation in typecache.go. Both encoding and decoding call into this. |
This adds support for a new struct tag "optional". Using this tag, structs used for RLP encoding/decoding can be extended in a backwards-compatible way, by adding new fields at the end.
This adds support for a new struct tag "optional". Using this tag, structs used for RLP
encoding/decoding can be extended in a backwards-compatible way, by adding new fields at
the end.