Skip to content

Commit

Permalink
Fix separator-printing when skipping zero-fields
Browse files Browse the repository at this point in the history
Also, incorporate more tests
  • Loading branch information
2opremio committed Aug 27, 2020
1 parent 571c72f commit 456249b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
11 changes: 5 additions & 6 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@ func Example() {
a, b int
}
var x = []myType{{1, 2}, {3, 4}, {5, 6}}
fmt.Printf("%# v", pretty.Formatter(x))
fmt.Printf("%# v\n", pretty.Formatter(x))

var zeroedFields = []myType{{33, 0}, {a: 0, b: 34}}
// Note the '+' in the format
fmt.Printf("%# +v", pretty.Formatter(zeroedFields))
// output:
// []pretty_test.myType{
// {a:1, b:2},
// {a:3, b:4},
// {a:5, b:6},
// }

var zeroedFields = []myType{{33, 0}, {a: 0, b: 34}}
// Note the '+' in the format
fmt.Printf("%# +v", pretty.Formatter(zeroedFields))
// output:
// []pretty_test.myType{
// {a:33},
// {b:34},
Expand Down
25 changes: 18 additions & 7 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,24 +178,35 @@ func (p *printer) printValue(v reflect.Value, showType, quote bool) {
writeByte(p, '\n')
pp = p.indent()
}
type field struct {
name string
t reflect.Type
value reflect.Value
}
fields := make([]field, 0, v.NumField())
// Collect fields, filtering out zero fields if needed
for i := 0; i < v.NumField(); i++ {
fValue := getField(v, i)
if p.skipZeroFields && !nonzero(fValue) {
value := getField(v, i)
if p.skipZeroFields && !nonzero(value) {
continue
}
f := t.Field(i)
fields = append(fields, field{f.Name, f.Type, value})
}
for i, field := range fields {
showTypeInStruct := true
if f := t.Field(i); f.Name != "" {
io.WriteString(pp, f.Name)
if field.name != "" {
io.WriteString(pp, field.name)
writeByte(pp, ':')
if expand {
writeByte(pp, '\t')
}
showTypeInStruct = labelType(f.Type)
showTypeInStruct = labelType(field.t)
}
pp.printValue(fValue, showTypeInStruct, true)
pp.printValue(field.value, showTypeInStruct, true)
if expand {
io.WriteString(pp, ",\n")
} else if i < v.NumField()-1 {
} else if i < len(fields)-1 {
io.WriteString(pp, ", ")
}
}
Expand Down
20 changes: 20 additions & 0 deletions formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,22 @@ var gosyntaxSkipZeroFields = []test{
otherLongFieldName: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
}`,
},
{
LongStructTypeName{
longFieldName: long,
otherLongFieldName: "",
},
`pretty.LongStructTypeName{
longFieldName: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
}`,
},
{
LongStructTypeName{
longFieldName: "",
otherLongFieldName: "",
},
`pretty.LongStructTypeName{}`,
},
{
&LongStructTypeName{
longFieldName: &LongStructTypeName{},
Expand Down Expand Up @@ -249,12 +265,16 @@ var gosyntaxSkipZeroFields = []test{
LongStructTypeName{nil, nil},
[]byte{1, 2, 3},
T{3, 4},
T{0, 4},
T{3, 0},
LongStructTypeName{long, nil},
},
`[]interface {}{
pretty.LongStructTypeName{},
[]uint8{0x1, 0x2, 0x3},
pretty.T{x:3, y:4},
pretty.T{y:4},
pretty.T{x:3},
pretty.LongStructTypeName{
longFieldName: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
},
Expand Down

0 comments on commit 456249b

Please sign in to comment.