@@ -132,6 +132,16 @@ var _ = Describe("ReplaceOp.Apply", func() {
132
132
Expect (res ).To (Equal ([]interface {}{1 , 2 , 3 , 10 }))
133
133
})
134
134
135
+ It ("prepends new item" , func () {
136
+ res , err := ReplaceOp {Path : MustNewPointerFromString ("/+" ), Value : 10 }.Apply ([]interface {}{})
137
+ Expect (err ).ToNot (HaveOccurred ())
138
+ Expect (res ).To (Equal ([]interface {}{10 }))
139
+
140
+ res , err = ReplaceOp {Path : MustNewPointerFromString ("/+" ), Value : 10 }.Apply ([]interface {}{1 , 2 , 3 })
141
+ Expect (err ).ToNot (HaveOccurred ())
142
+ Expect (res ).To (Equal ([]interface {}{10 , 1 , 2 , 3 }))
143
+ })
144
+
135
145
It ("appends nested array item" , func () {
136
146
doc := []interface {}{[]interface {}{10 , 11 , 12 }, 2 , 3 }
137
147
@@ -140,6 +150,14 @@ var _ = Describe("ReplaceOp.Apply", func() {
140
150
Expect (res ).To (Equal ([]interface {}{[]interface {}{10 , 11 , 12 , 100 }, 2 , 3 }))
141
151
})
142
152
153
+ It ("prepends nested array item" , func () {
154
+ doc := []interface {}{[]interface {}{10 , 11 , 12 }, 2 , 3 }
155
+
156
+ res , err := ReplaceOp {Path : MustNewPointerFromString ("/0/+" ), Value : 100 }.Apply (doc )
157
+ Expect (err ).ToNot (HaveOccurred ())
158
+ Expect (res ).To (Equal ([]interface {}{[]interface {}{100 , 10 , 11 , 12 }, 2 , 3 }))
159
+ })
160
+
143
161
It ("appends array item from an array that is inside a map" , func () {
144
162
doc := map [interface {}]interface {}{
145
163
"abc" : []interface {}{1 , 2 , 3 },
@@ -153,6 +171,19 @@ var _ = Describe("ReplaceOp.Apply", func() {
153
171
}))
154
172
})
155
173
174
+ It ("prepends array item from an array that is inside a map" , func () {
175
+ doc := map [interface {}]interface {}{
176
+ "abc" : []interface {}{1 , 2 , 3 },
177
+ }
178
+
179
+ res , err := ReplaceOp {Path : MustNewPointerFromString ("/abc/+" ), Value : 10 }.Apply (doc )
180
+ Expect (err ).ToNot (HaveOccurred ())
181
+
182
+ Expect (res ).To (Equal (map [interface {}]interface {}{
183
+ "abc" : []interface {}{10 , 1 , 2 , 3 },
184
+ }))
185
+ })
186
+
156
187
It ("returns an error if after last index token is not last" , func () {
157
188
ptr := NewPointer ([]Token {RootToken {}, AfterLastIndexToken {}, KeyToken {}})
158
189
@@ -162,6 +193,15 @@ var _ = Describe("ReplaceOp.Apply", func() {
162
193
"Expected after last index token to be last in path '/-/'" ))
163
194
})
164
195
196
+ It ("returns an error if before first index token is not last" , func () {
197
+ ptr := NewPointer ([]Token {RootToken {}, BeforeFirstIndexToken {}, KeyToken {}})
198
+
199
+ _ , err := ReplaceOp {Path : ptr }.Apply ([]interface {}{})
200
+ Expect (err ).To (HaveOccurred ())
201
+ Expect (err .Error ()).To (Equal (
202
+ "Expected before first index token to be last in path '/+/'" ))
203
+ })
204
+
165
205
It ("returns an error if it's not an array being accessed" , func () {
166
206
_ , err := ReplaceOp {Path : MustNewPointerFromString ("/-" )}.Apply (map [interface {}]interface {}{})
167
207
Expect (err ).To (HaveOccurred ())
@@ -175,6 +215,20 @@ var _ = Describe("ReplaceOp.Apply", func() {
175
215
Expect (err .Error ()).To (Equal (
176
216
"Expected to find an array at path '/key/-' but found 'map[interface {}]interface {}'" ))
177
217
})
218
+
219
+ It ("returns an error if it's not an array being accessed" , func () {
220
+ _ , err := ReplaceOp {Path : MustNewPointerFromString ("/+" )}.Apply (map [interface {}]interface {}{})
221
+ Expect (err ).To (HaveOccurred ())
222
+ Expect (err .Error ()).To (Equal (
223
+ "Expected to find an array at path '/+' but found 'map[interface {}]interface {}'" ))
224
+
225
+ doc := map [interface {}]interface {}{"key" : map [interface {}]interface {}{}}
226
+
227
+ _ , err = ReplaceOp {Path : MustNewPointerFromString ("/key/+" )}.Apply (doc )
228
+ Expect (err ).To (HaveOccurred ())
229
+ Expect (err .Error ()).To (Equal (
230
+ "Expected to find an array at path '/key/+' but found 'map[interface {}]interface {}'" ))
231
+ })
178
232
})
179
233
180
234
Describe ("array item with matching key and value" , func () {
@@ -429,6 +483,18 @@ var _ = Describe("ReplaceOp.Apply", func() {
429
483
}))
430
484
})
431
485
486
+ It ("creates missing key with array value for index access if key is not expected to exist" , func () {
487
+ doc := map [interface {}]interface {}{"xyz" : "xyz" }
488
+
489
+ res , err := ReplaceOp {Path : MustNewPointerFromString ("/abc?/+" ), Value : 1 }.Apply (doc )
490
+ Expect (err ).ToNot (HaveOccurred ())
491
+
492
+ Expect (res ).To (Equal (map [interface {}]interface {}{
493
+ "abc" : []interface {}{1 },
494
+ "xyz" : "xyz" ,
495
+ }))
496
+ })
497
+
432
498
It ("returns an error if missing key needs to be created but next access does not make sense" , func () {
433
499
doc := map [interface {}]interface {}{"xyz" : "xyz" }
434
500
0 commit comments