forked from oapi-codegen/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonmerge.go
34 lines (30 loc) · 856 Bytes
/
jsonmerge.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package runtime
import (
"encoding/json"
"github.com/apapsch/go-jsonmerge/v2"
)
// JsonMerge merges two JSON representation into a single object. `data` is the
// existing representation and `patch` is the new data to be merged in
//
// Deprecated: Use JSONMerge instead.
func JsonMerge(data, patch json.RawMessage) (json.RawMessage, error) {
return JSONMerge(data, patch)
}
// JSONMerge merges two JSON representation into a single object. `data` is the
// existing representation and `patch` is the new data to be merged in
func JSONMerge(data, patch json.RawMessage) (json.RawMessage, error) {
merger := jsonmerge.Merger{
CopyNonexistent: true,
}
if data == nil {
data = []byte(`{}`)
}
if patch == nil {
patch = []byte(`{}`)
}
merged, err := merger.MergeBytes(data, patch)
if err != nil {
return nil, err
}
return merged, nil
}