From 3297dd3c04a68cd260f4e4bb699c0f6115d32ff2 Mon Sep 17 00:00:00 2001 From: William Poussier Date: Sun, 4 Jun 2023 00:25:27 +0200 Subject: [PATCH] refactor: remove Compare(JSON)Opts functions This commit update the prototypes of the Compare and CompareJSON functions to take an option list as a third variadic argument, and remove their CompareOpts and CompareJSONOpts counterpart. --- bench_test.go | 4 ++-- compare.go | 18 ++---------------- example_test.go | 32 +++++--------------------------- 3 files changed, 9 insertions(+), 45 deletions(-) diff --git a/bench_test.go b/bench_test.go index c64e51e..475d835 100644 --- a/bench_test.go +++ b/bench_test.go @@ -93,7 +93,7 @@ func subBenchmarks(b *testing.B, src, tgt, tgtUnordered []byte) { b.Skip() } for i := 0; i < b.N; i++ { - patch, err := CompareJSONOpts(src, bb.afterBytes, bb.opts...) + patch, err := CompareJSON(src, bb.afterBytes, bb.opts...) if err != nil { b.Error(err) } @@ -105,7 +105,7 @@ func subBenchmarks(b *testing.B, src, tgt, tgtUnordered []byte) { b.Skip() } for i := 0; i < b.N; i++ { - patch, err := CompareOpts(before, after, bb.opts...) + patch, err := Compare(before, after, bb.opts...) if err != nil { b.Error(err) } diff --git a/compare.go b/compare.go index 91f7808..24859b4 100644 --- a/compare.go +++ b/compare.go @@ -5,14 +5,7 @@ import "encoding/json" // Compare compares the JSON representations of the // given values and returns the differences relative // to the former as a list of JSON Patch operations. -func Compare(source, target interface{}) (Patch, error) { - var d Differ - return compare(&d, source, target) -} - -// CompareOpts is similar to Compare, but also accepts -// a list of options to configure the behavior. -func CompareOpts(source, target interface{}, opts ...Option) (Patch, error) { +func Compare(source, target interface{}, opts ...Option) (Patch, error) { var d Differ d.applyOpts(opts...) @@ -22,14 +15,7 @@ func CompareOpts(source, target interface{}, opts ...Option) (Patch, error) { // CompareJSON compares the given JSON documents and // returns the differences relative to the former as // a list of JSON Patch operations. -func CompareJSON(source, target []byte) (Patch, error) { - var d Differ - return compareJSON(&d, source, target, json.Unmarshal) -} - -// CompareJSONOpts is similar to CompareJSON, but also -// accepts a list of options to configure the behavior. -func CompareJSONOpts(source, target []byte, opts ...Option) (Patch, error) { +func CompareJSON(source, target []byte, opts ...Option) (Patch, error) { var d Differ d.applyOpts(opts...) diff --git a/example_test.go b/example_test.go index 8da9865..088999c 100644 --- a/example_test.go +++ b/example_test.go @@ -87,28 +87,6 @@ func ExampleCompare() { // {"op":"remove","path":"/spec/volumes/0/emptyDir/medium"} } -func ExampleCompareOpts() { - oldPod := createPod() - newPod := createPod() - - newPod.Spec.Volumes = append(newPod.Spec.Volumes, oldPod.Spec.Volumes[0]) - - patch, err := jsondiff.CompareOpts( - oldPod, - newPod, - jsondiff.Factorize(), - jsondiff.Rationalize(), - ) - if err != nil { - log.Fatal(err) - } - for _, op := range patch { - fmt.Printf("%s\n", op) - } - // Output: - // {"op":"copy","from":"/spec/volumes/0","path":"/spec/volumes/-"} -} - func ExampleCompareJSON() { type Phone struct { Type string `json:"type"` @@ -154,7 +132,7 @@ func ExampleInvertible() { source := `{"a":"1","b":"2"}` target := `{"a":"3","c":"4"}` - patch, err := jsondiff.CompareJSONOpts( + patch, err := jsondiff.CompareJSON( []byte(source), []byte(target), jsondiff.Invertible(), @@ -177,7 +155,7 @@ func ExampleFactorize() { source := `{"a":[1,2,3],"b":{"foo":"bar"}}` target := `{"a":[1,2,3],"c":[1,2,3],"d":{"foo":"bar"}}` - patch, err := jsondiff.CompareJSONOpts( + patch, err := jsondiff.CompareJSON( []byte(source), []byte(target), jsondiff.Factorize(), @@ -197,7 +175,7 @@ func ExampleIgnores() { source := `{"A":"bar","B":"baz","C":"foo"}` target := `{"A":"rab","B":"baz","D":"foo"}` - patch, err := jsondiff.CompareJSONOpts( + patch, err := jsondiff.CompareJSON( []byte(source), []byte(target), jsondiff.Ignores("/A", "/C", "/D"), @@ -218,7 +196,7 @@ func ExampleMarshalFunc() { newPod.Spec.Containers[0].Name = "nginx" newPod.Spec.Volumes[0].Name = "data" - patch, err := jsondiff.CompareOpts( + patch, err := jsondiff.Compare( oldPod, newPod, jsondiff.MarshalFunc(func(v any) ([]byte, error) { @@ -246,7 +224,7 @@ func ExampleUnmarshalFunc() { source := `{"A":"bar","B":3.14,"C":false}` target := `{"A":"baz","B":3.14159,"C":true}` - patch, err := jsondiff.CompareJSONOpts( + patch, err := jsondiff.CompareJSON( []byte(source), []byte(target), jsondiff.UnmarshalFunc(func(b []byte, v any) error {