-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsondiff.go
235 lines (222 loc) · 6.98 KB
/
jsondiff.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package json_diff
import (
"bytes"
"encoding/json"
"fmt"
"github.com/520MianXiangDuiXiang520/json-diff/decode"
"github.com/pkg/errors"
"strconv"
)
func diffSlice(diffs *diffs, path string, source, patch *decode.JsonNode, option JsonDiffOption) {
lcsList := longestCommonSubsequence(source.Children, patch.Children)
lcsIdx := 0
srcIdx := 0
tarIdx := 0
pos := 0
for lcsIdx < len(lcsList) {
pathBuffer := bytes.NewBufferString(path)
srcNode := source.Children[srcIdx]
lcsNode := lcsList[lcsIdx]
tarNode := patch.Children[tarIdx]
if lcsNode.Equal(srcNode) && lcsNode.Equal(tarNode) {
lcsIdx++
srcIdx++
tarIdx++
pos++
} else {
if lcsNode.Equal(srcNode) {
pathBuffer.WriteString("/")
pathBuffer.WriteString(strconv.Itoa(pos))
diffs.add(newDiffNode(DiffTypeAdd, pathBuffer.String(), tarNode, "", option))
tarIdx++
pos++
} else if lcsNode.Equal(tarNode) {
pathBuffer.WriteString("/")
pathBuffer.WriteString(strconv.Itoa(pos))
diffs.add(newDiffNode(DiffTypeRemove, pathBuffer.String(), srcNode, "", option))
srcIdx++
} else {
pathBuffer.WriteString("/")
pathBuffer.WriteString(strconv.Itoa(pos))
diff(diffs, pathBuffer.String(), srcNode, tarNode, option)
srcIdx++
tarIdx++
pos++
}
}
}
for srcIdx < len(source.Children) && tarIdx < len(patch.Children) {
pathBuffer := bytes.NewBufferString(path)
srcNode := source.Children[srcIdx]
tarNode := patch.Children[tarIdx]
pathBuffer.WriteString("/")
pathBuffer.WriteString(strconv.Itoa(pos))
diff(diffs, pathBuffer.String(), srcNode, tarNode, option)
srcIdx++
tarIdx++
pos++
}
// 如果 source 或 patch 后面还有,属于 add 或 remove
for ; srcIdx < len(source.Children); srcIdx++ {
pathBuffer := bytes.NewBufferString(path)
pathBuffer.WriteString("/")
pathBuffer.WriteString(strconv.Itoa(pos))
diffs.add(newDiffNode(DiffTypeRemove, pathBuffer.String(), source.Children[srcIdx], "", option))
pos++
}
for ; tarIdx < len(patch.Children); tarIdx++ {
pathBuffer := bytes.NewBufferString(path)
pathBuffer.WriteString("/")
pathBuffer.WriteString(strconv.Itoa(pos))
diffs.add(newDiffNode(DiffTypeAdd, pathBuffer.String(), patch.Children[tarIdx], "", option))
pos++
}
}
func diffObject(diffs *diffs, path string, source, patch *decode.JsonNode, option JsonDiffOption) {
for srcKey, srcValue := range source.ChildrenMap {
tarVal, tarOk := patch.ChildrenMap[srcKey]
currPath := fmt.Sprintf("%s/%s", path, srcKey)
if !tarOk {
diffs.add(newDiffNode(DiffTypeRemove, currPath, srcValue, "", option))
continue
}
diff(diffs, currPath, srcValue, tarVal, option)
}
for tarKey, tarVal := range patch.ChildrenMap {
_, srcOk := source.ChildrenMap[tarKey]
if !srcOk {
currPath := fmt.Sprintf("%s/%s", path, tarKey)
diffs.add(newDiffNode(DiffTypeAdd, currPath, tarVal, "", option))
}
}
}
func diff(diffs *diffs, path string, source, patch *decode.JsonNode, option JsonDiffOption) {
if source == nil && patch != nil {
diffs.add(newDiffNode(DiffTypeAdd, path, patch, "", option))
}
if source != nil && patch == nil {
diffs.add(newDiffNode(DiffTypeRemove, path, nil, "", option))
}
if source != nil && patch != nil {
if source.Type == decode.JsonNodeTypeObject && patch.Type == decode.JsonNodeTypeObject {
diffObject(diffs, path, source, patch, option)
} else if source.Type == decode.JsonNodeTypeSlice && patch.Type == decode.JsonNodeTypeSlice {
diffSlice(diffs, path, source, patch, option)
} else {
// 两个都是 JsonNodeTypeValue
if !source.Equal(patch) {
diffs.add(newDiffNode(DiffTypeReplace, path, patch, "", option))
}
}
}
}
// GetDiffNode 比较两个 JsonNode 之间的差异,并返回 JsonNode 格式的差异结果
func GetDiffNode(sourceJsonNode, patchJsonNode *decode.JsonNode, options ...JsonDiffOption) *decode.JsonNode {
option := JsonDiffOption(0)
for _, o := range options {
option |= o
}
diffs := newDiffs()
diff(diffs, "", sourceJsonNode, patchJsonNode, option)
doOption(diffs, option, sourceJsonNode, patchJsonNode)
return diffs.d
}
// AsDiffs 比较 patch 相比于 source 的差别,返回 json 格式的差异文档。
func AsDiffs(source, patch []byte, options ...JsonDiffOption) ([]byte, error) {
// sourceJsonNode, err := decode.Unmarshal(source)
sourceJsonNode, err := Unmarshal(source)
if err != nil {
return nil, errors.Wrap(err, "fail to unmarshal src")
}
// patchJsonNode, err := decode.Unmarshal(patch)
patchJsonNode, err := Unmarshal(patch)
if err != nil {
return nil, errors.Wrap(err, "fail to unmarshal tar")
}
dict := marshalSlice(GetDiffNode(sourceJsonNode, patchJsonNode, options...))
return json.Marshal(dict)
}
func merge(srcNode, diffNode *decode.JsonNode) error {
for _, diff := range diffNode.Children {
if diff.Type != decode.JsonNodeTypeObject {
return errors.WithStack(decode.BadDiffsError)
}
op := diff.ChildrenMap["op"].Value
path := diff.ChildrenMap["path"].Value.(string)
switch op {
case "add":
err := decode.AddPath(srcNode, path, diff.ChildrenMap["value"])
if err != nil {
return err
}
case "remove":
_, err := decode.RemovePath(srcNode, path)
if err != nil {
return err
}
case "replace":
val := diff.ChildrenMap["value"]
_, err := decode.ReplacePath(srcNode, path, val)
if err != nil {
return err
}
case "move":
from := diff.ChildrenMap["from"].Value.(string)
_, err := decode.MovePath(srcNode, from, path)
if err != nil {
return err
}
case "copy":
from := diff.ChildrenMap["from"].Value.(string)
err := decode.CopyPath(srcNode, from, path)
if err != nil {
return err
}
case "test":
err := decode.ATestPath(srcNode, path, diff.ChildrenMap["value"])
if err != nil {
return err
}
default:
return errors.New(fmt.Sprintf("bad diffs: %v", diff))
}
}
return nil
}
// MergeDiff 根据差异文档 diff 还原 source 的差异
func MergeDiff(source, diff []byte) ([]byte, error) {
// diffNode, err := decode.Unmarshal(diff)
diffNode, err := Unmarshal(diff)
if err != nil {
return nil, errors.Wrap(err, "fail to unmarshal diff data")
}
// srcNode, err := decode.Unmarshal(source)
srcNode, err := Unmarshal(source)
if err != nil {
return nil, errors.Wrap(err, "fail to unmarshal source data")
}
result, err := MergeDiffNode(srcNode, diffNode)
if err != nil {
return nil, errors.Wrap(err, "fail to merge diff")
}
return decode.Marshal(result)
}
// MergeDiffNode 将 JsonNode 类型的 diffs 应用于源 source 上,并返回合并后的新 jsonNode 对象
// 如果 diffs 不合法,第二个参数将会返回 BadDiffsError
func MergeDiffNode(source, diffs *decode.JsonNode) (*decode.JsonNode, error) {
if diffs == nil {
return source, nil
}
if diffs.Type != decode.JsonNodeTypeSlice {
return nil, errors.New("bad diffs")
}
copyNode, err := DeepCopy(source)
if err != nil {
return nil, errors.Wrap(err, "fail to deep copy source")
}
err = merge(copyNode, diffs)
if err != nil {
return nil, errors.Wrap(err, "fail to merge")
}
return copyNode, nil
}