-
Notifications
You must be signed in to change notification settings - Fork 18
Remove support for decoding positional encoding (structs as arrays) from code generator #37
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package tests | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/algorand/msgp/msgp" | ||
| ) | ||
|
|
||
| func wantArrayForMapError(t *testing.T, err error) { | ||
| t.Helper() | ||
| if err == nil { | ||
| t.Fatal("expected array-encoded map-struct to be rejected, got nil") | ||
| } | ||
| te, ok := msgp.Cause(err).(msgp.TypeError) | ||
| if !ok { | ||
| t.Fatalf("expected msgp.TypeError, got %T: %v", msgp.Cause(err), err) | ||
| } | ||
| if te.Method != msgp.MapType || te.Encoded != msgp.ArrayType { | ||
| t.Fatalf("expected map-wanted/array-encoded TypeError, got %+v", te) | ||
| } | ||
| } | ||
|
|
||
| func TestMapStructRejectsArray(t *testing.T) { | ||
| // A single-field map-struct encoded as a one-element array. | ||
| bts := msgp.AppendArrayHeader(nil, 1) | ||
| bts = msgp.AppendString(bts, "hello") | ||
|
|
||
| var out Inner | ||
| _, err := out.UnmarshalMsg(bts) | ||
| wantArrayForMapError(t, err) | ||
| if out.X != "" { | ||
| t.Fatalf("rejected decode must not populate fields, got X=%q", out.X) | ||
| } | ||
| } | ||
|
|
||
| func TestMapStructRejectsMultiFieldArray(t *testing.T) { | ||
| // An array whose elements line up positionally with Go field order. | ||
| bts := msgp.AppendArrayHeader(nil, 2) | ||
| bts = msgp.AppendString(bts, "somereq") | ||
| bts = msgp.AppendString(bts, "someopt") | ||
|
|
||
| var out MapRequiredOmitEmpty | ||
| _, err := out.UnmarshalMsg(bts) | ||
| wantArrayForMapError(t, err) | ||
| } | ||
|
|
||
| func TestMapStructRejectsEmptyArray(t *testing.T) { | ||
| // A zero-length array. msgp once special-cased mfixarray(0) inside | ||
| // ReadMapHeaderBytes as an empty map for go-codec parity; that special case | ||
| // was folded into the now-removed fallback, so a zero-length array is a | ||
| // plain map/array type mismatch today. | ||
| bts := msgp.AppendArrayHeader(nil, 0) | ||
|
|
||
| var out Inner | ||
| _, err := out.UnmarshalMsg(bts) | ||
| wantArrayForMapError(t, err) | ||
| } | ||
|
|
||
| func TestTupleStructStillDecodesArray(t *testing.T) { | ||
| // Contrast: a struct that genuinely opts into array encoding via | ||
| // //msgp:tuple still round-trips through its array form. Only the implicit | ||
| // map-struct fallback was removed, not real tuple support. | ||
| in := TupleRequired{A: "x", B: 7} | ||
| var out TupleRequired | ||
| if _, err := out.UnmarshalMsg(in.MarshalMsg(nil)); err != nil { | ||
| t.Fatalf("expected tuple array decode to succeed, got %v", err) | ||
| } | ||
| if out != in { | ||
| t.Fatalf("tuple round trip mismatch: got %+v, want %+v", out, in) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What's the "tuple" path?
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.
you can specify
//msgp:tupleand have a struct encoded as a positional array. It doesn't look like we've ever used this feature though.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.
can/should we remove that as well?
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.
I feel less concerned about it, as it is an explicit opt-in, and not automatic fallback behavior that is being generated whether you specified array/tuple encoding or not.
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.
But I could remove it