-
Notifications
You must be signed in to change notification settings - Fork 225
Marshal resource types by value, not only by pointer #6551
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2256f27
Marshal resource types by value, not only by pointer
denik 42be3e7
Check embedded types too, not just the surface type
denik 553b1ba
Widen the detector: named non-struct types, json:"-", recursive pointers
denik 80ef0da
Report per resource type, like the other tests in the file
denik a16e960
Add explicit marshal regression tests for postgres_branch
denik 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,44 @@ | ||
| package resources | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestPostgresBranchConfigMarshalHonorsForceSendFields pins the reason | ||
| // PostgresBranchConfig.MarshalJSON is declared on a value receiver. | ||
| // | ||
| // PurgeOnDelete is omitempty, so its zero value is emitted only because | ||
| // ForceSendFields names it, and the SDK marshaler is what honors ForceSendFields. | ||
| // With a pointer-receiver MarshalJSON, only *T satisfies json.Marshaler, so | ||
| // marshalling a value falls back to plain encoding/json -- which ignores | ||
| // ForceSendFields (tagged json:"-") and would drop the field. | ||
| func TestPostgresBranchConfigMarshalHonorsForceSendFields(t *testing.T) { | ||
| c := PostgresBranchConfig{BranchId: "b1", Parent: "projects/p1"} | ||
| c.ForceSendFields = []string{"PurgeOnDelete"} | ||
|
|
||
| b, err := json.Marshal(c) | ||
| require.NoError(t, err) | ||
| assert.JSONEq(t, `{"branch_id":"b1","parent":"projects/p1","purge_on_delete":false}`, string(b)) | ||
| } | ||
|
|
||
| // TestPostgresBranchMarshalValueAndPointerAgree guards the invariant directly: | ||
| // a value-receiver MarshalJSON makes json.Marshal(x) and json.Marshal(&x) | ||
| // produce the same bytes. A pointer-only marshaler would send them down two | ||
| // different code paths. | ||
| func TestPostgresBranchMarshalValueAndPointerAgree(t *testing.T) { | ||
| b := PostgresBranch{} | ||
| b.ID = "the-id" | ||
| b.BranchId = "b1" | ||
| b.Parent = "projects/p1" | ||
|
|
||
| byValue, err := json.Marshal(b) | ||
| require.NoError(t, err) | ||
| byPointer, err := json.Marshal(&b) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, string(byPointer), string(byValue)) | ||
| assert.JSONEq(t, `{"id":"the-id","lifecycle":{},"branch_id":"b1","parent":"projects/p1"}`, string(byValue)) | ||
| } | ||
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
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
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
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
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.
I assume a marshaller on the struct itself is also used for the pointer type then?
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.
Ok, I see you have a test right below.