From f948516369ffcd33ed68d8dd95ffe3a120142e79 Mon Sep 17 00:00:00 2001 From: Thomas NJ Shadwell Date: Tue, 26 Feb 2013 19:43:45 +0000 Subject: [PATCH 1/2] revert previous mis-commits, add ability to allow deeper pretty-printing after an error or Stringer interface is encountered. --- spew/common.go | 15 +++++++++++++++ spew/config.go | 40 +++++++--------------------------------- 2 files changed, 22 insertions(+), 33 deletions(-) diff --git a/spew/common.go b/spew/common.go index 6b8bfec..11ec612 100644 --- a/spew/common.go +++ b/spew/common.go @@ -150,11 +150,26 @@ func handleMethods(cs *ConfigState, w io.Writer, v reflect.Value) (handled bool) switch iface := viface.(type) { case error: defer catchPanic(w, v) + if cs.ContinueOnMethod { + w.Write(append(openParenBytes, []byte(iface.Error())...)) + w.Write(closeParenBytes) + w.Write(spaceBytes) + + return false + } + w.Write([]byte(iface.Error())) return true case fmt.Stringer: defer catchPanic(w, v) + if cs.ContinueOnMethod { + w.Write(append(openParenBytes, []byte(iface.String())...)) + w.Write(closeParenBytes) + w.Write(spaceBytes) + + return false + } w.Write([]byte(iface.String())) return true } diff --git a/spew/config.go b/spew/config.go index 41b2bc8..6d5501e 100644 --- a/spew/config.go +++ b/spew/config.go @@ -62,6 +62,13 @@ type ConfigState struct { // interface with a pointer receiver should not be mutating their state // inside these interface methods. DisablePointerMethods bool + + //ContinueOnMethod specifies whether recursion should stop once + //a Stringer or an error interface is encountered. + // + //It defaults to false, meaning that it does not pretty-print + //the internals of Stringers or errors. + ContinueOnMethod bool } // Config is the active configuration of the top-level functions. @@ -151,39 +158,6 @@ func (c *ConfigState) Println(a ...interface{}) (n int, err error) { return fmt.Println(c.convertArgs(a)...) } -// Sprint is a wrapper for fmt.Sprint that treats each argument as if it were -// passed with a Formatter interface returned by c.NewFormatter. It returns -// the resulting string. See NewFormatter for formatting details. -// -// This function is shorthand for the following syntax: -// -// fmt.Sprint(c.NewFormatter(a), c.NewFormatter(b)) -func (c *ConfigState) Sprint(a ...interface{}) string { - return fmt.Sprint(c.convertArgs(a)...) -} - -// Sprintf is a wrapper for fmt.Sprintf that treats each argument as if it were -// passed with a Formatter interface returned by c.NewFormatter. It returns -// the resulting string. See NewFormatter for formatting details. -// -// This function is shorthand for the following syntax: -// -// fmt.Sprintf(format, c.NewFormatter(a), c.NewFormatter(b)) -func (c *ConfigState) Sprintf(format string, a ...interface{}) string { - return fmt.Sprintf(format, c.convertArgs(a)...) -} - -// Sprintln is a wrapper for fmt.Sprintln that treats each argument as if it -// were passed with a Formatter interface returned by c.NewFormatter. It -// returns the resulting string. See NewFormatter for formatting details. -// -// This function is shorthand for the following syntax: -// -// fmt.Sprintln(c.NewFormatter(a), c.NewFormatter(b)) -func (c *ConfigState) Sprintln(a ...interface{}) string { - return fmt.Sprintln(c.convertArgs(a)...) -} - /* NewFormatter returns a custom formatter that satisfies the fmt.Formatter interface. As a result, it integrates cleanly with standard fmt package From 6d6046a9de11932943e5ef05d72c0ee2f02f1797 Mon Sep 17 00:00:00 2001 From: Thomas NJ Shadwell Date: Tue, 26 Feb 2013 19:48:56 +0000 Subject: [PATCH 2/2] Re-added accidentally removed lines --- spew/config.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/spew/config.go b/spew/config.go index 6d5501e..ea521cc 100644 --- a/spew/config.go +++ b/spew/config.go @@ -158,6 +158,39 @@ func (c *ConfigState) Println(a ...interface{}) (n int, err error) { return fmt.Println(c.convertArgs(a)...) } +// Sprint is a wrapper for fmt.Sprint that treats each argument as if it were +// passed with a Formatter interface returned by c.NewFormatter. It returns +// the resulting string. See NewFormatter for formatting details. +// +// This function is shorthand for the following syntax: +// +// fmt.Sprint(c.NewFormatter(a), c.NewFormatter(b)) +func (c *ConfigState) Sprint(a ...interface{}) string { + return fmt.Sprint(c.convertArgs(a)...) +} + +// Sprintf is a wrapper for fmt.Sprintf that treats each argument as if it were +// passed with a Formatter interface returned by c.NewFormatter. It returns +// the resulting string. See NewFormatter for formatting details. +// +// This function is shorthand for the following syntax: +// +// fmt.Sprintf(format, c.NewFormatter(a), c.NewFormatter(b)) +func (c *ConfigState) Sprintf(format string, a ...interface{}) string { + return fmt.Sprintf(format, c.convertArgs(a)...) +} + +// Sprintln is a wrapper for fmt.Sprintln that treats each argument as if it +// were passed with a Formatter interface returned by c.NewFormatter. It +// returns the resulting string. See NewFormatter for formatting details. +// +// This function is shorthand for the following syntax: +// +// fmt.Sprintln(c.NewFormatter(a), c.NewFormatter(b)) +func (c *ConfigState) Sprintln(a ...interface{}) string { + return fmt.Sprintln(c.convertArgs(a)...) +} + /* NewFormatter returns a custom formatter that satisfies the fmt.Formatter interface. As a result, it integrates cleanly with standard fmt package