Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Equal ¶
Equal checks the equality of json.
The intended purpose is for unit testing equality of json. It is not intended to be used outside of that purpose.
Example ¶
package main
import (
"fmt"
"github.com/chanced/cmpjson"
)
func main() {
dataA := []byte(`{
"fieldA": ["1", "2", "3"],
"fieldB": "str"
}`)
dataB := []byte(`{
"fieldB": "str",
"fieldA": ["2","3"]
}`)
if ok, diff, err := cmpjson.Equal(dataA, dataB); !ok {
fmt.Println(diff)
if err != nil {
fmt.Println(err)
}
}
}
Output: [ { "op": "remove", "path": "/fieldA/2" }, { "op": "replace", "path": "/fieldA/0", "value": "2" }, { "op": "replace", "path": "/fieldA/1", "value": "3" } ]
func MustDiff ¶
func MustDiff(a interface{}, b interface{}) []byte
MustDiff returns a JSON patch of the differences between of a and b.
MustDiff panics if there is an error marshaling a or b (if necessary) or performing the diff
func MustEqual ¶
MustEqual checks the equality of json.
It panics if there is an error marshaling or unmarshaling to keep the interface clean & simple.
The intended purpose is for unit testing equality of json. It is not intended to be used outside of that purpose.
Example ¶
package main
import (
"fmt"
"github.com/chanced/cmpjson"
)
func main() {
dataA := map[string]interface{}{
"fieldA": []interface{}{"1", "2", "3"},
"fieldB": "initial",
}
dataB := map[string]interface{}{
"fieldA": []interface{}{"1", "2"},
"fieldB": "newval",
}
if ok, diff := cmpjson.MustEqual(dataA, dataB); !ok {
fmt.Println(diff)
} else {
fmt.Println("dataA and dataB were equal")
}
}
Output: [ { "op": "remove", "path": "/fieldA/2" }, { "op": "replace", "path": "/fieldB", "value": "newval" } ]
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.