Skip to content
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

Merged
merged 1 commit into from
Aug 28, 2020

Conversation

andy-retailnext
Copy link
Contributor

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.

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.
@CLAassistant
Copy link

CLAassistant commented Aug 4, 2020

CLA assistant check
All committers have signed the CLA.

@codecov
Copy link

codecov bot commented Aug 4, 2020

Codecov Report

Merging #854 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           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           
Impacted Files Coverage Δ
zapcore/field.go 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 53a3870...c8cee25. Read the comment docs.

Copy link
Collaborator

@prashantv prashantv left a 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)
Copy link
Collaborator

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)
Copy link
Collaborator

@abhinav abhinav Aug 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
val := reflect.ValueOf(stringer)
// See also https://golang.org/src/fmt/print.go#L540.
val := reflect.ValueOf(stringer)

Copy link
Collaborator

@prashantv prashantv left a 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.

@abhinav abhinav merged commit 663c590 into uber-go:master Aug 28, 2020
cgxxv pushed a commit to cgxxv/zap that referenced this pull request Mar 25, 2022
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

4 participants