-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 function for supporting JSON equality checks using byte slices #1513
base: master
Are you sure you want to change the base?
Conversation
Hi ! |
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 is a good implementation.
I have questions about whether JSONEq should have ever been added. My preference would always be your API should yield stably formatted output. If JSONEq should have been added then it should have always accepted a []byte rather than a string. I'm not going to merge this without input from other maintainers, any thoughts @dolmen?
It would be useful to see if there is a real world example where the string is prohibitively expensive.
@GCrispino can you re-base to run the latest version of the tests. |
I note that YAMLEq also accepts a string, that's an even more problematic assertion. |
c1830b9
to
528f908
Compare
528f908
to
29a3e3b
Compare
@brackendawson rebased my branch against master and re ran |
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.
Still approve, as per my previous comments.
@brackendawson thanks! Is there something else I should do, or are you only expecting review from another maintainer? |
Nothing else needed from you at this time, just wait for another maintainer to opine. |
Summary
This PR adds a new function into the
assert
package,JSONEqBytes
, that does the same asJSONEq
, but by receiving[]byte
values as parameter instead ofstring
ones.Changes
assert/assertions.go
:The implementation of
JSONEqBytes
is pretty much what was inJSONEq
, but having it receive[]byte
values and then not needing to convert these values to[]byte
when callingjson.Unmarshal
inside it.Then, the implementation of
JSONEq
was changed such that it just callsJSONEqBytes
by converting its original arguments to[]byte
values (which it already did when callingjson.Unmarshal
)assert/assertions_test.go
:Tests for
JSONEqBytes
function were replicated from the tests fromJSONEq
.Motivation
It is common in Go to store JSON text in
[]byte
variables instead ofstring
values, so having a function that allows to receive these values when doing JSON-equality checks without having to cast them to string can be useful.Maybe not as important, it would also avoid unnecessary
[]byte
-to-string
conversions, which could add up when dealing with big JSON payloads.Example of usage: