@@ -29,11 +29,12 @@ const (
2929
3030// Default configuration values for the Dumper.
3131const (
32- defaultMaxDepth = 15
33- defaultMaxItems = 100
34- defaultMaxStringLen = 100000
35- defaultMaxStackDepth = 10
36- initialCallerSkip = 2
32+ defaultDisableMethods = false
33+ defaultMaxDepth = 15
34+ defaultMaxItems = 100
35+ defaultMaxStringLen = 100000
36+ defaultMaxStackDepth = 10
37+ initialCallerSkip = 2
3738)
3839
3940// defaultDumper is the default Dumper instance used by Dump and DumpStr functions.
@@ -86,6 +87,7 @@ type Dumper struct {
8687 maxStringLen int
8788 writer io.Writer
8889 skippedStackFrames int
90+ disableMethods bool
8991
9092 // callerFn is used to get the caller information.
9193 // It defaults to [runtime.Caller], it is here to be overridden for testing purposes.
@@ -149,15 +151,26 @@ func WithSkipStackFrames(n int) Option {
149151 }
150152}
151153
154+ // WithDisableMethods will determine if interface methods such as the stringer
155+ // interface should be called for types implementing it. This can be useful if
156+ // you want to see the internals of types implementing a method.
157+ func WithDisableMethods (b bool ) Option {
158+ return func (d * Dumper ) * Dumper {
159+ d .disableMethods = b
160+ return d
161+ }
162+ }
163+
152164// NewDumper creates a new Dumper with the given options applied.
153165// Defaults are used for any setting not overridden.
154166func NewDumper (opts ... Option ) * Dumper {
155167 d := & Dumper {
156- maxDepth : defaultMaxDepth ,
157- maxItems : defaultMaxItems ,
158- maxStringLen : defaultMaxStringLen ,
159- writer : os .Stdout ,
160- callerFn : runtime .Caller ,
168+ maxDepth : defaultMaxDepth ,
169+ maxItems : defaultMaxItems ,
170+ maxStringLen : defaultMaxStringLen ,
171+ disableMethods : defaultDisableMethods ,
172+ writer : os .Stdout ,
173+ callerFn : runtime .Caller ,
161174 }
162175 for _ , opt := range opts {
163176 d = opt (d )
@@ -404,7 +417,7 @@ func (d *Dumper) printValue(tw *tabwriter.Writer, v reflect.Value, indent int, v
404417 return
405418 }
406419
407- if s := asStringer (v ); s != "" {
420+ if s := d . asStringer (v ); s != "" {
408421 fmt .Fprint (tw , s )
409422 return
410423 }
@@ -455,7 +468,7 @@ func (d *Dumper) printValue(tw *tabwriter.Writer, v reflect.Value, indent int, v
455468 }
456469 indentPrint (tw , indent + 1 , colorize (colorYellow , symbol )+ field .Name )
457470 fmt .Fprint (tw , " => " )
458- if s := asStringer (fieldVal ); s != "" {
471+ if s := d . asStringer (fieldVal ); s != "" {
459472 fmt .Fprint (tw , s )
460473 } else {
461474 d .printValue (tw , fieldVal , indent + 1 , visited )
@@ -535,7 +548,11 @@ func (d *Dumper) printValue(tw *tabwriter.Writer, v reflect.Value, indent int, v
535548}
536549
537550// asStringer checks if the value implements fmt.Stringer and returns its string representation.
538- func asStringer (v reflect.Value ) string {
551+ func (d * Dumper ) asStringer (v reflect.Value ) string {
552+ if d .disableMethods {
553+ return ""
554+ }
555+
539556 val := v
540557 if ! val .CanInterface () {
541558 val = forceExported (val )
0 commit comments