Skip to content

Commit

Permalink
Merge pull request #48 from snorwin/float-support
Browse files Browse the repository at this point in the history
Add support for float32 and float64
  • Loading branch information
snorwin authored Apr 16, 2024
2 parents 42d6241 + 64f5298 commit 341d33e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
51 changes: 38 additions & 13 deletions patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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"`
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand All @@ -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"
}
Expand Down
12 changes: 12 additions & 0 deletions walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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())
}
Expand Down

0 comments on commit 341d33e

Please sign in to comment.