Skip to content

Add a MergePatchWithOptions method #211

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions v5/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,18 @@ var errBadMergeTypes = fmt.Errorf("Mismatched JSON Documents")
// applying this resulting merged merge patch to a document yields the same
// as merging each merge patch to the document in succession.
func MergeMergePatches(patch1Data, patch2Data []byte) ([]byte, error) {
return doMergePatch(patch1Data, patch2Data, true)
return doMergePatch(patch1Data, patch2Data, true, NewApplyOptions())
}

// MergePatch merges the patchData into the docData.
func MergePatch(docData, patchData []byte) ([]byte, error) {
return doMergePatch(docData, patchData, false)
return doMergePatch(docData, patchData, false, NewApplyOptions())
}
func MergePatchWithOptions(docData, patchData []byte, options *ApplyOptions) ([]byte, error) {
return doMergePatch(docData, patchData, false, options)
}

func doMergePatch(docData, patchData []byte, mergeMerge bool) ([]byte, error) {
func doMergePatch(docData, patchData []byte, mergeMerge bool, options *ApplyOptions) ([]byte, error) {
if !json.Valid(docData) {
return nil, ErrBadJSONDoc
}
Expand All @@ -128,8 +131,6 @@ func doMergePatch(docData, patchData []byte, mergeMerge bool) ([]byte, error) {
return nil, ErrBadJSONPatch
}

options := NewApplyOptions()

doc := &partialDoc{
opts: options,
}
Expand Down
28 changes: 28 additions & 0 deletions v5/merge_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package jsonpatch

import (
"bytes"
"encoding/json"
"fmt"
"testing"
)
Expand Down Expand Up @@ -695,3 +697,29 @@ func TestMergeMergePatches(t *testing.T) {
}
}
}

func TestMergePatchWithOptions(t *testing.T) {
b := &bytes.Buffer{}
enc := json.NewEncoder(b)
enc.SetEscapeHTML(false)

v := struct {
X string
}{X: "&<>"}

if err := enc.Encode(&v); err != nil {
t.Fatal(err)
}
target := []byte(`{"key1": "val1", "key2": "val2"}`)

opts := NewApplyOptions()
opts.EscapeHTML = false

modified, err := MergePatchWithOptions(b.Bytes(), target, opts)
if err != nil {
t.Fatal(err)
}
if !compareJSON(string(modified), `{"X":"&<>","key1":"val1","key2":"val2"}`) {
t.Fatalf("testMergePatchWithOptions fails for %s", string(modified))
}
}
Loading