aboutsummaryrefslogtreecommitdiff
path: root/src/context
diff options
context:
space:
mode:
authorBrad Fitzpatrick <bradfitz@golang.org>2019-02-27 20:08:36 +0000
committerBrad Fitzpatrick <bradfitz@golang.org>2019-03-27 02:37:56 +0000
commit724a86fcede55d0e80da4a779ef64a2eb5d235a8 (patch)
tree2e162222e9eb4eb604efd70f0627ba57868c12ce /src/context
parentf2e51f00158c2dcdff37c573c24f798d1e63db31 (diff)
downloadgo-724a86fcede55d0e80da4a779ef64a2eb5d235a8.tar.gz
go-724a86fcede55d0e80da4a779ef64a2eb5d235a8.zip
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 <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/context')
-rw-r--r--src/context/context.go35
-rw-r--r--src/context/context_test.go2
2 files changed, 32 insertions, 5 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{} {
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)
}