From 4143c5b303c1488310f26b988ae9d173480b3afc Mon Sep 17 00:00:00 2001 From: piaoxue1949 <891837834@qq.com> Date: Thu, 11 Jul 2024 10:03:36 +0800 Subject: [PATCH] fix: dumper.printRValue panic when unexported fields in the structure are of type time.Time; (#183) Co-authored-by: yujin <91837834@qq.com> --- dump/dumper.go | 7 ++++++- dump/dumper_test.go | 19 +++++++++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/dump/dumper.go b/dump/dumper.go index bb2d69e02..0861d8cbd 100644 --- a/dump/dumper.go +++ b/dump/dumper.go @@ -279,7 +279,12 @@ func (d *Dumper) printRValue(t reflect.Type, v reflect.Value) { // up: special handel time.Time struct if t == timeType { - timeStr := v.Interface().(time.Time).Format(time.RFC3339) + var timeStr string + if v.CanInterface() { + timeStr = v.Interface().(time.Time).Format(time.RFC3339) + } else { + timeStr = v.String() + } d.printf("time.Time(%s),\n", d.ColorTheme.string(timeStr)) break } diff --git a/dump/dumper_test.go b/dump/dumper_test.go index 525c43398..55778701b 100644 --- a/dump/dumper_test.go +++ b/dump/dumper_test.go @@ -7,6 +7,7 @@ import ( "os" "reflect" "testing" + "time" "unsafe" "github.com/gookit/color" @@ -425,17 +426,19 @@ func TestStruct_WithNested(_ *testing.T) { func TestDumper_Dump_userType(_ *testing.T) { type testSt struct { - name string - mod fs.FileMode - Mod2 fs.FileMode - Age int + name string + mod fs.FileMode + Mod2 fs.FileMode + Age int + createdAt time.Time } st := testSt{ - name: "inhere", - mod: 0777, - Mod2: 0775, - Age: 23, + name: "inhere", + mod: 0777, + Mod2: 0775, + Age: 23, + createdAt: time.Now(), } fmt.Println("------ use dumper ------")