Skip to content

Commit

Permalink
Store outputwriter data as 2d array of interfaces
Browse files Browse the repository at this point in the history
... instead of 2d array of strings done prior to this commit.

AddRow accepts a variatic list of interface{}s, yet immediately converts
each of them to their golang string representation.

This change retains these values as interface{}'s instead.
The idea is to delay any necessary conversion to the point when we are
actually rendering the values.

This is particularly useful when rendering container values like
maps/lists into their valid YAML/JSON forms.

Fixes: #102

Signed-off-by: Vui Lam <[email protected]>
  • Loading branch information
vuil committed Sep 29, 2023
1 parent 1e5dc62 commit 72bf81c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
33 changes: 21 additions & 12 deletions component/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const (
type outputwriter struct {
out io.Writer
keys []string
values [][]string
values [][]interface{}
outputFormat OutputType
}

Expand All @@ -67,12 +67,8 @@ func (ow *outputwriter) SetKeys(headerKeys ...string) {

// AddRow appends a new row to our table.
func (ow *outputwriter) AddRow(items ...interface{}) {
row := []string{}

// Make sure all values are ultimately strings
for _, item := range items {
row = append(row, fmt.Sprintf("%v", item))
}
row := []interface{}{}
row = append(row, items...)
ow.values = append(ow.values, row)
}

Expand All @@ -90,15 +86,15 @@ func (ow *outputwriter) Render() {
}
}

func (ow *outputwriter) dataStruct() []map[string]string {
data := []map[string]string{}
func (ow *outputwriter) dataStruct() []map[string]interface{} {
data := []map[string]interface{}{}
keys := ow.keys
for i, k := range keys {
keys[i] = strings.ToLower(strings.ReplaceAll(k, " ", "_"))
}

for _, itemValues := range ow.values {
item := map[string]string{}
item := map[string]interface{}{}
for i, value := range itemValues {
if i == len(keys) {
continue
Expand Down Expand Up @@ -192,7 +188,7 @@ func renderListTable(ow *outputwriter) {
// There are more headers than values, leave it blank
continue
}
row = append(row, data[i])
row = append(row, fmt.Sprintf("%v", data[i]))
}
headerLabel := strings.ToUpper(header) + ":"
values := strings.Join(row, ", ")
Expand Down Expand Up @@ -222,6 +218,19 @@ func renderTable(ow *outputwriter) {
table.SetColWidth(colWidth)
table.SetTablePadding("\t\t")
table.SetHeader(ow.keys)
table.AppendBulk(ow.values)
table.AppendBulk(convertInterfaceToString(ow.values))
table.Render()
}

func convertInterfaceToString(values [][]interface{}) [][]string {
result := [][]string{}
for _, valuesRow := range values {
row := []string{}
for _, field := range valuesRow {
row = append(row, fmt.Sprintf("%v", field))
}

result = append(result, row)
}
return result
}
22 changes: 14 additions & 8 deletions component/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func TestNewOutputWriterNonStrings(t *testing.T) {
output := b.String()
require.NotNil(t, output)

// extra leading newline for better formatting
// leading newline, added for readability, should be trimmed during compare
// note: the trailing spaces in this string are intentional
expected := `
A B C
Expand All @@ -296,11 +296,13 @@ func TestNewOutputWriterYAMLNonStrings(t *testing.T) {
output := b.String()
require.NotNil(t, output)

// extra leading newline for better formatting
// leading newline, added for readability, should be trimmed during compare
expected := `
- a: "1"
b: map[b:bar f:foo]
c: "2"
b:
b: bar
f: foo
c: 2
`
require.Equal(t, expected[1:], output)
}
Expand All @@ -315,15 +317,19 @@ func TestNewOutputWriterJSONNonStrings(t *testing.T) {
output := b.String()
require.NotNil(t, output)

// extra leading newline for better formatting
// leading newline, added for readability, should be trimmed during compare
expected := `
[
{
"a": "1",
"b": "map[b:bar f:foo]",
"c": "2"
"b": {
"b": "bar",
"f": "foo"
},
"c": 2
}
]`

require.Equal(t, expected[1:], output)
}

Expand All @@ -338,7 +344,7 @@ func TestNewOutputWriterTableListNonStrings(t *testing.T) {
output := b.String()
require.NotNil(t, output)

// extra leading newline for better formatting
// leading newline, added for readability, should be trimmed during compare
expected := `
A: 1, 3
B: map[b:bar f:foo], 4
Expand Down

0 comments on commit 72bf81c

Please sign in to comment.