go.net/context: remove the Key type; replace it with interface{}. This
makes the Context interface dependent only on standard packages, which
means types in other packages can implement this interface without
depending on go.net/context.
Remove the NewKey function and add examples showing how to use
unexported types to avoid key collisions. This is the same model used
by http://www.gorillatoolkit.org/pkg/context, except we associate values
with a specific Context instead of storing them in a package-level map.
LGTM=crawshaw
R=golang-codereviews, crawshaw, dsymonds
CC=golang-codereviews, rsc
https://golang.org/cl/104480044
diff --git a/context/context_test.go b/context/context_test.go
index 2afc1e7..518b9a9 100644
--- a/context/context_test.go
+++ b/context/context_test.go
@@ -249,41 +249,55 @@
}
}
-var k1, k2 = NewKey("k1"), NewKey("k2")
+type key1 int
+type key2 int
+
+var k1 = key1(1)
+var k2 = key2(1) // same int as k1, different type
+var k3 = key2(3) // same type as k2, different int
func TestValues(t *testing.T) {
- check := func(c Context, nm, v1, v2 string) {
+ check := func(c Context, nm, v1, v2, v3 string) {
if v, ok := c.Value(k1).(string); ok == (len(v1) == 0) || v != v1 {
t.Errorf(`%s.Value(k1).(string) = %q, %t want %q, %t`, nm, v, ok, v1, len(v1) != 0)
}
if v, ok := c.Value(k2).(string); ok == (len(v2) == 0) || v != v2 {
t.Errorf(`%s.Value(k2).(string) = %q, %t want %q, %t`, nm, v, ok, v2, len(v2) != 0)
}
+ if v, ok := c.Value(k3).(string); ok == (len(v3) == 0) || v != v3 {
+ t.Errorf(`%s.Value(k3).(string) = %q, %t want %q, %t`, nm, v, ok, v3, len(v3) != 0)
+ }
}
c0 := Background()
- check(c0, "c0", "", "")
+ check(c0, "c0", "", "", "")
c1 := WithValue(nil, k1, "c1k1")
- check(c1, "c1", "c1k1", "")
+ check(c1, "c1", "c1k1", "", "")
c2 := WithValue(c1, k2, "c2k2")
- check(c2, "c2", "c1k1", "c2k2")
+ check(c2, "c2", "c1k1", "c2k2", "")
- c3 := WithValue(c2, k1, nil)
- check(c3, "c3", "", "c2k2")
+ c3 := WithValue(c2, k3, "c3k3")
+ check(c3, "c2", "c1k1", "c2k2", "c3k3")
+
+ c4 := WithValue(c3, k1, nil)
+ check(c4, "c4", "", "c2k2", "c3k3")
o0 := otherContext{Background()}
- check(o0, "o0", "", "")
+ check(o0, "o0", "", "", "")
o1 := otherContext{WithValue(nil, k1, "c1k1")}
- check(o1, "o1", "c1k1", "")
+ check(o1, "o1", "c1k1", "", "")
o2 := WithValue(o1, k2, "o2k2")
- check(o2, "o2", "c1k1", "o2k2")
+ check(o2, "o2", "c1k1", "o2k2", "")
- o3 := otherContext{c3}
- check(o3, "o3", "", "c2k2")
+ o3 := otherContext{c4}
+ check(o3, "o3", "", "c2k2", "c3k3")
+
+ o4 := WithValue(o3, k3, nil)
+ check(o4, "o4", "", "c2k2", "")
}
func TestAllocs(t *testing.T) {