Skip to content

Commit

Permalink
attributes: use fmt.Sprint(v) instead of v.String() and recover, let …
Browse files Browse the repository at this point in the history
  • Loading branch information
searKing committed Sep 16, 2023
1 parent 5936668 commit e8fe6ad
Showing 1 changed file with 1 addition and 17 deletions.
18 changes: 1 addition & 17 deletions attributes/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,26 +125,10 @@ func (a *Attributes) String() string {
const nilAngleString = "<nil>"

func str(x any) (s string) {
defer func() {
if r := recover(); r != nil {
// If it panics with a nil pointer, just say "<nil>". The likeliest causes are a
// [fmt.Stringer] that fails to guard against nil or a nil pointer for a
// value receiver, and in either case, "<nil>" is a nice result.
//
// Adapted from the code in fmt/print.go.
if v := reflect.ValueOf(x); v.Kind() == reflect.Pointer && v.IsNil() {
s = nilAngleString
return
}

// The panic was likely not caused by fmt.Stringer.
panic(r)
}
}()
if x == nil { // NOTE: typed nils will not be caught by this check
return nilAngleString
} else if v, ok := x.(fmt.Stringer); ok {
return v.String()
return fmt.Sprint(v)
} else if v, ok := x.(string); ok {
return v
}
Expand Down

0 comments on commit e8fe6ad

Please sign in to comment.