Skip to content

Commit 341d33e

Browse files
authored
Merge pull request #48 from snorwin/float-support
Add support for float32 and float64
2 parents 42d6241 + 64f5298 commit 341d33e

File tree

4 files changed

+53
-16
lines changed

4 files changed

+53
-16
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/snorwin/jsonpatch
33
go 1.22
44

55
require (
6-
github.com/evanphx/json-patch v5.9.0+incompatible
6+
github.com/evanphx/json-patch/v5 v5.9.0
77
github.com/go-faker/faker/v4 v4.4.1
88
github.com/onsi/ginkgo/v2 v2.17.1
99
github.com/onsi/gomega v1.32.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMn
44
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
55
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
66
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7-
github.com/evanphx/json-patch v5.9.0+incompatible h1:fBXyNpNMuTTDdquAq/uisOr2lShz4oaXpDTX2bLe7ls=
8-
github.com/evanphx/json-patch v5.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
7+
github.com/evanphx/json-patch/v5 v5.9.0 h1:kcBlZQbplgElYIlo/n1hJbls2z/1awpXxpRi0/FOJfg=
8+
github.com/evanphx/json-patch/v5 v5.9.0/go.mod h1:VNkHZ/282BpEyt/tObQO8s5CMPmYYq14uClGH4abBuQ=
99
github.com/go-faker/faker/v4 v4.4.1 h1:LY1jDgjVkBZWIhATCt+gkl0x9i/7wC61gZx73GTFb+Q=
1010
github.com/go-faker/faker/v4 v4.4.1/go.mod h1:HRLrjis+tYsbFtIHufEPTAIzcZiRu0rS9EYl2Ccwme4=
1111
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=

patch_test.go

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
. "github.com/onsi/ginkgo/v2"
1010
. "github.com/onsi/gomega"
1111

12+
jsonpatch2 "github.com/evanphx/json-patch/v5"
1213
"github.com/go-faker/faker/v4"
13-
jsonpatch2 "github.com/evanphx/json-patch"
1414

1515
"github.com/snorwin/jsonpatch"
1616
)
@@ -34,6 +34,8 @@ type B struct {
3434
Uint32 uint32 `json:"uint32"`
3535
Uint64 uint64 `json:"uint64"`
3636
UintPtr uintptr `json:"ptr" faker:"-"`
37+
Float32 float32 `json:"float32"`
38+
Float64 float64 `json:"float64"`
3739
Time time.Time `json:"time"`
3840
}
3941

@@ -47,12 +49,13 @@ type C struct {
4749
}
4850

4951
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"`
5659
}
5760

5861
type E struct {
@@ -158,6 +161,19 @@ var _ = Describe("JSONPatch", func() {
158161
// no change
159162
testPatch(B{Uint: 1, Uint8: 1, Uint16: 1, Uint32: 1, Uint64: 1}, B{Uint: 1, Uint8: 1, Uint16: 1, Uint32: 1, Uint64: 1})
160163
})
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+
})
161177
It("time", func() {
162178
now := time.Now()
163179
// add
@@ -227,6 +243,17 @@ var _ = Describe("JSONPatch", func() {
227243
testPatchWithExpected([]int{3, 1}, []int{1, 2, 3}, []int{1, 3}, jsonpatch.IgnoreSliceOrder())
228244
testPatchWithExpected([]int{3, 2}, []int{1, 2, 3}, []int{2, 3}, jsonpatch.IgnoreSliceOrder())
229245
})
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+
})
230257
It("uint slice ignore order", func() {
231258
// add
232259
testPatchWithExpected([]uint{1, 2, 3}, []uint{1, 3}, []uint{1, 3, 2}, jsonpatch.IgnoreSliceOrder())
@@ -315,19 +342,17 @@ var _ = Describe("JSONPatch", func() {
315342
})
316343
})
317344
Context("CreateJsonPatch_with_predicates", func() {
318-
var (
319-
predicate jsonpatch.Predicate
320-
)
345+
var predicate jsonpatch.Predicate
321346
BeforeEach(func() {
322347
predicate = jsonpatch.Funcs{
323-
AddFunc: func(path jsonpatch.JSONPointer, modified interface{}) bool {
348+
AddFunc: func(_ jsonpatch.JSONPointer, modified interface{}) bool {
324349
if b, ok := modified.(B); ok {
325350
return b.Bool || b.Int > 2
326351
}
327352

328353
return true
329354
},
330-
ReplaceFunc: func(path jsonpatch.JSONPointer, modified, current interface{}) bool {
355+
ReplaceFunc: func(_ jsonpatch.JSONPointer, modified, current interface{}) bool {
331356
if modifiedC, ok := modified.(C); ok {
332357
if currentC, ok := current.(C); ok {
333358
return len(modifiedC.StrMap) > len(currentC.StrMap)
@@ -336,7 +361,7 @@ var _ = Describe("JSONPatch", func() {
336361

337362
return true
338363
},
339-
RemoveFunc: func(path jsonpatch.JSONPointer, current interface{}) bool {
364+
RemoveFunc: func(_ jsonpatch.JSONPointer, current interface{}) bool {
340365
if b, ok := current.(B); ok {
341366
return b.Str != "don't remove me"
342367
}

walker.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ func (w *walker) walk(modified, current reflect.Value, pointer JSONPointer) erro
4949
if modified.Uint() != current.Uint() {
5050
w.replace(pointer, modified.Uint(), current.Uint())
5151
}
52+
case reflect.Float32:
53+
if modified.Float() != current.Float() {
54+
w.replace(pointer, float32(modified.Float()), float32(current.Float()))
55+
}
56+
case reflect.Float64:
57+
if modified.Float() != current.Float() {
58+
w.replace(pointer, modified.Float(), current.Float())
59+
}
5260
case reflect.Bool:
5361
if modified.Bool() != current.Bool() {
5462
w.replace(pointer, modified.Bool(), current.Bool())
@@ -309,6 +317,10 @@ func extractIgnoreSliceOrderMatchValue(value reflect.Value, fieldName string) st
309317
return strconv.FormatInt(value.Int(), 10)
310318
case reflect.Uint, reflect.Uintptr, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
311319
return strconv.FormatUint(value.Uint(), 10)
320+
case reflect.Float32:
321+
return strconv.FormatFloat(value.Float(), 'f', -1, 32)
322+
case reflect.Float64:
323+
return strconv.FormatFloat(value.Float(), 'f', -1, 64)
312324
case reflect.Bool:
313325
return strconv.FormatBool(value.Bool())
314326
}

0 commit comments

Comments
 (0)