-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Show "<nil>" for nil Stringer. #854
Conversation
In encodeStringer, instead of returning an error when a panic occurs when calling String() on a nil pointer, use the string value "<nil>" like the fmt package does. It is not always possible to handle this case by fixing the implementation of String to not panic. This requires implementing String with a pointer receiver, which doesn't work if you need to be able to call String on non-addressable values.
Codecov Report
@@ Coverage Diff @@
## master #854 +/- ##
=======================================
Coverage 98.35% 98.35%
=======================================
Files 43 43
Lines 2368 2372 +4
=======================================
+ Hits 2329 2333 +4
Misses 32 32
Partials 7 7
Continue to review full report at Codecov.
|
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.
I think this is reasonable, it matches the behaviour of the standard library.
@@ -208,7 +208,12 @@ func addFields(enc ObjectEncoder, fields []Field) { | |||
func encodeStringer(key string, stringer interface{}, enc ObjectEncoder) (err error) { | |||
defer func() { | |||
if v := recover(); v != nil { | |||
err = fmt.Errorf("PANIC=%v", v) | |||
val := reflect.ValueOf(stringer) |
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.
This block looks very similar to the logic in the fmt package uses to determine if nil
should be used,
https://golang.org/src/fmt/print.go#L540
Should we reference this?
@@ -208,7 +208,12 @@ func addFields(enc ObjectEncoder, fields []Field) { | |||
func encodeStringer(key string, stringer interface{}, enc ObjectEncoder) (err error) { | |||
defer func() { | |||
if v := recover(); v != nil { | |||
err = fmt.Errorf("PANIC=%v", v) | |||
val := reflect.ValueOf(stringer) |
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.
val := reflect.ValueOf(stringer) | |
// See also https://golang.org/src/fmt/print.go#L540. | |
val := reflect.ValueOf(stringer) |
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.
I'll add the reference to the stdlib as a follow up.
In encodeStringer, instead of returning an error when a panic occurs when calling String() on a nil pointer, use the string value "<nil>" like the fmt package does. It is not always possible to handle this case by fixing the implementation of String to not panic. This requires implementing String with a pointer receiver, which doesn't work if you need to be able to call String on non-addressable values.
In encodeStringer, instead of returning an error when a panic
occurs when calling String() on a nil pointer, use the string value
"" like the fmt package does.
It is not always possible to handle this case by fixing the
implementation of String to not panic. This requires implementing
String with a pointer receiver, which doesn't work if you need to
be able to call String on non-addressable values.