-
Notifications
You must be signed in to change notification settings - Fork 912
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
Conversation
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.
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.
bson/marshal.go
Outdated
return formatted, nil | ||
} | ||
|
||
func MarshalExtJSONIndent(val interface{}, canonical, escapeHTML bool, dst *bytes.Buffer, prefix, indent string) ([]byte, error) { |
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.
Modified this function's parameters as well to match json.Indent()
's parameters.
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.
Assume you mean modified IndentExtJSON
to match json.Indent()
's params; that sounds good.
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.
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).
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 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).
bson/marshal.go
Outdated
return formatted, nil | ||
} | ||
|
||
func MarshalExtJSONIndent(val interface{}, canonical, escapeHTML bool, dst *bytes.Buffer, prefix, indent string) ([]byte, error) { |
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.
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) { |
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.
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).
… to match json package
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.
Almost there!
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.
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) { |
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 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).
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.
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) |
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.
Optional: This can be simplified to just return any error returned by json.Indent
E.g.
return json.Indent(dst, src, prefix, indent)
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.