From 724a86fcede55d0e80da4a779ef64a2eb5d235a8 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 27 Feb 2019 20:08:36 +0000 Subject: context: don't depend on fmt So the net package doesn't indirectly depend on unicode tables. But we're still not quite there, because a new test added in this CL reveals that we still have a path to unicode via: deps_test.go:570: TODO(issue 30440): policy violation: net => sort => reflect => unicode Updates #30440 Change-Id: I710c2061dfbaa8e866c92e6c824bd8df35784165 Reviewed-on: https://go-review.googlesource.com/c/go/+/169080 Reviewed-by: Ian Lance Taylor Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot --- src/context/context.go | 35 +++++++++++++++++++++++++++++++---- src/context/context_test.go | 2 +- 2 files changed, 32 insertions(+), 5 deletions(-) (limited to 'src/context') 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 "" +} + 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{} { diff --git a/src/context/context_test.go b/src/context/context_test.go index f73f2837b8..0cec169915 100644 --- a/src/context/context_test.go +++ b/src/context/context_test.go @@ -343,7 +343,7 @@ func XTestValues(t testingT) { c1 := WithValue(Background(), k1, "c1k1") check(c1, "c1", "c1k1", "", "") - if got, want := fmt.Sprint(c1), `context.Background.WithValue(1, "c1k1")`; got != want { + if got, want := fmt.Sprint(c1), `context.Background.WithValue(type context.key1, val c1k1)`; got != want { t.Errorf("c.String() = %q want %q", got, want) } -- cgit v1.3