Repository navigation
Bump required Go version to 1.27 and implement json/v2.UnmarshalerFrom and json/v2.MarshalerTo
#16
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
6 commits
Select commit
Hold shift + click to select a range
1b34b8c
Require Go 1.27 and update dependencies
jonathansharman d3e6223
Implement json/v2 custom marshalers
jonathansharman 12ab834
Remove invalid json tag comma
jonathansharman 1b96917
Run go fix
jonathansharman b1449f5
Downgrade required Go version to minor version
jonathansharman ebdce27
Handle ReadToken errors and don't leave updates partially modified du…
jonathansharman 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
| module github.com/nicheinc/nullable/v2 | ||
|
|
||
| go 1.24 | ||
| go 1.27 | ||
|
|
||
| require github.com/nicheinc/expect v0.2.0 | ||
|
|
||
| require ( | ||
| github.com/google/go-cmp v0.5.9 // indirect | ||
| golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 // indirect | ||
| github.com/google/go-cmp v0.7.0 // indirect | ||
| golang.org/x/exp v0.0.0-20260908205506-85c1c2202aba // indirect | ||
| ) |
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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= | ||
| github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
| github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= | ||
| github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= | ||
| github.com/nicheinc/expect v0.2.0 h1:Z0xKpZiDQsRuxhm2HsUh4M9datV1QgM/DpCFWvN/rpY= | ||
| github.com/nicheinc/expect v0.2.0/go.mod h1:NRiUkkvrrIz1Uj0VccPt3ZBZcqGU3RnPSK55oYVSJiY= | ||
| golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 h1:5llv2sWeaMSnA3w2kS57ouQQ4pudlXrR0dCgw51QK9o= | ||
| golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= | ||
| golang.org/x/exp v0.0.0-20260908205506-85c1c2202aba h1:Ck8QetSgk912qxWLMCKxd0in+aiyBQyDSMae6e/xmpU= | ||
| golang.org/x/exp v0.0.0-20260908205506-85c1c2202aba/go.mod h1:50RgIsmK7OwqzTTeqcSXQW8SswW0o8fRcDxmqGluJ8E= |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| package nup | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "encoding/json/v2" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
|
|
@@ -17,6 +17,35 @@ var ( | |
| var _ updateMarshaller = &SliceUpdate[int]{} | ||
|
|
||
| func TestSliceUpdate_MarshalJSON(t *testing.T) { | ||
| type testCase struct { | ||
| update SliceUpdate[int] | ||
| expected string | ||
| } | ||
| run := func(name string, testCase testCase) { | ||
| t.Helper() | ||
| t.Run(name, func(t *testing.T) { | ||
| t.Helper() | ||
| actual, err := testCase.update.MarshalJSON() | ||
| expect.ErrorNil(t, err) | ||
| expect.Equal(t, string(actual), testCase.expected) | ||
| }) | ||
| } | ||
|
|
||
| run("Noop", testCase{ | ||
| update: SliceNoop[int](), | ||
| expected: "null", | ||
| }) | ||
| run("Remove", testCase{ | ||
| update: SliceRemove[int](), | ||
| expected: "null", | ||
| }) | ||
| run("Set", testCase{ | ||
| update: SliceRemoveOrSet(testSlice1), | ||
| expected: "[1]", | ||
| }) | ||
| } | ||
|
|
||
| func TestSliceUpdate_MarshalJSONTo(t *testing.T) { | ||
| type testCase struct { | ||
| update SliceUpdate[int] | ||
| expected string | ||
|
|
@@ -40,49 +69,99 @@ func TestSliceUpdate_MarshalJSON(t *testing.T) { | |
| expected: "null", | ||
| }) | ||
| run("Set", testCase{ | ||
| update: SliceRemoveOrSet[int](testSlice1), | ||
| update: SliceRemoveOrSet(testSlice1), | ||
| expected: "[1]", | ||
| }) | ||
| } | ||
|
|
||
| func TestSliceUpdate_UnmarshalJSON(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| json string | ||
| expected SliceUpdate[int] | ||
| }{ | ||
| { | ||
| name: "EmptyJSONObject", | ||
| json: `{}`, | ||
| expected: SliceNoop[int](), | ||
| }, | ||
| { | ||
| name: "NullUpdate", | ||
| json: `{"update": null}`, | ||
| expected: SliceRemove[int](), | ||
| }, | ||
| { | ||
| name: "EmptyUpdate", | ||
| json: `{"update": []}`, | ||
| expected: SliceRemoveOrSet([]int{}), | ||
| }, | ||
| { | ||
| name: "NonemptyUpdate", | ||
| json: fmt.Sprintf(`{"update": %v}`, testSlice1), | ||
| expected: SliceRemoveOrSet(testSlice1), | ||
| }, | ||
| type testCase struct { | ||
| json string | ||
| expected SliceUpdate[int] | ||
| errorCheck expect.ErrorCheck | ||
| } | ||
| run := func(name string, testCase testCase) { | ||
| t.Helper() | ||
| t.Run(name, func(t *testing.T) { | ||
| t.Helper() | ||
| var update SliceUpdate[int] | ||
| err := update.UnmarshalJSON([]byte(testCase.json)) | ||
|
Comment on lines
+87
to
+88
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling |
||
| testCase.errorCheck(t, err) | ||
| expect.Equal(t, update, testCase.expected) | ||
| }) | ||
| } | ||
|
|
||
| for _, testCase := range testCases { | ||
| t.Run(testCase.name, func(t *testing.T) { | ||
| run("MalformedNull", testCase{ | ||
| json: `nup`, | ||
| expected: SliceNoop[int](), | ||
| errorCheck: expect.ErrorNonNil, | ||
| }) | ||
| run("Null", testCase{ | ||
| json: `null`, | ||
| expected: SliceRemove[int](), | ||
| errorCheck: expect.ErrorNil, | ||
| }) | ||
| run("Empty", testCase{ | ||
| json: `[]`, | ||
| expected: SliceRemoveOrSet([]int{}), | ||
| errorCheck: expect.ErrorNil, | ||
| }) | ||
| run("Nonempty", testCase{ | ||
| json: fmt.Sprint(testSlice1), | ||
| expected: SliceRemoveOrSet(testSlice1), | ||
| errorCheck: expect.ErrorNil, | ||
| }) | ||
| } | ||
|
|
||
| func TestSliceUpdate_UnmarshalJSONFrom(t *testing.T) { | ||
| type testCase struct { | ||
| json string | ||
| expected SliceUpdate[int] | ||
| errorCheck expect.ErrorCheck | ||
| } | ||
| run := func(name string, testCase testCase) { | ||
| t.Helper() | ||
| t.Run(name, func(t *testing.T) { | ||
| t.Helper() | ||
| var dst struct { | ||
| Update SliceUpdate[int] `json:"update"` | ||
| } | ||
| err := json.Unmarshal([]byte(testCase.json), &dst) | ||
| expect.ErrorNil(t, err) | ||
| testCase.errorCheck(t, err) | ||
| expect.Equal(t, dst.Update, testCase.expected) | ||
| }) | ||
| } | ||
|
|
||
| run("EmptyJSONObject", testCase{ | ||
| json: `{}`, | ||
| expected: SliceNoop[int](), | ||
| errorCheck: expect.ErrorNil, | ||
| }) | ||
| run("MalformedNullUpdate", testCase{ | ||
| json: `{"update": nup}`, | ||
| expected: SliceNoop[int](), | ||
| errorCheck: expect.ErrorNonNil, | ||
| }) | ||
| run("NullUpdate", testCase{ | ||
| json: `{"update": null}`, | ||
| expected: SliceRemove[int](), | ||
| errorCheck: expect.ErrorNil, | ||
| }) | ||
| run("EmptyUpdate", testCase{ | ||
| json: `{"update": []}`, | ||
| expected: SliceRemoveOrSet([]int{}), | ||
| errorCheck: expect.ErrorNil, | ||
| }) | ||
| run("MalformedNonemptyUpdate", testCase{ | ||
| json: `{"update": invalid}`, | ||
| expected: SliceNoop[int](), | ||
| errorCheck: expect.ErrorNonNil, | ||
| }) | ||
| run("NonemptyUpdate", testCase{ | ||
| json: fmt.Sprintf(`{"update": %v}`, testSlice1), | ||
| expected: SliceRemoveOrSet(testSlice1), | ||
| errorCheck: expect.ErrorNil, | ||
| }) | ||
| } | ||
|
|
||
| func TestSliceRemoveOrSet(t *testing.T) { | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.