Skip to content
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

add: MarshalJSON methods for *Array and *Document #70

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

KrishnaSindhur
Copy link

@KrishnaSindhur KrishnaSindhur commented Dec 18, 2024

Closes #49

wirebson/document.go Outdated Show resolved Hide resolved
@AlekSi AlekSi requested a review from Copilot December 19, 2024 05:14

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")
Copy link

codecov bot commented Dec 19, 2024

Codecov Report

Attention: Patch coverage is 61.29032% with 12 lines in your changes missing coverage. Please review.

Project coverage is 62.96%. Comparing base (725f032) to head (cbfdd87).

Files with missing lines Patch % Lines
wirebson/document.go 65.38% 6 Missing and 3 partials ⚠️
wirebson/array.go 40.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@            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     
Files with missing lines Coverage Δ
wirebson/array.go 61.70% <40.00%> (-1.22%) ⬇️
wirebson/document.go 48.07% <65.38%> (+3.46%) ⬆️

... and 1 file with indirect coverage changes

Flag Coverage Δ
test 62.96% <61.29%> (-0.07%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Copy link
Member

@AlekSi AlekSi left a 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

wirebson/array.go Outdated Show resolved Hide resolved
Copy link
Member

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

Copy link
Member

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.

Comment on lines 264 to 265
jsonObject := make([]byte, 0)
jsonObject = append(jsonObject, '{')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
jsonObject := make([]byte, 0)
jsonObject = append(jsonObject, '{')
res := []byte{'{'}

Copy link
Author

@KrishnaSindhur KrishnaSindhur Dec 19, 2024

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)

Copy link
Member

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

Copy link
Member

@AlekSi AlekSi Dec 19, 2024

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

Copy link
Author

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.

Copy link
Member

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

Copy link
Author

@KrishnaSindhur KrishnaSindhur Dec 19, 2024

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.

Copy link
Member

@AlekSi AlekSi left a 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

@@ -788,6 +789,30 @@ func TestNormal(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, tc.raw, raw)
})

t.Run("ArrayMarshalJSON", func(t *testing.T) {
Copy link
Member

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.

@@ -741,6 +742,27 @@ var decodeTestCases = []decodeTestCase{
},
}

var marshalJSONTestCases = []normalTestCase{
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In progress
Development

Successfully merging this pull request may close these issues.

Add MarshalJSON methods to *Document / *Array
2 participants