fix(cli/format): correctly display pointer values

This commit is contained in:
wpetit 2024-02-26 17:02:28 +01:00
parent 4a8add1d3d
commit cd6857b8dd
2 changed files with 28 additions and 9 deletions

View File

@ -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

View File

@ -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} | <nil> | <nil> |
| 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
}