@@ -36,8 +36,12 @@ type B struct {
36
36
}
37
37
38
38
type C struct {
39
- Str string `json:"str,omitempty"`
40
- Map map [string ]string `json:"map"`
39
+ Str string `json:"str,omitempty"`
40
+ StrMap map [string ]string `json:"strmap"`
41
+ IntMap map [string ]int `json:"intmap"`
42
+ BoolMap map [string ]bool `json:"boolmap"`
43
+ StructMap map [string ]B `json:"structmap"`
44
+ PtrMap map [string ]* B `json:"ptrmap"`
41
45
}
42
46
43
47
type D struct {
@@ -154,17 +158,28 @@ var _ = Describe("JSONPatch", func() {
154
158
})
155
159
})
156
160
Context ("CreateJsonPatch_map" , func () {
157
- It ("map" , func () {
161
+ It ("string map" , func () {
158
162
// add
159
- testPatch (C {Map : map [string ]string {"key1" : "value1" }}, C {})
163
+ testPatch (C {StrMap : map [string ]string {"key1" : "value1" }}, C {})
160
164
// remove
161
- testPatch (C {Map : map [string ]string {}}, C {Map : map [string ]string {"key1" : "value1" }})
165
+ testPatch (C {StrMap : map [string ]string {}}, C {StrMap : map [string ]string {"key1" : "value1" }})
162
166
// replace
163
- testPatch (C {Map : map [string ]string {"key1" : "value1" , "key2" : "value2" , "key3" : "value3" }}, C {Map : map [string ]string {}})
164
- testPatch (C {Map : map [string ]string {"key1" : "value1" , "key2" : "value2" , "key3" : "value3" }}, C {Map : map [string ]string {"key1" : "value1" }})
165
- testPatch (C {Map : map [string ]string {"key1" : "value1" }}, C {Map : map [string ]string {"key1" : "value2" }})
167
+ testPatch (C {StrMap : map [string ]string {"key1" : "value1" , "key2" : "value2" , "key3" : "value3" }}, C {StrMap : map [string ]string {}})
168
+ testPatch (C {StrMap : map [string ]string {"key1" : "value1" , "key2" : "value2" , "key3" : "value3" }}, C {StrMap : map [string ]string {"key1" : "value1" }})
169
+ testPatch (C {StrMap : map [string ]string {"key1" : "value1" }}, C {StrMap : map [string ]string {"key1" : "value2" }})
166
170
// no change
167
- testPatch (C {Map : map [string ]string {"key1" : "value1" , "key2" : "value2" }}, C {Map : map [string ]string {"key1" : "value1" , "key2" : "value2" }})
171
+ testPatch (C {StrMap : map [string ]string {"key1" : "value1" , "key2" : "value2" }}, C {StrMap : map [string ]string {"key1" : "value1" , "key2" : "value2" }})
172
+ })
173
+ It ("struct map" , func () {
174
+ // add
175
+ testPatch (C {StructMap : map [string ]B {"key1" : {Str : "value1" }}}, C {})
176
+ testPatch (C {StructMap : map [string ]B {"key1" : {Str : "value1" }, "key2" : {Str : "value2" }}}, C {StructMap : map [string ]B {"key1" : {Str : "value1" }}})
177
+ // remove
178
+ testPatch (C {StructMap : map [string ]B {"key1" : {Str : "value1" }}}, C {StructMap : map [string ]B {"key1" : {Str : "value1" }, "key2" : {Str : "value2" }}})
179
+ // replace
180
+ testPatch (C {StructMap : map [string ]B {"key1" : {Str : "value1" , Bool : true }}}, C {StructMap : map [string ]B {"key1" : {Str : "old" }}})
181
+ // no change
182
+ testPatch (C {StructMap : map [string ]B {"key1" : {Str : "value1" , Bool : true }, "key2" : {Str : "value2" }}}, C {StructMap : map [string ]B {"key1" : {Str : "value1" , Bool : true }, "key2" : {Str : "value2" }}})
168
183
})
169
184
})
170
185
Context ("CreateJsonPatch_slice" , func () {
@@ -234,14 +249,14 @@ var _ = Describe("JSONPatch", func() {
234
249
testPatchWithExpected (D {StructSliceWithKey : []C {{Str : "key1" }, {Str : "key3" }}}, D {StructSliceWithKey : []C {{Str : "key1" }, {Str : "key2" }, {Str : "key3" }, {Str : "key4" }}}, D {StructSliceWithKey : []C {{Str : "key1" }, {Str : "key3" }}}, jsonpatch .IgnoreSliceOrderWithPattern ([]jsonpatch.IgnorePattern {{"/structsWithKey" , "str" }}))
235
250
testPatchWithExpected (D {StructSliceWithKey : []C {{Str : "key3" }, {Str : "key2" }}}, D {StructSliceWithKey : []C {{Str : "key1" }, {Str : "key2" }, {Str : "key3" }, {Str : "key4" }}}, D {StructSliceWithKey : []C {{Str : "key2" }, {Str : "key3" }}}, jsonpatch .IgnoreSliceOrderWithPattern ([]jsonpatch.IgnorePattern {{"/structsWithKey" , "str" }}))
236
251
// replace
237
- testPatchWithExpected (D {StructSliceWithKey : []C {{Str : "key" , Map : map [string ]string {"key" : "value1" }}}}, D {StructSliceWithKey : []C {{Str : "key" , Map : map [string ]string {"key" : "value2" }}}}, D {StructSliceWithKey : []C {{Str : "key" , Map : map [string ]string {"key" : "value1" }}}}, jsonpatch .IgnoreSliceOrderWithPattern ([]jsonpatch.IgnorePattern {{"/structsWithKey" , "str" }}))
238
- testPatchWithExpected (D {StructSliceWithKey : []C {{Str : "key" , Map : map [string ]string {"key1" : "value" }}}}, D {StructSliceWithKey : []C {{Str : "key" , Map : map [string ]string {"key1" : "value" }}}}, D {StructSliceWithKey : []C {{Str : "key" , Map : map [string ]string {"key1" : "value" }}}}, jsonpatch .IgnoreSliceOrderWithPattern ([]jsonpatch.IgnorePattern {{"/structsWithKey" , "str" }}))
252
+ testPatchWithExpected (D {StructSliceWithKey : []C {{Str : "key" , StrMap : map [string ]string {"key" : "value1" }}}}, D {StructSliceWithKey : []C {{Str : "key" , StrMap : map [string ]string {"key" : "value2" }}}}, D {StructSliceWithKey : []C {{Str : "key" , StrMap : map [string ]string {"key" : "value1" }}}}, jsonpatch .IgnoreSliceOrderWithPattern ([]jsonpatch.IgnorePattern {{"/structsWithKey" , "str" }}))
253
+ testPatchWithExpected (D {StructSliceWithKey : []C {{Str : "key" , StrMap : map [string ]string {"key1" : "value" }}}}, D {StructSliceWithKey : []C {{Str : "key" , StrMap : map [string ]string {"key1" : "value" }}}}, D {StructSliceWithKey : []C {{Str : "key" , StrMap : map [string ]string {"key1" : "value" }}}}, jsonpatch .IgnoreSliceOrderWithPattern ([]jsonpatch.IgnorePattern {{"/structsWithKey" , "str" }}))
239
254
// mixed
240
255
testPatchWithExpected (D {StructSliceWithKey : []C {{Str : "key1" }, {Str : "new" }, {Str : "key3" }}}, D {StructSliceWithKey : []C {{Str : "key1" }, {Str : "key2" }, {Str : "key3" }, {Str : "key4" }}}, D {StructSliceWithKey : []C {{Str : "key1" }, {Str : "key3" }, {Str : "new" }}}, jsonpatch .IgnoreSliceOrderWithPattern ([]jsonpatch.IgnorePattern {{"/structsWithKey" , "str" }}))
241
256
testPatchWithExpected (D {StructSliceWithKey : []C {{Str : "key3" }, {Str : "key2" }, {Str : "new" }}}, D {StructSliceWithKey : []C {{Str : "key1" }, {Str : "key2" }, {Str : "key3" }, {Str : "key4" }}}, D {StructSliceWithKey : []C {{Str : "key2" }, {Str : "key3" }, {Str : "new" }}}, jsonpatch .IgnoreSliceOrderWithPattern ([]jsonpatch.IgnorePattern {{"/structsWithKey" , "str" }}))
242
257
// no change
243
- testPatchWithExpected (D {StructSliceWithKey : []C {{Str : "key3" }, {Str : "key2" , Map : map [string ]string {"key" : "value" }}}}, D {StructSliceWithKey : []C {{Str : "key2" , Map : map [string ]string {"key" : "value" }}, {Str : "key3" }}}, D {StructSliceWithKey : []C {{Str : "key2" , Map : map [string ]string {"key" : "value" }}, {Str : "key3" }}}, jsonpatch .IgnoreSliceOrderWithPattern ([]jsonpatch.IgnorePattern {{"/structsWithKey" , "str" }}))
244
- testPatchWithExpected (D {StructSliceWithKey : []C {{Str : "key2" , Map : map [string ]string {"key" : "value" }}, {Str : "key3" }}}, D {StructSliceWithKey : []C {{Str : "key2" , Map : map [string ]string {"key" : "value" }}, {Str : "key3" }}}, D {StructSliceWithKey : []C {{Str : "key2" , Map : map [string ]string {"key" : "value" }}, {Str : "key3" }}}, jsonpatch .IgnoreSliceOrderWithPattern ([]jsonpatch.IgnorePattern {{"/structsWithKey" , "str" }}))
258
+ testPatchWithExpected (D {StructSliceWithKey : []C {{Str : "key3" }, {Str : "key2" , StrMap : map [string ]string {"key" : "value" }}}}, D {StructSliceWithKey : []C {{Str : "key2" , StrMap : map [string ]string {"key" : "value" }}, {Str : "key3" }}}, D {StructSliceWithKey : []C {{Str : "key2" , StrMap : map [string ]string {"key" : "value" }}, {Str : "key3" }}}, jsonpatch .IgnoreSliceOrderWithPattern ([]jsonpatch.IgnorePattern {{"/structsWithKey" , "str" }}))
259
+ testPatchWithExpected (D {StructSliceWithKey : []C {{Str : "key2" , StrMap : map [string ]string {"key" : "value" }}, {Str : "key3" }}}, D {StructSliceWithKey : []C {{Str : "key2" , StrMap : map [string ]string {"key" : "value" }}, {Str : "key3" }}}, D {StructSliceWithKey : []C {{Str : "key2" , StrMap : map [string ]string {"key" : "value" }}, {Str : "key3" }}}, jsonpatch .IgnoreSliceOrderWithPattern ([]jsonpatch.IgnorePattern {{"/structsWithKey" , "str" }}))
245
260
})
246
261
})
247
262
Context ("CreateJsonPatch_escape_pointer" , func () {
@@ -302,7 +317,7 @@ var _ = Describe("JSONPatch", func() {
302
317
ReplaceFunc : func (path jsonpatch.JSONPointer , modified , current interface {}) bool {
303
318
if modifiedC , ok := modified .(C ); ok {
304
319
if currentC , ok := current .(C ); ok {
305
- return len (modifiedC .Map ) > len (currentC .Map )
320
+ return len (modifiedC .StrMap ) > len (currentC .StrMap )
306
321
}
307
322
}
308
323
@@ -322,14 +337,14 @@ var _ = Describe("JSONPatch", func() {
322
337
testPatchWithExpected (G {B : & B {Bool : true , Str : "str" }}, G {}, G {B : & B {Bool : true , Str : "str" }}, jsonpatch .WithPredicate (predicate ))
323
338
testPatchWithExpected (G {B : & B {Int : 7 , Str : "str" }}, G {}, G {B : & B {Int : 7 , Str : "str" }}, jsonpatch .WithPredicate (predicate ))
324
339
// don't add
325
- testPatchWithExpected (G {B : & B {Bool : false , Str : "str" }, C : C {Map : map [string ]string {"key" : "value" }}}, G {}, G {C : C {Map : map [string ]string {"key" : "value" }}}, jsonpatch .WithPredicate (predicate ))
326
- testPatchWithExpected (G {B : & B {Int : 0 , Str : "str" }, C : C {Map : map [string ]string {"key" : "value" }}}, G {}, G {C : C {Map : map [string ]string {"key" : "value" }}}, jsonpatch .WithPredicate (predicate ))
340
+ testPatchWithExpected (G {B : & B {Bool : false , Str : "str" }, C : C {StrMap : map [string ]string {"key" : "value" }}}, G {}, G {C : C {StrMap : map [string ]string {"key" : "value" }}}, jsonpatch .WithPredicate (predicate ))
341
+ testPatchWithExpected (G {B : & B {Int : 0 , Str : "str" }, C : C {StrMap : map [string ]string {"key" : "value" }}}, G {}, G {C : C {StrMap : map [string ]string {"key" : "value" }}}, jsonpatch .WithPredicate (predicate ))
327
342
})
328
343
It ("predicate_replace" , func () {
329
344
// replace
330
- testPatchWithExpected (G {C : C {Str : "new" , Map : map [string ]string {"key" : "value" }}}, G {C : C {Str : "old" }}, G {C : C {Str : "new" , Map : map [string ]string {"key" : "value" }}}, jsonpatch .WithPredicate (predicate ))
345
+ testPatchWithExpected (G {C : C {Str : "new" , StrMap : map [string ]string {"key" : "value" }}}, G {C : C {Str : "old" }}, G {C : C {Str : "new" , StrMap : map [string ]string {"key" : "value" }}}, jsonpatch .WithPredicate (predicate ))
331
346
// don't replace
332
- testPatchWithExpected (G {C : C {Str : "new" }}, G {C : C {Str : "old" , Map : map [string ]string {"key" : "value" }}}, G {C : C {Str : "old" , Map : map [string ]string {"key" : "value" }}}, jsonpatch .WithPredicate (predicate ))
347
+ testPatchWithExpected (G {C : C {Str : "new" }}, G {C : C {Str : "old" , StrMap : map [string ]string {"key" : "value" }}}, G {C : C {Str : "old" , StrMap : map [string ]string {"key" : "value" }}}, jsonpatch .WithPredicate (predicate ))
333
348
})
334
349
It ("predicate_remove" , func () {
335
350
// remove
@@ -398,10 +413,6 @@ var _ = Describe("JSONPatch", func() {
398
413
_ , err := jsonpatch .CreateJSONPatch (I {1 }, I {"str" })
399
414
Ω (err ).Should (HaveOccurred ())
400
415
})
401
- It ("invalid map (map[string]int)" , func () {
402
- _ , err := jsonpatch .CreateJSONPatch (I {map [string ]int {"key" : 2 }}, I {map [string ]int {"key" : 3 }})
403
- Ω (err ).Should (HaveOccurred ())
404
- })
405
416
It ("invalid map (map[int]string)" , func () {
406
417
_ , err := jsonpatch .CreateJSONPatch (I {map [int ]string {1 : "value" }}, I {map [int ]string {2 : "value" }})
407
418
Ω (err ).Should (HaveOccurred ())
@@ -428,7 +439,7 @@ var _ = Describe("JSONPatch", func() {
428
439
Ω (err ).ShouldNot (HaveOccurred ())
429
440
})
430
441
431
- for i := 0 ; i < 100 ; i ++ {
442
+ for i := 0 ; i < 1000 ; i ++ {
432
443
It ("fuzzy " + strconv .Itoa (i ), func () {
433
444
testPatch (modified , current )
434
445
})
0 commit comments