Skip to content

Commit

Permalink
fix: dumper.printRValue panic when unexported fields in the structure…
Browse files Browse the repository at this point in the history
… are of type time.Time; (#183)

Co-authored-by: yujin <[email protected]>
  • Loading branch information
piaoxue1949 and yujin authored Jul 11, 2024
1 parent bfe7659 commit 4143c5b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
7 changes: 6 additions & 1 deletion dump/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
19 changes: 11 additions & 8 deletions dump/dumper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"reflect"
"testing"
"time"
"unsafe"

"github.com/gookit/color"
Expand Down Expand Up @@ -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 ------")
Expand Down

0 comments on commit 4143c5b

Please sign in to comment.