From cd6857b8dd56ec2f18a2d2c8e8a11e61ba73d0c2 Mon Sep 17 00:00:00 2001 From: William Petit Date: Mon, 26 Feb 2024 17:02:28 +0100 Subject: [PATCH] fix(cli/format): correctly display pointer values --- cli/format/table/prop.go | 4 ++++ cli/format/table/writer_test.go | 33 ++++++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/cli/format/table/prop.go b/cli/format/table/prop.go index 92e6839..8293384 100644 --- a/cli/format/table/prop.go +++ b/cli/format/table/prop.go @@ -40,6 +40,10 @@ func getFieldValue(obj any, name string) string { return "" } + if fieldValue.Kind() == reflect.Pointer && !fieldValue.IsNil() { + fieldValue = fieldValue.Elem() + } + switch fieldValue.Kind() { case reflect.Map: fallthrough diff --git a/cli/format/table/writer_test.go b/cli/format/table/writer_test.go index a7b91db..fb82716 100644 --- a/cli/format/table/writer_test.go +++ b/cli/format/table/writer_test.go @@ -2,6 +2,7 @@ package table import ( "bytes" + "fmt" "strings" "testing" @@ -10,9 +11,15 @@ import ( ) type dummyItem struct { - MyString string - MyInt int - MySub subItem + MyString string + MyInt int + MySub subItem + MySubPtr *subItem + MyPointStr *string +} + +func (d *dummyItem) MyMethod() string { + return fmt.Sprintf("myMethod: %s | %d", d.MyString, d.MyInt) } type subItem struct { @@ -33,6 +40,10 @@ var dummyItems = []any{ MySub: subItem{ MyBool: true, }, + MySubPtr: &subItem{ + MyBool: false, + }, + MyPointStr: strPtr("Test"), }, } @@ -45,12 +56,12 @@ func TestWriterNoHints(t *testing.T) { t.Fatalf("%+v", errors.WithStack(err)) } - expected := `+----------+-------+------------------+ -| MYSTRING | MYINT | MYSUB | -+----------+-------+------------------+ -| Foo | 1 | {"MyBool":false} | -| Bar | 0 | {"MyBool":true} | -+----------+-------+------------------+` + expected := `+----------+-------+------------------+------------------+------------+ +| MYSTRING | MYINT | MYSUB | MYSUBPTR | MYPOINTSTR | ++----------+-------+------------------+------------------+------------+ +| Foo | 1 | {"MyBool":false} | | | +| Bar | 0 | {"MyBool":true} | {"MyBool":false} | Test | ++----------+-------+------------------+------------------+------------+` if e, g := strings.TrimSpace(expected), strings.TrimSpace(buf.String()); e != g { t.Errorf("buf.String(): expected \n%v\ngot\n%v", e, g) @@ -84,3 +95,7 @@ func TestWriterWithPropHints(t *testing.T) { t.Errorf("buf.String(): expected \n%v\ngot\n%v", e, g) } } + +func strPtr(str string) *string { + return &str +}