Skip to content
Open
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
16 changes: 13 additions & 3 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,18 @@ func (p *printer) printValue(v reflect.Value, showType, quote bool) {
writeByte(p, '\n')
pp = p.indent()
}
for i := 0; i < v.NumField(); i++ {

numfields := v.NumField()
writecomma := false
for i := 0; i < numfields; i++ {
fieldTag := t.Field(i)
val := fieldTag.Tag.Get("pretty")
if val == "-" {
continue
}
if writecomma && !expand && i > 0 {
io.WriteString(pp, ", ")
}
showTypeInStruct := true
if f := t.Field(i); f.Name != "" {
io.WriteString(pp, f.Name)
Expand All @@ -178,12 +189,11 @@ func (p *printer) printValue(v reflect.Value, showType, quote bool) {
writeByte(pp, '\t')
}
showTypeInStruct = labelType(f.Type)
writecomma = true
}
pp.printValue(getField(v, i), showTypeInStruct, true)
if expand {
io.WriteString(pp, ",\n")
} else if i < v.NumField()-1 {
io.WriteString(pp, ", ")
}
}
if expand {
Expand Down
50 changes: 50 additions & 0 deletions formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,53 @@ func TestCycle(t *testing.T) {
*iv = *i
t.Logf("Example long interface cycle:\n%# v", Formatter(i))
}
func TestStructFieldTags1(t *testing.T) {
type testFieldTagCase struct {
tc interface{}
want string
}

type fieldTagTest1 struct {
z string `pretty:"-"`
x, y int
}
type commaTest1 struct {
z string
x, y int `pretty:"-"`
}
type commaTest2 struct {
x, y int
private string `pretty:"-"`
a, b, c int
}
type structInInterfaceTest struct {
fields []interface{}
}

// httpGet, _ := http.NewRequest(http.MethodGet, "https://example.org/", nil)
for i, tc := range []testFieldTagCase{
// {tc: struct {
// r *http.Request `pretty:"-"`
// name string
// }{r: httpGet, name: "http Request test"},
// want: `struct { r *http.Request "pretty:\"-\""; name string }{
// name: "http Request test",
// }`},
{tc: fieldTagTest1{x: 1, y: 2, z: "should be ignored"}, want: `pretty.fieldTagTest1{x:1, y:2}`},
{tc: commaTest1{x: 1, y: 2, z: "should be displayed with no commas"}, want: `pretty.commaTest1{z:"should be displayed with no commas"}`},
{tc: commaTest2{x: 1, y: 2, private: "should be ignored", a: 42, b: 3, c: 4}, want: `pretty.commaTest2{x:1, y:2, a:42, b:3, c:4}`},
{tc: structInInterfaceTest{fields: []interface{}{commaTest2{x: 1, y: 2, private: "should be ignored", a: 42, b: 3, c: 4}}}, want: `pretty.structInInterfaceTest{
fields: {
pretty.commaTest2{x:1, y:2, a:42, b:3, c:4},
},
}`},
} {
got := Sprint(tc.tc)
// fmt.Fprintf(os.Stderr, "\ntest %d:\n%s\n", i+1, got)
if got != tc.want {
t.Logf("test %d: Got: %s", i+1, got)
t.Logf("test %d Wanted: %s", i+1, tc.want)
t.Fail()
}
}
}