Skip to content

Commit

Permalink
only merge the first-level fields of json, and assign values directly…
Browse files Browse the repository at this point in the history
… to deeper fields
  • Loading branch information
zealllot committed Mar 22, 2022
1 parent 6a7ad8a commit 0e5a7a1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
21 changes: 19 additions & 2 deletions merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ func merge(cur, patch *lazyNode, mergeMerge bool) *lazyNode {
if err != nil {
return patch
}

mergeDocs(curDoc, patchDoc, mergeMerge)
mergeDocs2(curDoc, patchDoc, mergeMerge)

return cur
}
Expand Down Expand Up @@ -50,6 +49,24 @@ func mergeDocs(doc, patch *partialDoc, mergeMerge bool) {
}
}

func mergeDocs2(doc, patch *partialDoc, mergeMerge bool) {
for k, v := range *patch {
if v == nil {
if mergeMerge {
(*doc)[k] = nil
} else {
delete(*doc, k)
}
} else {
if !mergeMerge {
pruneNulls(v)
}

(*doc)[k] = v
}
}
}

func pruneNulls(n *lazyNode) {
sub, err := n.intoDoc()

Expand Down
17 changes: 17 additions & 0 deletions print.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package jsonpatch

import (
"bytes"
"encoding/json"
"fmt"
)

func PrintJson(data []byte) {
var out bytes.Buffer
err := json.Indent(&out, data, "", " ")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(out.String())
}

0 comments on commit 0e5a7a1

Please sign in to comment.