Skip to content

GODRIVER-946: Add IndentExtJSON and MarshalExtJSONIndent functions #781

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 9 commits into from
Nov 2, 2021

Conversation

gabbyasuncion
Copy link
Contributor

GODRIVER-946

Adds IndentExtJSON and MarshalExtJSONIndent functions to marshal.go that use the json and bytes go packages. Also adds test coverage to marshal_test.go to verify that these functions behave as expected.

@gabbyasuncion gabbyasuncion marked this pull request as ready for review October 26, 2021 20:23
Copy link
Contributor

@benjirewis benjirewis left a comment

Choose a reason for hiding this comment

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

Great job just hopping into the bson package! Marshal/Unmarshal logic and the bson parser/library are some of the densest pieces of the driver IMO. Let me know if any of my comments don't make sense.

@kevinAlbs kevinAlbs requested a review from matthewdale October 27, 2021 17:12
bson/marshal.go Outdated
return formatted, nil
}

func MarshalExtJSONIndent(val interface{}, canonical, escapeHTML bool, dst *bytes.Buffer, prefix, indent string) ([]byte, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Modified this function's parameters as well to match json.Indent()'s parameters.

Copy link
Contributor

Choose a reason for hiding this comment

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

Assume you mean modified IndentExtJSON to match json.Indent()'s params; that sounds good.

Copy link
Collaborator

Choose a reason for hiding this comment

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

MarshalExtJSONIndent accepts an output bytes.Buffer param and returns a []byte, both containing the same value. Remove the redundant bytes.Buffer param (follows the json.MarshalIndent function signature).

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree on removing the redundant bytes.Buffer param. Interestingly, many of the other MarshalExtJSON function above also have a redundant output param and return value (changing those would be API-breaking, though).

@kevinAlbs kevinAlbs removed their request for review October 28, 2021 03:43
bson/marshal.go Outdated
return formatted, nil
}

func MarshalExtJSONIndent(val interface{}, canonical, escapeHTML bool, dst *bytes.Buffer, prefix, indent string) ([]byte, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Assume you mean modified IndentExtJSON to match json.Indent()'s params; that sounds good.

bson/marshal.go Outdated
return formatted, nil
}

func MarshalExtJSONIndent(val interface{}, canonical, escapeHTML bool, dst *bytes.Buffer, prefix, indent string) ([]byte, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

MarshalExtJSONIndent accepts an output bytes.Buffer param and returns a []byte, both containing the same value. Remove the redundant bytes.Buffer param (follows the json.MarshalIndent function signature).

Copy link
Contributor

@benjirewis benjirewis left a comment

Choose a reason for hiding this comment

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

Almost there!

Copy link
Contributor

@benjirewis benjirewis left a comment

Choose a reason for hiding this comment

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

LGTM % remaining open thread. Great job! 🧑‍🔧

bson/marshal.go Outdated
return formatted, nil
}

func MarshalExtJSONIndent(val interface{}, canonical, escapeHTML bool, dst *bytes.Buffer, prefix, indent string) ([]byte, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree on removing the redundant bytes.Buffer param. Interestingly, many of the other MarshalExtJSON function above also have a redundant output param and return value (changing those would be API-breaking, though).

Copy link
Collaborator

@matthewdale matthewdale left a comment

Choose a reason for hiding this comment

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

Looks good with an optional suggestion 👍

bson/marshal.go Outdated
@@ -221,3 +224,25 @@ func MarshalExtJSONAppendWithContext(ec bsoncodec.EncodeContext, dst []byte, val

return *sw, nil
}

func IndentExtJSON(dst *bytes.Buffer, src []byte, prefix, indent string) error {
err := json.Indent(dst, src, prefix, indent)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Optional: This can be simplified to just return any error returned by json.Indent

E.g.

return json.Indent(dst, src, prefix, indent)

@gabbyasuncion gabbyasuncion merged commit 15ace22 into mongodb:master Nov 2, 2021
@gabbyasuncion gabbyasuncion deleted the godriver-946 branch November 2, 2021 17:10
faem pushed a commit to kubedb/mongo-go-driver that referenced this pull request Mar 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants