Skip to content

Commit a6af851

Browse files
committed
improved nil check
1 parent deac9ea commit a6af851

File tree

2 files changed

+104
-6
lines changed

2 files changed

+104
-6
lines changed

diff/diff.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func diff(c config, lhs, rhs interface{}, visited *visited) (Differ, error) {
4646
lhsVal, lhs := indirectValueOf(lhs)
4747
rhsVal, rhs := indirectValueOf(rhs)
4848

49-
if d, ok := nilCheck(lhs, rhs); ok {
49+
if d, ok := nilCheck(lhsVal, rhsVal, lhs, rhs); ok {
5050
return d, nil
5151
}
5252

@@ -115,12 +115,15 @@ func valueIsScalar(v reflect.Value) bool {
115115
}
116116
}
117117

118-
func nilCheck(lhs, rhs interface{}) (Differ, bool) {
119-
if lhs == nil && rhs == nil {
120-
return scalar{lhs, rhs}, true
118+
func nilCheck(lhsVal, rhsVal reflect.Value, lhs, rhs interface{}) (Differ, bool) {
119+
lhsNil := lhs == nil || (lhsVal.Kind() == reflect.Ptr && lhsVal.IsNil())
120+
rhsNil := rhs == nil || (rhsVal.Kind() == reflect.Ptr && rhsVal.IsNil())
121+
122+
if lhsNil && rhsNil {
123+
return scalar{lhsVal, rhsVal}, true
121124
}
122-
if lhs == nil || rhs == nil {
123-
return types{lhs, rhs}, true
125+
if lhsNil || rhsNil {
126+
return types{lhsVal, rhsVal}, true
124127
}
125128

126129
return nil, false

diff/diff_test.go

+95
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"reflect"
88
"strings"
99
"testing"
10+
"time"
1011
)
1112

1213
type mockedStream struct {
@@ -36,6 +37,8 @@ func (s erroringMockedStream) NextValue() (interface{}, error) {
3637
}
3738

3839
func TestDiff(t *testing.T) {
40+
type CustomType int
41+
3942
for _, test := range []struct {
4043
LHS interface{}
4144
RHS interface{}
@@ -111,6 +114,98 @@ func TestDiff(t *testing.T) {
111114
RHS: mockStream(4, 5, 6),
112115
Want: ContentDiffer,
113116
},
117+
{
118+
LHS: CustomType(1),
119+
RHS: CustomType(1),
120+
Want: Identical,
121+
},
122+
{
123+
LHS: CustomType(1),
124+
RHS: CustomType(2),
125+
Want: ContentDiffer,
126+
},
127+
{
128+
LHS: CustomType(1),
129+
RHS: 2,
130+
Want: ContentDiffer,
131+
},
132+
{
133+
LHS: new(CustomType),
134+
RHS: new(CustomType),
135+
Want: Identical,
136+
},
137+
{
138+
LHS: time.Time{},
139+
RHS: time.Time{},
140+
Want: Identical,
141+
},
142+
{
143+
LHS: new(time.Time),
144+
RHS: new(time.Time),
145+
Want: Identical,
146+
},
147+
{
148+
LHS: struct {
149+
Foo *int
150+
}{},
151+
RHS: struct {
152+
Foo *int
153+
}{},
154+
Want: Identical,
155+
},
156+
{
157+
LHS: struct {
158+
Foo *int
159+
}{
160+
Foo: new(int),
161+
},
162+
RHS: struct {
163+
Foo *int
164+
}{},
165+
Want: ContentDiffer,
166+
},
167+
{
168+
LHS: struct {
169+
Foo *int
170+
}{
171+
Foo: func() *int {
172+
i := 42
173+
174+
return &i
175+
}(),
176+
},
177+
RHS: struct {
178+
Foo *int
179+
}{
180+
Foo: func() *int {
181+
i := 42
182+
183+
return &i
184+
}(),
185+
},
186+
Want: Identical,
187+
},
188+
{
189+
LHS: struct {
190+
Foo *int
191+
}{
192+
Foo: func() *int {
193+
i := 42
194+
195+
return &i
196+
}(),
197+
},
198+
RHS: struct {
199+
Foo *int
200+
}{
201+
Foo: func() *int {
202+
i := 84
203+
204+
return &i
205+
}(),
206+
},
207+
Want: ContentDiffer,
208+
},
114209
} {
115210
diff, err := Diff(test.LHS, test.RHS)
116211

0 commit comments

Comments
 (0)