Skip to content

Commit

Permalink
refactor: remove Compare(JSON)Opts functions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wI2L committed Jun 3, 2023
1 parent 5497a04 commit 3297dd3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 45 deletions.
4 changes: 2 additions & 2 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
18 changes: 2 additions & 16 deletions compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)

Expand All @@ -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...)

Expand Down
32 changes: 5 additions & 27 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
Expand All @@ -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"),
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3297dd3

Please sign in to comment.