Skip to content

Commit 1dce32a

Browse files
test: 添加测试case, change: 暴露两个私有方法
1 parent c2d895b commit 1dce32a

File tree

3 files changed

+214
-56
lines changed

3 files changed

+214
-56
lines changed

jsondiff.go

+37-30
Original file line numberDiff line numberDiff line change
@@ -202,22 +202,23 @@ func diff(diffs *diffs, path string, source, patch *JsonNode, option JsonDiffOpt
202202
}
203203
}
204204

205-
func getDiff(source, patch []byte, options ...JsonDiffOption) *JsonNode {
206-
sourceJsonNode := Unmarshal(source)
207-
patchJsonNode := Unmarshal(patch)
208-
option := JsonDiffOption(0)
209-
for _, o := range options {
210-
option |= o
211-
}
212-
diffs := newDiffs()
213-
diff(diffs, "", sourceJsonNode, patchJsonNode, option)
214-
doOption(diffs, option, sourceJsonNode, patchJsonNode)
215-
return diffs.d
205+
// GetDiffNode 比较两个 JsonNode 之间的差异,并返回 JsonNode 格式的差异结果
206+
func GetDiffNode(sourceJsonNode, patchJsonNode *JsonNode, options ...JsonDiffOption) *JsonNode {
207+
option := JsonDiffOption(0)
208+
for _, o := range options {
209+
option |= o
210+
}
211+
diffs := newDiffs()
212+
diff(diffs, "", sourceJsonNode, patchJsonNode, option)
213+
doOption(diffs, option, sourceJsonNode, patchJsonNode)
214+
return diffs.d
216215
}
217216

218217
// AsDiffs 比较 patch 相比于 source 的差别,返回 json 格式的差异文档。
219218
func AsDiffs(source, patch []byte, options ...JsonDiffOption) ([]byte, error) {
220-
dict := marshalSlice(getDiff(source, patch, options...))
219+
sourceJsonNode := Unmarshal(source)
220+
patchJsonNode := Unmarshal(patch)
221+
dict := marshalSlice(GetDiffNode(sourceJsonNode, patchJsonNode, options...))
221222
return json.Marshal(dict)
222223
}
223224

@@ -315,26 +316,32 @@ func mergeTest(srcNode *JsonNode, path string, value *JsonNode) error {
315316
// 根据差异文档 diff 还原 source 的差异
316317
func MergeDiff(source, diff []byte) ([]byte, error) {
317318
diffNode := Unmarshal(diff)
318-
if diffNode == nil {
319-
return nil, nil
320-
}
321-
if diffNode.Type != JsonNodeTypeSlice {
322-
return nil, errors.New("DabDiff")
323-
}
324319
srcNode := Unmarshal(source)
325-
326-
// guarantee atomicity
327-
copyNode := new(JsonNode)
328-
err := DeepCopy(copyNode, srcNode)
320+
result, err := MergeDiffNode(srcNode, diffNode)
329321
if err != nil {
330-
return nil, err
331-
}
332-
err = merge(copyNode, diffNode)
333-
if err != nil {
334-
return nil, err
335-
}
336-
srcNode = copyNode
337-
return Marshal(srcNode)
322+
return nil, err
323+
}
324+
return Marshal(result)
325+
}
326+
327+
func MergeDiffNode(source, diffs *JsonNode) (*JsonNode, error) {
328+
if diffs == nil {
329+
return source, nil
330+
}
331+
if diffs.Type != JsonNodeTypeSlice {
332+
return nil, errors.New("DabDiff")
333+
}
334+
copyNode := new(JsonNode)
335+
err := DeepCopy(copyNode, source)
336+
if err != nil {
337+
return nil, err
338+
}
339+
err = merge(copyNode, diffs)
340+
if err != nil {
341+
return nil, err
342+
}
343+
source = copyNode
344+
return source, nil
338345
}
339346

340347
func beautifyJsonString(data []byte) {

jsondiff_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ func TestGetDiff(t *testing.T) {
150150
}
151151
for _, tt := range tss {
152152
t.Run(tt.name, func(t *testing.T) {
153-
diffs := getDiff(tt.args.source, tt.args.patch, tt.args.options...)
153+
src := Unmarshal(tt.args.source)
154+
pat := Unmarshal(tt.args.patch)
155+
diffs := GetDiffNode(src, pat, tt.args.options...)
154156
if !eq(diffs, tt.want) {
155157
got, _ := Marshal(diffs)
156158
want, _ := Marshal(tt.want)

test_data/getDiffTest.json

+174-25
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,81 @@
11
{
22
"name": "GetDiffTest",
33
"cases": [
4+
{
5+
"name": "add",
6+
"src": {
7+
"A": 1,
8+
"B": [
9+
1,
10+
2,
11+
3
12+
],
13+
"C": {
14+
"CA": 1
15+
}
16+
},
17+
"patch": {
18+
"A": 1,
19+
"B": [
20+
1,
21+
2,
22+
3,
23+
4
24+
],
25+
"C": {
26+
"CA": 1,
27+
"CB": 2,
28+
"CC": {
29+
"CCA": 1
30+
}
31+
}
32+
},
33+
"want": [
34+
{
35+
"op": "add",
36+
"path": "/B/3",
37+
"value": 4
38+
},
39+
{
40+
"op": "add",
41+
"path": "/C/CB",
42+
"value": 2
43+
},
44+
{
45+
"op": "add",
46+
"path": "/C/CC",
47+
"value": {
48+
"CCA": 1
49+
}
50+
}
51+
],
52+
"want-error": false,
53+
"options": []
54+
},
455
{
556
"name": "replace",
657
"src": {
758
"A": 1,
8-
"B": [1, 2, 3],
59+
"B": [
60+
1,
61+
2,
62+
3
63+
],
964
"C": {
1065
"CA": 1
1166
}
1267
},
1368
"patch": {
1469
"A": 2,
15-
"B": [1, 2, 4],
70+
"B": [
71+
1,
72+
2,
73+
4
74+
],
1675
"C": {
17-
"CA": {"CAA": 1}
76+
"CA": {
77+
"CAA": 1
78+
}
1879
}
1980
},
2081
"want": [
@@ -31,7 +92,9 @@
3192
{
3293
"op": "replace",
3394
"path": "/C/CA",
34-
"value": {"CAA": 1}
95+
"value": {
96+
"CAA": 1
97+
}
3598
}
3699
],
37100
"want-error": false,
@@ -41,19 +104,37 @@
41104
"name": "copy",
42105
"src": {
43106
"A": 1,
44-
"B": [1, 2, 3],
107+
"B": [
108+
1,
109+
2,
110+
3
111+
],
45112
"C": {
46-
"CA": {"CAA": 1}
113+
"CA": {
114+
"CAA": 1
115+
}
47116
}
48117
},
49118
"patch": {
50119
"A": 1,
51-
"B": [1, 2, 3],
120+
"B": [
121+
1,
122+
2,
123+
3
124+
],
52125
"C": {
53-
"CA": {"CAA": 1},
54-
"CB": {"CAA": 1}
126+
"CA": {
127+
"CAA": 1
128+
},
129+
"CB": {
130+
"CAA": 1
131+
}
55132
},
56-
"D": [1, 2, 3]
133+
"D": [
134+
1,
135+
2,
136+
3
137+
]
57138
},
58139
"want": [
59140
{
@@ -68,21 +149,35 @@
68149
}
69150
],
70151
"want-error": false,
71-
"options": [1]
152+
"options": [
153+
1
154+
]
72155
},
73156
{
74157
"name": "move",
75158
"src": {
76159
"A": 1,
77-
"B": [1, 2, 3],
160+
"B": [
161+
1,
162+
2,
163+
3
164+
],
78165
"C": {
79-
"CA": {"CAA": 1}
166+
"CA": {
167+
"CAA": 1
168+
}
80169
}
81170
},
82171
"patch": {
83-
"E": [1, 2, 3],
172+
"E": [
173+
1,
174+
2,
175+
3
176+
],
84177
"F": {
85-
"CA": {"CAA": 1}
178+
"CA": {
179+
"CAA": 1
180+
}
86181
},
87182
"D": 1
88183
},
@@ -104,21 +199,48 @@
104199
}
105200
],
106201
"want-error": false,
107-
"options": [4]
202+
"options": [
203+
4
204+
]
108205
},
109206
{
110207
"name": "remove",
111208
"src": {
112209
"A": 1,
113-
"B": [1, 2, 3, {"BA": 1, "BB": [1, 2]}],
210+
"B": [
211+
1,
212+
2,
213+
3,
214+
{
215+
"BA": 1,
216+
"BB": [
217+
1,
218+
2
219+
]
220+
}
221+
],
114222
"C": {
115-
"CA": {"CAA": 1}
223+
"CA": {
224+
"CAA": 1
225+
}
116226
}
117227
},
118228
"patch": {
119-
"B": [1, 2, 3, {"BA": 1, "BB": [1]}],
229+
"B": [
230+
1,
231+
2,
232+
3,
233+
{
234+
"BA": 1,
235+
"BB": [
236+
1
237+
]
238+
}
239+
],
120240
"C": {
121-
"CA": {"CAA": 1}
241+
"CA": {
242+
"CAA": 1
243+
}
122244
}
123245
},
124246
"want": [
@@ -138,15 +260,40 @@
138260
"name": "full-remove",
139261
"src": {
140262
"A": 1,
141-
"B": [1, 2, 3, {"BA": 1, "BB": [1, 2]}],
263+
"B": [
264+
1,
265+
2,
266+
3,
267+
{
268+
"BA": 1,
269+
"BB": [
270+
1,
271+
2
272+
]
273+
}
274+
],
142275
"C": {
143-
"CA": {"CAA": 1}
276+
"CA": {
277+
"CAA": 1
278+
}
144279
}
145280
},
146281
"patch": {
147-
"B": [1, 2, 3, {"BA": 1, "BB": [1]}],
282+
"B": [
283+
1,
284+
2,
285+
3,
286+
{
287+
"BA": 1,
288+
"BB": [
289+
1
290+
]
291+
}
292+
],
148293
"C": {
149-
"CA": {"CAA": 1}
294+
"CA": {
295+
"CAA": 1
296+
}
150297
}
151298
},
152299
"want": [
@@ -162,7 +309,9 @@
162309
}
163310
],
164311
"want-error": false,
165-
"options": [8]
312+
"options": [
313+
8
314+
]
166315
}
167316
]
168317
}

0 commit comments

Comments
 (0)