Skip to content

Commit

Permalink
Label structs when they are members of other structs.
Browse files Browse the repository at this point in the history
Adds the type label for non-pointer struct fields.
  • Loading branch information
cee-dub committed Jul 22, 2014
1 parent bc9499c commit 18947b1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
13 changes: 11 additions & 2 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package pretty

import (
"fmt"
"github.com/kr/text"
"io"
"reflect"
"strconv"
"text/tabwriter"

"github.com/kr/text"
)

const (
Expand Down Expand Up @@ -156,7 +157,7 @@ func (p *printer) printValue(v reflect.Value, showType, quote bool) {
if expand {
writeByte(pp, '\t')
}
showTypeInStruct = f.Type.Kind() == reflect.Interface
showTypeInStruct = labelType(f.Type)
}
pp.printValue(getField(v, i), showTypeInStruct, true)
if expand {
Expand Down Expand Up @@ -275,6 +276,14 @@ func canExpand(t reflect.Type) bool {
return false
}

func labelType(t reflect.Type) bool {
switch t.Kind() {
case reflect.Interface, reflect.Struct:
return true
}
return false
}

func (p *printer) fmtString(s string, quote bool) {
if quote {
s = strconv.Quote(s)
Expand Down
4 changes: 3 additions & 1 deletion formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type LongStructTypeName struct {

type SA struct {
t *T
v T
}

type T struct {
Expand Down Expand Up @@ -55,9 +56,10 @@ var gosyntax = []test{
},
{F(5), "pretty.F(5)"},
{
SA{&T{1, 2}},
SA{&T{1, 2}, T{3, 4}},
`pretty.SA{
t: &pretty.T{x:1, y:2},
v: pretty.T{x:3, y:4},
}`,
},
{
Expand Down

0 comments on commit 18947b1

Please sign in to comment.