Removed use of the context package to maintain backwards compatability with go < 1.7
diff --git a/format/format.go b/format/format.go
index 8fde56b..5cd89bc 100644
--- a/format/format.go
+++ b/format/format.go
@@ -4,11 +4,11 @@
package format
import (
- "context"
"fmt"
"reflect"
"strconv"
"strings"
+ "time"
)
// Use MaxDepth to set the maximum recursion depth when printing deeply nested objects
@@ -29,7 +29,17 @@
Set PrintContextObjects = true to enable printing of the context internals.
*/
var PrintContextObjects = false
-var contextType = reflect.TypeOf((*context.Context)(nil)).Elem()
+
+// Ctx interface defined here to keep backwards compatability with go < 1.7
+// It matches the context.Context interface
+type Ctx interface {
+ Deadline() (deadline time.Time, ok bool)
+ Done() <-chan struct{}
+ Err() error
+ Value(key interface{}) interface{}
+}
+
+var contextType = reflect.TypeOf((*Ctx)(nil)).Elem()
//The default indentation string emitted by the format package
var Indent = " "
@@ -137,7 +147,7 @@
if !PrintContextObjects {
if value.Type().Implements(contextType) && indentation > 1 {
- return "<suppressed>"
+ return "<suppressed context>"
}
}
diff --git a/format/format_test.go b/format/format_test.go
index 1b6f559..4d4b36c 100644
--- a/format/format_test.go
+++ b/format/format_test.go
@@ -1,9 +1,7 @@
package format_test
import (
- "context"
"fmt"
- "net/http"
"strings"
"time"
@@ -75,6 +73,25 @@
return "string"
}
+type ctx struct {
+}
+
+func (c *ctx) Deadline() (deadline time.Time, ok bool) {
+ return time.Time{}, false
+}
+
+func (c *ctx) Done() <-chan struct{} {
+ return nil
+}
+
+func (c *ctx) Err() error {
+ return nil
+}
+
+func (c *ctx) Value(key interface{}) interface{} {
+ return nil
+}
+
var _ = Describe("Format", func() {
match := func(typeRepresentation string, valueRepresentation string, args ...interface{}) types.GomegaMatcher {
if len(args) > 0 {
@@ -470,16 +487,21 @@
})
Describe("Printing a context.Context field", func() {
- req, _ := http.NewRequest("GET", "http://example.com/test", nil)
- ctx, _ := context.WithTimeout(context.Background(), time.Second)
- reqWithContext := req.WithContext(ctx)
+
+ type structWithContext struct {
+ Context Ctx
+ Value string
+ }
+
+ context := ctx{}
+ objWithContext := structWithContext{Value: "some-value", Context: &context}
It("Suppresses the content by default", func() {
- Ω(Object(reqWithContext, 1)).Should(ContainSubstring("<suppressed>"))
+ Ω(Object(objWithContext, 1)).Should(ContainSubstring("<suppressed context>"))
})
It("Doesn't supress the context if it's the object being printed", func() {
- Ω(Object(ctx, 1)).ShouldNot(MatchRegexp("^.*<suppressed>$"))
+ Ω(Object(context, 1)).ShouldNot(MatchRegexp("^.*<suppressed context>$"))
})
Context("PrintContextObjects is set", func() {
@@ -492,7 +514,7 @@
})
It("Prints the context", func() {
- Ω(Object(reqWithContext, 1)).ShouldNot(ContainSubstring("<suppressed>"))
+ Ω(Object(objWithContext, 1)).ShouldNot(ContainSubstring("<suppressed context>"))
})
})
})