-
Notifications
You must be signed in to change notification settings - Fork 7
add: MarshalJSON methods for *Array and *Document #70
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
Conversation
Signed-off-by: krishna sindhur <krishna.sindhur@thinkbyte.ai>
Signed-off-by: krishna sindhur <krishna.sindhur@thinkbyte.ai>
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.
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
Comments suppressed due to low confidence (4)
wirebson/document_test.go:54
- Consider adding a test case for marshalling an empty Document to ensure it marshals correctly.
t.Run("MarshalJSON_PreserveOrder", func(t *testing.T) {
wirebson/document.go:261
- The error message 'nil Document' is unclear. It should be more descriptive, such as 'Document is nil and cannot be marshaled to JSON'.
return nil, lazyerrors.Errorf("nil Document")
wirebson/document.go:259
- The manual construction of the JSON object in 'Document.MarshalJSON' is error-prone. Consider using 'json.Marshal' for the entire 'doc.fields' slice to simplify the implementation and avoid potential issues.
func (doc *Document) MarshalJSON() ([]byte, error) {
wirebson/array.go:185
- [nitpick] The error message 'nil Array' is unclear. Consider changing it to 'Cannot marshal a nil Array'.
return nil, lazyerrors.Errorf("nil Array")
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #70 +/- ##
==========================================
- Coverage 63.03% 62.96% -0.07%
==========================================
Files 41 41
Lines 2221 2252 +31
==========================================
+ Hits 1400 1418 +18
- Misses 645 654 +9
- Partials 176 180 +4
... and 1 file with indirect coverage changes
Flags with carried forward coverage won't be shown. Click here to find out more. |
AlekSi
left a comment
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.
Please also run linters locally
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.
Please update tests, benchmarks, and fuzzing in bson_test.go instead
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.
Please don't resolve conversations. Please also update existing tests/benchmarks/fuzzes, not add new ones.
wirebson/document.go
Outdated
| jsonObject := make([]byte, 0) | ||
| jsonObject = append(jsonObject, '{') |
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.
| jsonObject := make([]byte, 0) | |
| jsonObject = append(jsonObject, '{') | |
| res := []byte{'{'} |
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.
@AlekSi any changes required here? I have updated to must.NotBeZero(doc)
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.
Well, yeah, I even wrote them
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'm not sure why this conversation was resolved without changes applied
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 have not added test for error scenarios In MarshalJSON is it ok? Because I don't see any error scenarios tests added for encode functions also!! Please correct me if I am wrong. If necessary I can add test for error scenarios.
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.
This comment does not belong to this conversation, where I asked you to replace make+append combo with a single assignment, and even provided a suggested change
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.
Sorry but I can't see your comments you added to replace make+append to single line, Which ended with some confusions.
Signed-off-by: krishna sindhur <krishna.sindhur@thinkbyte.ai>
AlekSi
left a comment
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.
Review comments were "resolved", but not addressed
wirebson/bson_test.go
Outdated
| assert.Equal(t, tc.raw, raw) | ||
| }) | ||
|
|
||
| t.Run("ArrayMarshalJSON", func(t *testing.T) { |
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.
There is no point in running those subtests for each tc with the same input and expected output. Instead, all subtests use tc.
Please slow down, check all tests in this file, and make sure your changes follow its structure.
wirebson/bson_test.go
Outdated
| }, | ||
| } | ||
|
|
||
| var marshalJSONTestCases = []normalTestCase{ |
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.
Please update existing normalTestCases variable instead
AlekSi
left a comment
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.
The main comment: please follow the existing code (and tests) structure
| data, err := json.Marshal(array) | ||
| assert.NoError(t, err) | ||
| expected := `["value1",42,true]` | ||
| assert.JSONEq(t, expected, string(data)) |
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.
assert.JSONEq, per its documentation, ignores the filed order – something we care about.
I'm also not sure why we have that test at all, given that bson_test.go should already test it.
| raw RawDocument | ||
| doc *Document | ||
| mi string | ||
| isMarshalJSON bool // isMarshalJSON: Set to true if the test case is meant for JSON marshalling. |
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.
All tests should be meant for JSON marshaling and unmarshalling.
Check how mi, doc, and raw are used. Add another field that would contain expected JSON representation and test both decoding and encoding.
| { | ||
| name: "MarshalJsonOrderedFields", | ||
| doc: MustDocument( | ||
| "first", int32(1), | ||
| "second", "two", | ||
| "third", float64(3.0), | ||
| ), | ||
| mi: `{ | ||
| "first": 1, | ||
| "second": "two", | ||
| "third": 3.0 | ||
| }`, | ||
| isMarshalJSON: true, | ||
| }, | ||
| { | ||
| name: "MarshalJsonEmptyDocument", | ||
| doc: MustDocument(), | ||
| mi: `{}`, | ||
| isMarshalJSON: true, | ||
| }, |
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.
Please update existing test cases (I said that before, didn't I?), not add new ones. Please do not try different ways of adding only a few tests instead of updating existing ones.
| ls := doc.LogValue().Resolve().String() | ||
| assert.NotContains(t, ls, "panicked") | ||
| assert.NotContains(t, ls, "called too many times") | ||
| if tc.raw != nil { |
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.
Please revert that change
| // MarshalJSON implements the json.Marshaler interface for Document. | ||
| // It converts the Document into a JSON object representation while preserving the order of fields. |
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.
Note the format of comments for methods that implement the interface and do the same.
|
I made nessisary changes in #71. Thank you! |
Closes #49