Implement support for %#v and %#+v in Formatter.

This commit implements feature request #3.  In particular, it allows the
formatter to respond to %#v and %#+v.  The # flag (%#v) adds type
information to the output and the combination of the # and + flags (%#+v)
adds both type information and pointer information.  This allows the
consumer a choice between displaying types, pointer information, or both.
This commit is contained in:
Dave Collins 2013-01-17 18:43:51 -06:00
parent c5fba05307
commit 1f81f22357
5 changed files with 124 additions and 62 deletions

View File

@ -70,7 +70,7 @@ var (
iBytes = []byte("i")
trueBytes = []byte("true")
falseBytes = []byte("false")
interfaceBytes = []byte("(interface {}) ")
interfaceBytes = []byte("(interface {})")
commaNewlineBytes = []byte(",\n")
newlineBytes = []byte("\n")
openBraceBytes = []byte("{")
@ -102,16 +102,6 @@ var (
// hexDigits is used to map a decimal value to a hex digit.
var hexDigits = "0123456789abcdef"
// unpackValue returns values inside of non-nil interfaces when possible.
// This is useful for data types like structs, arrays, slices, and maps which
// can contain varying types packed inside an interface.
func unpackValue(v reflect.Value) reflect.Value {
if v.Kind() == reflect.Interface && !v.IsNil() {
v = v.Elem()
}
return v
}
// catchPanic handles any panics that might occur during the handleMethods
// calls.
func catchPanic(w io.Writer, v reflect.Value) {

View File

@ -51,10 +51,13 @@ information use Dump or Fdump:
spew.Fdump(someWriter, myVar1, myVar2, ...)
Alternatively, if you would prefer to use format strings with a compacted inline
printing style, use the convenience wrappers Printf, Fprintf, etc with either
%v (most compact) or %+v (adds pointer addresses):
printing style, use the convenience wrappers Printf, Fprintf, etc with
%v (most compact), %+v (adds pointer addresses), %#v (adds types), or
%#+v (adds types and pointer addresses):
spew.Printf("myVar1: %v -- myVar2: %+v", myVar1, myVar2)
spew.Printf("myVar3: %#v -- myVar4: %#+v", myVar3, myVar4)
spew.Fprintf(someWriter, "myVar1: %v -- myVar2: %+v", myVar1, myVar2)
spew.Fprintf(someWriter, "myVar3: %#v -- myVar4: %#+v", myVar3, myVar4)
Configuration Options
@ -118,31 +121,40 @@ so that it integrates cleanly with standard fmt package printing functions. The
formatter is useful for inline printing of smaller data types similar to the
standard %v format specifier.
The spew formatter only responds to the %v and %+v verb combinations. Any other
variations such as %x, %q, and %#v will be sent to the the standard fmt package
for formatting. In addition, the spew formatter ignores the width and precision
arguments (however they will still work on the format specifiers spew does not
handle).
The custom formatter only responds to the %v (most compact), %+v (adds pointer
addresses), %#v (adds types), or %#+v (adds types and pointer addresses) verb
combinations. Any other verbs such as %x and %q will be sent to the the
standard fmt package for formatting. In addition, the custom formatter ignores
the width and precision arguments (however they will still work on the format
specifiers not handled by the custom formatter).
Custom Formatter Usage
The simplest way to make use of the spew custom formatter is to call one of the
convenience functions such as spew.Printf, spew.Println, or spew.Printf. The
functions have the exact same syntax you are most likely already familiar with:
functions have syntax you are most likely already familiar with:
spew.Printf("myVar1: %v -- myVar2: %+v", myVar1, myVar2)
spew.Printf("myVar3: %#v -- myVar4: %#+v", myVar3, myVar4)
spew.Println(myVar, myVar2)
spew.Fprintf(os.Stderr, "myVar1: %v -- myVar2: %+v", myVar1, myVar2)
spew.Fprintf(os.Stderr, "myVar3: %#v -- myVar4: %#+v", myVar3, myVar4)
See the Index for the full list convenience functions.
Sample Formatter Output
Double pointer to a uint8 via %v:
<**>5
Double pointer to a uint8:
%v: <**>5
%+v: <**>(0xf8400420d0->0xf8400420c8)5
%#v: (**uint8)5
%#+v: (**uint8)(0xf8400420d0->0xf8400420c8)5
Circular struct with a uint8 field and a pointer to itself via %+v:
{ui8:1 c:<*>(0xf84002d200){ui8:1 c:<*>(0xf84002d200)<shown>}}
Pointer to circular struct with a uint8 field and a pointer to itself:
%v: <*>{1 <*><shown>}
%+v: <*>(0xf84003e260){ui8:1 c:<*>(0xf84003e260)<shown>}
%#v: (*main.circular){ui8:(uint8)1 c:(*main.circular)<shown>}
%#+v: (*main.circular)(0xf84003e260){ui8:(uint8)1 c:(*main.circular)(0xf84003e260)<shown>}
See the Printf example for details on the setup of variables being shown
here.

View File

@ -45,6 +45,16 @@ func (d *dumpState) pad() {
d.w.Write(bytes.Repeat([]byte(d.cs.Indent), d.depth))
}
// unpackValue returns values inside of non-nil interfaces when possible.
// This is useful for data types like structs, arrays, slices, and maps which
// can contain varying types packed inside an interface.
func (d *dumpState) unpackValue(v reflect.Value) reflect.Value {
if v.Kind() == reflect.Interface && !v.IsNil() {
v = v.Elem()
}
return v
}
// dumpPtr handles formatting of pointers by indirecting them as necessary.
func (d *dumpState) dumpPtr(v reflect.Value) {
// Remove pointers at or below the current depth from map used to detect
@ -191,7 +201,7 @@ func (d *dumpState) dump(v reflect.Value) {
} else {
numEntries := v.Len()
for i := 0; i < numEntries; i++ {
d.dump(unpackValue(v.Index(i)))
d.dump(d.unpackValue(v.Index(i)))
if i < (numEntries - 1) {
d.w.Write(commaNewlineBytes)
} else {
@ -223,10 +233,10 @@ func (d *dumpState) dump(v reflect.Value) {
numEntries := v.Len()
keys := v.MapKeys()
for i, key := range keys {
d.dump(unpackValue(key))
d.dump(d.unpackValue(key))
d.w.Write(colonSpaceBytes)
d.ignoreNextPad = true
d.dump(unpackValue(v.MapIndex(key)))
d.dump(d.unpackValue(v.MapIndex(key)))
if i < (numEntries - 1) {
d.w.Write(commaNewlineBytes)
} else {
@ -253,7 +263,7 @@ func (d *dumpState) dump(v reflect.Value) {
d.w.Write([]byte(vtf.Name))
d.w.Write(colonSpaceBytes)
d.ignoreNextPad = true
d.dump(unpackValue(v.Field(i)))
d.dump(d.unpackValue(v.Field(i)))
if i < (numFields - 1) {
d.w.Write(commaNewlineBytes)
} else {
@ -289,6 +299,7 @@ func fdump(cs *ConfigState, w io.Writer, a ...interface{}) {
for _, arg := range a {
if arg == nil {
w.Write(interfaceBytes)
w.Write(spaceBytes)
w.Write(nilAngleBytes)
w.Write(newlineBytes)
continue

View File

@ -32,11 +32,12 @@ const supportedFlags = "0-+# "
// be used to get a new Formatter which can be used directly as arguments
// in standard fmt package printing calls.
type formatState struct {
value interface{}
depth int
pointers map[uintptr]int // Holds map of points and depth they were seen at
fs fmt.State
cs *ConfigState
value interface{}
fs fmt.State
depth int
pointers map[uintptr]int
ignoreNextType bool
cs *ConfigState
}
// buildDefaultFormat recreates the original format string without precision
@ -85,10 +86,24 @@ func (f *formatState) constructOrigFormat(verb rune) (format string) {
return format
}
// unpackValue returns values inside of non-nil interfaces when possible and
// ensures that types for values which have been unpacked from an interface
// are displayed when the show types flag is also set.
// This is useful for data types like structs, arrays, slices, and maps which
// can contain varying types packed inside an interface.
func (f *formatState) unpackValue(v reflect.Value) reflect.Value {
if v.Kind() == reflect.Interface && !v.IsNil() {
f.ignoreNextType = false
v = v.Elem()
}
return v
}
// formatPtr handles formatting of pointers by indirecting them as necessary.
func (f *formatState) formatPtr(v reflect.Value) {
// Display nil if top level pointer is nil.
if v.IsNil() {
showTypes := f.fs.Flag('#')
if v.IsNil() && (!showTypes || f.ignoreNextType) {
f.fs.Write(nilAngleBytes)
return
}
@ -101,8 +116,6 @@ func (f *formatState) formatPtr(v reflect.Value) {
}
}
plusSyntax := f.fs.Flag('+')
// Keep list of all dereferenced pointers to possibly show later.
pointerChain := make([]uintptr, 0)
@ -123,6 +136,7 @@ func (f *formatState) formatPtr(v reflect.Value) {
pointerChain = append(pointerChain, addr)
if pd, ok := f.pointers[addr]; ok && pd < f.depth {
cycleFound = true
indirects--
break
}
f.pointers[addr] = f.depth
@ -137,13 +151,23 @@ func (f *formatState) formatPtr(v reflect.Value) {
}
}
// Display indirection level.
f.fs.Write(openAngleBytes)
f.fs.Write([]byte(strings.Repeat("*", indirects)))
f.fs.Write(closeAngleBytes)
// Display type or indirection level depending on flags.
if showTypes && !f.ignoreNextType {
f.fs.Write(openParenBytes)
f.fs.Write(bytes.Repeat(asteriskBytes, indirects))
f.fs.Write([]byte(ve.Type().String()))
f.fs.Write(closeParenBytes)
} else {
if nilFound || cycleFound {
indirects += strings.Count(ve.Type().String(), "*")
}
f.fs.Write(openAngleBytes)
f.fs.Write([]byte(strings.Repeat("*", indirects)))
f.fs.Write(closeAngleBytes)
}
// Display pointer information depending on flags.
if plusSyntax && (len(pointerChain) > 0) {
if f.fs.Flag('+') && (len(pointerChain) > 0) {
f.fs.Write(openParenBytes)
for i, addr := range pointerChain {
if i > 0 {
@ -163,6 +187,7 @@ func (f *formatState) formatPtr(v reflect.Value) {
f.fs.Write(circularShortBytes)
default:
f.ignoreNextType = true
f.format(ve)
}
}
@ -172,9 +197,23 @@ func (f *formatState) formatPtr(v reflect.Value) {
// dealing with and formats it appropriately. It is a recursive function,
// however circular data structures are detected and handled properly.
func (f *formatState) format(v reflect.Value) {
// Handle pointers specially.
kind := v.Kind()
if kind == reflect.Ptr {
f.formatPtr(v)
return
}
// Print type information unless already handled elsewhere.
if !f.ignoreNextType && f.fs.Flag('#') {
f.fs.Write(openParenBytes)
f.fs.Write([]byte(v.Type().String()))
f.fs.Write(closeParenBytes)
}
f.ignoreNextType = false
// Call Stringer/error interfaces if they exist and the handle methods
// flag is enabled.
kind := v.Kind()
if !f.cs.DisableMethods {
if (kind != reflect.Invalid) && (kind != reflect.Interface) {
if handled := handleMethods(f.cs, f.fs, v); handled {
@ -219,7 +258,8 @@ func (f *formatState) format(v reflect.Value) {
if i > 0 {
f.fs.Write(spaceBytes)
}
f.format(unpackValue(v.Index(i)))
f.ignoreNextType = true
f.format(f.unpackValue(v.Index(i)))
}
}
f.depth--
@ -231,6 +271,10 @@ func (f *formatState) format(v reflect.Value) {
case reflect.Interface:
// Do nothing. We should never get here due to unpackValue calls
case reflect.Ptr:
// Do nothing. We should never get here since pointers have already
// been handled above.
case reflect.Map:
f.fs.Write(openMapBytes)
f.depth++
@ -242,17 +286,16 @@ func (f *formatState) format(v reflect.Value) {
if i > 0 {
f.fs.Write(spaceBytes)
}
f.format(unpackValue(key))
f.ignoreNextType = true
f.format(f.unpackValue(key))
f.fs.Write(colonBytes)
f.format(unpackValue(v.MapIndex(key)))
f.ignoreNextType = true
f.format(f.unpackValue(v.MapIndex(key)))
}
}
f.depth--
f.fs.Write(closeMapBytes)
case reflect.Ptr:
f.formatPtr(v)
case reflect.Struct:
numFields := v.NumField()
f.fs.Write(openBraceBytes)
@ -266,11 +309,11 @@ func (f *formatState) format(v reflect.Value) {
f.fs.Write(spaceBytes)
}
vtf := vt.Field(i)
if f.fs.Flag('+') {
if f.fs.Flag('+') || f.fs.Flag('#') {
f.fs.Write([]byte(vtf.Name))
f.fs.Write(colonBytes)
}
f.format(unpackValue(v.Field(i)))
f.format(f.unpackValue(v.Field(i)))
}
}
f.depth--
@ -299,17 +342,21 @@ func (f *formatState) format(v reflect.Value) {
func (f *formatState) Format(fs fmt.State, verb rune) {
f.fs = fs
// Use standard formatting for verbs that are not v or #v.
if (verb != 'v') || (verb == 'v' && fs.Flag('#')) {
// Use standard formatting for verbs that are not v.
if verb != 'v' {
format := f.constructOrigFormat(verb)
fmt.Fprintf(fs, format, f.value)
return
}
if f.value == nil {
fmt.Fprint(fs, string(nilAngleBytes))
if fs.Flag('#') {
fs.Write(interfaceBytes)
}
fs.Write(nilAngleBytes)
return
}
f.format(reflect.ValueOf(f.value))
}
@ -327,11 +374,12 @@ interface. As a result, it integrates cleanly with standard fmt package
printing functions. The formatter is useful for inline printing of smaller data
types similar to the standard %v format specifier.
The custom formatter only responds to the %v and %+v verb combinations. Any
other variations such as %x, %q, and %#v will be sent to the the standard fmt
package for formatting. In addition, the custom formatter ignores the width and
precision arguments (however they will still work on the format specifiers not
handled by the custom formatter).
The custom formatter only responds to the %v (most compact), %+v (adds pointer
addresses), %#v (adds types), or %#+v (adds types and pointer addresses) verb
combinations. Any other verbs such as %x and %q will be sent to the the
standard fmt package for formatting. In addition, the custom formatter ignores
the width and precision arguments (however they will still work on the format
specifiers not handled by the custom formatter).
Typically this function shouldn't be called directly. It is much easier to make
use of the custom formatter by calling one of the convenience functions such as

View File

@ -226,11 +226,12 @@ interface. As a result, it integrates cleanly with standard fmt package
printing functions. The formatter is useful for inline printing of smaller data
types similar to the standard %v format specifier.
The custom formatter only responds to the %v and %+v verb combinations. Any
other variations such as %x, %q, and %#v will be sent to the the standard fmt
package for formatting. In addition, the custom formatter ignores the width and
precision arguments (however they will still work on the format specifiers not
handled by the custom formatter).
The custom formatter only responds to the %v (most compact), %+v (adds pointer
addresses), %#v (adds types), or %#+v (adds types and pointer addresses) verb
combinations. Any other verbs such as %x and %q will be sent to the the
standard fmt package for formatting. In addition, the custom formatter ignores
the width and precision arguments (however they will still work on the format
specifiers not handled by the custom formatter).
Typically this function shouldn't be called directly. It is much easier to make
use of the custom formatter by calling one of the convenience functions such as