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 particular useful when rendering container values like
maps/lists in their valid YAML/JSON forms.
  • Loading branch information
vuil committed Sep 29, 2023
1 parent cbdebba commit 4644106
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 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, item...)
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
}
14 changes: 10 additions & 4 deletions component/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,10 @@ func TestNewOutputWriterYAMLNonStrings(t *testing.T) {
// extra leading newline for better formatting
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 @@ -320,10 +322,14 @@ func TestNewOutputWriterJSONNonStrings(t *testing.T) {
[
{
"a": "1",
"b": "map[b:bar f:foo]",
"c": "2"
"b": {
"b": "bar",
"f": "foo"
},
"c": 2
}
]`

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

Expand Down

0 comments on commit 4644106

Please sign in to comment.