Add GoStringer support (#68)
Print values using the `GoString()` method (from `fmt.GoStringer()`) if implemented.
diff --git a/formatter.go b/formatter.go
index f3718ea..249f089 100644
--- a/formatter.go
+++ b/formatter.go
@@ -98,6 +98,14 @@
return
}
+ if v.IsValid() && v.CanInterface() {
+ i := v.Interface()
+ if goStringer, ok := i.(fmt.GoStringer); ok {
+ io.WriteString(p, goStringer.GoString())
+ return
+ }
+ }
+
switch v.Kind() {
case reflect.Bool:
p.printInline(v, v.Bool(), showType)
diff --git a/formatter_test.go b/formatter_test.go
index 10ce57a..f23c700 100644
--- a/formatter_test.go
+++ b/formatter_test.go
@@ -62,6 +62,19 @@
}
}
+type StructWithPrivateFields struct {
+ A string
+ b string
+}
+
+func NewStructWithPrivateFields(a string) StructWithPrivateFields {
+ return StructWithPrivateFields{a, "fixedb"}
+}
+
+func (s StructWithPrivateFields) GoString() string {
+ return fmt.Sprintf("NewStructWithPrivateFields(%q)", s.A)
+}
+
var gosyntax = []test{
{nil, `nil`},
{"", `""`},
@@ -84,6 +97,7 @@
`[]string{"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"}`,
},
{F(5), "pretty.F(5)"},
+ { NewStructWithPrivateFields("foo"), `NewStructWithPrivateFields("foo")`},
{
SA{&T{1, 2}, T{3, 4}},
`pretty.SA{