Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 4706c84

Browse files
authored
fix: patch extra tabs in nested tablewriter output (#276)
1 parent df10559 commit 4706c84

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

pkg/tablewriter/table_output.golden

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Name birthday month first_nested second_nested Age
2+
Tom 12 234-0934 2340-234234 28.12
3+
Jerry 3 aflfafe-afjlk falj-fjlkjlkadf 36.22

pkg/tablewriter/tablewriter.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tablewriter
22

33
import (
44
"fmt"
5+
"io"
56
"os"
67
"reflect"
78
"strings"
@@ -22,7 +23,7 @@ func StructValues(data interface{}) string {
2223
continue
2324
}
2425
if shouldFlatten(v.Type().Field(i)) {
25-
fmt.Fprintf(s, "%v\t", StructValues(v.Field(i).Interface()))
26+
fmt.Fprintf(s, "%v", StructValues(v.Field(i).Interface()))
2627
continue
2728
}
2829
fmt.Fprintf(s, "%v\t", v.Field(i).Interface())
@@ -46,14 +47,17 @@ func StructFieldNames(data interface{}) string {
4647
continue
4748
}
4849
if shouldFlatten(field) {
49-
fmt.Fprintf(s, "%s\t", StructFieldNames(reflect.New(field.Type).Interface()))
50+
fmt.Fprintf(s, "%s", StructFieldNames(reflect.New(field.Type).Interface()))
5051
continue
5152
}
5253
fmt.Fprintf(s, "%s\t", fieldName(field))
5354
}
5455
return s.String()
5556
}
5657

58+
// The output io.Writer for WriteTable. This is globally defined to allow overriding in tests.
59+
var tableOutput io.Writer = os.Stdout
60+
5761
// WriteTable writes the given list elements to stdout in a human readable
5862
// tabular format. Headers abide by the `table` struct tag.
5963
//
@@ -63,7 +67,7 @@ func WriteTable(length int, each func(i int) interface{}) error {
6367
if length < 1 {
6468
return nil
6569
}
66-
w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
70+
w := tabwriter.NewWriter(tableOutput, 0, 0, 4, ' ', 0)
6771
defer func() { _ = w.Flush() }() // Best effort.
6872
for ix := 0; ix < length; ix++ {
6973
item := each(ix)

pkg/tablewriter/tablewriter_test.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package tablewriter
2+
3+
import (
4+
"bytes"
5+
"flag"
6+
"io/ioutil"
7+
"testing"
8+
9+
"cdr.dev/slog/sloggers/slogtest/assert"
10+
)
11+
12+
var write = flag.Bool("write", false, "write to the golden files")
13+
14+
func TestTableWriter(t *testing.T) {
15+
type NestedRow struct {
16+
NestedOne string `table:"first_nested"`
17+
NestedTwo string `table:"second_nested"`
18+
}
19+
20+
type Row struct {
21+
ID string `table:"-"`
22+
Name string
23+
BirthdayMonth int `table:"birthday month"`
24+
Nested NestedRow `table:"_"`
25+
Age float32
26+
}
27+
28+
items := []Row{
29+
{
30+
ID: "13123lkjqlkj-2f323l--f23f",
31+
Name: "Tom",
32+
BirthdayMonth: 12,
33+
Age: 28.12,
34+
Nested: NestedRow{
35+
NestedOne: "234-0934",
36+
NestedTwo: "2340-234234",
37+
},
38+
},
39+
{
40+
ID: "afwaflkj23kl-2f323l--f23f",
41+
Name: "Jerry",
42+
BirthdayMonth: 3,
43+
Age: 36.22,
44+
Nested: NestedRow{
45+
NestedOne: "aflfafe-afjlk",
46+
NestedTwo: "falj-fjlkjlkadf",
47+
},
48+
},
49+
}
50+
51+
buf := bytes.NewBuffer(nil)
52+
tableOutput = buf
53+
err := WriteTable(len(items), func(i int) interface{} { return items[i] })
54+
assert.Success(t, "write table", err)
55+
56+
assertGolden(t, "table_output.golden", buf.Bytes())
57+
}
58+
59+
func assertGolden(t *testing.T, path string, output []byte) {
60+
if *write {
61+
err := ioutil.WriteFile(path, output, 0777)
62+
assert.Success(t, "write file", err)
63+
return
64+
}
65+
goldenContent, err := ioutil.ReadFile(path)
66+
assert.Success(t, "read golden file", err)
67+
assert.Equal(t, "golden content matches", string(goldenContent), string(output))
68+
}

0 commit comments

Comments
 (0)