9
9
. "github.com/onsi/ginkgo/v2"
10
10
. "github.com/onsi/gomega"
11
11
12
+ jsonpatch2 "github.com/evanphx/json-patch/v5"
12
13
"github.com/go-faker/faker/v4"
13
- jsonpatch2 "github.com/evanphx/json-patch"
14
14
15
15
"github.com/snorwin/jsonpatch"
16
16
)
@@ -34,6 +34,8 @@ type B struct {
34
34
Uint32 uint32 `json:"uint32"`
35
35
Uint64 uint64 `json:"uint64"`
36
36
UintPtr uintptr `json:"ptr" faker:"-"`
37
+ Float32 float32 `json:"float32"`
38
+ Float64 float64 `json:"float64"`
37
39
Time time.Time `json:"time"`
38
40
}
39
41
@@ -47,12 +49,13 @@ type C struct {
47
49
}
48
50
49
51
type D struct {
50
- PtrSlice []* B `json:"ptr"`
51
- StructSlice []C `json:"structs"`
52
- StringSlice []string `json:"strs"`
53
- IntSlice []int `json:"ints"`
54
- StructSliceWithKey []C `json:"structsWithKey"`
55
- PtrSliceWithKey []* B `json:"ptrWithKey"`
52
+ PtrSlice []* B `json:"ptr"`
53
+ StructSlice []C `json:"structs"`
54
+ StringSlice []string `json:"strs"`
55
+ IntSlice []int `json:"ints"`
56
+ FloatSlice []float64 `json:"floats"`
57
+ StructSliceWithKey []C `json:"structsWithKey"`
58
+ PtrSliceWithKey []* B `json:"ptrWithKey"`
56
59
}
57
60
58
61
type E struct {
@@ -158,6 +161,19 @@ var _ = Describe("JSONPatch", func() {
158
161
// no change
159
162
testPatch (B {Uint : 1 , Uint8 : 1 , Uint16 : 1 , Uint32 : 1 , Uint64 : 1 }, B {Uint : 1 , Uint8 : 1 , Uint16 : 1 , Uint32 : 1 , Uint64 : 1 })
160
163
})
164
+ It ("float" , func () {
165
+ // add
166
+ testPatch (B {Float32 : 1.1 , Float64 : 2.2 }, B {})
167
+ // remove
168
+ testPatch (B {}, B {Float32 : 1.1 , Float64 : 2.2 })
169
+ // replace
170
+ testPatch (B {Float32 : 1.1 , Float64 : 2.2 }, B {Float32 : 1.12 , Float64 : 2.22 })
171
+ // mixed
172
+ testPatch (B {Float32 : 1.1 }, B {Float64 : 2.2 })
173
+ testPatch (B {Float32 : 1.0 , Float64 : 2.0 }, B {Float64 : 2.2 })
174
+ // no change
175
+ testPatch (B {Float32 : 1.1 , Float64 : 2.2 }, B {Float32 : 1.1 , Float64 : 2.2 })
176
+ })
161
177
It ("time" , func () {
162
178
now := time .Now ()
163
179
// add
@@ -227,6 +243,17 @@ var _ = Describe("JSONPatch", func() {
227
243
testPatchWithExpected ([]int {3 , 1 }, []int {1 , 2 , 3 }, []int {1 , 3 }, jsonpatch .IgnoreSliceOrder ())
228
244
testPatchWithExpected ([]int {3 , 2 }, []int {1 , 2 , 3 }, []int {2 , 3 }, jsonpatch .IgnoreSliceOrder ())
229
245
})
246
+ It ("float slice ignore order" , func () {
247
+ // add
248
+ testPatchWithExpected ([]float32 {1.1 , 2.1 , 3.1 }, []float32 {1.1 , 3.1 }, []float32 {1.1 , 3.1 , 2.1 }, jsonpatch .IgnoreSliceOrder ())
249
+ testPatchWithExpected ([]float64 {1.1 , 2.1 , 3.1 }, []float64 {1.1 , 2.1 }, []float64 {1.1 , 2.1 , 3.1 }, jsonpatch .IgnoreSliceOrder ())
250
+ // no change
251
+ testPatchWithExpected ([]float32 {3.1 , 2.1 , 1.1 }, []float32 {1.1 , 2.1 , 3.1 }, []float32 {1.1 , 2.1 , 3.1 }, jsonpatch .IgnoreSliceOrder ())
252
+ testPatchWithExpected ([]float64 {1.1 , 2.1 , 3.1 }, []float64 {3.1 , 2.1 , 1.1 }, []float64 {3.1 , 2.1 , 1.1 }, jsonpatch .IgnoreSliceOrder ())
253
+ // remove
254
+ testPatchWithExpected ([]float32 {3.1 , 1.1 }, []float32 {1.1 , 2.1 , 3.1 }, []float32 {1.1 , 3.1 }, jsonpatch .IgnoreSliceOrder ())
255
+ testPatchWithExpected ([]float64 {3.1 , 2.1 }, []float64 {1.1 , 2.1 , 3.1 }, []float64 {2.1 , 3.1 }, jsonpatch .IgnoreSliceOrder ())
256
+ })
230
257
It ("uint slice ignore order" , func () {
231
258
// add
232
259
testPatchWithExpected ([]uint {1 , 2 , 3 }, []uint {1 , 3 }, []uint {1 , 3 , 2 }, jsonpatch .IgnoreSliceOrder ())
@@ -315,19 +342,17 @@ var _ = Describe("JSONPatch", func() {
315
342
})
316
343
})
317
344
Context ("CreateJsonPatch_with_predicates" , func () {
318
- var (
319
- predicate jsonpatch.Predicate
320
- )
345
+ var predicate jsonpatch.Predicate
321
346
BeforeEach (func () {
322
347
predicate = jsonpatch.Funcs {
323
- AddFunc : func (path jsonpatch.JSONPointer , modified interface {}) bool {
348
+ AddFunc : func (_ jsonpatch.JSONPointer , modified interface {}) bool {
324
349
if b , ok := modified .(B ); ok {
325
350
return b .Bool || b .Int > 2
326
351
}
327
352
328
353
return true
329
354
},
330
- ReplaceFunc : func (path jsonpatch.JSONPointer , modified , current interface {}) bool {
355
+ ReplaceFunc : func (_ jsonpatch.JSONPointer , modified , current interface {}) bool {
331
356
if modifiedC , ok := modified .(C ); ok {
332
357
if currentC , ok := current .(C ); ok {
333
358
return len (modifiedC .StrMap ) > len (currentC .StrMap )
@@ -336,7 +361,7 @@ var _ = Describe("JSONPatch", func() {
336
361
337
362
return true
338
363
},
339
- RemoveFunc : func (path jsonpatch.JSONPointer , current interface {}) bool {
364
+ RemoveFunc : func (_ jsonpatch.JSONPointer , current interface {}) bool {
340
365
if b , ok := current .(B ); ok {
341
366
return b .Str != "don't remove me"
342
367
}
0 commit comments