New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
check for nil pointers before calling IsZero. #67
Conversation
This avoids panics when a nil pointer to a type that implements zeroable interface is passed to IsZero Signed-off-by: Michał Szczygieł <1153719+mszczygiel@users.noreply.github.com>
util.go
Outdated
v := reflect.ValueOf(data) | ||
// check for nil data | ||
switch v.Kind() { | ||
case reflect.Interface, reflect.Ptr: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about also checking for reflect.Slice
and removing the (now duplicated) check below then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed, thanks!
util_test.go
Outdated
type dummyZeroable struct { | ||
zero bool | ||
} | ||
|
||
func (d dummyZeroable) IsZero() bool { | ||
return d.zero | ||
} | ||
func TestZeroableNilIsZero(t *testing.T) { | ||
var d *dummyZeroable | ||
|
||
assert.True(t, IsZero(d)) | ||
} | ||
|
||
func TestZeroableInterfaceIsRespected(t *testing.T) { | ||
assert.False(t, IsZero(dummyZeroable{false})) | ||
assert.True(t, IsZero(dummyZeroable{true})) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we integrate this is the existing TestIsZero
test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we can. Moved, thanks!
Codecov Report
@@ Coverage Diff @@
## master #67 +/- ##
==========================================
+ Coverage 85.49% 85.59% +0.10%
==========================================
Files 13 13
Lines 1565 1569 +4
==========================================
+ Hits 1338 1343 +5
+ Misses 194 193 -1
Partials 33 33
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
Signed-off-by: Michał Szczygieł <1153719+mszczygiel@users.noreply.github.com>
Signed-off-by: Michał Szczygieł <1153719+mszczygiel@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thx!
Looks good. Thanks. |
This avoids panics when a nil pointer to a type that implements zeroable interface is passed to IsZero
fixes #66