Skip to content

Commit

Permalink
👔 up: dump - update and fix the dump value Stringer logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 16, 2023
1 parent b3ce62f commit 0babcc7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
4 changes: 4 additions & 0 deletions dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dump

import (
"bytes"
"fmt"
"io"
"os"
"reflect"
Expand Down Expand Up @@ -43,6 +44,9 @@ var (
opts.Output = os.Stdout
opts.ShowFlag = Fnopos
})

// some type init
stringerType = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
)

// Theme color code/tag map for dump
Expand Down
12 changes: 6 additions & 6 deletions dump/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,11 @@ func (d *Dumper) printRValue(t reflect.Type, v reflect.Value) {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
intStr := strconv.FormatInt(v.Int(), 10)
intStr = d.ColorTheme.integer(intStr)
d.printf("%s(%s),%s\n", t.String(), intStr, d.rvStringer(v))
d.printf("%s(%s),%s\n", t.String(), intStr, d.rvStringer(t, v))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
intStr := strconv.FormatUint(v.Uint(), 10)
intStr = d.ColorTheme.integer(intStr)
d.printf("%s(%s),%s\n", t.String(), intStr, d.rvStringer(v))
d.printf("%s(%s),%s\n", t.String(), intStr, d.rvStringer(t, v))
case reflect.String:
strVal := d.ColorTheme.string(v.String())
lenTip := d.ColorTheme.valTip("#len=" + strconv.Itoa(v.Len()))
Expand Down Expand Up @@ -395,10 +395,10 @@ func (d *Dumper) checkCyclicRef(t reflect.Type, v reflect.Value) (goon bool) {
return true
}

func (d *Dumper) rvStringer(rv reflect.Value) string {
val := rv.Interface()
if s, ok := val.(fmt.Stringer); ok {
return d.ColorTheme.valTip(` #str: "` + s.String() + `"`)
func (d *Dumper) rvStringer(rt reflect.Type, rv reflect.Value) string {
// fmt.Println("Implements fmt.Stringer:", t.Implements(stringerType))
if rv.CanInterface() && rt.Implements(stringerType) {
return d.ColorTheme.valTip(` #str: "` + rv.Interface().(fmt.Stringer).String() + `"`)
}
return ""
}
Expand Down
24 changes: 24 additions & 0 deletions dump/dumper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dump
import (
"bytes"
"fmt"
"io/fs"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -419,3 +420,26 @@ func TestStruct_WithNested(t *testing.T) {
// Github: string("https://github.com/inhere"),
// }
}

func TestDumper_Dump_userType(t *testing.T) {
type testSt struct {
name string
mod fs.FileMode
Mod2 fs.FileMode
Age int
}

st := testSt{
name: "inhere",
mod: 0777,
Mod2: 0775,
Age: 23,
}

fmt.Println("------ use dumper ------")
P(st)
fmt.Println("------ use fmt.Println ------")
fmt.Println(st)
fmt.Println("------ use fmt.Printf ------")
fmt.Printf("%+v\n", st)
}

0 comments on commit 0babcc7

Please sign in to comment.