Skip to content

Commit eadb4ab

Browse files
author
Benito
committed
dsc order collection window
1 parent c79cf18 commit eadb4ab

File tree

2 files changed

+70
-12
lines changed

2 files changed

+70
-12
lines changed

jsonpatch.go

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,35 +109,63 @@ func CreatePatch(a, b []byte) ([]Operation, error) {
109109
keysOriginal[k] = true
110110
}
111111

112-
if len(modified) == len(original) {
113-
// very specific case of a moving window of collections in ascending order
114-
diffCount := 0
112+
if len(modified) == len(original) && len(original) > 2 {
113+
// moving window of collections in ascending order
114+
diffAsc := 0
115115
length := len(modified) - 1
116116
for key := range modified {
117117
// first element of the original cant be found in the modified
118118
if key < length && string(original[0]) == string(modified[key]) {
119-
diffCount++
119+
diffAsc++
120120
break
121121
}
122122
// last element of the modified cant be found in the original
123123
if key > 0 && string(modified[length]) == string(original[key]) {
124-
diffCount++
124+
diffAsc++
125125
break
126126
}
127-
// other then first original and last modified all elements are the same
127+
// other than the first original and last modified all elements are the same
128128
if key < length && string(original[key+1]) != string(modified[key]) {
129-
diffCount++
129+
diffAsc++
130130
break
131131
}
132132
}
133133

134-
if diffCount == 0 {
134+
if diffAsc == 0 {
135135
pFirst := makePath(path, 0)
136136
pLast := makePath(path, length)
137137
patch = append([]Operation{NewPatch("add", pLast, modified[0])}, patch...)
138138
patch = append([]Operation{NewPatch("remove", pFirst, nil)}, patch...)
139139
return patch, nil
140140
}
141+
142+
// moving window of collections in descending order
143+
diffDsc := 0
144+
for key := range modified {
145+
// first element of the modified cant be found in the original
146+
if key < length && string(modified[0]) == string(original[key]) {
147+
diffDsc++
148+
break
149+
}
150+
// last element of the original cant be found in the modified
151+
if key > 0 && string(original[length]) == string(modified[key]) {
152+
diffDsc++
153+
break
154+
}
155+
// other than the first modified and last original all elements are the same
156+
if key < length && string(modified[key+1]) != string(original[key]) {
157+
diffDsc++
158+
break
159+
}
160+
}
161+
162+
if diffDsc == 0 {
163+
pFirst := makePath(path, 0)
164+
pLast := makePath(path, length+1)
165+
patch = append([]Operation{NewPatch("remove", pLast, nil)}, patch...)
166+
patch = append([]Operation{NewPatch("add", pFirst, modified[0])}, patch...)
167+
return patch, nil
168+
}
141169
}
142170

143171
for key, bv := range modified {

jsonpatch_simple_test.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var empty = `{}`
2020
var collection = `[{"created":1564944548180294000,"updated":0,"index":"15b7ccc66f7878a8","data":"eyJuYW1lIjoibmFtZTEifQ=="},{"created":1564944548172292600,"updated":0,"index":"15b7ccc66efe6194","data":"eyJuYW1lIjoibmFtZTAifQ=="}]`
2121
var emptyCollection = `[]`
2222

23-
var collectionWindowBefore = `[{
23+
var collectionWindowAscBefore = `[{
2424
"test":"1"
2525
},
2626
{
@@ -30,7 +30,7 @@ var collectionWindowBefore = `[{
3030
"test":"3"
3131
}]`
3232

33-
var collectionWindowAfter = `[{
33+
var collectionWindowAscAfter = `[{
3434
"test":"2"
3535
},
3636
{
@@ -40,15 +40,35 @@ var collectionWindowAfter = `[{
4040
"test":"4"
4141
}]`
4242

43+
var collectionWindowDscBefore = `[{
44+
"test":"3"
45+
},
46+
{
47+
"test":"2"
48+
},
49+
{
50+
"test":"1"
51+
}]`
52+
53+
var collectionWindowDscAfter = `[{
54+
"test":"4"
55+
},
56+
{
57+
"test":"3"
58+
},
59+
{
60+
"test":"2"
61+
}]`
62+
4363
func TestMultipleRemove(t *testing.T) {
4464
patch, e := CreatePatch([]byte(collection), []byte(emptyCollection))
4565
assert.NoError(t, e)
4666
assert.Equal(t, len(patch), 2, "the patch should be the same lenght as the collection")
4767
assert.Equal(t, patch[0].Path, "/1", "the patch should have descending order by path")
4868
}
4969

50-
func TestCollectionWindowMove(t *testing.T) {
51-
patch, e := CreatePatch([]byte(collectionWindowBefore), []byte(collectionWindowAfter))
70+
func TestCollectionWindowAscMove(t *testing.T) {
71+
patch, e := CreatePatch([]byte(collectionWindowAscBefore), []byte(collectionWindowAscAfter))
5272
assert.NoError(t, e)
5373
assert.Equal(t, 2, len(patch), "the patch should have one insert and one remove")
5474
assert.Equal(t, "remove", patch[0].Operation, "the patch should remove the first position")
@@ -57,6 +77,16 @@ func TestCollectionWindowMove(t *testing.T) {
5777
assert.Equal(t, "/2", patch[1].Path, "the patch should have descending order by path")
5878
}
5979

80+
func TestCollectionWindowDscMove(t *testing.T) {
81+
patch, e := CreatePatch([]byte(collectionWindowDscBefore), []byte(collectionWindowDscAfter))
82+
assert.NoError(t, e)
83+
assert.Equal(t, 2, len(patch), "the patch should have one insert and one remove")
84+
assert.Equal(t, "add", patch[0].Operation, "the patch should remove the first position")
85+
assert.Equal(t, "/0", patch[0].Path, "the patch should have descending order by path")
86+
assert.Equal(t, "remove", patch[1].Operation, "the patch should add on the last position")
87+
assert.Equal(t, "/3", patch[1].Path, "the patch should have descending order by path")
88+
}
89+
6090
func TestOneNullReplace(t *testing.T) {
6191
patch, e := CreatePatch([]byte(simplef), []byte(simpleG))
6292
assert.NoError(t, e)

0 commit comments

Comments
 (0)