Skip to content
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

Shortcut when two json documents are same #37

Merged
merged 3 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions v2/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func BenchmarkCreatePatch(b *testing.B) {
},
{
"large array",
largeArray(1000),
largeArray(1000),
largeArray(1000, "a"),
largeArray(1000, "b"),
},
{
"simple",
Expand All @@ -40,7 +40,7 @@ func BenchmarkCreatePatch(b *testing.B) {
}
}

func largeArray(size int) string {
func largeArray(size int, val string) string {
type nested struct {
A, B string
}
Expand All @@ -49,7 +49,7 @@ func largeArray(size int) string {
}
a := example{}
for i := 0; i < size; i++ {
a.Objects = append(a.Objects, nested{A: "a", B: "b"})
a.Objects = append(a.Objects, nested{A: "a", B: val})
}
res, _ := json.Marshal(a)
return string(res)
Expand Down
4 changes: 4 additions & 0 deletions v2/jsonpatch.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jsonpatch

import (
"bytes"
"encoding/json"
"fmt"
"reflect"
Expand Down Expand Up @@ -64,6 +65,9 @@ func NewOperation(op, path string, value interface{}) Operation {
//
// An error will be returned if any of the two documents are invalid.
func CreatePatch(a, b []byte) ([]Operation, error) {
if bytes.Equal(a, b) {
return []Operation{}, nil
}
var aI interface{}
var bI interface{}
err := json.Unmarshal(a, &aI)
Expand Down