Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(cli/cliui): handle typed nil and null time in tables#15984

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall commits
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
15 changes: 14 additions & 1 deletioncli/cliui/table.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,8 @@ import (
"github.com/fatih/structtag"
"github.com/jedib0t/go-pretty/v6/table"
"golang.org/x/xerrors"

"github.com/coder/coder/v2/codersdk"
)

// Table creates a new table with standardized styles.
Expand DownExpand Up@@ -195,6 +197,12 @@ func renderTable(out any, sort string, headers table.Row, filterColumns []string
if val != nil {
v = val.Format(time.RFC3339)
}
case codersdk.NullTime:
if val.Valid {
v = val.Time.Format(time.RFC3339)
} else {
v = nil
}
case *int64:
if val != nil {
v = *val
Expand All@@ -204,8 +212,13 @@ func renderTable(out any, sort string, headers table.Row, filterColumns []string
v = val.String()
}
case fmt.Stringer:
if val != nil {
// Protect against typed nils since fmt.Stringer is an interface.
vv := reflect.ValueOf(v)
nilPtr := vv.Kind() == reflect.Ptr && vv.IsNil()
if val != nil && !nilPtr {
v = val.String()
} else if nilPtr {
v = nil
}
}

Expand Down
65 changes: 38 additions & 27 deletionscli/cliui/table_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
package cliui_test

import (
"database/sql"
"fmt"
"log"
"strings"
Expand All@@ -11,6 +12,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
)

type stringWrapper struct {
Expand All@@ -24,18 +26,20 @@ func (s stringWrapper) String() string {
}

type tableTest1 struct {
Name string `table:"name,default_sort"`
NotIncluded string // no table tag
Age int `table:"age"`
Roles []string `table:"roles"`
Sub1 tableTest2 `table:"sub_1,recursive"`
Sub2 *tableTest2 `table:"sub_2,recursive"`
Sub3 tableTest3 `table:"sub 3,recursive"`
Sub4 tableTest2 `table:"sub 4"` // not recursive
Name string `table:"name,default_sort"`
AltName *stringWrapper `table:"alt_name"`
NotIncluded string // no table tag
Age int `table:"age"`
Roles []string `table:"roles"`
Sub1 tableTest2 `table:"sub_1,recursive"`
Sub2 *tableTest2 `table:"sub_2,recursive"`
Sub3 tableTest3 `table:"sub 3,recursive"`
Sub4 tableTest2 `table:"sub 4"` // not recursive

// Types with special formatting.
Time time.Time `table:"time"`
TimePtr *time.Time `table:"time_ptr"`
Time time.Time `table:"time"`
TimePtr *time.Time `table:"time_ptr"`
NullTime codersdk.NullTime `table:"null_time"`
}

type tableTest2 struct {
Expand All@@ -62,9 +66,10 @@ func Test_DisplayTable(t *testing.T) {
// Not sorted by name or age to test sorting.
in := []tableTest1{
{
Name: "bar",
Age: 20,
Roles: []string{"a"},
Name: "bar",
AltName: &stringWrapper{str: "bar alt"},
Age: 20,
Roles: []string{"a"},
Sub1: tableTest2{
Name: stringWrapper{str: "bar1"},
Age: 21,
Expand All@@ -82,6 +87,12 @@ func Test_DisplayTable(t *testing.T) {
},
Time: someTime,
TimePtr: nil,
NullTime: codersdk.NullTime{
NullTime: sql.NullTime{
Time: someTime,
Valid: true,
},
},
},
{
Name: "foo",
Expand DownExpand Up@@ -138,10 +149,10 @@ func Test_DisplayTable(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
bar20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil>
baz30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil>
foo10 [a, b, c] foo1 11 foo2 12 foo3 13 {foo4 14 } 2022-08-02T15:49:10Z 2022-08-02T15:49:10Z
NAMEALT NAMEAGE 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 NULL TIME
barbar alt20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil> 2022-08-02T15:49:10Z
baz<nil>30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil> <nil>
foo<nil>10 [a, b, c] foo1 11 foo2 12 foo3 13 {foo4 14 } 2022-08-02T15:49:10Z 2022-08-02T15:49:10Z <nil>
`

// Test with non-pointer values.
Expand All@@ -165,10 +176,10 @@ foo 10 [a, b, c] foo1 11 foo2 12 foo3
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
foo10 [a, b, c] foo1 11 foo2 12 foo3 13 {foo4 14 } 2022-08-02T15:49:10Z 2022-08-02T15:49:10Z
bar20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil>
baz30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil>
NAMEALT NAMEAGE 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 NULL TIME
foo<nil>10 [a, b, c] foo1 11 foo2 12 foo3 13 {foo4 14 } 2022-08-02T15:49:10Z 2022-08-02T15:49:10Z <nil>
barbar alt20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil> 2022-08-02T15:49:10Z
baz<nil>30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil> <nil>
`

out, err := cliui.DisplayTable(in, "age", nil)
Expand DownExpand Up@@ -235,12 +246,12 @@ Alice 25
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
bar20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil>
---------------------------------------------------------------------------------------------------------------------------------------------------------------
baz30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil>
---------------------------------------------------------------------------------------------------------------------------------------------------------------
foo10 [a, b, c] foo1 11 foo2 12 foo3 13 {foo4 14 } 2022-08-02T15:49:10Z 2022-08-02T15:49:10Z
NAMEALT NAMEAGE 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 NULL TIME
barbar alt20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil> 2022-08-02T15:49:10Z
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
baz<nil>30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil> <nil>
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
foo<nil>10 [a, b, c] foo1 11 foo2 12 foo3 13 {foo4 14 } 2022-08-02T15:49:10Z 2022-08-02T15:49:10Z <nil>
`

var inlineIn []any
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp