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 "" return ""
} }
if fieldValue.Kind() == reflect.Pointer && !fieldValue.IsNil() {
fieldValue = fieldValue.Elem()
}
switch fieldValue.Kind() { switch fieldValue.Kind() {
case reflect.Map: case reflect.Map:
fallthrough fallthrough

View File

@ -2,6 +2,7 @@ package table
import ( import (
"bytes" "bytes"
"fmt"
"strings" "strings"
"testing" "testing"
@ -10,9 +11,15 @@ import (
) )
type dummyItem struct { type dummyItem struct {
MyString string MyString string
MyInt int MyInt int
MySub subItem MySub subItem
MySubPtr *subItem
MyPointStr *string
}
func (d *dummyItem) MyMethod() string {
return fmt.Sprintf("myMethod: %s | %d", d.MyString, d.MyInt)
} }
type subItem struct { type subItem struct {
@ -33,6 +40,10 @@ var dummyItems = []any{
MySub: subItem{ MySub: subItem{
MyBool: true, MyBool: true,
}, },
MySubPtr: &subItem{
MyBool: false,
},
MyPointStr: strPtr("Test"),
}, },
} }
@ -45,12 +56,12 @@ func TestWriterNoHints(t *testing.T) {
t.Fatalf("%+v", errors.WithStack(err)) t.Fatalf("%+v", errors.WithStack(err))
} }
expected := `+----------+-------+------------------+ expected := `+----------+-------+------------------+------------------+------------+
| MYSTRING | MYINT | MYSUB | | MYSTRING | MYINT | MYSUB | MYSUBPTR | MYPOINTSTR |
+----------+-------+------------------+ +----------+-------+------------------+------------------+------------+
| Foo | 1 | {"MyBool":false} | | Foo | 1 | {"MyBool":false} | <nil> | <nil> |
| Bar | 0 | {"MyBool":true} | | Bar | 0 | {"MyBool":true} | {"MyBool":false} | Test |
+----------+-------+------------------+` +----------+-------+------------------+------------------+------------+`
if e, g := strings.TrimSpace(expected), strings.TrimSpace(buf.String()); e != g { if e, g := strings.TrimSpace(expected), strings.TrimSpace(buf.String()); e != g {
t.Errorf("buf.String(): expected \n%v\ngot\n%v", 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) t.Errorf("buf.String(): expected \n%v\ngot\n%v", e, g)
} }
} }
func strPtr(str string) *string {
return &str
}