Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions pkg/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ func DiffStructureds(x, y any, opts ...Option) (*Result, error) {
}

func (d *differ) diff(path []PathStep, vx, vy reflect.Value) error {
if !vx.IsValid() && !vy.IsValid() {
return nil
}

if !vx.IsValid() {
if d.equateEmpty && isEmptyInterface(vy) {
return nil
Expand Down Expand Up @@ -403,14 +407,18 @@ func floatNumber(v reflect.Value) float64 {
switch v.Kind() {
case reflect.Float32, reflect.Float64:
return v.Float()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return float64(v.Uint())
Comment on lines +410 to +411
default:
return float64(v.Int())
}
}

func isNumberValue(v reflect.Value) bool {
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Float32, reflect.Float64:
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
return true
default:
return false
Expand All @@ -419,7 +427,9 @@ func isNumberValue(v reflect.Value) bool {

func convertToNumber(v reflect.Value) (reflect.Value, bool) {
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Float32, reflect.Float64:
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
return v, true
case reflect.String:
if n, err := strconv.ParseFloat(v.String(), 64); err == nil {
Expand Down
81 changes: 81 additions & 0 deletions pkg/diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,87 @@ func TestDiff_Array(t *testing.T) {
}
}

func TestDiff_Uint(t *testing.T) {
testcases := []struct {
name string
old unstructured.Unstructured
new unstructured.Unstructured
options []Option
diffNum int
}{
{
name: "identical uint values have no diff",
old: unstructured.Unstructured{
Object: map[string]any{
"spec": map[string]any{
"port": uint(8080),
},
},
},
new: unstructured.Unstructured{
Object: map[string]any{
"spec": map[string]any{
"port": uint(8080),
},
},
},
diffNum: 0,
},
{
name: "differing uint values report diff",
old: unstructured.Unstructured{
Object: map[string]any{
"spec": map[string]any{
"port": uint(8080),
},
},
},
new: unstructured.Unstructured{
Object: map[string]any{
"spec": map[string]any{
"port": uint(9090),
},
},
},
diffNum: 1,
},
{
name: "uint and numeric string equated with WithCompareNumberAndNumericString",
old: unstructured.Unstructured{
Object: map[string]any{
"spec": map[string]any{
"port": uint64(8080),
},
},
},
new: unstructured.Unstructured{
Object: map[string]any{
"spec": map[string]any{
"port": "8080",
},
},
},
options: []Option{WithCompareNumberAndNumericString()},
diffNum: 0,
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
result, err := DiffUnstructureds(tc.old, tc.new, "", tc.options...)
require.NoError(t, err)
assert.Equal(t, tc.diffNum, result.NumNodes())
})
}
}

func TestDiff_BothInvalid(t *testing.T) {
d := &differ{result: &Result{}}
err := d.diff(nil, reflect.Value{}, reflect.Value{})
require.NoError(t, err)
assert.Equal(t, 0, d.result.NumNodes())
}

func TestIsEmptyInterface(t *testing.T) {
testcases := []struct {
name string
Expand Down
12 changes: 8 additions & 4 deletions pkg/diff/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,12 @@ func renderNodeValue(v reflect.Value, prefix string) (string, bool) {
case reflect.String:
return v.String(), false

case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(v.Int(), 10), false

case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.FormatUint(v.Uint(), 10), false

case reflect.Float32, reflect.Float64:
return strconv.FormatFloat(v.Float(), 'f', -1, 64), false

Expand All @@ -216,10 +218,12 @@ func RenderPrimitiveValue(v reflect.Value) string {
case reflect.String:
return v.String()

case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(v.Int(), 10)

case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.FormatUint(v.Uint(), 10)

case reflect.Float32, reflect.Float64:
return strconv.FormatFloat(v.Float(), 'f', -1, 64)

Expand Down
10 changes: 10 additions & 0 deletions pkg/diff/renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,16 @@ func TestRenderPrimitiveValue(t *testing.T) {
value: 1,
expected: "1",
},
{
name: "uint",
value: uint(42),
expected: "42",
},
{
name: "uint64",
value: uint64(100),
expected: "100",
},
{
name: "float",
value: 1.25,
Expand Down
Loading