Skip to content

Commit 456249b

Browse files
committed
Fix separator-printing when skipping zero-fields
Also, incorporate more tests
1 parent 571c72f commit 456249b

3 files changed

Lines changed: 43 additions & 13 deletions

File tree

example_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,17 @@ func Example() {
1010
a, b int
1111
}
1212
var x = []myType{{1, 2}, {3, 4}, {5, 6}}
13-
fmt.Printf("%# v", pretty.Formatter(x))
13+
fmt.Printf("%# v\n", pretty.Formatter(x))
14+
15+
var zeroedFields = []myType{{33, 0}, {a: 0, b: 34}}
16+
// Note the '+' in the format
17+
fmt.Printf("%# +v", pretty.Formatter(zeroedFields))
1418
// output:
1519
// []pretty_test.myType{
1620
// {a:1, b:2},
1721
// {a:3, b:4},
1822
// {a:5, b:6},
1923
// }
20-
21-
var zeroedFields = []myType{{33, 0}, {a: 0, b: 34}}
22-
// Note the '+' in the format
23-
fmt.Printf("%# +v", pretty.Formatter(zeroedFields))
24-
// output:
2524
// []pretty_test.myType{
2625
// {a:33},
2726
// {b:34},

formatter.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,24 +178,35 @@ func (p *printer) printValue(v reflect.Value, showType, quote bool) {
178178
writeByte(p, '\n')
179179
pp = p.indent()
180180
}
181+
type field struct {
182+
name string
183+
t reflect.Type
184+
value reflect.Value
185+
}
186+
fields := make([]field, 0, v.NumField())
187+
// Collect fields, filtering out zero fields if needed
181188
for i := 0; i < v.NumField(); i++ {
182-
fValue := getField(v, i)
183-
if p.skipZeroFields && !nonzero(fValue) {
189+
value := getField(v, i)
190+
if p.skipZeroFields && !nonzero(value) {
184191
continue
185192
}
193+
f := t.Field(i)
194+
fields = append(fields, field{f.Name, f.Type, value})
195+
}
196+
for i, field := range fields {
186197
showTypeInStruct := true
187-
if f := t.Field(i); f.Name != "" {
188-
io.WriteString(pp, f.Name)
198+
if field.name != "" {
199+
io.WriteString(pp, field.name)
189200
writeByte(pp, ':')
190201
if expand {
191202
writeByte(pp, '\t')
192203
}
193-
showTypeInStruct = labelType(f.Type)
204+
showTypeInStruct = labelType(field.t)
194205
}
195-
pp.printValue(fValue, showTypeInStruct, true)
206+
pp.printValue(field.value, showTypeInStruct, true)
196207
if expand {
197208
io.WriteString(pp, ",\n")
198-
} else if i < v.NumField()-1 {
209+
} else if i < len(fields)-1 {
199210
io.WriteString(pp, ", ")
200211
}
201212
}

formatter_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,22 @@ var gosyntaxSkipZeroFields = []test{
218218
otherLongFieldName: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
219219
}`,
220220
},
221+
{
222+
LongStructTypeName{
223+
longFieldName: long,
224+
otherLongFieldName: "",
225+
},
226+
`pretty.LongStructTypeName{
227+
longFieldName: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
228+
}`,
229+
},
230+
{
231+
LongStructTypeName{
232+
longFieldName: "",
233+
otherLongFieldName: "",
234+
},
235+
`pretty.LongStructTypeName{}`,
236+
},
221237
{
222238
&LongStructTypeName{
223239
longFieldName: &LongStructTypeName{},
@@ -249,12 +265,16 @@ var gosyntaxSkipZeroFields = []test{
249265
LongStructTypeName{nil, nil},
250266
[]byte{1, 2, 3},
251267
T{3, 4},
268+
T{0, 4},
269+
T{3, 0},
252270
LongStructTypeName{long, nil},
253271
},
254272
`[]interface {}{
255273
pretty.LongStructTypeName{},
256274
[]uint8{0x1, 0x2, 0x3},
257275
pretty.T{x:3, y:4},
276+
pretty.T{y:4},
277+
pretty.T{x:3},
258278
pretty.LongStructTypeName{
259279
longFieldName: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
260280
},

0 commit comments

Comments
 (0)