/
json.go
92 lines (77 loc) · 2.17 KB
/
json.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package utls
import (
"fmt"
"strings"
jsonpatch "github.com/evanphx/json-patch"
"github.com/tidwall/sjson"
"github.com/wI2L/jsondiff"
)
func MergeJson(mergeA, toB []byte) (result []byte, changed bool, err error) {
ops, err := jsondiff.CompareJSON(toB, mergeA)
if err != nil {
return nil, false, fmt.Errorf("error comparing json: %w", err)
}
var addOps []jsondiff.Operation
for _, op := range ops {
switch t := op.Type; t {
case jsondiff.OperationAdd:
addOps = append(addOps, op)
case jsondiff.OperationRemove, jsondiff.OperationReplace:
continue
default:
panic(fmt.Sprintf("unexpected operation type: %s", t))
}
}
if len(addOps) == 0 {
return toB, false, nil
}
var opStrings []string
for _, op := range addOps {
opStrings = append(opStrings, op.String())
}
patchString := "[" + strings.Join(opStrings, ",") + "]"
jpatch, err := jsonpatch.DecodePatch([]byte(patchString))
if err != nil {
return nil, false, fmt.Errorf("error decoding patch: %w", err)
}
result, err = jpatch.Apply(toB)
if err != nil {
return nil, false, fmt.Errorf("error applying patch: %w", err)
}
return result, true, nil
}
func SubtractJson(fromA, subtractB []byte) (result []byte, changed bool, err error) {
ops, err := jsondiff.CompareJSON(subtractB, fromA)
if err != nil {
return nil, false, fmt.Errorf("error comparing json: %w", err)
}
var addOps []jsondiff.Operation
for _, op := range ops {
switch t := op.Type; t {
case jsondiff.OperationAdd, jsondiff.OperationReplace:
addOps = append(addOps, op)
case jsondiff.OperationRemove:
continue
default:
panic(fmt.Sprintf("unexpected operation type: %s", t))
}
}
res := "{}"
for _, op := range addOps {
jsonPath := JsonPatchPathToJsonPath(op.Path)
var err error
res, err = sjson.Set(res, jsonPath, op.Value)
if err != nil {
return nil, false, fmt.Errorf("error setting value by jsonpath: %w", err)
}
}
return []byte(res), string(fromA) != res, nil
}
func JsonPatchPathToJsonPath(path string) string {
if strings.HasPrefix(path, "/") {
path = path[1:]
}
path = strings.ReplaceAll(path, ".", `\.`)
path = strings.ReplaceAll(path, ":", `\:`)
return strings.ReplaceAll(path, "/", ".")
}