Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
27 changes: 27 additions & 0 deletions formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ type test struct {
s string
}

type passtest struct {
v interface{}
f, s string
}

type LongStructTypeName struct {
longFieldName interface{}
otherLongFieldName interface{}
Expand All @@ -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`},
{"", `""`},
Expand Down