forked from mattbaird/jsonpatch
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from howardjohn/v2/improvements
Various improvements to performance and stability
- Loading branch information
Showing
6 changed files
with
174 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package jsonpatch_test | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"gomodules.xyz/jsonpatch/v2" | ||
) | ||
|
||
func BenchmarkCreatePatch(b *testing.B) { | ||
cases := []struct { | ||
name string | ||
a, b string | ||
}{ | ||
{ | ||
"complex", | ||
superComplexBase, | ||
superComplexA, | ||
}, | ||
{ | ||
"large array", | ||
largeArray(1000), | ||
largeArray(1000), | ||
}, | ||
{ | ||
"simple", | ||
simpleA, | ||
simpleB, | ||
}, | ||
} | ||
|
||
for _, tt := range cases { | ||
b.Run(tt.name, func(b *testing.B) { | ||
at := []byte(tt.a) | ||
bt := []byte(tt.b) | ||
for n := 0; n < b.N; n++ { | ||
_, _ = jsonpatch.CreatePatch(at, bt) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func largeArray(size int) string { | ||
type nested struct { | ||
A, B string | ||
} | ||
type example struct { | ||
Objects []nested | ||
} | ||
a := example{} | ||
for i := 0; i < size; i++ { | ||
a.Objects = append(a.Objects, nested{A: "a", B: "b"}) | ||
} | ||
res, _ := json.Marshal(a) | ||
return string(res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package jsonpatch_test | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
jp "github.com/evanphx/json-patch" | ||
"github.com/stretchr/testify/assert" | ||
"gomodules.xyz/jsonpatch/v2" | ||
) | ||
|
||
func FuzzCreatePatch(f *testing.F) { | ||
add := func(a, b string) { | ||
f.Add([]byte(a), []byte(b)) | ||
} | ||
add(simpleA, simpleB) | ||
add(superComplexBase, superComplexA) | ||
add(hyperComplexBase, hyperComplexA) | ||
add(arraySrc, arrayDst) | ||
add(empty, simpleA) | ||
add(point, lineString) | ||
f.Fuzz(func(t *testing.T, a, b []byte) { | ||
checkFuzz(t, a, b) | ||
}) | ||
} | ||
|
||
func checkFuzz(t *testing.T, src, dst []byte) { | ||
t.Logf("Test: %v -> %v", string(src), string(dst)) | ||
patch, err := jsonpatch.CreatePatch(src, dst) | ||
if err != nil { | ||
// Ok to error, src or dst may be invalid | ||
t.Skip() | ||
} | ||
|
||
// Applying library only works with arrays and structs, no primitives | ||
// We still do CreatePatch to make sure it doesn't panic | ||
if isPrimitive(src) || isPrimitive(dst) { | ||
return | ||
} | ||
|
||
for _, p := range patch { | ||
if p.Path == "" { | ||
// json-patch doesn't handle this properly, but it is valid | ||
return | ||
} | ||
} | ||
|
||
data, err := json.Marshal(patch) | ||
assert.Nil(t, err) | ||
|
||
t.Logf("Applying patch %v", string(data)) | ||
p2, err := jp.DecodePatch(data) | ||
assert.Nil(t, err) | ||
|
||
d2, err := p2.Apply(src) | ||
assert.Nil(t, err) | ||
|
||
assert.JSONEq(t, string(dst), string(d2)) | ||
} | ||
|
||
func isPrimitive(data []byte) bool { | ||
return data[0] != '{' && data[0] != '[' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
module gomodules.xyz/jsonpatch/v2 | ||
|
||
go 1.12 | ||
go 1.20 | ||
|
||
require ( | ||
github.com/evanphx/json-patch v0.5.2 | ||
github.com/stretchr/testify v1.3.0 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.0 // indirect | ||
github.com/pkg/errors v0.9.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters