From 64f5298ea2d5c31f4486f1ef628a0849afb2577d Mon Sep 17 00:00:00 2001 From: Norwin Schnyder Date: Tue, 16 Apr 2024 14:56:19 +0000 Subject: [PATCH] add support for float32 and float64 --- go.mod | 2 +- go.sum | 4 ++-- patch_test.go | 51 ++++++++++++++++++++++++++++++++++++++------------- walker.go | 12 ++++++++++++ 4 files changed, 53 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index 207feb4..c0406b4 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/snorwin/jsonpatch go 1.22 require ( - github.com/evanphx/json-patch v5.9.0+incompatible + github.com/evanphx/json-patch/v5 v5.9.0 github.com/go-faker/faker/v4 v4.4.1 github.com/onsi/ginkgo/v2 v2.17.1 github.com/onsi/gomega v1.32.0 diff --git a/go.sum b/go.sum index b2242b5..0ab6210 100644 --- a/go.sum +++ b/go.sum @@ -4,8 +4,8 @@ github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMn github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/evanphx/json-patch v5.9.0+incompatible h1:fBXyNpNMuTTDdquAq/uisOr2lShz4oaXpDTX2bLe7ls= -github.com/evanphx/json-patch v5.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= +github.com/evanphx/json-patch/v5 v5.9.0 h1:kcBlZQbplgElYIlo/n1hJbls2z/1awpXxpRi0/FOJfg= +github.com/evanphx/json-patch/v5 v5.9.0/go.mod h1:VNkHZ/282BpEyt/tObQO8s5CMPmYYq14uClGH4abBuQ= github.com/go-faker/faker/v4 v4.4.1 h1:LY1jDgjVkBZWIhATCt+gkl0x9i/7wC61gZx73GTFb+Q= github.com/go-faker/faker/v4 v4.4.1/go.mod h1:HRLrjis+tYsbFtIHufEPTAIzcZiRu0rS9EYl2Ccwme4= github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= diff --git a/patch_test.go b/patch_test.go index 8e4bc75..8dd7b3e 100644 --- a/patch_test.go +++ b/patch_test.go @@ -9,8 +9,8 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + jsonpatch2 "github.com/evanphx/json-patch/v5" "github.com/go-faker/faker/v4" - jsonpatch2 "github.com/evanphx/json-patch" "github.com/snorwin/jsonpatch" ) @@ -34,6 +34,8 @@ type B struct { Uint32 uint32 `json:"uint32"` Uint64 uint64 `json:"uint64"` UintPtr uintptr `json:"ptr" faker:"-"` + Float32 float32 `json:"float32"` + Float64 float64 `json:"float64"` Time time.Time `json:"time"` } @@ -47,12 +49,13 @@ type C struct { } type D struct { - PtrSlice []*B `json:"ptr"` - StructSlice []C `json:"structs"` - StringSlice []string `json:"strs"` - IntSlice []int `json:"ints"` - StructSliceWithKey []C `json:"structsWithKey"` - PtrSliceWithKey []*B `json:"ptrWithKey"` + PtrSlice []*B `json:"ptr"` + StructSlice []C `json:"structs"` + StringSlice []string `json:"strs"` + IntSlice []int `json:"ints"` + FloatSlice []float64 `json:"floats"` + StructSliceWithKey []C `json:"structsWithKey"` + PtrSliceWithKey []*B `json:"ptrWithKey"` } type E struct { @@ -158,6 +161,19 @@ var _ = Describe("JSONPatch", func() { // no change testPatch(B{Uint: 1, Uint8: 1, Uint16: 1, Uint32: 1, Uint64: 1}, B{Uint: 1, Uint8: 1, Uint16: 1, Uint32: 1, Uint64: 1}) }) + It("float", func() { + // add + testPatch(B{Float32: 1.1, Float64: 2.2}, B{}) + // remove + testPatch(B{}, B{Float32: 1.1, Float64: 2.2}) + // replace + testPatch(B{Float32: 1.1, Float64: 2.2}, B{Float32: 1.12, Float64: 2.22}) + // mixed + testPatch(B{Float32: 1.1}, B{Float64: 2.2}) + testPatch(B{Float32: 1.0, Float64: 2.0}, B{Float64: 2.2}) + // no change + testPatch(B{Float32: 1.1, Float64: 2.2}, B{Float32: 1.1, Float64: 2.2}) + }) It("time", func() { now := time.Now() // add @@ -227,6 +243,17 @@ var _ = Describe("JSONPatch", func() { testPatchWithExpected([]int{3, 1}, []int{1, 2, 3}, []int{1, 3}, jsonpatch.IgnoreSliceOrder()) testPatchWithExpected([]int{3, 2}, []int{1, 2, 3}, []int{2, 3}, jsonpatch.IgnoreSliceOrder()) }) + It("float slice ignore order", func() { + // add + testPatchWithExpected([]float32{1.1, 2.1, 3.1}, []float32{1.1, 3.1}, []float32{1.1, 3.1, 2.1}, jsonpatch.IgnoreSliceOrder()) + testPatchWithExpected([]float64{1.1, 2.1, 3.1}, []float64{1.1, 2.1}, []float64{1.1, 2.1, 3.1}, jsonpatch.IgnoreSliceOrder()) + // no change + testPatchWithExpected([]float32{3.1, 2.1, 1.1}, []float32{1.1, 2.1, 3.1}, []float32{1.1, 2.1, 3.1}, jsonpatch.IgnoreSliceOrder()) + testPatchWithExpected([]float64{1.1, 2.1, 3.1}, []float64{3.1, 2.1, 1.1}, []float64{3.1, 2.1, 1.1}, jsonpatch.IgnoreSliceOrder()) + // remove + testPatchWithExpected([]float32{3.1, 1.1}, []float32{1.1, 2.1, 3.1}, []float32{1.1, 3.1}, jsonpatch.IgnoreSliceOrder()) + testPatchWithExpected([]float64{3.1, 2.1}, []float64{1.1, 2.1, 3.1}, []float64{2.1, 3.1}, jsonpatch.IgnoreSliceOrder()) + }) It("uint slice ignore order", func() { // add testPatchWithExpected([]uint{1, 2, 3}, []uint{1, 3}, []uint{1, 3, 2}, jsonpatch.IgnoreSliceOrder()) @@ -315,19 +342,17 @@ var _ = Describe("JSONPatch", func() { }) }) Context("CreateJsonPatch_with_predicates", func() { - var ( - predicate jsonpatch.Predicate - ) + var predicate jsonpatch.Predicate BeforeEach(func() { predicate = jsonpatch.Funcs{ - AddFunc: func(path jsonpatch.JSONPointer, modified interface{}) bool { + AddFunc: func(_ jsonpatch.JSONPointer, modified interface{}) bool { if b, ok := modified.(B); ok { return b.Bool || b.Int > 2 } return true }, - ReplaceFunc: func(path jsonpatch.JSONPointer, modified, current interface{}) bool { + ReplaceFunc: func(_ jsonpatch.JSONPointer, modified, current interface{}) bool { if modifiedC, ok := modified.(C); ok { if currentC, ok := current.(C); ok { return len(modifiedC.StrMap) > len(currentC.StrMap) @@ -336,7 +361,7 @@ var _ = Describe("JSONPatch", func() { return true }, - RemoveFunc: func(path jsonpatch.JSONPointer, current interface{}) bool { + RemoveFunc: func(_ jsonpatch.JSONPointer, current interface{}) bool { if b, ok := current.(B); ok { return b.Str != "don't remove me" } diff --git a/walker.go b/walker.go index 68ac57e..6da556a 100644 --- a/walker.go +++ b/walker.go @@ -49,6 +49,14 @@ func (w *walker) walk(modified, current reflect.Value, pointer JSONPointer) erro if modified.Uint() != current.Uint() { w.replace(pointer, modified.Uint(), current.Uint()) } + case reflect.Float32: + if modified.Float() != current.Float() { + w.replace(pointer, float32(modified.Float()), float32(current.Float())) + } + case reflect.Float64: + if modified.Float() != current.Float() { + w.replace(pointer, modified.Float(), current.Float()) + } case reflect.Bool: if modified.Bool() != current.Bool() { w.replace(pointer, modified.Bool(), current.Bool()) @@ -309,6 +317,10 @@ func extractIgnoreSliceOrderMatchValue(value reflect.Value, fieldName string) st return strconv.FormatInt(value.Int(), 10) case reflect.Uint, reflect.Uintptr, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: return strconv.FormatUint(value.Uint(), 10) + case reflect.Float32: + return strconv.FormatFloat(value.Float(), 'f', -1, 32) + case reflect.Float64: + return strconv.FormatFloat(value.Float(), 'f', -1, 64) case reflect.Bool: return strconv.FormatBool(value.Bool()) }