diff options
Diffstat (limited to 'src/context/context.go')
| -rw-r--r-- | src/context/context.go | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/src/context/context.go b/src/context/context.go index 36f83c7b5b..77298f6531 100644 --- a/src/context/context.go +++ b/src/context/context.go @@ -49,7 +49,6 @@ package context import ( "errors" - "fmt" "internal/reflectlite" "sync" "time" @@ -338,8 +337,19 @@ func (c *cancelCtx) Err() error { return err } +type stringer interface { + String() string +} + +func contextName(c Context) string { + if s, ok := c.(stringer); ok { + return s.String() + } + return reflectlite.TypeOf(c).String() +} + func (c *cancelCtx) String() string { - return fmt.Sprintf("%v.WithCancel", c.Context) + return contextName(c.Context) + ".WithCancel" } // cancel closes c.done, cancels each of c's children, and, if @@ -420,7 +430,9 @@ func (c *timerCtx) Deadline() (deadline time.Time, ok bool) { } func (c *timerCtx) String() string { - return fmt.Sprintf("%v.WithDeadline(%s [%s])", c.cancelCtx.Context, c.deadline, time.Until(c.deadline)) + return contextName(c.cancelCtx.Context) + ".WithDeadline(" + + c.deadline.String() + " [" + + time.Until(c.deadline).String() + "])" } func (c *timerCtx) cancel(removeFromParent bool, err error) { @@ -481,8 +493,23 @@ type valueCtx struct { key, val interface{} } +// stringify tries a bit to stringify v, without using fmt, since we don't +// want context depending on the unicode tables. This is only used by +// *valueCtx.String(). +func stringify(v interface{}) string { + if s, ok := v.(stringer); ok { + return s.String() + } + if s, ok := v.(string); ok { + return s + } + return "<not Stringer>" +} + func (c *valueCtx) String() string { - return fmt.Sprintf("%v.WithValue(%#v, %#v)", c.Context, c.key, c.val) + return contextName(c.Context) + ".WithValue(type " + + reflectlite.TypeOf(c.key).String() + + ", val " + stringify(c.val) + ")" } func (c *valueCtx) Value(key interface{}) interface{} { |
