Skip to content

Commit

Permalink
Better handle numnbers that differ in type. (#651)
Browse files Browse the repository at this point in the history
When checking for equal number values, normalize values that differ in
type to `float64` before performing the comparison.
  • Loading branch information
pgavlin authored Jul 24, 2019
1 parent 7e3b99e commit c705fd3
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,20 @@ func makePatchSlice(path []interface{}, v interface{}) interface{} {
}
}

// equalNumbers returns true if both a and b are number values (int64 or float64). Note that if a this will fail if
// either value is not representable as a float64.
func equalNumbers(a, b interface{}) bool {
aKind, bKind := reflect.TypeOf(a).Kind(), reflect.TypeOf(b).Kind()
if aKind == bKind {
return reflect.DeepEqual(a, b)
}

if aKind == reflect.Float64 {
return a.(float64) == float64(b.(int64))
}
return float64(a.(int64)) == b.(float64)
}

// patchConverter carries context for convertPatchToDiff.
type patchConverter struct {
forceNew []string
Expand Down Expand Up @@ -1478,7 +1492,7 @@ func (pc *patchConverter) addPatchValueToDiff(
}
diffKind = pulumirpc.PropertyDiff_UPDATE
default:
if reflect.DeepEqual(v, old) {
if reflect.DeepEqual(v, old) || equalNumbers(v, old) {
// From RFC 7386 (the JSON Merge Patch spec):
//
// If the patch is anything other than an object, the result will always be to replace the entire
Expand Down

0 comments on commit c705fd3

Please sign in to comment.