Initial implementation.
This commit is contained in:
parent
d9f15c7ff8
commit
1a599b7b25
|
@ -0,0 +1,13 @@
|
|||
Copyright (c) 2012-2013 Dave Collins <dave@davec.name>
|
||||
|
||||
Permission to use, copy, modify, and distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
@ -0,0 +1,241 @@
|
|||
/*
|
||||
* Copyright (c) 2013 Dave Collins <dave@davec.name>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
package spew
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// reflectValue mirrors the struct layout of the reflect package Value type.
|
||||
var reflectValue struct {
|
||||
typ unsafe.Pointer
|
||||
val unsafe.Pointer
|
||||
flag uintptr
|
||||
}
|
||||
|
||||
// flagIndir indicates whether the value field of a reflect.Value is the actual
|
||||
// data or a pointer to the data.
|
||||
const flagIndir = 1 << 1
|
||||
|
||||
// unsafeReflectValue converts the passed reflect.Value into a one that bypasses
|
||||
// the typical safety restrictions preventing access to unaddressable and
|
||||
// unexported data. It works by digging the raw pointer to the underlying
|
||||
// value out of the protected value and generating a new unprotected (unsafe)
|
||||
// reflect.Value to it.
|
||||
//
|
||||
// This allows us to check for implementations of the Stringer and error
|
||||
// interfaces to be used for pretty printing ordinarily unaddressable and
|
||||
// inaccessible values such as unexported struct fields.
|
||||
func unsafeReflectValue(v reflect.Value) (rv reflect.Value) {
|
||||
indirects := 1
|
||||
vt := v.Type()
|
||||
upv := unsafe.Pointer(uintptr(unsafe.Pointer(&v)) + unsafe.Offsetof(reflectValue.val))
|
||||
rvf := *(*uintptr)(unsafe.Pointer(uintptr(unsafe.Pointer(&v)) + unsafe.Offsetof(reflectValue.flag)))
|
||||
if rvf&flagIndir != 0 {
|
||||
vt = reflect.PtrTo(v.Type())
|
||||
indirects++
|
||||
}
|
||||
|
||||
pv := reflect.NewAt(vt, upv)
|
||||
rv = pv
|
||||
for i := 0; i < indirects; i++ {
|
||||
rv = rv.Elem()
|
||||
}
|
||||
return rv
|
||||
}
|
||||
|
||||
// Some constants in the form of bytes to avoid string overhead. This mirrors
|
||||
// the technique used in the fmt package.
|
||||
var (
|
||||
panicBytes = []byte("(PANIC=")
|
||||
plusBytes = []byte("+")
|
||||
iBytes = []byte("i")
|
||||
trueBytes = []byte("true")
|
||||
falseBytes = []byte("false")
|
||||
interfaceBytes = []byte("(interface {}) ")
|
||||
commaNewlineBytes = []byte(",\n")
|
||||
newlineBytes = []byte("\n")
|
||||
openBraceBytes = []byte("{")
|
||||
openBraceNewlineBytes = []byte("{\n")
|
||||
closeBraceBytes = []byte("}")
|
||||
closeBraceNewlinBytes = []byte("}\n")
|
||||
asteriskBytes = []byte("*")
|
||||
colonSpaceBytes = []byte(": ")
|
||||
openParenBytes = []byte("(")
|
||||
closeParenBytes = []byte(")")
|
||||
spaceBytes = []byte(" ")
|
||||
pointerChainBytes = []byte("->")
|
||||
nilAngleBytes = []byte("<nil>")
|
||||
maxNewlineBytes = []byte("<max depth reached>\n")
|
||||
maxShortBytes = []byte("<max>")
|
||||
circularBytes = []byte("<already shown>")
|
||||
circularShortBytes = []byte("<shown>")
|
||||
invalidAngleBytes = []byte("<invalid>")
|
||||
percentBytes = []byte("%")
|
||||
precisionBytes = []byte(".")
|
||||
openAngleBytes = []byte("<")
|
||||
closeAngleBytes = []byte(">")
|
||||
openMapBytes = []byte("map[")
|
||||
closeMapBytes = []byte("]")
|
||||
)
|
||||
|
||||
// hexDigits is used to map a decimal value to a hex digit.
|
||||
var hexDigits = "0123456789abcdef"
|
||||
|
||||
// unpackValue returns values inside of non-nil inteferfaces 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) {
|
||||
if err := recover(); err != nil {
|
||||
w.Write(panicBytes)
|
||||
fmt.Fprintf(w, "%v", err)
|
||||
w.Write(closeParenBytes)
|
||||
}
|
||||
}
|
||||
|
||||
// handleMethods attempts to call the Error and String methods on the underlying
|
||||
// type the passed reflect.Value represents and outputes the result to Writer w.
|
||||
//
|
||||
// It handles panics in any called methods by catching and displaying the error
|
||||
// as the formatted value.
|
||||
func handleMethods(w io.Writer, v reflect.Value) (handled bool) {
|
||||
// We need an interface to check if the type implements the error or
|
||||
// Stringer interface. However, the reflect package won't give us an
|
||||
// an interface on certain things like unexported struct fields in order
|
||||
// to enforce visibility rules. We use unsafe to bypass these restrictions
|
||||
// since this package does not mutate the values.
|
||||
if !v.CanInterface() {
|
||||
v = unsafeReflectValue(v)
|
||||
}
|
||||
|
||||
// Choose whether or not to do error and Stringer interface lookups against
|
||||
// the base type or a pointer to the base type depending on settings.
|
||||
// Technically calling one of these methods with a pointer receiver can
|
||||
// mutate the value, however, types which choose to satisify an error or
|
||||
// Stringer interface with a pointer receiver should not be mutating their
|
||||
// state inside these interface methods.
|
||||
var viface interface{}
|
||||
if !Config.DisablePointerMethods {
|
||||
if !v.CanAddr() {
|
||||
v = unsafeReflectValue(v)
|
||||
}
|
||||
viface = v.Addr().Interface()
|
||||
} else {
|
||||
viface = v.Interface()
|
||||
}
|
||||
|
||||
// Is it an error or Stringer?
|
||||
switch iface := viface.(type) {
|
||||
case error:
|
||||
defer catchPanic(w, v)
|
||||
w.Write([]byte(iface.Error()))
|
||||
return true
|
||||
|
||||
case fmt.Stringer:
|
||||
defer catchPanic(w, v)
|
||||
w.Write([]byte(iface.String()))
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// printBool outputs a boolean value as true or false to Writer w.
|
||||
func printBool(w io.Writer, val bool) {
|
||||
if val {
|
||||
w.Write(trueBytes)
|
||||
} else {
|
||||
w.Write(falseBytes)
|
||||
}
|
||||
}
|
||||
|
||||
// printInt outputs a signed integer value to Writer w.
|
||||
func printInt(w io.Writer, val int64) {
|
||||
w.Write([]byte(strconv.FormatInt(val, 10)))
|
||||
}
|
||||
|
||||
// printUint outputs an unsigned integer value to Writer w.
|
||||
func printUint(w io.Writer, val uint64) {
|
||||
w.Write([]byte(strconv.FormatUint(val, 10)))
|
||||
}
|
||||
|
||||
// printFloat outputs a floating point value using the specified precision,
|
||||
// which is expected to be 32 or 64bit, to Writer w.
|
||||
func printFloat(w io.Writer, val float64, precision int) {
|
||||
w.Write([]byte(strconv.FormatFloat(val, 'g', -1, precision)))
|
||||
}
|
||||
|
||||
// printComplex outputs a complex value using the specified float precision
|
||||
// for the real and imaginary parts to Writer w.
|
||||
func printComplex(w io.Writer, c complex128, floatPrecision int) {
|
||||
r := real(c)
|
||||
w.Write(openParenBytes)
|
||||
w.Write([]byte(strconv.FormatFloat(r, 'g', -1, floatPrecision)))
|
||||
i := imag(c)
|
||||
if i >= 0 {
|
||||
w.Write(plusBytes)
|
||||
}
|
||||
w.Write([]byte(strconv.FormatFloat(i, 'g', -1, floatPrecision)))
|
||||
w.Write(iBytes)
|
||||
w.Write(closeParenBytes)
|
||||
}
|
||||
|
||||
// printHexPtr outputs a uintptr formatted as hexidecimal with a leading '0x'
|
||||
// prefix to Writer w.
|
||||
func printHexPtr(w io.Writer, p uintptr) {
|
||||
// Null pointer.
|
||||
num := uint64(p)
|
||||
if num == 0 {
|
||||
w.Write(nilAngleBytes)
|
||||
return
|
||||
}
|
||||
|
||||
// Max uint64 is 16 bytes in hex + 2 bytes for '0x' prefix
|
||||
buf := make([]byte, 18)
|
||||
|
||||
// It's simpler to construct the hex string right to left.
|
||||
base := uint64(16)
|
||||
i := len(buf) - 1
|
||||
for num >= base {
|
||||
buf[i] = hexDigits[num%base]
|
||||
num /= base
|
||||
i--
|
||||
}
|
||||
buf[i] = hexDigits[num]
|
||||
|
||||
// Add '0x' prefix.
|
||||
i--
|
||||
buf[i] = 'x'
|
||||
i--
|
||||
buf[i] = '0'
|
||||
|
||||
// Strip unused leading bytes.
|
||||
buf = buf[i:]
|
||||
w.Write(buf)
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Copyright (c) 2013 Dave Collins <dave@davec.name>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
package spew
|
||||
|
||||
// ConfigState is used to describe configuration options used by spew to format
|
||||
// and display values. There is currently only a single global instance, Config,
|
||||
// that is used to control all Formatter and Dump functionality. This state
|
||||
// is designed so that it would be fairly simple to add the ability to have
|
||||
// unique config per Formatter or dumpState instance if there is demand for
|
||||
// such a feature.
|
||||
type ConfigState struct {
|
||||
// MaxDepth controls the maximum number of levels to descend into nested
|
||||
// data structures. The default, 0, means there is no limit.
|
||||
//
|
||||
// NOTE: Circular data structures are properly detected, so it is not
|
||||
// necessary to set this value unless you specifically want to limit deeply
|
||||
// nested data structures.
|
||||
MaxDepth int
|
||||
|
||||
// Indent specifies the string to use for each indentation level. It is
|
||||
// a single space by default. If you would like more indentation, you might
|
||||
// set this to a tab with "\t" or perhaps two spaces with " ".
|
||||
Indent string
|
||||
|
||||
// DisableMethods specifies whether or not error and Stringer interfaces are
|
||||
// invoked for types that implement them.
|
||||
DisableMethods bool
|
||||
|
||||
// DisablePointerMethods specifies whether or not to check for and invoke
|
||||
// error and Stringer interfaces on types which only accept a pointer
|
||||
// receiver when the current type is not a pointer.
|
||||
//
|
||||
// NOTE: This might be an unsafe action since calling one of these methods
|
||||
// with a pointer receiver could technically mutate the value, however,
|
||||
// in practice, types which choose to satisify an error or Stringer
|
||||
// interface with a pointer receiver should not be mutating their state
|
||||
// inside these interface methods.
|
||||
DisablePointerMethods bool
|
||||
}
|
||||
|
||||
// Config is the active configuration in use by spew. The configuration
|
||||
// can be changed by modifying the contents of spew.Config.
|
||||
var Config ConfigState = ConfigState{Indent: " "}
|
|
@ -0,0 +1,148 @@
|
|||
/*
|
||||
* Copyright (c) 2013 Dave Collins <dave@davec.name>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
Package spew implements a deep pretty printer for Go data structures to aid in
|
||||
debugging.
|
||||
|
||||
A quick overview of the additional features spew provides over the built-in
|
||||
printing facilities for Go data types are as follows:
|
||||
|
||||
* Pointers are dereferenced and followed
|
||||
* Circular data structures are detected and handled properly
|
||||
* Custom error/Stringer interfaces are optionally invoked, including
|
||||
on unexported types
|
||||
* Custom types which only implement the error/Stringer interfaces via
|
||||
a pointer receiver are optionally invoked when passing non-pointer
|
||||
variables
|
||||
|
||||
There are two different approaches spew allows for dumping Go data structures:
|
||||
|
||||
* Dump style which prints with newlines, customizable indentation,
|
||||
and additional debug information such as types and all pointer addresses
|
||||
used to indirect to the final value
|
||||
* A custom Formatter interface that integrates cleanly with the standard fmt
|
||||
package and replaces %v and %+v to provide inline printing similar
|
||||
to the default %v while providing the additional functionality outlined
|
||||
above and passing unsupported format verb/flag combinations such a %x,
|
||||
%q, and %#v along to fmt
|
||||
|
||||
Quick Start
|
||||
|
||||
This section demonstrates how to quickly get started with spew. See the
|
||||
sections below for further details on formatting and configuration options.
|
||||
|
||||
To dump a variable with full newlines, indentation, type, and pointer
|
||||
information use Dump or Fdump:
|
||||
spew.Dump(myVar1, myVar2, ...)
|
||||
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):
|
||||
spew.Printf("myVar1: %v -- myVar2: %+v", myVar1, myVar2)
|
||||
spew.Fprintf(someWriter, "myVar1: %v -- myVar2: %+v", myVar1, myVar2)
|
||||
|
||||
Configuration Options
|
||||
|
||||
The following configuration options are available:
|
||||
spew.Config.MaxDepth
|
||||
Maximum number of levels to descend into nested data structures.
|
||||
There is no limit by default.
|
||||
|
||||
spew.Config.Indent
|
||||
String to use for each indentation level for Dump functions.
|
||||
It is a single space by default. A popular alternative is "\t".
|
||||
|
||||
spew.Config.DisableMethods
|
||||
Disables invocation of error and Stringer interface methods.
|
||||
Method invocation is enabled by default.
|
||||
|
||||
spew.Config.DisablePointerMethods
|
||||
Disables invocation of error and Stringer interface methods on types
|
||||
which only accept pointer receivers from non-pointer variables.
|
||||
Pointer method invocation is enabled by default.
|
||||
|
||||
Dump Usage
|
||||
|
||||
Simply call spew.Dump with a list of variables you want to dump:
|
||||
|
||||
spew.Dump(myVar1, myVar2, ...)
|
||||
|
||||
You may also call spew.Fdump if you would prefer to output to an arbitrary
|
||||
io.Writer. For example, to dump to standard error:
|
||||
|
||||
spew.Fdump(os.Stderr, myVar1, myVar2, ...)
|
||||
|
||||
Sample Dump Output
|
||||
|
||||
See the Dump example for details on the setup of the types and variables being
|
||||
shown here.
|
||||
|
||||
(main.Foo) {
|
||||
unexportedField: (*main.Bar)(0xf84002e210)({
|
||||
flag: (main.Flag) flagTwo,
|
||||
data: (uintptr) <nil>
|
||||
}),
|
||||
ExportedField: (map[interface {}]interface {}) {
|
||||
(string) "one": (bool) true
|
||||
}
|
||||
}
|
||||
|
||||
Custom Formatter
|
||||
|
||||
Spew provides a custom formatter the implements the fmt.Formatter interface
|
||||
so that 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).
|
||||
|
||||
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:
|
||||
|
||||
spew.Printf("myVar1: %v -- myVar2: %+v", myVar1, myVar2)
|
||||
spew.Println(myVar, myVar2)
|
||||
spew.Fprintf(os.Stderr, "myVar1: %v -- myVar2: %+v", myVar1, myVar2)
|
||||
|
||||
See the Index for the full list convenience functions.
|
||||
|
||||
Sample Formatter Output
|
||||
|
||||
Double pointer to a uint8 via %v:
|
||||
<**>5
|
||||
|
||||
Circular struct with a uint8 field and a pointer to itself via %+v:
|
||||
{ui8:1 c:<*>(0xf84002d200){ui8:1 c:<*>(0xf84002d200)<shown>}}
|
||||
|
||||
See the Printf example for details on the setup of variables being shown
|
||||
here.
|
||||
|
||||
Errors
|
||||
|
||||
Since it is possible for custom error/Stringer interfaces to panic, spew
|
||||
detects them and handles them internally by printing the panic information
|
||||
inline with the output. Since spew is intended to provide deep pretty printing
|
||||
capabilities on structures, it intentionally does not return any errors.
|
||||
*/
|
||||
package spew
|
|
@ -0,0 +1,323 @@
|
|||
/*
|
||||
* Copyright (c) 2013 Dave Collins <dave@davec.name>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
package spew
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// dumpState contains information about the state of a dump operation.
|
||||
type dumpState struct {
|
||||
w io.Writer
|
||||
depth int
|
||||
pointers map[uintptr]int
|
||||
ignoreNextType bool
|
||||
ignoreNextPad bool
|
||||
}
|
||||
|
||||
// pad performs indentation according to the depth level and Config.Indent
|
||||
// option.
|
||||
func (d *dumpState) pad() {
|
||||
if d.ignoreNextPad {
|
||||
d.ignoreNextPad = false
|
||||
return
|
||||
}
|
||||
d.w.Write(bytes.Repeat([]byte(Config.Indent), d.depth))
|
||||
}
|
||||
|
||||
// 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
|
||||
// circular refs.
|
||||
for k, depth := range d.pointers {
|
||||
if depth >= d.depth {
|
||||
delete(d.pointers, k)
|
||||
}
|
||||
}
|
||||
|
||||
// Keep list of all dereferenced pointers to show later.
|
||||
pointerChain := make([]uintptr, 0)
|
||||
|
||||
// Figure out how many levels of indirection there are by derferencing
|
||||
// pointers and unpacking interfaces down the chain while detecting circular
|
||||
// references.
|
||||
nilFound := false
|
||||
cycleFound := false
|
||||
indirects := 0
|
||||
ve := v
|
||||
for ve.Kind() == reflect.Ptr {
|
||||
indirects++
|
||||
if ve.IsNil() {
|
||||
nilFound = true
|
||||
break
|
||||
}
|
||||
addr := ve.Pointer()
|
||||
pointerChain = append(pointerChain, addr)
|
||||
if pd, ok := d.pointers[addr]; ok && pd < d.depth {
|
||||
cycleFound = true
|
||||
indirects--
|
||||
break
|
||||
}
|
||||
d.pointers[addr] = d.depth
|
||||
|
||||
ve = ve.Elem()
|
||||
if ve.Kind() == reflect.Interface {
|
||||
if ve.IsNil() {
|
||||
nilFound = true
|
||||
break
|
||||
}
|
||||
ve = ve.Elem()
|
||||
}
|
||||
}
|
||||
|
||||
// Display type information.
|
||||
d.w.Write(openParenBytes)
|
||||
d.w.Write(bytes.Repeat(asteriskBytes, indirects))
|
||||
d.w.Write([]byte(ve.Type().String()))
|
||||
d.w.Write(closeParenBytes)
|
||||
|
||||
// Display pointer information.
|
||||
d.w.Write(openParenBytes)
|
||||
for i, addr := range pointerChain {
|
||||
if i > 0 {
|
||||
d.w.Write(pointerChainBytes)
|
||||
}
|
||||
printHexPtr(d.w, addr)
|
||||
}
|
||||
d.w.Write(closeParenBytes)
|
||||
|
||||
// Display dereferenced value.
|
||||
d.w.Write(openParenBytes)
|
||||
switch {
|
||||
case nilFound == true:
|
||||
d.w.Write(nilAngleBytes)
|
||||
|
||||
case cycleFound == true:
|
||||
d.w.Write(circularBytes)
|
||||
|
||||
default:
|
||||
d.ignoreNextType = true
|
||||
d.dump(ve)
|
||||
}
|
||||
d.w.Write(closeParenBytes)
|
||||
}
|
||||
|
||||
// dump is the main workhorse for dumping a value. It uses the passed reflect
|
||||
// value to figure out what kind of object we are dealing with and formats it
|
||||
// appropriately. It is a recursive function, however circular data structures
|
||||
// are detected and handled properly.
|
||||
func (d *dumpState) dump(v reflect.Value) {
|
||||
// Handle pointers specially.
|
||||
kind := v.Kind()
|
||||
if kind == reflect.Ptr {
|
||||
d.pad()
|
||||
d.dumpPtr(v)
|
||||
return
|
||||
}
|
||||
|
||||
// Print type information unless already handled elsewhere.
|
||||
if !d.ignoreNextType {
|
||||
d.pad()
|
||||
d.w.Write(openParenBytes)
|
||||
d.w.Write([]byte(v.Type().String()))
|
||||
d.w.Write(closeParenBytes)
|
||||
d.w.Write(spaceBytes)
|
||||
}
|
||||
d.ignoreNextType = false
|
||||
|
||||
// Call error/Stringer interfaces if they exist and the handle methods flag
|
||||
// is enabled
|
||||
if !Config.DisableMethods {
|
||||
if (kind != reflect.Invalid) && (kind != reflect.Interface) {
|
||||
if handled := handleMethods(d.w, v); handled {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch kind {
|
||||
case reflect.Invalid:
|
||||
d.w.Write(invalidAngleBytes)
|
||||
|
||||
case reflect.Bool:
|
||||
printBool(d.w, v.Bool())
|
||||
|
||||
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
|
||||
printInt(d.w, v.Int())
|
||||
|
||||
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
|
||||
printUint(d.w, v.Uint())
|
||||
|
||||
case reflect.Float32:
|
||||
printFloat(d.w, v.Float(), 32)
|
||||
|
||||
case reflect.Float64:
|
||||
printFloat(d.w, v.Float(), 64)
|
||||
|
||||
case reflect.Complex64:
|
||||
printComplex(d.w, v.Complex(), 32)
|
||||
|
||||
case reflect.Complex128:
|
||||
printComplex(d.w, v.Complex(), 64)
|
||||
|
||||
case reflect.Array, reflect.Slice:
|
||||
d.w.Write(openBraceNewlineBytes)
|
||||
d.depth++
|
||||
if (Config.MaxDepth != 0) && (d.depth > Config.MaxDepth) {
|
||||
d.pad()
|
||||
d.w.Write(maxNewlineBytes)
|
||||
} else {
|
||||
numEntries := v.Len()
|
||||
for i := 0; i < numEntries; i++ {
|
||||
d.dump(unpackValue(v.Index(i)))
|
||||
if i < (numEntries - 1) {
|
||||
d.w.Write(commaNewlineBytes)
|
||||
} else {
|
||||
d.w.Write(newlineBytes)
|
||||
}
|
||||
}
|
||||
}
|
||||
d.depth--
|
||||
d.pad()
|
||||
d.w.Write(closeBraceBytes)
|
||||
|
||||
case reflect.String:
|
||||
d.w.Write([]byte(strconv.Quote(v.String())))
|
||||
|
||||
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 pointer have already
|
||||
// been handled above.
|
||||
|
||||
case reflect.Map:
|
||||
d.w.Write(openBraceNewlineBytes)
|
||||
d.depth++
|
||||
if (Config.MaxDepth != 0) && (d.depth > Config.MaxDepth) {
|
||||
d.pad()
|
||||
d.w.Write(maxNewlineBytes)
|
||||
} else {
|
||||
numEntries := v.Len()
|
||||
keys := v.MapKeys()
|
||||
for i, key := range keys {
|
||||
d.dump(unpackValue(key))
|
||||
d.w.Write(colonSpaceBytes)
|
||||
d.ignoreNextPad = true
|
||||
d.dump(unpackValue(v.MapIndex(key)))
|
||||
if i < (numEntries - 1) {
|
||||
d.w.Write(commaNewlineBytes)
|
||||
} else {
|
||||
d.w.Write(newlineBytes)
|
||||
}
|
||||
}
|
||||
}
|
||||
d.depth--
|
||||
d.pad()
|
||||
d.w.Write(closeBraceBytes)
|
||||
|
||||
case reflect.Struct:
|
||||
d.w.Write(openBraceNewlineBytes)
|
||||
d.depth++
|
||||
if (Config.MaxDepth != 0) && (d.depth > Config.MaxDepth) {
|
||||
d.pad()
|
||||
d.w.Write(maxNewlineBytes)
|
||||
} else {
|
||||
vt := v.Type()
|
||||
numFields := v.NumField()
|
||||
for i := 0; i < numFields; i++ {
|
||||
d.pad()
|
||||
vtf := vt.Field(i)
|
||||
d.w.Write([]byte(vtf.Name))
|
||||
d.w.Write(colonSpaceBytes)
|
||||
d.ignoreNextPad = true
|
||||
d.dump(unpackValue(v.Field(i)))
|
||||
if i < (numFields - 1) {
|
||||
d.w.Write(commaNewlineBytes)
|
||||
} else {
|
||||
d.w.Write(newlineBytes)
|
||||
}
|
||||
}
|
||||
}
|
||||
d.depth--
|
||||
d.pad()
|
||||
d.w.Write(closeBraceBytes)
|
||||
|
||||
case reflect.Uintptr:
|
||||
printHexPtr(d.w, uintptr(v.Uint()))
|
||||
|
||||
case reflect.UnsafePointer, reflect.Chan, reflect.Func:
|
||||
printHexPtr(d.w, v.Pointer())
|
||||
|
||||
// There were not any other types at the time this code was written, but
|
||||
// fall back to letting the default fmt package handle it in case any new
|
||||
// types are added.
|
||||
default:
|
||||
if v.CanInterface() {
|
||||
fmt.Fprintf(d.w, "%v", v.Interface())
|
||||
} else {
|
||||
fmt.Fprintf(d.w, "%v", v.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fdump formats and displays the passed arguments to io.Writer w. It formats
|
||||
// exactly the same as Dump.
|
||||
func Fdump(w io.Writer, a ...interface{}) {
|
||||
for _, arg := range a {
|
||||
if arg == nil {
|
||||
w.Write(interfaceBytes)
|
||||
w.Write(nilAngleBytes)
|
||||
w.Write(newlineBytes)
|
||||
continue
|
||||
}
|
||||
|
||||
d := dumpState{w: w}
|
||||
d.pointers = make(map[uintptr]int)
|
||||
d.dump(reflect.ValueOf(arg))
|
||||
d.w.Write(newlineBytes)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Dump displays the passed parameters to standard out with newlines, customizable
|
||||
indentation, and additional debug information such as complete types and all
|
||||
pointer addresses used to indirect to the final value. It provides the
|
||||
following features over the built-in printing facilities provided by the fmt
|
||||
package:
|
||||
|
||||
* Pointers are dereferenced and followed
|
||||
* Circular data structures are detected and handled properly
|
||||
* Custom error/Stringer interfaces are optionally invoked, including
|
||||
on unexported types
|
||||
* Custom types which only implement the error/Stringer interfaces via
|
||||
a pointer receiver are optionally invoked when passing non-pointer
|
||||
variables
|
||||
|
||||
The configuration options are controlled by an exported package global,
|
||||
spew.Config. See ConfigState for options documentation.
|
||||
|
||||
See Fdump if you would prefer dump to an arbitrary io.Writer.
|
||||
*/
|
||||
func Dump(a ...interface{}) {
|
||||
Fdump(os.Stdout, a...)
|
||||
}
|
|
@ -0,0 +1,675 @@
|
|||
/*
|
||||
* Copyright (c) 2013 Dave Collins <dave@davec.name>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
Test Summary:
|
||||
NOTE: For each test, a pointer and double pointer to the base test element
|
||||
are also tested to ensure proper indirection across all types.
|
||||
|
||||
- Max int8, int16, int32, int64, int
|
||||
- Max uint8, uint16, uint32, uint64, uint
|
||||
- Boolean true and false
|
||||
- Standard complex64 and complex128
|
||||
- Array containing standard ints
|
||||
- Array containing type with custom formatter on pointer receiver only
|
||||
- Slice containing standard float32 values
|
||||
- Slice containing type with custom formatter on pointer receiver only
|
||||
- Standard string
|
||||
- Nil interface
|
||||
- Map with string keys and int vals
|
||||
- Map with custom formatter type on pointer receiver only keys and vals
|
||||
- Map with interface keys and values
|
||||
- Struct with primitives
|
||||
- Struct that contains another struct
|
||||
- Struct that contains custom type with Stringer pointer interface via both
|
||||
exported and unexported fields
|
||||
- Uintptr to 0 (null pointer)
|
||||
- Uintptr address of real variable
|
||||
- Unsafe.Pointer to 0 (null pointer)
|
||||
- Unsafe.Pointer to address of real variable
|
||||
- Nil channel
|
||||
- Standard int channel
|
||||
- Function with no params and no returns
|
||||
- Function with param and no returns
|
||||
- Function with multiple params and multiple returns
|
||||
- Struct that is circular through self referencing
|
||||
- Structs that are circular through cross referencing
|
||||
- Structs that are indirectly circular
|
||||
*/
|
||||
|
||||
package spew_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"testing"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// custom type to test Stinger interface on pointer receiver.
|
||||
type pstringer string
|
||||
|
||||
// String implements the Stringer interface for testing invocation of custom
|
||||
// stringers on types with only pointer receivers.
|
||||
func (s *pstringer) String() string {
|
||||
return "stringer " + string(*s)
|
||||
}
|
||||
|
||||
// xref1 and xref2 are cross referencing structs for testing circular reference
|
||||
// detection.
|
||||
type xref1 struct {
|
||||
ps2 *xref2
|
||||
}
|
||||
type xref2 struct {
|
||||
ps1 *xref1
|
||||
}
|
||||
|
||||
// indirCir1, indirCir2, and indirCir3 are used to generate an indirect circular
|
||||
// reference for testing detection.
|
||||
type indirCir1 struct {
|
||||
ps2 *indirCir2
|
||||
}
|
||||
type indirCir2 struct {
|
||||
ps3 *indirCir3
|
||||
}
|
||||
type indirCir3 struct {
|
||||
ps1 *indirCir1
|
||||
}
|
||||
|
||||
// dumpTest is used to describe a test to be perfomed against the Dump method.
|
||||
type dumpTest struct {
|
||||
in interface{}
|
||||
want string
|
||||
}
|
||||
|
||||
// dumpTests houses all of the tests to be performed against the Dump method.
|
||||
var dumpTests = make([]dumpTest, 0)
|
||||
|
||||
// addDumpTest is a helper method to append the passed input and desired result
|
||||
// to dumpTests
|
||||
func addDumpTest(in interface{}, want string) {
|
||||
test := dumpTest{in, want}
|
||||
dumpTests = append(dumpTests, test)
|
||||
}
|
||||
|
||||
func addIntTests() {
|
||||
// Max int8.
|
||||
v := int8(127)
|
||||
pv := &v
|
||||
vAddr := fmt.Sprintf("%p", pv)
|
||||
pvAddr := fmt.Sprintf("%p", &pv)
|
||||
vt := "int8"
|
||||
vs := "127"
|
||||
addDumpTest(v, "("+vt+") "+vs+"\n")
|
||||
addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n")
|
||||
addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n")
|
||||
|
||||
// Max int16.
|
||||
v2 := int16(32767)
|
||||
pv2 := &v2
|
||||
v2Addr := fmt.Sprintf("%p", pv2)
|
||||
pv2Addr := fmt.Sprintf("%p", &pv2)
|
||||
v2t := "int16"
|
||||
v2s := "32767"
|
||||
addDumpTest(v2, "("+v2t+") "+v2s+"\n")
|
||||
addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n")
|
||||
addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n")
|
||||
|
||||
// Max int32.
|
||||
v3 := int32(2147483647)
|
||||
pv3 := &v3
|
||||
v3Addr := fmt.Sprintf("%p", pv3)
|
||||
pv3Addr := fmt.Sprintf("%p", &pv3)
|
||||
v3t := "int32"
|
||||
v3s := "2147483647"
|
||||
addDumpTest(v3, "("+v3t+") "+v3s+"\n")
|
||||
addDumpTest(pv3, "(*"+v3t+")("+v3Addr+")("+v3s+")\n")
|
||||
addDumpTest(&pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")("+v3s+")\n")
|
||||
|
||||
// Max int64.
|
||||
v4 := int64(9223372036854775807)
|
||||
pv4 := &v4
|
||||
v4Addr := fmt.Sprintf("%p", pv4)
|
||||
pv4Addr := fmt.Sprintf("%p", &pv4)
|
||||
v4t := "int64"
|
||||
v4s := "9223372036854775807"
|
||||
addDumpTest(v4, "("+v4t+") "+v4s+"\n")
|
||||
addDumpTest(pv4, "(*"+v4t+")("+v4Addr+")("+v4s+")\n")
|
||||
addDumpTest(&pv4, "(**"+v4t+")("+pv4Addr+"->"+v4Addr+")("+v4s+")\n")
|
||||
|
||||
// Max int.
|
||||
v5 := int(2147483647)
|
||||
pv5 := &v5
|
||||
v5Addr := fmt.Sprintf("%p", pv5)
|
||||
pv5Addr := fmt.Sprintf("%p", &pv5)
|
||||
v5t := "int"
|
||||
v5s := "2147483647"
|
||||
addDumpTest(v5, "("+v5t+") "+v5s+"\n")
|
||||
addDumpTest(pv5, "(*"+v5t+")("+v5Addr+")("+v5s+")\n")
|
||||
addDumpTest(&pv5, "(**"+v5t+")("+pv5Addr+"->"+v5Addr+")("+v5s+")\n")
|
||||
}
|
||||
|
||||
func addUintTests() {
|
||||
// Max uint8.
|
||||
v := uint8(255)
|
||||
pv := &v
|
||||
vAddr := fmt.Sprintf("%p", pv)
|
||||
pvAddr := fmt.Sprintf("%p", &pv)
|
||||
vt := "uint8"
|
||||
vs := "255"
|
||||
addDumpTest(v, "("+vt+") "+vs+"\n")
|
||||
addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n")
|
||||
addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n")
|
||||
|
||||
// Max uint16.
|
||||
v2 := uint16(65535)
|
||||
pv2 := &v2
|
||||
v2Addr := fmt.Sprintf("%p", pv2)
|
||||
pv2Addr := fmt.Sprintf("%p", &pv2)
|
||||
v2t := "uint16"
|
||||
v2s := "65535"
|
||||
addDumpTest(v2, "("+v2t+") "+v2s+"\n")
|
||||
addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n")
|
||||
addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n")
|
||||
|
||||
// Max uint32.
|
||||
v3 := uint32(4294967295)
|
||||
pv3 := &v3
|
||||
v3Addr := fmt.Sprintf("%p", pv3)
|
||||
pv3Addr := fmt.Sprintf("%p", &pv3)
|
||||
v3t := "uint32"
|
||||
v3s := "4294967295"
|
||||
addDumpTest(v3, "("+v3t+") "+v3s+"\n")
|
||||
addDumpTest(pv3, "(*"+v3t+")("+v3Addr+")("+v3s+")\n")
|
||||
addDumpTest(&pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")("+v3s+")\n")
|
||||
|
||||
// Max uint64.
|
||||
v4 := uint64(18446744073709551615)
|
||||
pv4 := &v4
|
||||
v4Addr := fmt.Sprintf("%p", pv4)
|
||||
pv4Addr := fmt.Sprintf("%p", &pv4)
|
||||
v4t := "uint64"
|
||||
v4s := "18446744073709551615"
|
||||
addDumpTest(v4, "("+v4t+") "+v4s+"\n")
|
||||
addDumpTest(pv4, "(*"+v4t+")("+v4Addr+")("+v4s+")\n")
|
||||
addDumpTest(&pv4, "(**"+v4t+")("+pv4Addr+"->"+v4Addr+")("+v4s+")\n")
|
||||
|
||||
// Max uint.
|
||||
v5 := uint(4294967295)
|
||||
pv5 := &v5
|
||||
v5Addr := fmt.Sprintf("%p", pv5)
|
||||
pv5Addr := fmt.Sprintf("%p", &pv5)
|
||||
v5t := "uint"
|
||||
v5s := "4294967295"
|
||||
addDumpTest(v5, "("+v5t+") "+v5s+"\n")
|
||||
addDumpTest(pv5, "(*"+v5t+")("+v5Addr+")("+v5s+")\n")
|
||||
addDumpTest(&pv5, "(**"+v5t+")("+pv5Addr+"->"+v5Addr+")("+v5s+")\n")
|
||||
}
|
||||
|
||||
func addBoolTests() {
|
||||
// Boolean true.
|
||||
v := bool(true)
|
||||
pv := &v
|
||||
vAddr := fmt.Sprintf("%p", pv)
|
||||
pvAddr := fmt.Sprintf("%p", &pv)
|
||||
vt := "bool"
|
||||
vs := "true"
|
||||
addDumpTest(v, "("+vt+") "+vs+"\n")
|
||||
addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n")
|
||||
addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n")
|
||||
|
||||
// Boolean false.
|
||||
v2 := bool(false)
|
||||
pv2 := &v2
|
||||
v2Addr := fmt.Sprintf("%p", pv2)
|
||||
pv2Addr := fmt.Sprintf("%p", &pv2)
|
||||
v2t := "bool"
|
||||
v2s := "false"
|
||||
addDumpTest(v2, "("+v2t+") "+v2s+"\n")
|
||||
addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n")
|
||||
addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n")
|
||||
}
|
||||
|
||||
func addFloatTests() {
|
||||
// Standard float32.
|
||||
v := float32(3.1415)
|
||||
pv := &v
|
||||
vAddr := fmt.Sprintf("%p", pv)
|
||||
pvAddr := fmt.Sprintf("%p", &pv)
|
||||
vt := "float32"
|
||||
vs := "3.1415"
|
||||
addDumpTest(v, "("+vt+") "+vs+"\n")
|
||||
addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n")
|
||||
addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n")
|
||||
|
||||
// Standard float64.
|
||||
v2 := float64(3.1415926)
|
||||
pv2 := &v2
|
||||
v2Addr := fmt.Sprintf("%p", pv2)
|
||||
pv2Addr := fmt.Sprintf("%p", &pv2)
|
||||
v2t := "float64"
|
||||
v2s := "3.1415926"
|
||||
addDumpTest(v2, "("+v2t+") "+v2s+"\n")
|
||||
addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n")
|
||||
addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n")
|
||||
}
|
||||
|
||||
func addComplexTests() {
|
||||
// Standard complex64.
|
||||
v := complex(float32(6), -2)
|
||||
pv := &v
|
||||
vAddr := fmt.Sprintf("%p", pv)
|
||||
pvAddr := fmt.Sprintf("%p", &pv)
|
||||
vt := "complex64"
|
||||
vs := "(6-2i)"
|
||||
addDumpTest(v, "("+vt+") "+vs+"\n")
|
||||
addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n")
|
||||
addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n")
|
||||
|
||||
// Standard complex128.
|
||||
v2 := complex(float64(-6), 2)
|
||||
pv2 := &v2
|
||||
v2Addr := fmt.Sprintf("%p", pv2)
|
||||
pv2Addr := fmt.Sprintf("%p", &pv2)
|
||||
v2t := "complex128"
|
||||
v2s := "(-6+2i)"
|
||||
addDumpTest(v2, "("+v2t+") "+v2s+"\n")
|
||||
addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n")
|
||||
addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n")
|
||||
}
|
||||
|
||||
func addArrayTests() {
|
||||
// Array containing standard ints.
|
||||
v := [3]int{1, 2, 3}
|
||||
pv := &v
|
||||
vAddr := fmt.Sprintf("%p", pv)
|
||||
pvAddr := fmt.Sprintf("%p", &pv)
|
||||
vt := "int"
|
||||
vs := "{\n (" + vt + ") 1,\n (" + vt + ") 2,\n (" + vt + ") 3\n}"
|
||||
addDumpTest(v, "([3]"+vt+") "+vs+"\n")
|
||||
addDumpTest(pv, "(*[3]"+vt+")("+vAddr+")("+vs+")\n")
|
||||
addDumpTest(&pv, "(**[3]"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n")
|
||||
|
||||
// Array containing type with custom formatter on pointer receiver only.
|
||||
v2 := [3]pstringer{"1", "2", "3"}
|
||||
pv2 := &v2
|
||||
v2Addr := fmt.Sprintf("%p", pv2)
|
||||
pv2Addr := fmt.Sprintf("%p", &pv2)
|
||||
v2t := "spew_test.pstringer"
|
||||
v2s := "{\n (" + v2t + ") stringer 1,\n (" + v2t + ") stringer 2,\n (" +
|
||||
v2t + ") stringer 3\n}"
|
||||
addDumpTest(v2, "([3]"+v2t+") "+v2s+"\n")
|
||||
addDumpTest(pv2, "(*[3]"+v2t+")("+v2Addr+")("+v2s+")\n")
|
||||
addDumpTest(&pv2, "(**[3]"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n")
|
||||
}
|
||||
|
||||
func addSliceTests() {
|
||||
// Slice containing standard float32 values.
|
||||
v := []float32{3.14, 6.28, 12.56}
|
||||
pv := &v
|
||||
vAddr := fmt.Sprintf("%p", pv)
|
||||
pvAddr := fmt.Sprintf("%p", &pv)
|
||||
vt := "float32"
|
||||
vs := "{\n (" + vt + ") 3.14,\n (" + vt + ") 6.28,\n (" + vt + ") 12.56\n}"
|
||||
addDumpTest(v, "([]"+vt+") "+vs+"\n")
|
||||
addDumpTest(pv, "(*[]"+vt+")("+vAddr+")("+vs+")\n")
|
||||
addDumpTest(&pv, "(**[]"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n")
|
||||
|
||||
// Slice containing type with custom formatter on pointer receiver only.
|
||||
v2 := []pstringer{"1", "2", "3"}
|
||||
pv2 := &v2
|
||||
v2Addr := fmt.Sprintf("%p", pv2)
|
||||
pv2Addr := fmt.Sprintf("%p", &pv2)
|
||||
v2t := "spew_test.pstringer"
|
||||
v2s := "{\n (" + v2t + ") stringer 1,\n (" + v2t + ") stringer 2,\n (" +
|
||||
v2t + ") stringer 3\n}"
|
||||
addDumpTest(v2, "([]"+v2t+") "+v2s+"\n")
|
||||
addDumpTest(pv2, "(*[]"+v2t+")("+v2Addr+")("+v2s+")\n")
|
||||
addDumpTest(&pv2, "(**[]"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n")
|
||||
}
|
||||
|
||||
func addStringTests() {
|
||||
// Standard string.
|
||||
v := "test"
|
||||
pv := &v
|
||||
vAddr := fmt.Sprintf("%p", pv)
|
||||
pvAddr := fmt.Sprintf("%p", &pv)
|
||||
vt := "string"
|
||||
vs := "\"test\""
|
||||
addDumpTest(v, "("+vt+") "+vs+"\n")
|
||||
addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n")
|
||||
addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n")
|
||||
}
|
||||
|
||||
func addNilInterfaceTests() {
|
||||
// Nil interface.
|
||||
var v interface{}
|
||||
pv := &v
|
||||
vAddr := fmt.Sprintf("%p", pv)
|
||||
pvAddr := fmt.Sprintf("%p", &pv)
|
||||
vt := "interface {}"
|
||||
vs := "<nil>"
|
||||
addDumpTest(v, "("+vt+") "+vs+"\n")
|
||||
addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n")
|
||||
addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n")
|
||||
}
|
||||
|
||||
func addMapTests() {
|
||||
// Map with string keys and int vals.
|
||||
v := map[string]int{"one": 1}
|
||||
pv := &v
|
||||
vAddr := fmt.Sprintf("%p", pv)
|
||||
pvAddr := fmt.Sprintf("%p", &pv)
|
||||
vt := "map[string]int"
|
||||
vt1 := "string"
|
||||
vt2 := "int"
|
||||
vs := "{\n (" + vt1 + ") \"one\": (" + vt2 + ") 1\n}"
|
||||
addDumpTest(v, "("+vt+") "+vs+"\n")
|
||||
addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n")
|
||||
addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n")
|
||||
|
||||
// Map with custom formatter type on pointer receiver only keys and vals.
|
||||
v2 := map[pstringer]pstringer{"one": "1"}
|
||||
pv2 := &v2
|
||||
v2Addr := fmt.Sprintf("%p", pv2)
|
||||
pv2Addr := fmt.Sprintf("%p", &pv2)
|
||||
v2t := "map[spew_test.pstringer]spew_test.pstringer"
|
||||
v2t1 := "spew_test.pstringer"
|
||||
v2t2 := "spew_test.pstringer"
|
||||
v2s := "{\n (" + v2t1 + ") stringer one: (" + v2t2 + ") stringer 1\n}"
|
||||
addDumpTest(v2, "("+v2t+") "+v2s+"\n")
|
||||
addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n")
|
||||
addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n")
|
||||
|
||||
// Map with interface keys and values.
|
||||
v3 := map[interface{}]interface{}{"one": 1}
|
||||
pv3 := &v3
|
||||
v3Addr := fmt.Sprintf("%p", pv3)
|
||||
pv3Addr := fmt.Sprintf("%p", &pv3)
|
||||
v3t := "map[interface {}]interface {}"
|
||||
v3t1 := "string"
|
||||
v3t2 := "int"
|
||||
v3s := "{\n (" + v3t1 + ") \"one\": (" + v3t2 + ") 1\n}"
|
||||
addDumpTest(v3, "("+v3t+") "+v3s+"\n")
|
||||
addDumpTest(pv3, "(*"+v3t+")("+v3Addr+")("+v3s+")\n")
|
||||
addDumpTest(&pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")("+v3s+")\n")
|
||||
}
|
||||
|
||||
func addStructTests() {
|
||||
// Struct with primitives.
|
||||
type s1 struct {
|
||||
a int8
|
||||
b uint8
|
||||
}
|
||||
v := s1{127, 255}
|
||||
pv := &v
|
||||
vAddr := fmt.Sprintf("%p", pv)
|
||||
pvAddr := fmt.Sprintf("%p", &pv)
|
||||
vt := "spew_test.s1"
|
||||
vt2 := "int8"
|
||||
vt3 := "uint8"
|
||||
vs := "{\n a: (" + vt2 + ") 127,\n b: (" + vt3 + ") 255\n}"
|
||||
addDumpTest(v, "("+vt+") "+vs+"\n")
|
||||
addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n")
|
||||
addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n")
|
||||
|
||||
// Struct that contains another struct.
|
||||
type s2 struct {
|
||||
s1 s1
|
||||
b bool
|
||||
}
|
||||
v2 := s2{s1{127, 255}, true}
|
||||
pv2 := &v2
|
||||
v2Addr := fmt.Sprintf("%p", pv2)
|
||||
pv2Addr := fmt.Sprintf("%p", &pv2)
|
||||
v2t := "spew_test.s2"
|
||||
v2t2 := "spew_test.s1"
|
||||
v2t3 := "int8"
|
||||
v2t4 := "uint8"
|
||||
v2t5 := "bool"
|
||||
v2s := "{\n s1: (" + v2t2 + ") {\n a: (" + v2t3 + ") 127,\n b: (" +
|
||||
v2t4 + ") 255\n },\n b: (" + v2t5 + ") true\n}"
|
||||
addDumpTest(v2, "("+v2t+") "+v2s+"\n")
|
||||
addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n")
|
||||
addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n")
|
||||
|
||||
// Struct that contains custom type with Stringer pointer interface via both
|
||||
// exported and unexported fields.
|
||||
type s3 struct {
|
||||
s pstringer
|
||||
S pstringer
|
||||
}
|
||||
v3 := s3{"test", "test2"}
|
||||
pv3 := &v3
|
||||
v3Addr := fmt.Sprintf("%p", pv3)
|
||||
pv3Addr := fmt.Sprintf("%p", &pv3)
|
||||
v3t := "spew_test.s3"
|
||||
v3t2 := "spew_test.pstringer"
|
||||
v3s := "{\n s: (" + v3t2 + ") stringer test,\n S: (" + v3t2 +
|
||||
") stringer test2\n}"
|
||||
addDumpTest(v3, "("+v3t+") "+v3s+"\n")
|
||||
addDumpTest(pv3, "(*"+v3t+")("+v3Addr+")("+v3s+")\n")
|
||||
addDumpTest(&pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")("+v3s+")\n")
|
||||
}
|
||||
|
||||
func addUintptrTests() {
|
||||
// Null pointer.
|
||||
v := uintptr(0)
|
||||
pv := &v
|
||||
vAddr := fmt.Sprintf("%p", pv)
|
||||
pvAddr := fmt.Sprintf("%p", &pv)
|
||||
vt := "uintptr"
|
||||
vs := "<nil>"
|
||||
addDumpTest(v, "("+vt+") "+vs+"\n")
|
||||
addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n")
|
||||
addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n")
|
||||
|
||||
// Address of real variable.
|
||||
i := 1
|
||||
v2 := uintptr(unsafe.Pointer(&i))
|
||||
pv2 := &v2
|
||||
v2Addr := fmt.Sprintf("%p", pv2)
|
||||
pv2Addr := fmt.Sprintf("%p", &pv2)
|
||||
v2t := "uintptr"
|
||||
v2s := fmt.Sprintf("%p", &i)
|
||||
addDumpTest(v2, "("+v2t+") "+v2s+"\n")
|
||||
addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n")
|
||||
addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n")
|
||||
}
|
||||
|
||||
func addUnsafePointerTests() {
|
||||
// Null pointer.
|
||||
v := unsafe.Pointer(uintptr(0))
|
||||
pv := &v
|
||||
vAddr := fmt.Sprintf("%p", pv)
|
||||
pvAddr := fmt.Sprintf("%p", &pv)
|
||||
vt := "unsafe.Pointer"
|
||||
vs := "<nil>"
|
||||
addDumpTest(v, "("+vt+") "+vs+"\n")
|
||||
addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n")
|
||||
addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n")
|
||||
|
||||
// Address of real variable.
|
||||
i := 1
|
||||
v2 := unsafe.Pointer(&i)
|
||||
pv2 := &v2
|
||||
v2Addr := fmt.Sprintf("%p", pv2)
|
||||
pv2Addr := fmt.Sprintf("%p", &pv2)
|
||||
v2t := "unsafe.Pointer"
|
||||
v2s := fmt.Sprintf("%p", &i)
|
||||
addDumpTest(v2, "("+v2t+") "+v2s+"\n")
|
||||
addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n")
|
||||
addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n")
|
||||
}
|
||||
|
||||
func addChanTests() {
|
||||
// Nil channel.
|
||||
var v chan int
|
||||
pv := &v
|
||||
vAddr := fmt.Sprintf("%p", pv)
|
||||
pvAddr := fmt.Sprintf("%p", &pv)
|
||||
vt := "chan int"
|
||||
vs := "<nil>"
|
||||
addDumpTest(v, "("+vt+") "+vs+"\n")
|
||||
addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n")
|
||||
addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n")
|
||||
|
||||
// Real channel.
|
||||
v2 := make(chan int)
|
||||
pv2 := &v2
|
||||
v2Addr := fmt.Sprintf("%p", pv2)
|
||||
pv2Addr := fmt.Sprintf("%p", &pv2)
|
||||
v2t := "chan int"
|
||||
v2s := fmt.Sprintf("%p", v2)
|
||||
addDumpTest(v2, "("+v2t+") "+v2s+"\n")
|
||||
addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n")
|
||||
addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n")
|
||||
}
|
||||
|
||||
func addFuncTests() {
|
||||
// Function with no params and no returns.
|
||||
v := addIntTests
|
||||
pv := &v
|
||||
vAddr := fmt.Sprintf("%p", pv)
|
||||
pvAddr := fmt.Sprintf("%p", &pv)
|
||||
vt := "func()"
|
||||
vs := fmt.Sprintf("%p", v)
|
||||
addDumpTest(v, "("+vt+") "+vs+"\n")
|
||||
addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n")
|
||||
addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n")
|
||||
|
||||
// Function with param and no returns.
|
||||
v2 := TestDump
|
||||
pv2 := &v2
|
||||
v2Addr := fmt.Sprintf("%p", pv2)
|
||||
pv2Addr := fmt.Sprintf("%p", &pv2)
|
||||
v2t := "func(*testing.T)"
|
||||
v2s := fmt.Sprintf("%p", v2)
|
||||
addDumpTest(v2, "("+v2t+") "+v2s+"\n")
|
||||
addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n")
|
||||
addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n")
|
||||
|
||||
// Function with multiple params and multiple returns.
|
||||
var v3 = func(i int, s string) (b bool, err error) {
|
||||
return true, nil
|
||||
}
|
||||
pv3 := &v3
|
||||
v3Addr := fmt.Sprintf("%p", pv3)
|
||||
pv3Addr := fmt.Sprintf("%p", &pv3)
|
||||
v3t := "func(int, string) (bool, error)"
|
||||
v3s := fmt.Sprintf("%p", v3)
|
||||
addDumpTest(v3, "("+v3t+") "+v3s+"\n")
|
||||
addDumpTest(pv3, "(*"+v3t+")("+v3Addr+")("+v3s+")\n")
|
||||
addDumpTest(&pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")("+v3s+")\n")
|
||||
}
|
||||
|
||||
func addCircularTests() {
|
||||
// Struct that is circular through self referencing.
|
||||
type circular struct {
|
||||
c *circular
|
||||
}
|
||||
v := circular{nil}
|
||||
v.c = &v
|
||||
pv := &v
|
||||
vAddr := fmt.Sprintf("%p", pv)
|
||||
pvAddr := fmt.Sprintf("%p", &pv)
|
||||
vt := "spew_test.circular"
|
||||
vs := "{\n c: (*" + vt + ")(" + vAddr + ")({\n c: (*" + vt + ")(" +
|
||||
vAddr + ")(<already shown>)\n })\n}"
|
||||
vs2 := "{\n c: (*" + vt + ")(" + vAddr + ")(<already shown>)\n}"
|
||||
addDumpTest(v, "("+vt+") "+vs+"\n")
|
||||
addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs2+")\n")
|
||||
addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs2+")\n")
|
||||
|
||||
// Structs that are circular through cross referencing.
|
||||
v2 := xref1{nil}
|
||||
ts2 := xref2{&v2}
|
||||
v2.ps2 = &ts2
|
||||
pv2 := &v2
|
||||
ts2Addr := fmt.Sprintf("%p", &ts2)
|
||||
v2Addr := fmt.Sprintf("%p", pv2)
|
||||
pv2Addr := fmt.Sprintf("%p", &pv2)
|
||||
v2t := "spew_test.xref1"
|
||||
v2t2 := "spew_test.xref2"
|
||||
v2s := "{\n ps2: (*" + v2t2 + ")(" + ts2Addr + ")({\n ps1: (*" + v2t +
|
||||
")(" + v2Addr + ")({\n ps2: (*" + v2t2 + ")(" + ts2Addr +
|
||||
")(<already shown>)\n })\n })\n}"
|
||||
v2s2 := "{\n ps2: (*" + v2t2 + ")(" + ts2Addr + ")({\n ps1: (*" + v2t +
|
||||
")(" + v2Addr + ")(<already shown>)\n })\n}"
|
||||
addDumpTest(v2, "("+v2t+") "+v2s+"\n")
|
||||
addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s2+")\n")
|
||||
addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s2+")\n")
|
||||
|
||||
// Structs that are indirectly circular.
|
||||
v3 := indirCir1{nil}
|
||||
tic2 := indirCir2{nil}
|
||||
tic3 := indirCir3{&v3}
|
||||
tic2.ps3 = &tic3
|
||||
v3.ps2 = &tic2
|
||||
pv3 := &v3
|
||||
tic2Addr := fmt.Sprintf("%p", &tic2)
|
||||
tic3Addr := fmt.Sprintf("%p", &tic3)
|
||||
v3Addr := fmt.Sprintf("%p", pv3)
|
||||
pv3Addr := fmt.Sprintf("%p", &pv3)
|
||||
v3t := "spew_test.indirCir1"
|
||||
v3t2 := "spew_test.indirCir2"
|
||||
v3t3 := "spew_test.indirCir3"
|
||||
v3s := "{\n ps2: (*" + v3t2 + ")(" + tic2Addr + ")({\n ps3: (*" + v3t3 +
|
||||
")(" + tic3Addr + ")({\n ps1: (*" + v3t + ")(" + v3Addr +
|
||||
")({\n ps2: (*" + v3t2 + ")(" + tic2Addr +
|
||||
")(<already shown>)\n })\n })\n })\n}"
|
||||
v3s2 := "{\n ps2: (*" + v3t2 + ")(" + tic2Addr + ")({\n ps3: (*" + v3t3 +
|
||||
")(" + tic3Addr + ")({\n ps1: (*" + v3t + ")(" + v3Addr +
|
||||
")(<already shown>)\n })\n })\n}"
|
||||
addDumpTest(v3, "("+v3t+") "+v3s+"\n")
|
||||
addDumpTest(pv3, "(*"+v3t+")("+v3Addr+")("+v3s2+")\n")
|
||||
addDumpTest(&pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")("+v3s2+")\n")
|
||||
}
|
||||
|
||||
// TestDump executes all of the tests described by dumpTests.
|
||||
func TestDump(t *testing.T) {
|
||||
t.Logf("Running %d tests", len(dumpTests))
|
||||
for i, test := range dumpTests {
|
||||
buf := new(bytes.Buffer)
|
||||
spew.Fdump(buf, test.in)
|
||||
s := buf.String()
|
||||
if test.want != buf.String() {
|
||||
t.Errorf("Dump #%d\n got: %s want: %s", i, s, test.want)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Setup tests.f
|
||||
func init() {
|
||||
addIntTests()
|
||||
addUintTests()
|
||||
addBoolTests()
|
||||
addFloatTests()
|
||||
addComplexTests()
|
||||
addArrayTests()
|
||||
addSliceTests()
|
||||
addStringTests()
|
||||
addNilInterfaceTests()
|
||||
addMapTests()
|
||||
addStructTests()
|
||||
addUintptrTests()
|
||||
addUnsafePointerTests()
|
||||
addChanTests()
|
||||
addFuncTests()
|
||||
addCircularTests()
|
||||
}
|
|
@ -0,0 +1,132 @@
|
|||
/*
|
||||
* Copyright (c) 2013 Dave Collins <dave@davec.name>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
package spew_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
type Flag int
|
||||
|
||||
const (
|
||||
flagOne Flag = iota
|
||||
flagTwo
|
||||
)
|
||||
|
||||
var flagStrings = map[Flag]string{
|
||||
flagOne: "flagOne",
|
||||
flagTwo: "flagTwo",
|
||||
}
|
||||
|
||||
func (f Flag) String() string {
|
||||
if s, ok := flagStrings[f]; ok {
|
||||
return s
|
||||
}
|
||||
return fmt.Sprintf("Unknown flag (%d)", int(f))
|
||||
}
|
||||
|
||||
type Bar struct {
|
||||
flag Flag
|
||||
data uintptr
|
||||
}
|
||||
|
||||
type Foo struct {
|
||||
unexportedField Bar
|
||||
ExportedField map[interface{}]interface{}
|
||||
}
|
||||
|
||||
// This example demonstrates how to use Dump to dump variables to stdout.
|
||||
func ExampleDump() {
|
||||
// The following package level declarations are assumed for this example:
|
||||
/*
|
||||
type Flag int
|
||||
|
||||
const (
|
||||
flagOne Flag = iota
|
||||
flagTwo
|
||||
)
|
||||
|
||||
var flagStrings = map[Flag]string{
|
||||
flagOne: "flagOne",
|
||||
flagTwo: "flagTwo",
|
||||
}
|
||||
|
||||
func (f Flag) String() string {
|
||||
if s, ok := flagStrings[f]; ok {
|
||||
return s
|
||||
}
|
||||
return fmt.Sprintf("Unknown flag (%d)", int(f))
|
||||
}
|
||||
|
||||
type Bar struct {
|
||||
flag Flag
|
||||
data uintptr
|
||||
}
|
||||
|
||||
type Foo struct {
|
||||
unexportedField Bar
|
||||
ExportedField map[interface{}]interface{}
|
||||
}
|
||||
*/
|
||||
|
||||
// Setup some sample data structures for the example.
|
||||
bar := Bar{Flag(flagTwo), uintptr(0)}
|
||||
s1 := Foo{bar, map[interface{}]interface{}{"one": true}}
|
||||
f := Flag(5)
|
||||
|
||||
// Dump!
|
||||
spew.Dump(s1, f)
|
||||
|
||||
// Output:
|
||||
// (spew_test.Foo) {
|
||||
// unexportedField: (spew_test.Bar) {
|
||||
// flag: (spew_test.Flag) flagTwo,
|
||||
// data: (uintptr) <nil>
|
||||
// },
|
||||
// ExportedField: (map[interface {}]interface {}) {
|
||||
// (string) "one": (bool) true
|
||||
// }
|
||||
// }
|
||||
// (spew_test.Flag) Unknown flag (5)
|
||||
//
|
||||
}
|
||||
|
||||
// This example demonstrates how to use Printf to display a variable with a
|
||||
// format string and inline formatting.
|
||||
func ExamplePrintf() {
|
||||
// Create a double pointer to a uint 8.
|
||||
ui8 := uint8(5)
|
||||
pui8 := &ui8
|
||||
ppui8 := &pui8
|
||||
|
||||
// Create a circular data type.
|
||||
type circular struct {
|
||||
ui8 uint8
|
||||
c *circular
|
||||
}
|
||||
c := circular{ui8: 1}
|
||||
c.c = &c
|
||||
|
||||
// Print!
|
||||
spew.Printf("ppui8: %v\n", ppui8)
|
||||
spew.Printf("circular: %v\n", c)
|
||||
|
||||
// Output:
|
||||
// ppui8: <**>5
|
||||
// circular: {1 <*>{1 <*><shown>}}
|
||||
}
|
|
@ -0,0 +1,338 @@
|
|||
/*
|
||||
* Copyright (c) 2013 Dave Collins <dave@davec.name>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
package spew
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// supportedFlags is a list of all the character flags supported by fmt package.
|
||||
const supportedFlags = "0-+# "
|
||||
|
||||
// formatState implements the fmt.Formatter interface and contains information
|
||||
// about the state of a formatting operation. The NewFormatter function can
|
||||
// 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{}
|
||||
buffer bytes.Buffer
|
||||
depth int
|
||||
pointers map[uintptr]int // Holds map of points and depth they were seen at
|
||||
fs fmt.State
|
||||
}
|
||||
|
||||
// buildDefaultFormat recreates the original format string without precision
|
||||
// and width information to pass in to fmt.Sprintf in the case of an
|
||||
// unrecognized type. Unless new types are added to the language, this
|
||||
// function won't ever be called.
|
||||
func (f *formatState) buildDefaultFormat() (format string) {
|
||||
buf := bytes.NewBuffer(percentBytes)
|
||||
|
||||
for _, flag := range supportedFlags {
|
||||
if f.fs.Flag(int(flag)) {
|
||||
buf.WriteRune(flag)
|
||||
}
|
||||
}
|
||||
|
||||
buf.WriteRune('v')
|
||||
|
||||
format = buf.String()
|
||||
return format
|
||||
}
|
||||
|
||||
// constructOrigFormat recreates the original format string including precision
|
||||
// and width information to pass along to the standard fmt package. This allows
|
||||
// automatic deferral of all format strings this package doesn't support.
|
||||
func (f *formatState) constructOrigFormat(verb rune) (format string) {
|
||||
buf := bytes.NewBuffer(percentBytes)
|
||||
|
||||
for _, flag := range supportedFlags {
|
||||
if f.fs.Flag(int(flag)) {
|
||||
buf.WriteRune(flag)
|
||||
}
|
||||
}
|
||||
|
||||
if width, ok := f.fs.Width(); ok {
|
||||
buf.WriteString(strconv.Itoa(width))
|
||||
}
|
||||
|
||||
if precision, ok := f.fs.Precision(); ok {
|
||||
buf.Write(precisionBytes)
|
||||
buf.WriteString(strconv.Itoa(precision))
|
||||
}
|
||||
|
||||
buf.WriteRune(verb)
|
||||
|
||||
format = buf.String()
|
||||
return format
|
||||
}
|
||||
|
||||
// formatPtr handles formatting of pointers by indirecting them as necessary.
|
||||
func (f *formatState) formatPtr(v reflect.Value) {
|
||||
// Display nil if top level poiner is nil.
|
||||
if v.IsNil() {
|
||||
f.buffer.Write(nilAngleBytes)
|
||||
return
|
||||
}
|
||||
|
||||
// Remove pointers at or below the current depth from map used to detect
|
||||
// circular refs.
|
||||
for k, depth := range f.pointers {
|
||||
if depth >= f.depth {
|
||||
delete(f.pointers, k)
|
||||
}
|
||||
}
|
||||
|
||||
plusSyntax := f.fs.Flag('+')
|
||||
|
||||
// Keep list of all dereferenced pointers to possibly show later.
|
||||
pointerChain := make([]uintptr, 0)
|
||||
|
||||
// Figure out how many levels of indirection there are by derferencing
|
||||
// pointers and unpacking interfaces down the chain while detecting circular
|
||||
// references.
|
||||
nilFound := false
|
||||
cycleFound := false
|
||||
indirects := 0
|
||||
ve := v
|
||||
for ve.Kind() == reflect.Ptr {
|
||||
indirects++
|
||||
if ve.IsNil() {
|
||||
nilFound = true
|
||||
break
|
||||
}
|
||||
addr := ve.Pointer()
|
||||
pointerChain = append(pointerChain, addr)
|
||||
if pd, ok := f.pointers[addr]; ok && pd < f.depth {
|
||||
cycleFound = true
|
||||
break
|
||||
}
|
||||
f.pointers[addr] = f.depth
|
||||
|
||||
ve = ve.Elem()
|
||||
if ve.Kind() == reflect.Interface {
|
||||
if ve.IsNil() {
|
||||
nilFound = true
|
||||
break
|
||||
}
|
||||
ve = ve.Elem()
|
||||
}
|
||||
}
|
||||
|
||||
// Display indirection level.
|
||||
f.buffer.Write(openAngleBytes)
|
||||
f.buffer.WriteString(strings.Repeat("*", indirects))
|
||||
f.buffer.Write(closeAngleBytes)
|
||||
|
||||
// Display pointer information depending on flags.
|
||||
if plusSyntax && (len(pointerChain) > 0) {
|
||||
f.buffer.Write(openParenBytes)
|
||||
for i, addr := range pointerChain {
|
||||
if i > 0 {
|
||||
f.buffer.Write(pointerChainBytes)
|
||||
}
|
||||
printHexPtr(&f.buffer, addr)
|
||||
}
|
||||
f.buffer.Write(closeParenBytes)
|
||||
}
|
||||
|
||||
// Display dereferenced value.
|
||||
switch {
|
||||
case nilFound == true:
|
||||
f.buffer.Write(nilAngleBytes)
|
||||
|
||||
case cycleFound == true:
|
||||
f.buffer.Write(circularShortBytes)
|
||||
|
||||
default:
|
||||
f.format(ve)
|
||||
}
|
||||
}
|
||||
|
||||
// format is the main workhorse for providing the Formatter interface. It
|
||||
// uses the passed reflect value to figure out what kind of object we are
|
||||
// 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) {
|
||||
// Call error/Stringer interfaces if they exist and the handle methods
|
||||
// flag is enabled.
|
||||
kind := v.Kind()
|
||||
if !Config.DisableMethods {
|
||||
if (kind != reflect.Invalid) && (kind != reflect.Interface) {
|
||||
if handled := handleMethods(&f.buffer, v); handled {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch kind {
|
||||
case reflect.Invalid:
|
||||
f.buffer.Write(invalidAngleBytes)
|
||||
|
||||
case reflect.Bool:
|
||||
printBool(&f.buffer, v.Bool())
|
||||
|
||||
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
|
||||
printInt(&f.buffer, v.Int())
|
||||
|
||||
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
|
||||
printUint(&f.buffer, v.Uint())
|
||||
|
||||
case reflect.Float32:
|
||||
printFloat(&f.buffer, v.Float(), 32)
|
||||
|
||||
case reflect.Float64:
|
||||
printFloat(&f.buffer, v.Float(), 64)
|
||||
|
||||
case reflect.Complex64:
|
||||
printComplex(&f.buffer, v.Complex(), 32)
|
||||
|
||||
case reflect.Complex128:
|
||||
printComplex(&f.buffer, v.Complex(), 64)
|
||||
|
||||
case reflect.Array, reflect.Slice:
|
||||
f.buffer.WriteRune('[')
|
||||
f.depth++
|
||||
if (Config.MaxDepth != 0) && (f.depth > Config.MaxDepth) {
|
||||
f.buffer.Write(maxShortBytes)
|
||||
} else {
|
||||
numEntries := v.Len()
|
||||
for i := 0; i < numEntries; i++ {
|
||||
if i > 0 {
|
||||
f.buffer.WriteRune(' ')
|
||||
}
|
||||
f.format(unpackValue(v.Index(i)))
|
||||
}
|
||||
}
|
||||
f.depth--
|
||||
f.buffer.WriteRune(']')
|
||||
|
||||
case reflect.String:
|
||||
f.buffer.WriteString(v.String())
|
||||
|
||||
case reflect.Interface:
|
||||
// Do nothing. We should never get here due to unpackValue calls
|
||||
|
||||
case reflect.Map:
|
||||
f.buffer.Write(openMapBytes)
|
||||
f.depth++
|
||||
if (Config.MaxDepth != 0) && (f.depth > Config.MaxDepth) {
|
||||
f.buffer.Write(maxShortBytes)
|
||||
} else {
|
||||
keys := v.MapKeys()
|
||||
for i, key := range keys {
|
||||
if i > 0 {
|
||||
f.buffer.WriteRune(' ')
|
||||
}
|
||||
f.format(unpackValue(key))
|
||||
f.buffer.WriteRune(':')
|
||||
f.format(unpackValue(v.MapIndex(key)))
|
||||
}
|
||||
}
|
||||
f.depth--
|
||||
f.buffer.Write(closeMapBytes)
|
||||
|
||||
case reflect.Ptr:
|
||||
f.formatPtr(v)
|
||||
|
||||
case reflect.Struct:
|
||||
numFields := v.NumField()
|
||||
f.buffer.WriteRune('{')
|
||||
f.depth++
|
||||
if (Config.MaxDepth != 0) && (f.depth > Config.MaxDepth) {
|
||||
f.buffer.Write(maxShortBytes)
|
||||
} else {
|
||||
vt := v.Type()
|
||||
for i := 0; i < numFields; i++ {
|
||||
if i > 0 {
|
||||
f.buffer.WriteRune(' ')
|
||||
}
|
||||
vtf := vt.Field(i)
|
||||
if f.fs.Flag('+') {
|
||||
f.buffer.WriteString(vtf.Name)
|
||||
f.buffer.WriteRune(':')
|
||||
}
|
||||
f.format(unpackValue(v.Field(i)))
|
||||
}
|
||||
}
|
||||
f.depth--
|
||||
f.buffer.WriteRune('}')
|
||||
|
||||
case reflect.Uintptr:
|
||||
printHexPtr(&f.buffer, uintptr(v.Uint()))
|
||||
|
||||
case reflect.UnsafePointer, reflect.Chan, reflect.Func:
|
||||
printHexPtr(&f.buffer, v.Pointer())
|
||||
|
||||
// There were not any other types at the time this code was written, but
|
||||
// fall back to letting the default fmt package handle it if any get added.
|
||||
default:
|
||||
format := f.buildDefaultFormat()
|
||||
if v.CanInterface() {
|
||||
f.buffer.WriteString(fmt.Sprintf(format, v.Interface()))
|
||||
} else {
|
||||
f.buffer.WriteString(fmt.Sprintf(format, v.String()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Format satisfies the fmt.Formatter interface. See NewFormatter for usage
|
||||
// details.
|
||||
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('#')) {
|
||||
format := f.constructOrigFormat(verb)
|
||||
fmt.Fprintf(fs, format, f.value)
|
||||
return
|
||||
}
|
||||
|
||||
if f.value == nil {
|
||||
fmt.Fprint(fs, string(nilAngleBytes))
|
||||
return
|
||||
}
|
||||
|
||||
f.format(reflect.ValueOf(f.value))
|
||||
f.buffer.WriteTo(fs)
|
||||
}
|
||||
|
||||
/*
|
||||
NewFormatter returns a custom formatter that satisfies the fmt.Formatter
|
||||
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).
|
||||
|
||||
Typically this function shouldn't be called directly. It is much easier to make
|
||||
use of the custom formatter is to call one of the convenience functions such as
|
||||
Printf, Println, or Printf.
|
||||
*/
|
||||
func NewFormatter(v interface{}) (f fmt.Formatter) {
|
||||
fs := &formatState{value: v}
|
||||
fs.pointers = make(map[uintptr]int)
|
||||
return fs
|
||||
}
|
|
@ -0,0 +1,115 @@
|
|||
/*
|
||||
* Copyright (c) 2013 Dave Collins <dave@davec.name>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
package spew
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
// Errorf is a wrapper for fmt.Errorf that treats each argument as if it were
|
||||
// passed with a default Formatter interface returned by NewFormatter. It
|
||||
// returns the formatted string as a value that satisfies error. See
|
||||
// NewFormatter for formatting details.
|
||||
//
|
||||
// This function is shorthand for the following syntax:
|
||||
//
|
||||
// fmt.Errorf(format, spew.NewFormatter(a), spew.NewFormatter(b))
|
||||
func Errorf(format string, a ...interface{}) (err error) {
|
||||
return fmt.Errorf(format, convertArgs(a)...)
|
||||
}
|
||||
|
||||
// Fprint is a wrapper for fmt.Fprint that treats each argument as if it were
|
||||
// passed with a default Formatter interface returned by NewFormatter. It
|
||||
// returns the number of bytes written and any write error encountered. See
|
||||
// NewFormatter for formatting details.
|
||||
//
|
||||
// This function is shorthand for the following syntax:
|
||||
//
|
||||
// fmt.Fprint(w, spew.NewFormatter(a), spew.NewFormatter(b))
|
||||
func Fprint(w io.Writer, a ...interface{}) (n int, err error) {
|
||||
return fmt.Fprint(w, convertArgs(a)...)
|
||||
}
|
||||
|
||||
// Fprintf is a wrapper for fmt.Fprintf that treats each argument as if it were
|
||||
// passed with a default Formatter interface returned by NewFormatter. It
|
||||
// returns the number of bytes written and any write error encountered. See
|
||||
// NewFormatter for formatting details.
|
||||
//
|
||||
// This function is shorthand for the following syntax:
|
||||
//
|
||||
// fmt.Fprintf(w, format, spew.NewFormatter(a), spew.NewFormatter(b))
|
||||
func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
|
||||
return fmt.Fprintf(w, format, convertArgs(a)...)
|
||||
}
|
||||
|
||||
// Fprintln is a wrapper for fmt.Fprintln that treats each argument as if it
|
||||
// passed with a default Formatter interface returned by NewFormatter. See
|
||||
// NewFormatter for formatting details.
|
||||
//
|
||||
// This function is shorthand for the following syntax:
|
||||
//
|
||||
// fmt.Fprintln(w, spew.NewFormatter(a), spew.NewFormatter(b))
|
||||
func Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
|
||||
return fmt.Fprintln(w, convertArgs(a)...)
|
||||
}
|
||||
|
||||
// Print is a wrapper for fmt.Print that treats each argument as if it were
|
||||
// passed with a default Formatter interface returned by NewFormatter. It
|
||||
// returns the number of bytes written and any write error encountered. See
|
||||
// NewFormatter for formatting details.
|
||||
//
|
||||
// This function is shorthand for the following syntax:
|
||||
//
|
||||
// fmt.Print(spew.NewFormatter(a), spew.NewFormatter(b))
|
||||
func Print(a ...interface{}) (n int, err error) {
|
||||
return fmt.Print(convertArgs(a)...)
|
||||
}
|
||||
|
||||
// Printf is a wrapper for fmt.Printf that treats each argument as if it were
|
||||
// passed with a default Formatter interface returned by NewFormatter. It
|
||||
// returns the number of bytes written and any write error encountered. See
|
||||
// NewFormatter for formatting details.
|
||||
//
|
||||
// This function is shorthand for the following syntax:
|
||||
//
|
||||
// fmt.Printf(format, spew.NewFormatter(a), spew.NewFormatter(b))
|
||||
func Printf(format string, a ...interface{}) (n int, err error) {
|
||||
return fmt.Printf(format, convertArgs(a)...)
|
||||
}
|
||||
|
||||
// Println is a wrapper for fmt.Println that treats each argument as if it were
|
||||
// passed with a default Formatter interface returned by NewFormatter. It
|
||||
// returns the number of bytes written and any write error encountered. See
|
||||
// NewFormatter for formatting details.
|
||||
//
|
||||
// This function is shorthand for the following syntax:
|
||||
//
|
||||
// fmt.Println(spew.NewFormatter(a), spew.NewFormatter(b))
|
||||
func Println(a ...interface{}) (n int, err error) {
|
||||
return fmt.Println(convertArgs(a)...)
|
||||
}
|
||||
|
||||
// convertArgs accepts a slice of arguments and returns a slice of the same
|
||||
// length with each argument converted to a default spew Formatter interface.
|
||||
func convertArgs(args []interface{}) (formatters []interface{}) {
|
||||
formatters = make([]interface{}, len(args))
|
||||
for index, arg := range args {
|
||||
formatters[index] = NewFormatter(arg)
|
||||
}
|
||||
return formatters
|
||||
}
|
Loading…
Reference in New Issue