Add function for supporting JSON equality checks using byte slices #1513
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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: