Skip to content

Commit 2913fe8

Browse files
authored
fix(cli/cliui): handle ptr to custom type (coder#16200)
1 parent 4ba0b39 commit 2913fe8

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

cli/cliui/table.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ func renderTable(out any, sort string, headers table.Row, filterColumns []string
203203
} else {
204204
v = nil
205205
}
206+
case *string:
207+
if val != nil {
208+
v = *val
209+
}
206210
case *int64:
207211
if val != nil {
208212
v = *val
@@ -240,6 +244,18 @@ func renderTable(out any, sort string, headers table.Row, filterColumns []string
240244
}
241245
}
242246

247+
// Last resort, just get the interface value to avoid printing
248+
// pointer values. For example, if we have a `*MyType("value")`
249+
// which is defined as `type MyType string`, we want to print
250+
// the string value, not the pointer.
251+
if v != nil {
252+
vv := reflect.ValueOf(v)
253+
for vv.Kind() == reflect.Ptr && !vv.IsNil() {
254+
vv = vv.Elem()
255+
}
256+
v = vv.Interface()
257+
}
258+
243259
rowSlice[i] = v
244260
}
245261

cli/cliui/table_test.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ func (s stringWrapper) String() string {
2525
return s.str
2626
}
2727

28+
type myString string
29+
2830
type tableTest1 struct {
2931
Name string `table:"name,default_sort"`
3032
AltName *stringWrapper `table:"alt_name"`
@@ -40,6 +42,7 @@ type tableTest1 struct {
4042
Time time.Time `table:"time"`
4143
TimePtr *time.Time `table:"time_ptr"`
4244
NullTime codersdk.NullTime `table:"null_time"`
45+
MyString *myString `table:"my_string"`
4346
}
4447

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

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

6670
// Not sorted by name or age to test sorting.
6771
in := []tableTest1{
@@ -93,6 +97,7 @@ func Test_DisplayTable(t *testing.T) {
9397
Valid: true,
9498
},
9599
},
100+
MyString: &myStr,
96101
},
97102
{
98103
Name: "foo",
@@ -149,10 +154,10 @@ func Test_DisplayTable(t *testing.T) {
149154
t.Parallel()
150155

151156
expected := `
152-
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
153-
bar bar alt 20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil> 2022-08-02T15:49:10Z
154-
baz <nil> 30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil> <nil>
155-
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>
157+
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
158+
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
159+
baz <nil> 30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil> <nil> <nil>
160+
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>
156161
`
157162

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

178183
expected := `
179-
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
180-
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>
181-
bar bar alt 20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil> 2022-08-02T15:49:10Z
182-
baz <nil> 30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil> <nil>
184+
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
185+
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>
186+
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
187+
baz <nil> 30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil> <nil> <nil>
183188
`
184189

185190
out, err := cliui.DisplayTable(in, "age", nil)
@@ -246,12 +251,12 @@ Alice 25
246251
t.Run("WithSeparator", func(t *testing.T) {
247252
t.Parallel()
248253
expected := `
249-
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
250-
bar bar alt 20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil> 2022-08-02T15:49:10Z
251-
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
252-
baz <nil> 30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil> <nil>
253-
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
254-
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>
254+
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
255+
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
256+
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
257+
baz <nil> 30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil> <nil> <nil>
258+
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
259+
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>
255260
`
256261

257262
var inlineIn []any

0 commit comments

Comments
 (0)