Skip to content

Commit 8b57af8

Browse files
committed
feat: invoke .String() if exists
1 parent 43b61c4 commit 8b57af8

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

godump.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@ func printValue(tw *tabwriter.Writer, v reflect.Value, indent int, visited map[u
175175
fmt.Fprint(tw, colorize(colorGray, "<invalid>"))
176176
return
177177
}
178+
// If value implements fmt.Stringer, use it
179+
if v.CanInterface() {
180+
// Skip using String() for reflect.Value so we can dump internals
181+
if _, skip := v.Interface().(reflect.Value); !skip {
182+
if s, ok := v.Interface().(fmt.Stringer); ok {
183+
fmt.Fprint(tw, colorize(colorLime, s.String())+colorize(colorGray, " #"+v.Type().String()))
184+
return
185+
}
186+
}
187+
}
188+
178189
// Custom handling for time.Time
179190
if v.Type().PkgPath() == "time" && v.Type().Name() == "Time" {
180191
if t, ok := v.Interface().(interface{ String() string }); ok {

0 commit comments

Comments
 (0)