Skip to content

fix(cli/cliui): handle ptr to custom type #16200

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 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions cli/cliui/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ func renderTable(out any, sort string, headers table.Row, filterColumns []string
} else {
v = nil
}
case *string:
if val != nil {
v = *val
}
case *int64:
if val != nil {
v = *val
Expand Down Expand Up @@ -240,6 +244,18 @@ func renderTable(out any, sort string, headers table.Row, filterColumns []string
}
}

// Last resort, just get the interface value to avoid printing
// pointer values. For example, if we have a `*MyType("value")`
// which is defined as `type MyType string`, we want to print
// the string value, not the pointer.
if v != nil {
vv := reflect.ValueOf(v)
for vv.Kind() == reflect.Ptr && !vv.IsNil() {
vv = vv.Elem()
}
v = vv.Interface()
}

rowSlice[i] = v
}

Expand Down
33 changes: 19 additions & 14 deletions cli/cliui/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func (s stringWrapper) String() string {
return s.str
}

type myString string

type tableTest1 struct {
Name string `table:"name,default_sort"`
AltName *stringWrapper `table:"alt_name"`
Expand All @@ -40,6 +42,7 @@ type tableTest1 struct {
Time time.Time `table:"time"`
TimePtr *time.Time `table:"time_ptr"`
NullTime codersdk.NullTime `table:"null_time"`
MyString *myString `table:"my_string"`
}

type tableTest2 struct {
Expand All @@ -62,6 +65,7 @@ func Test_DisplayTable(t *testing.T) {
t.Parallel()

someTime := time.Date(2022, 8, 2, 15, 49, 10, 0, time.UTC)
myStr := myString("my string")

// Not sorted by name or age to test sorting.
in := []tableTest1{
Expand Down Expand Up @@ -93,6 +97,7 @@ func Test_DisplayTable(t *testing.T) {
Valid: true,
},
},
MyString: &myStr,
},
{
Name: "foo",
Expand Down Expand Up @@ -149,10 +154,10 @@ func Test_DisplayTable(t *testing.T) {
t.Parallel()

expected := `
NAME ALT 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 NULL TIME
bar bar alt 20 [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>
NAME ALT 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 NULL TIME MY STRING
bar bar alt 20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil> 2022-08-02T15:49:10Z my string
baz <nil> 30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil> <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> <nil>
`

// Test with non-pointer values.
Expand All @@ -176,10 +181,10 @@ foo <nil> 10 [a, b, c] foo1 11 foo2 12 fo
t.Parallel()

expected := `
NAME ALT 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 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>
bar bar alt 20 [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>
NAME ALT 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 NULL TIME MY STRING
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> <nil>
bar bar alt 20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil> 2022-08-02T15:49:10Z my string
baz <nil> 30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil> <nil> <nil>
`

out, err := cliui.DisplayTable(in, "age", nil)
Expand Down Expand Up @@ -246,12 +251,12 @@ Alice 25
t.Run("WithSeparator", func(t *testing.T) {
t.Parallel()
expected := `
NAME ALT 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 NULL TIME
bar bar alt 20 [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>
NAME ALT 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 NULL TIME MY STRING
bar bar alt 20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil> 2022-08-02T15:49:10Z my string
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
baz <nil> 30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil> <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> <nil>
`

var inlineIn []any
Expand Down
Loading