Skip to content

feat(cli): add json output to coder speedtest #13475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
WIP
  • Loading branch information
ethanndickson committed Jun 5, 2024
commit 2a1eebe96530afb4e9088d06208c7fe5f3d59a80
21 changes: 18 additions & 3 deletions cli/cliui/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func Table() table.Writer {
return tableWriter
}

// This type can be supplied as part of a slice to DisplayTable or to a `TableFormat` `Format` call to render a separator
// Trailing or leading separators are ignored by go-pretty
// e.g. `[]any{someRow, TableSeparator, someRow}`
type TableSeparator struct{}

// filterTableColumns returns configurations to hide columns
Expand Down Expand Up @@ -49,8 +52,11 @@ func filterTableColumns(header table.Row, columns []string) []table.ColumnConfig
return columnConfigs
}

// DisplayTable renders a table as a string. The input argument must be a slice
// of structs. At least one field in the struct must have a `table:""` tag
// DisplayTable renders a table as a string. The input argument can be:
// - a struct slice
// - an interface slice, where the first element is a struct, and all other elements are of the same type, or a TableSeperator
//
// At least one field in the struct must have a `table:""` tag
// containing the name of the column in the outputted table.
//
// If `sort` is not specified, the field with the `table:"$NAME,default_sort"`
Expand All @@ -70,9 +76,18 @@ func DisplayTable(out any, sort string, filterColumns []string) (string, error)
if v.Kind() != reflect.Slice {
return "", xerrors.Errorf("DisplayTable called with a non-slice type")
}
var fst reflect.Type
if v.Type().Elem().Kind() == reflect.Interface {
if v.Len() == 0 {
return "", xerrors.Errorf("DisplayTable called with empty interface slice")
}
fst = reflect.Indirect(reflect.ValueOf(v.Index(0).Interface())).Type()
} else {
fst = v.Type().Elem()
}

// Get the list of table column headers.
headersRaw, defaultSort, err := typeToTableHeaders(v.Type().Elem(), true)
headersRaw, defaultSort, err := typeToTableHeaders(fst, true)
if err != nil {
return "", xerrors.Errorf("get table headers recursively for type %q: %w", v.Type().Elem().String(), err)
}
Expand Down
44 changes: 36 additions & 8 deletions cli/cliui/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,42 @@ Alice 25
compareTables(t, expected, out)
})

// This test ensures we can display dynamically typed slices
t.Run("Interfaces", func(t *testing.T) {
t.Parallel()

in := []any{tableTest1{}}
out, err := cliui.DisplayTable(in, "", nil)
t.Log("rendered table:\n" + out)
require.NoError(t, err)
other := []tableTest1{{}}
expected, err := cliui.DisplayTable(other, "", nil)
require.NoError(t, err)
compareTables(t, expected, out)
})

t.Run("WithSeparator", func(t *testing.T) {
t.Parallel()
expected := `
NAME AGE ROLES SUB 1 NAME SUB 1 AGE SUB 2 NAME SUB 2 AGE SUB 3 INNER NAME SUB 3 INNER AGE SUB 4 TIME TIME PTR
bar 20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil>
-------------------------------------------------------------------------------------------------------------------------------------------------------------
baz 30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil>
-------------------------------------------------------------------------------------------------------------------------------------------------------------
foo 10 [a b c] foo1 11 foo2 12 foo3 13 {foo4 14 } 2022-08-02T15:49:10Z 2022-08-02T15:49:10Z
`

var inlineIn []any
for _, v := range in {
inlineIn = append(inlineIn, v)
inlineIn = append(inlineIn, cliui.TableSeparator{})
}
out, err := cliui.DisplayTable(inlineIn, "", nil)
t.Log("rendered table:\n" + out)
require.NoError(t, err)
compareTables(t, expected, out)
})

// This test ensures that safeties against invalid use of `table` tags
// causes errors (even without data).
t.Run("Errors", func(t *testing.T) {
Expand Down Expand Up @@ -255,14 +291,6 @@ Alice 25
_, err := cliui.DisplayTable(in, "", nil)
require.Error(t, err)
})

t.Run("WithData", func(t *testing.T) {
t.Parallel()

in := []any{tableTest1{}}
_, err := cliui.DisplayTable(in, "", nil)
require.Error(t, err)
})
})

t.Run("NotStruct", func(t *testing.T) {
Expand Down