diff --git a/formatter.go b/formatter.go index 5efaa94..a317d7b 100644 --- a/formatter.go +++ b/formatter.go @@ -30,7 +30,7 @@ func Formatter(x interface{}) (f fmt.Formatter) { } func (fo formatter) String() string { - return fmt.Sprint(fo.v) // unwrap it + return fmt.Sprint(fo.v.Interface()) // unwrap it } func (fo formatter) passThrough(f fmt.State, c rune) { @@ -47,7 +47,7 @@ func (fo formatter) passThrough(f fmt.State, c rune) { s += fmt.Sprintf(".%d", p) } s += string(c) - fmt.Fprintf(f, s, fo.v) + fmt.Fprintf(f, s, fo.v.Interface()) } func (fo formatter) Format(f fmt.State, c rune) { diff --git a/formatter_test.go b/formatter_test.go index 5f3204e..c8c0b51 100644 --- a/formatter_test.go +++ b/formatter_test.go @@ -13,6 +13,11 @@ type test struct { s string } +type passtest struct { + v interface{} + f, s string +} + type LongStructTypeName struct { longFieldName interface{} otherLongFieldName interface{} @@ -33,8 +38,30 @@ func (f F) Format(s fmt.State, c rune) { fmt.Fprintf(s, "F(%d)", int(f)) } +type Stringer struct { i int } + +func (s *Stringer) String() string { return "foo" } + var long = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" +var passthrough = []passtest{ + {1, "%d", "1"}, + {"a", "%s", "a"}, + {&Stringer{}, "%s", "foo"}, +} + +func TestPassthrough(t *testing.T) { + for _, tt := range passthrough { + s := fmt.Sprintf(tt.f, Formatter(tt.v)) + if tt.s != s { + t.Errorf("expected %q", tt.s) + t.Errorf("got %q", s) + t.Errorf("expraw\n%s", tt.s) + t.Errorf("gotraw\n%s", s) + } + } +} + var gosyntax = []test{ {nil, `nil`}, {"", `""`},