├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── cov_report.sh ├── spew ├── bypass.go ├── bypasssafe.go ├── common.go ├── common_test.go ├── config.go ├── doc.go ├── dump.go ├── dump_test.go ├── dumpcgo_test.go ├── dumpnocgo_test.go ├── example_test.go ├── format.go ├── format_test.go ├── internal_test.go ├── internalunsafe_test.go ├── spew.go ├── spew_test.go └── testdata │ └── dumpcgo.go └── test_coverage.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | go_import_path: github.com/davecgh/go-spew 3 | go: 4 | - 1.6.x 5 | - 1.7.x 6 | - 1.8.x 7 | - 1.9.x 8 | - 1.10.x 9 | - 1.11.x 10 | - tip 11 | sudo: false 12 | install: 13 | - go get -v github.com/alecthomas/gometalinter 14 | - gometalinter --install 15 | script: 16 | - export PATH=$PATH:$HOME/gopath/bin 17 | - export GORACE="halt_on_error=1" 18 | - test -z "$(gometalinter --disable-all 19 | --enable=gofmt 20 | --enable=golint 21 | --enable=vet 22 | --enable=gosimple 23 | --enable=unconvert 24 | --deadline=4m ./spew | tee /dev/stderr)" 25 | - go test -v -race -tags safe ./spew 26 | - go test -v -race -tags testcgo ./spew -covermode=atomic -coverprofile=profile.cov 27 | after_success: 28 | - go get -v github.com/mattn/goveralls 29 | - goveralls -coverprofile=profile.cov -service=travis-ci 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | ISC License 2 | 3 | Copyright (c) 2012-2016 Dave Collins 4 | 5 | Permission to use, copy, modify, and/or distribute this software for any 6 | purpose with or without fee is hereby granted, provided that the above 7 | copyright notice and this permission notice appear in all copies. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | go-spew 2 | ======= 3 | 4 | [![Build Status](https://img.shields.io/travis/davecgh/go-spew.svg)](https://travis-ci.org/davecgh/go-spew) 5 | [![ISC License](http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org) 6 | [![Coverage Status](https://img.shields.io/coveralls/davecgh/go-spew.svg)](https://coveralls.io/r/davecgh/go-spew?branch=master) 7 | 8 | Go-spew implements a deep pretty printer for Go data structures to aid in 9 | debugging. A comprehensive suite of tests with 100% test coverage is provided 10 | to ensure proper functionality. See `test_coverage.txt` for the gocov coverage 11 | report. Go-spew is licensed under the liberal ISC license, so it may be used in 12 | open source or commercial projects. 13 | 14 | If you're interested in reading about how this package came to life and some 15 | of the challenges involved in providing a deep pretty printer, there is a blog 16 | post about it 17 | [here](https://web.archive.org/web/20160304013555/https://blog.cyphertite.com/go-spew-a-journey-into-dumping-go-data-structures/). 18 | 19 | ## Documentation 20 | 21 | [![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)](http://godoc.org/github.com/davecgh/go-spew/spew) 22 | 23 | Full `go doc` style documentation for the project can be viewed online without 24 | installing this package by using the excellent GoDoc site here: 25 | http://godoc.org/github.com/davecgh/go-spew/spew 26 | 27 | You can also view the documentation locally once the package is installed with 28 | the `godoc` tool by running `godoc -http=":6060"` and pointing your browser to 29 | http://localhost:6060/pkg/github.com/davecgh/go-spew/spew 30 | 31 | ## Installation 32 | 33 | ```bash 34 | $ go get -u github.com/davecgh/go-spew/spew 35 | ``` 36 | 37 | ## Quick Start 38 | 39 | Add this import line to the file you're working in: 40 | 41 | ```Go 42 | import "github.com/davecgh/go-spew/spew" 43 | ``` 44 | 45 | To dump a variable with full newlines, indentation, type, and pointer 46 | information use Dump, Fdump, or Sdump: 47 | 48 | ```Go 49 | spew.Dump(myVar1, myVar2, ...) 50 | spew.Fdump(someWriter, myVar1, myVar2, ...) 51 | str := spew.Sdump(myVar1, myVar2, ...) 52 | ``` 53 | 54 | Alternatively, if you would prefer to use format strings with a compacted inline 55 | printing style, use the convenience wrappers Printf, Fprintf, etc with %v (most 56 | compact), %+v (adds pointer addresses), %#v (adds types), or %#+v (adds types 57 | and pointer addresses): 58 | 59 | ```Go 60 | spew.Printf("myVar1: %v -- myVar2: %+v", myVar1, myVar2) 61 | spew.Printf("myVar3: %#v -- myVar4: %#+v", myVar3, myVar4) 62 | spew.Fprintf(someWriter, "myVar1: %v -- myVar2: %+v", myVar1, myVar2) 63 | spew.Fprintf(someWriter, "myVar3: %#v -- myVar4: %#+v", myVar3, myVar4) 64 | ``` 65 | 66 | ## Debugging a Web Application Example 67 | 68 | Here is an example of how you can use `spew.Sdump()` to help debug a web application. Please be sure to wrap your output using the `html.EscapeString()` function for safety reasons. You should also only use this debugging technique in a development environment, never in production. 69 | 70 | ```Go 71 | package main 72 | 73 | import ( 74 | "fmt" 75 | "html" 76 | "net/http" 77 | 78 | "github.com/davecgh/go-spew/spew" 79 | ) 80 | 81 | func handler(w http.ResponseWriter, r *http.Request) { 82 | w.Header().Set("Content-Type", "text/html") 83 | fmt.Fprintf(w, "Hi there, %s!", r.URL.Path[1:]) 84 | fmt.Fprintf(w, "") 85 | } 86 | 87 | func main() { 88 | http.HandleFunc("/", handler) 89 | http.ListenAndServe(":8080", nil) 90 | } 91 | ``` 92 | 93 | ## Sample Dump Output 94 | 95 | ``` 96 | (main.Foo) { 97 | unexportedField: (*main.Bar)(0xf84002e210)({ 98 | flag: (main.Flag) flagTwo, 99 | data: (uintptr) 100 | }), 101 | ExportedField: (map[interface {}]interface {}) { 102 | (string) "one": (bool) true 103 | } 104 | } 105 | ([]uint8) { 106 | 00000000 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 |............... | 107 | 00000010 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 |!"#$%&'()*+,-./0| 108 | 00000020 31 32 |12| 109 | } 110 | ``` 111 | 112 | ## Sample Formatter Output 113 | 114 | Double pointer to a uint8: 115 | ``` 116 | %v: <**>5 117 | %+v: <**>(0xf8400420d0->0xf8400420c8)5 118 | %#v: (**uint8)5 119 | %#+v: (**uint8)(0xf8400420d0->0xf8400420c8)5 120 | ``` 121 | 122 | Pointer to circular struct with a uint8 field and a pointer to itself: 123 | ``` 124 | %v: <*>{1 <*>} 125 | %+v: <*>(0xf84003e260){ui8:1 c:<*>(0xf84003e260)} 126 | %#v: (*main.circular){ui8:(uint8)1 c:(*main.circular)} 127 | %#+v: (*main.circular)(0xf84003e260){ui8:(uint8)1 c:(*main.circular)(0xf84003e260)} 128 | ``` 129 | 130 | ## Configuration Options 131 | 132 | Configuration of spew is handled by fields in the ConfigState type. For 133 | convenience, all of the top-level functions use a global state available via the 134 | spew.Config global. 135 | 136 | It is also possible to create a ConfigState instance that provides methods 137 | equivalent to the top-level functions. This allows concurrent configuration 138 | options. See the ConfigState documentation for more details. 139 | 140 | ``` 141 | * Indent 142 | String to use for each indentation level for Dump functions. 143 | It is a single space by default. A popular alternative is "\t". 144 | 145 | * MaxDepth 146 | Maximum number of levels to descend into nested data structures. 147 | There is no limit by default. 148 | 149 | * DisableMethods 150 | Disables invocation of error and Stringer interface methods. 151 | Method invocation is enabled by default. 152 | 153 | * DisablePointerMethods 154 | Disables invocation of error and Stringer interface methods on types 155 | which only accept pointer receivers from non-pointer variables. This option 156 | relies on access to the unsafe package, so it will not have any effect when 157 | running in environments without access to the unsafe package such as Google 158 | App Engine or with the "safe" build tag specified. 159 | Pointer method invocation is enabled by default. 160 | 161 | * DisablePointerAddresses 162 | DisablePointerAddresses specifies whether to disable the printing of 163 | pointer addresses. This is useful when diffing data structures in tests. 164 | 165 | * DisableCapacities 166 | DisableCapacities specifies whether to disable the printing of capacities 167 | for arrays, slices, maps and channels. This is useful when diffing data 168 | structures in tests. 169 | 170 | * ContinueOnMethod 171 | Enables recursion into types after invoking error and Stringer interface 172 | methods. Recursion after method invocation is disabled by default. 173 | 174 | * SortKeys 175 | Specifies map keys should be sorted before being printed. Use 176 | this to have a more deterministic, diffable output. Note that 177 | only native types (bool, int, uint, floats, uintptr and string) 178 | and types which implement error or Stringer interfaces are supported, 179 | with other types sorted according to the reflect.Value.String() output 180 | which guarantees display stability. Natural map order is used by 181 | default. 182 | 183 | * SpewKeys 184 | SpewKeys specifies that, as a last resort attempt, map keys should be 185 | spewed to strings and sorted by those strings. This is only considered 186 | if SortKeys is true. 187 | 188 | ``` 189 | 190 | ## Unsafe Package Dependency 191 | 192 | This package relies on the unsafe package to perform some of the more advanced 193 | features, however it also supports a "limited" mode which allows it to work in 194 | environments where the unsafe package is not available. By default, it will 195 | operate in this mode on Google App Engine and when compiled with GopherJS. The 196 | "safe" build tag may also be specified to force the package to build without 197 | using the unsafe package. 198 | 199 | ## License 200 | 201 | Go-spew is licensed under the [copyfree](http://copyfree.org) ISC License. 202 | -------------------------------------------------------------------------------- /cov_report.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # This script uses gocov to generate a test coverage report. 4 | # The gocov tool my be obtained with the following command: 5 | # go get github.com/axw/gocov/gocov 6 | # 7 | # It will be installed to $GOPATH/bin, so ensure that location is in your $PATH. 8 | 9 | # Check for gocov. 10 | if ! type gocov >/dev/null 2>&1; then 11 | echo >&2 "This script requires the gocov tool." 12 | echo >&2 "You may obtain it with the following command:" 13 | echo >&2 "go get github.com/axw/gocov/gocov" 14 | exit 1 15 | fi 16 | 17 | # Only run the cgo tests if gcc is installed. 18 | if type gcc >/dev/null 2>&1; then 19 | (cd spew && gocov test -tags testcgo | gocov report) 20 | else 21 | (cd spew && gocov test | gocov report) 22 | fi 23 | -------------------------------------------------------------------------------- /spew/bypass.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015-2016 Dave Collins 2 | // 3 | // Permission to use, copy, modify, and distribute this software for any 4 | // purpose with or without fee is hereby granted, provided that the above 5 | // copyright notice and this permission notice appear in all copies. 6 | // 7 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | 15 | // NOTE: Due to the following build constraints, this file will only be compiled 16 | // when the code is not running on Google App Engine, compiled by GopherJS, and 17 | // "-tags safe" is not added to the go build command line. The "disableunsafe" 18 | // tag is deprecated and thus should not be used. 19 | // Go versions prior to 1.4 are disabled because they use a different layout 20 | // for interfaces which make the implementation of unsafeReflectValue more complex. 21 | // +build !js,!appengine,!safe,!disableunsafe,go1.4 22 | 23 | package spew 24 | 25 | import ( 26 | "reflect" 27 | "unsafe" 28 | ) 29 | 30 | const ( 31 | // UnsafeDisabled is a build-time constant which specifies whether or 32 | // not access to the unsafe package is available. 33 | UnsafeDisabled = false 34 | 35 | // ptrSize is the size of a pointer on the current arch. 36 | ptrSize = unsafe.Sizeof((*byte)(nil)) 37 | ) 38 | 39 | type flag uintptr 40 | 41 | var ( 42 | // flagRO indicates whether the value field of a reflect.Value 43 | // is read-only. 44 | flagRO flag 45 | 46 | // flagAddr indicates whether the address of the reflect.Value's 47 | // value may be taken. 48 | flagAddr flag 49 | ) 50 | 51 | // flagKindMask holds the bits that make up the kind 52 | // part of the flags field. In all the supported versions, 53 | // it is in the lower 5 bits. 54 | const flagKindMask = flag(0x1f) 55 | 56 | // Different versions of Go have used different 57 | // bit layouts for the flags type. This table 58 | // records the known combinations. 59 | var okFlags = []struct { 60 | ro, addr flag 61 | }{{ 62 | // From Go 1.4 to 1.5 63 | ro: 1 << 5, 64 | addr: 1 << 7, 65 | }, { 66 | // Up to Go tip. 67 | ro: 1<<5 | 1<<6, 68 | addr: 1 << 8, 69 | }} 70 | 71 | var flagValOffset = func() uintptr { 72 | field, ok := reflect.TypeOf(reflect.Value{}).FieldByName("flag") 73 | if !ok { 74 | panic("reflect.Value has no flag field") 75 | } 76 | return field.Offset 77 | }() 78 | 79 | // flagField returns a pointer to the flag field of a reflect.Value. 80 | func flagField(v *reflect.Value) *flag { 81 | return (*flag)(unsafe.Pointer(uintptr(unsafe.Pointer(v)) + flagValOffset)) 82 | } 83 | 84 | // unsafeReflectValue converts the passed reflect.Value into a one that bypasses 85 | // the typical safety restrictions preventing access to unaddressable and 86 | // unexported data. It works by digging the raw pointer to the underlying 87 | // value out of the protected value and generating a new unprotected (unsafe) 88 | // reflect.Value to it. 89 | // 90 | // This allows us to check for implementations of the Stringer and error 91 | // interfaces to be used for pretty printing ordinarily unaddressable and 92 | // inaccessible values such as unexported struct fields. 93 | func unsafeReflectValue(v reflect.Value) reflect.Value { 94 | if !v.IsValid() || (v.CanInterface() && v.CanAddr()) { 95 | return v 96 | } 97 | flagFieldPtr := flagField(&v) 98 | *flagFieldPtr &^= flagRO 99 | *flagFieldPtr |= flagAddr 100 | return v 101 | } 102 | 103 | // Sanity checks against future reflect package changes 104 | // to the type or semantics of the Value.flag field. 105 | func init() { 106 | field, ok := reflect.TypeOf(reflect.Value{}).FieldByName("flag") 107 | if !ok { 108 | panic("reflect.Value has no flag field") 109 | } 110 | if field.Type.Kind() != reflect.TypeOf(flag(0)).Kind() { 111 | panic("reflect.Value flag field has changed kind") 112 | } 113 | type t0 int 114 | var t struct { 115 | A t0 116 | // t0 will have flagEmbedRO set. 117 | t0 118 | // a will have flagStickyRO set 119 | a t0 120 | } 121 | vA := reflect.ValueOf(t).FieldByName("A") 122 | va := reflect.ValueOf(t).FieldByName("a") 123 | vt0 := reflect.ValueOf(t).FieldByName("t0") 124 | 125 | // Infer flagRO from the difference between the flags 126 | // for the (otherwise identical) fields in t. 127 | flagPublic := *flagField(&vA) 128 | flagWithRO := *flagField(&va) | *flagField(&vt0) 129 | flagRO = flagPublic ^ flagWithRO 130 | 131 | // Infer flagAddr from the difference between a value 132 | // taken from a pointer and not. 133 | vPtrA := reflect.ValueOf(&t).Elem().FieldByName("A") 134 | flagNoPtr := *flagField(&vA) 135 | flagPtr := *flagField(&vPtrA) 136 | flagAddr = flagNoPtr ^ flagPtr 137 | 138 | // Check that the inferred flags tally with one of the known versions. 139 | for _, f := range okFlags { 140 | if flagRO == f.ro && flagAddr == f.addr { 141 | return 142 | } 143 | } 144 | panic("reflect.Value read-only flag has changed semantics") 145 | } 146 | -------------------------------------------------------------------------------- /spew/bypasssafe.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015-2016 Dave Collins 2 | // 3 | // Permission to use, copy, modify, and distribute this software for any 4 | // purpose with or without fee is hereby granted, provided that the above 5 | // copyright notice and this permission notice appear in all copies. 6 | // 7 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | 15 | // NOTE: Due to the following build constraints, this file will only be compiled 16 | // when the code is running on Google App Engine, compiled by GopherJS, or 17 | // "-tags safe" is added to the go build command line. The "disableunsafe" 18 | // tag is deprecated and thus should not be used. 19 | // +build js appengine safe disableunsafe !go1.4 20 | 21 | package spew 22 | 23 | import "reflect" 24 | 25 | const ( 26 | // UnsafeDisabled is a build-time constant which specifies whether or 27 | // not access to the unsafe package is available. 28 | UnsafeDisabled = true 29 | ) 30 | 31 | // unsafeReflectValue typically converts the passed reflect.Value into a one 32 | // that bypasses the typical safety restrictions preventing access to 33 | // unaddressable and unexported data. However, doing this relies on access to 34 | // the unsafe package. This is a stub version which simply returns the passed 35 | // reflect.Value when the unsafe package is not available. 36 | func unsafeReflectValue(v reflect.Value) reflect.Value { 37 | return v 38 | } 39 | -------------------------------------------------------------------------------- /spew/common.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | package spew 18 | 19 | import ( 20 | "bytes" 21 | "fmt" 22 | "io" 23 | "reflect" 24 | "sort" 25 | "strconv" 26 | ) 27 | 28 | // Some constants in the form of bytes to avoid string overhead. This mirrors 29 | // the technique used in the fmt package. 30 | var ( 31 | panicBytes = []byte("(PANIC=") 32 | plusBytes = []byte("+") 33 | iBytes = []byte("i") 34 | trueBytes = []byte("true") 35 | falseBytes = []byte("false") 36 | interfaceBytes = []byte("(interface {})") 37 | commaNewlineBytes = []byte(",\n") 38 | newlineBytes = []byte("\n") 39 | openBraceBytes = []byte("{") 40 | openBraceNewlineBytes = []byte("{\n") 41 | closeBraceBytes = []byte("}") 42 | asteriskBytes = []byte("*") 43 | colonBytes = []byte(":") 44 | colonSpaceBytes = []byte(": ") 45 | openParenBytes = []byte("(") 46 | closeParenBytes = []byte(")") 47 | spaceBytes = []byte(" ") 48 | pointerChainBytes = []byte("->") 49 | nilAngleBytes = []byte("") 50 | maxNewlineBytes = []byte("\n") 51 | maxShortBytes = []byte("") 52 | circularBytes = []byte("") 53 | circularShortBytes = []byte("") 54 | invalidAngleBytes = []byte("") 55 | openBracketBytes = []byte("[") 56 | closeBracketBytes = []byte("]") 57 | percentBytes = []byte("%") 58 | precisionBytes = []byte(".") 59 | openAngleBytes = []byte("<") 60 | closeAngleBytes = []byte(">") 61 | openMapBytes = []byte("map[") 62 | closeMapBytes = []byte("]") 63 | lenEqualsBytes = []byte("len=") 64 | capEqualsBytes = []byte("cap=") 65 | ) 66 | 67 | // hexDigits is used to map a decimal value to a hex digit. 68 | var hexDigits = "0123456789abcdef" 69 | 70 | // catchPanic handles any panics that might occur during the handleMethods 71 | // calls. 72 | func catchPanic(w io.Writer, v reflect.Value) { 73 | if err := recover(); err != nil { 74 | w.Write(panicBytes) 75 | fmt.Fprintf(w, "%v", err) 76 | w.Write(closeParenBytes) 77 | } 78 | } 79 | 80 | // handleMethods attempts to call the Error and String methods on the underlying 81 | // type the passed reflect.Value represents and outputes the result to Writer w. 82 | // 83 | // It handles panics in any called methods by catching and displaying the error 84 | // as the formatted value. 85 | func handleMethods(cs *ConfigState, w io.Writer, v reflect.Value) (handled bool) { 86 | // We need an interface to check if the type implements the error or 87 | // Stringer interface. However, the reflect package won't give us an 88 | // interface on certain things like unexported struct fields in order 89 | // to enforce visibility rules. We use unsafe, when it's available, 90 | // to bypass these restrictions since this package does not mutate the 91 | // values. 92 | if !v.CanInterface() { 93 | if UnsafeDisabled { 94 | return false 95 | } 96 | 97 | v = unsafeReflectValue(v) 98 | } 99 | 100 | // Choose whether or not to do error and Stringer interface lookups against 101 | // the base type or a pointer to the base type depending on settings. 102 | // Technically calling one of these methods with a pointer receiver can 103 | // mutate the value, however, types which choose to satisify an error or 104 | // Stringer interface with a pointer receiver should not be mutating their 105 | // state inside these interface methods. 106 | if !cs.DisablePointerMethods && !UnsafeDisabled && !v.CanAddr() { 107 | v = unsafeReflectValue(v) 108 | } 109 | if v.CanAddr() { 110 | v = v.Addr() 111 | } 112 | 113 | // Is it an error or Stringer? 114 | switch iface := v.Interface().(type) { 115 | case error: 116 | defer catchPanic(w, v) 117 | if cs.ContinueOnMethod { 118 | w.Write(openParenBytes) 119 | w.Write([]byte(iface.Error())) 120 | w.Write(closeParenBytes) 121 | w.Write(spaceBytes) 122 | return false 123 | } 124 | 125 | w.Write([]byte(iface.Error())) 126 | return true 127 | 128 | case fmt.Stringer: 129 | defer catchPanic(w, v) 130 | if cs.ContinueOnMethod { 131 | w.Write(openParenBytes) 132 | w.Write([]byte(iface.String())) 133 | w.Write(closeParenBytes) 134 | w.Write(spaceBytes) 135 | return false 136 | } 137 | w.Write([]byte(iface.String())) 138 | return true 139 | } 140 | return false 141 | } 142 | 143 | // printBool outputs a boolean value as true or false to Writer w. 144 | func printBool(w io.Writer, val bool) { 145 | if val { 146 | w.Write(trueBytes) 147 | } else { 148 | w.Write(falseBytes) 149 | } 150 | } 151 | 152 | // printInt outputs a signed integer value to Writer w. 153 | func printInt(w io.Writer, val int64, base int) { 154 | w.Write([]byte(strconv.FormatInt(val, base))) 155 | } 156 | 157 | // printUint outputs an unsigned integer value to Writer w. 158 | func printUint(w io.Writer, val uint64, base int) { 159 | w.Write([]byte(strconv.FormatUint(val, base))) 160 | } 161 | 162 | // printFloat outputs a floating point value using the specified precision, 163 | // which is expected to be 32 or 64bit, to Writer w. 164 | func printFloat(w io.Writer, val float64, precision int) { 165 | w.Write([]byte(strconv.FormatFloat(val, 'g', -1, precision))) 166 | } 167 | 168 | // printComplex outputs a complex value using the specified float precision 169 | // for the real and imaginary parts to Writer w. 170 | func printComplex(w io.Writer, c complex128, floatPrecision int) { 171 | r := real(c) 172 | w.Write(openParenBytes) 173 | w.Write([]byte(strconv.FormatFloat(r, 'g', -1, floatPrecision))) 174 | i := imag(c) 175 | if i >= 0 { 176 | w.Write(plusBytes) 177 | } 178 | w.Write([]byte(strconv.FormatFloat(i, 'g', -1, floatPrecision))) 179 | w.Write(iBytes) 180 | w.Write(closeParenBytes) 181 | } 182 | 183 | // printHexPtr outputs a uintptr formatted as hexadecimal with a leading '0x' 184 | // prefix to Writer w. 185 | func printHexPtr(w io.Writer, p uintptr) { 186 | // Null pointer. 187 | num := uint64(p) 188 | if num == 0 { 189 | w.Write(nilAngleBytes) 190 | return 191 | } 192 | 193 | // Max uint64 is 16 bytes in hex + 2 bytes for '0x' prefix 194 | buf := make([]byte, 18) 195 | 196 | // It's simpler to construct the hex string right to left. 197 | base := uint64(16) 198 | i := len(buf) - 1 199 | for num >= base { 200 | buf[i] = hexDigits[num%base] 201 | num /= base 202 | i-- 203 | } 204 | buf[i] = hexDigits[num] 205 | 206 | // Add '0x' prefix. 207 | i-- 208 | buf[i] = 'x' 209 | i-- 210 | buf[i] = '0' 211 | 212 | // Strip unused leading bytes. 213 | buf = buf[i:] 214 | w.Write(buf) 215 | } 216 | 217 | // valuesSorter implements sort.Interface to allow a slice of reflect.Value 218 | // elements to be sorted. 219 | type valuesSorter struct { 220 | values []reflect.Value 221 | strings []string // either nil or same len and values 222 | cs *ConfigState 223 | } 224 | 225 | // newValuesSorter initializes a valuesSorter instance, which holds a set of 226 | // surrogate keys on which the data should be sorted. It uses flags in 227 | // ConfigState to decide if and how to populate those surrogate keys. 228 | func newValuesSorter(values []reflect.Value, cs *ConfigState) sort.Interface { 229 | vs := &valuesSorter{values: values, cs: cs} 230 | if canSortSimply(vs.values[0].Kind()) { 231 | return vs 232 | } 233 | if !cs.DisableMethods { 234 | vs.strings = make([]string, len(values)) 235 | for i := range vs.values { 236 | b := bytes.Buffer{} 237 | if !handleMethods(cs, &b, vs.values[i]) { 238 | vs.strings = nil 239 | break 240 | } 241 | vs.strings[i] = b.String() 242 | } 243 | } 244 | if vs.strings == nil && cs.SpewKeys { 245 | vs.strings = make([]string, len(values)) 246 | for i := range vs.values { 247 | vs.strings[i] = Sprintf("%#v", vs.values[i].Interface()) 248 | } 249 | } 250 | return vs 251 | } 252 | 253 | // canSortSimply tests whether a reflect.Kind is a primitive that can be sorted 254 | // directly, or whether it should be considered for sorting by surrogate keys 255 | // (if the ConfigState allows it). 256 | func canSortSimply(kind reflect.Kind) bool { 257 | // This switch parallels valueSortLess, except for the default case. 258 | switch kind { 259 | case reflect.Bool: 260 | return true 261 | case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int: 262 | return true 263 | case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint: 264 | return true 265 | case reflect.Float32, reflect.Float64: 266 | return true 267 | case reflect.String: 268 | return true 269 | case reflect.Uintptr: 270 | return true 271 | case reflect.Array: 272 | return true 273 | } 274 | return false 275 | } 276 | 277 | // Len returns the number of values in the slice. It is part of the 278 | // sort.Interface implementation. 279 | func (s *valuesSorter) Len() int { 280 | return len(s.values) 281 | } 282 | 283 | // Swap swaps the values at the passed indices. It is part of the 284 | // sort.Interface implementation. 285 | func (s *valuesSorter) Swap(i, j int) { 286 | s.values[i], s.values[j] = s.values[j], s.values[i] 287 | if s.strings != nil { 288 | s.strings[i], s.strings[j] = s.strings[j], s.strings[i] 289 | } 290 | } 291 | 292 | // valueSortLess returns whether the first value should sort before the second 293 | // value. It is used by valueSorter.Less as part of the sort.Interface 294 | // implementation. 295 | func valueSortLess(a, b reflect.Value) bool { 296 | switch a.Kind() { 297 | case reflect.Bool: 298 | return !a.Bool() && b.Bool() 299 | case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int: 300 | return a.Int() < b.Int() 301 | case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint: 302 | return a.Uint() < b.Uint() 303 | case reflect.Float32, reflect.Float64: 304 | return a.Float() < b.Float() 305 | case reflect.String: 306 | return a.String() < b.String() 307 | case reflect.Uintptr: 308 | return a.Uint() < b.Uint() 309 | case reflect.Array: 310 | // Compare the contents of both arrays. 311 | l := a.Len() 312 | for i := 0; i < l; i++ { 313 | av := a.Index(i) 314 | bv := b.Index(i) 315 | if av.Interface() == bv.Interface() { 316 | continue 317 | } 318 | return valueSortLess(av, bv) 319 | } 320 | } 321 | return a.String() < b.String() 322 | } 323 | 324 | // Less returns whether the value at index i should sort before the 325 | // value at index j. It is part of the sort.Interface implementation. 326 | func (s *valuesSorter) Less(i, j int) bool { 327 | if s.strings == nil { 328 | return valueSortLess(s.values[i], s.values[j]) 329 | } 330 | return s.strings[i] < s.strings[j] 331 | } 332 | 333 | // sortValues is a sort function that handles both native types and any type that 334 | // can be converted to error or Stringer. Other inputs are sorted according to 335 | // their Value.String() value to ensure display stability. 336 | func sortValues(values []reflect.Value, cs *ConfigState) { 337 | if len(values) == 0 { 338 | return 339 | } 340 | sort.Sort(newValuesSorter(values, cs)) 341 | } 342 | -------------------------------------------------------------------------------- /spew/common_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | package spew_test 18 | 19 | import ( 20 | "fmt" 21 | "reflect" 22 | "testing" 23 | 24 | "github.com/davecgh/go-spew/spew" 25 | ) 26 | 27 | // custom type to test Stinger interface on non-pointer receiver. 28 | type stringer string 29 | 30 | // String implements the Stringer interface for testing invocation of custom 31 | // stringers on types with non-pointer receivers. 32 | func (s stringer) String() string { 33 | return "stringer " + string(s) 34 | } 35 | 36 | // custom type to test Stinger interface on pointer receiver. 37 | type pstringer string 38 | 39 | // String implements the Stringer interface for testing invocation of custom 40 | // stringers on types with only pointer receivers. 41 | func (s *pstringer) String() string { 42 | return "stringer " + string(*s) 43 | } 44 | 45 | // xref1 and xref2 are cross referencing structs for testing circular reference 46 | // detection. 47 | type xref1 struct { 48 | ps2 *xref2 49 | } 50 | type xref2 struct { 51 | ps1 *xref1 52 | } 53 | 54 | // indirCir1, indirCir2, and indirCir3 are used to generate an indirect circular 55 | // reference for testing detection. 56 | type indirCir1 struct { 57 | ps2 *indirCir2 58 | } 59 | type indirCir2 struct { 60 | ps3 *indirCir3 61 | } 62 | type indirCir3 struct { 63 | ps1 *indirCir1 64 | } 65 | 66 | // embed is used to test embedded structures. 67 | type embed struct { 68 | a string 69 | } 70 | 71 | // embedwrap is used to test embedded structures. 72 | type embedwrap struct { 73 | *embed 74 | e *embed 75 | } 76 | 77 | // panicer is used to intentionally cause a panic for testing spew properly 78 | // handles them 79 | type panicer int 80 | 81 | func (p panicer) String() string { 82 | panic("test panic") 83 | } 84 | 85 | // customError is used to test custom error interface invocation. 86 | type customError int 87 | 88 | func (e customError) Error() string { 89 | return fmt.Sprintf("error: %d", int(e)) 90 | } 91 | 92 | // stringizeWants converts a slice of wanted test output into a format suitable 93 | // for a test error message. 94 | func stringizeWants(wants []string) string { 95 | s := "" 96 | for i, want := range wants { 97 | if i > 0 { 98 | s += fmt.Sprintf("want%d: %s", i+1, want) 99 | } else { 100 | s += "want: " + want 101 | } 102 | } 103 | return s 104 | } 105 | 106 | // testFailed returns whether or not a test failed by checking if the result 107 | // of the test is in the slice of wanted strings. 108 | func testFailed(result string, wants []string) bool { 109 | for _, want := range wants { 110 | if result == want { 111 | return false 112 | } 113 | } 114 | return true 115 | } 116 | 117 | type sortableStruct struct { 118 | x int 119 | } 120 | 121 | func (ss sortableStruct) String() string { 122 | return fmt.Sprintf("ss.%d", ss.x) 123 | } 124 | 125 | type unsortableStruct struct { 126 | x int 127 | } 128 | 129 | type sortTestCase struct { 130 | input []reflect.Value 131 | expected []reflect.Value 132 | } 133 | 134 | func helpTestSortValues(tests []sortTestCase, cs *spew.ConfigState, t *testing.T) { 135 | getInterfaces := func(values []reflect.Value) []interface{} { 136 | interfaces := []interface{}{} 137 | for _, v := range values { 138 | interfaces = append(interfaces, v.Interface()) 139 | } 140 | return interfaces 141 | } 142 | 143 | for _, test := range tests { 144 | spew.SortValues(test.input, cs) 145 | // reflect.DeepEqual cannot really make sense of reflect.Value, 146 | // probably because of all the pointer tricks. For instance, 147 | // v(2.0) != v(2.0) on a 32-bits system. Turn them into interface{} 148 | // instead. 149 | input := getInterfaces(test.input) 150 | expected := getInterfaces(test.expected) 151 | if !reflect.DeepEqual(input, expected) { 152 | t.Errorf("Sort mismatch:\n %v != %v", input, expected) 153 | } 154 | } 155 | } 156 | 157 | // TestSortValues ensures the sort functionality for relect.Value based sorting 158 | // works as intended. 159 | func TestSortValues(t *testing.T) { 160 | v := reflect.ValueOf 161 | 162 | a := v("a") 163 | b := v("b") 164 | c := v("c") 165 | embedA := v(embed{"a"}) 166 | embedB := v(embed{"b"}) 167 | embedC := v(embed{"c"}) 168 | tests := []sortTestCase{ 169 | // No values. 170 | { 171 | []reflect.Value{}, 172 | []reflect.Value{}, 173 | }, 174 | // Bools. 175 | { 176 | []reflect.Value{v(false), v(true), v(false)}, 177 | []reflect.Value{v(false), v(false), v(true)}, 178 | }, 179 | // Ints. 180 | { 181 | []reflect.Value{v(2), v(1), v(3)}, 182 | []reflect.Value{v(1), v(2), v(3)}, 183 | }, 184 | // Uints. 185 | { 186 | []reflect.Value{v(uint8(2)), v(uint8(1)), v(uint8(3))}, 187 | []reflect.Value{v(uint8(1)), v(uint8(2)), v(uint8(3))}, 188 | }, 189 | // Floats. 190 | { 191 | []reflect.Value{v(2.0), v(1.0), v(3.0)}, 192 | []reflect.Value{v(1.0), v(2.0), v(3.0)}, 193 | }, 194 | // Strings. 195 | { 196 | []reflect.Value{b, a, c}, 197 | []reflect.Value{a, b, c}, 198 | }, 199 | // Array 200 | { 201 | []reflect.Value{v([3]int{3, 2, 1}), v([3]int{1, 3, 2}), v([3]int{1, 2, 3})}, 202 | []reflect.Value{v([3]int{1, 2, 3}), v([3]int{1, 3, 2}), v([3]int{3, 2, 1})}, 203 | }, 204 | // Uintptrs. 205 | { 206 | []reflect.Value{v(uintptr(2)), v(uintptr(1)), v(uintptr(3))}, 207 | []reflect.Value{v(uintptr(1)), v(uintptr(2)), v(uintptr(3))}, 208 | }, 209 | // SortableStructs. 210 | { 211 | // Note: not sorted - DisableMethods is set. 212 | []reflect.Value{v(sortableStruct{2}), v(sortableStruct{1}), v(sortableStruct{3})}, 213 | []reflect.Value{v(sortableStruct{2}), v(sortableStruct{1}), v(sortableStruct{3})}, 214 | }, 215 | // UnsortableStructs. 216 | { 217 | // Note: not sorted - SpewKeys is false. 218 | []reflect.Value{v(unsortableStruct{2}), v(unsortableStruct{1}), v(unsortableStruct{3})}, 219 | []reflect.Value{v(unsortableStruct{2}), v(unsortableStruct{1}), v(unsortableStruct{3})}, 220 | }, 221 | // Invalid. 222 | { 223 | []reflect.Value{embedB, embedA, embedC}, 224 | []reflect.Value{embedB, embedA, embedC}, 225 | }, 226 | } 227 | cs := spew.ConfigState{DisableMethods: true, SpewKeys: false} 228 | helpTestSortValues(tests, &cs, t) 229 | } 230 | 231 | // TestSortValuesWithMethods ensures the sort functionality for relect.Value 232 | // based sorting works as intended when using string methods. 233 | func TestSortValuesWithMethods(t *testing.T) { 234 | v := reflect.ValueOf 235 | 236 | a := v("a") 237 | b := v("b") 238 | c := v("c") 239 | tests := []sortTestCase{ 240 | // Ints. 241 | { 242 | []reflect.Value{v(2), v(1), v(3)}, 243 | []reflect.Value{v(1), v(2), v(3)}, 244 | }, 245 | // Strings. 246 | { 247 | []reflect.Value{b, a, c}, 248 | []reflect.Value{a, b, c}, 249 | }, 250 | // SortableStructs. 251 | { 252 | []reflect.Value{v(sortableStruct{2}), v(sortableStruct{1}), v(sortableStruct{3})}, 253 | []reflect.Value{v(sortableStruct{1}), v(sortableStruct{2}), v(sortableStruct{3})}, 254 | }, 255 | // UnsortableStructs. 256 | { 257 | // Note: not sorted - SpewKeys is false. 258 | []reflect.Value{v(unsortableStruct{2}), v(unsortableStruct{1}), v(unsortableStruct{3})}, 259 | []reflect.Value{v(unsortableStruct{2}), v(unsortableStruct{1}), v(unsortableStruct{3})}, 260 | }, 261 | } 262 | cs := spew.ConfigState{DisableMethods: false, SpewKeys: false} 263 | helpTestSortValues(tests, &cs, t) 264 | } 265 | 266 | // TestSortValuesWithSpew ensures the sort functionality for relect.Value 267 | // based sorting works as intended when using spew to stringify keys. 268 | func TestSortValuesWithSpew(t *testing.T) { 269 | v := reflect.ValueOf 270 | 271 | a := v("a") 272 | b := v("b") 273 | c := v("c") 274 | tests := []sortTestCase{ 275 | // Ints. 276 | { 277 | []reflect.Value{v(2), v(1), v(3)}, 278 | []reflect.Value{v(1), v(2), v(3)}, 279 | }, 280 | // Strings. 281 | { 282 | []reflect.Value{b, a, c}, 283 | []reflect.Value{a, b, c}, 284 | }, 285 | // SortableStructs. 286 | { 287 | []reflect.Value{v(sortableStruct{2}), v(sortableStruct{1}), v(sortableStruct{3})}, 288 | []reflect.Value{v(sortableStruct{1}), v(sortableStruct{2}), v(sortableStruct{3})}, 289 | }, 290 | // UnsortableStructs. 291 | { 292 | []reflect.Value{v(unsortableStruct{2}), v(unsortableStruct{1}), v(unsortableStruct{3})}, 293 | []reflect.Value{v(unsortableStruct{1}), v(unsortableStruct{2}), v(unsortableStruct{3})}, 294 | }, 295 | } 296 | cs := spew.ConfigState{DisableMethods: true, SpewKeys: true} 297 | helpTestSortValues(tests, &cs, t) 298 | } 299 | -------------------------------------------------------------------------------- /spew/config.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | package spew 18 | 19 | import ( 20 | "bytes" 21 | "fmt" 22 | "io" 23 | "os" 24 | ) 25 | 26 | // ConfigState houses the configuration options used by spew to format and 27 | // display values. There is a global instance, Config, that is used to control 28 | // all top-level Formatter and Dump functionality. Each ConfigState instance 29 | // provides methods equivalent to the top-level functions. 30 | // 31 | // The zero value for ConfigState provides no indentation. You would typically 32 | // want to set it to a space or a tab. 33 | // 34 | // Alternatively, you can use NewDefaultConfig to get a ConfigState instance 35 | // with default settings. See the documentation of NewDefaultConfig for default 36 | // values. 37 | type ConfigState struct { 38 | // Indent specifies the string to use for each indentation level. The 39 | // global config instance that all top-level functions use set this to a 40 | // single space by default. If you would like more indentation, you might 41 | // set this to a tab with "\t" or perhaps two spaces with " ". 42 | Indent string 43 | 44 | // MaxDepth controls the maximum number of levels to descend into nested 45 | // data structures. The default, 0, means there is no limit. 46 | // 47 | // NOTE: Circular data structures are properly detected, so it is not 48 | // necessary to set this value unless you specifically want to limit deeply 49 | // nested data structures. 50 | MaxDepth int 51 | 52 | // DisableMethods specifies whether or not error and Stringer interfaces are 53 | // invoked for types that implement them. 54 | DisableMethods bool 55 | 56 | // DisablePointerMethods specifies whether or not to check for and invoke 57 | // error and Stringer interfaces on types which only accept a pointer 58 | // receiver when the current type is not a pointer. 59 | // 60 | // NOTE: This might be an unsafe action since calling one of these methods 61 | // with a pointer receiver could technically mutate the value, however, 62 | // in practice, types which choose to satisify an error or Stringer 63 | // interface with a pointer receiver should not be mutating their state 64 | // inside these interface methods. As a result, this option relies on 65 | // access to the unsafe package, so it will not have any effect when 66 | // running in environments without access to the unsafe package such as 67 | // Google App Engine or with the "safe" build tag specified. 68 | DisablePointerMethods bool 69 | 70 | // DisablePointerAddresses specifies whether to disable the printing of 71 | // pointer addresses. This is useful when diffing data structures in tests. 72 | DisablePointerAddresses bool 73 | 74 | // DisableCapacities specifies whether to disable the printing of capacities 75 | // for arrays, slices, maps and channels. This is useful when diffing 76 | // data structures in tests. 77 | DisableCapacities bool 78 | 79 | // ContinueOnMethod specifies whether or not recursion should continue once 80 | // a custom error or Stringer interface is invoked. The default, false, 81 | // means it will print the results of invoking the custom error or Stringer 82 | // interface and return immediately instead of continuing to recurse into 83 | // the internals of the data type. 84 | // 85 | // NOTE: This flag does not have any effect if method invocation is disabled 86 | // via the DisableMethods or DisablePointerMethods options. 87 | ContinueOnMethod bool 88 | 89 | // SortKeys specifies map keys should be sorted before being printed. Use 90 | // this to have a more deterministic, diffable output. Note that only 91 | // native types (bool, int, uint, floats, uintptr and string) and types 92 | // that support the error or Stringer interfaces (if methods are 93 | // enabled) are supported, with other types sorted according to the 94 | // reflect.Value.String() output which guarantees display stability. 95 | SortKeys bool 96 | 97 | // SpewKeys specifies that, as a last resort attempt, map keys should 98 | // be spewed to strings and sorted by those strings. This is only 99 | // considered if SortKeys is true. 100 | SpewKeys bool 101 | } 102 | 103 | // Config is the active configuration of the top-level functions. 104 | // The configuration can be changed by modifying the contents of spew.Config. 105 | var Config = ConfigState{Indent: " "} 106 | 107 | // Errorf is a wrapper for fmt.Errorf that treats each argument as if it were 108 | // passed with a Formatter interface returned by c.NewFormatter. It returns 109 | // the formatted string as a value that satisfies error. See NewFormatter 110 | // for formatting details. 111 | // 112 | // This function is shorthand for the following syntax: 113 | // 114 | // fmt.Errorf(format, c.NewFormatter(a), c.NewFormatter(b)) 115 | func (c *ConfigState) Errorf(format string, a ...interface{}) (err error) { 116 | return fmt.Errorf(format, c.convertArgs(a)...) 117 | } 118 | 119 | // Fprint is a wrapper for fmt.Fprint that treats each argument as if it were 120 | // passed with a Formatter interface returned by c.NewFormatter. It returns 121 | // the number of bytes written and any write error encountered. See 122 | // NewFormatter for formatting details. 123 | // 124 | // This function is shorthand for the following syntax: 125 | // 126 | // fmt.Fprint(w, c.NewFormatter(a), c.NewFormatter(b)) 127 | func (c *ConfigState) Fprint(w io.Writer, a ...interface{}) (n int, err error) { 128 | return fmt.Fprint(w, c.convertArgs(a)...) 129 | } 130 | 131 | // Fprintf is a wrapper for fmt.Fprintf that treats each argument as if it were 132 | // passed with a Formatter interface returned by c.NewFormatter. It returns 133 | // the number of bytes written and any write error encountered. See 134 | // NewFormatter for formatting details. 135 | // 136 | // This function is shorthand for the following syntax: 137 | // 138 | // fmt.Fprintf(w, format, c.NewFormatter(a), c.NewFormatter(b)) 139 | func (c *ConfigState) Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) { 140 | return fmt.Fprintf(w, format, c.convertArgs(a)...) 141 | } 142 | 143 | // Fprintln is a wrapper for fmt.Fprintln that treats each argument as if it 144 | // passed with a Formatter interface returned by c.NewFormatter. See 145 | // NewFormatter for formatting details. 146 | // 147 | // This function is shorthand for the following syntax: 148 | // 149 | // fmt.Fprintln(w, c.NewFormatter(a), c.NewFormatter(b)) 150 | func (c *ConfigState) Fprintln(w io.Writer, a ...interface{}) (n int, err error) { 151 | return fmt.Fprintln(w, c.convertArgs(a)...) 152 | } 153 | 154 | // Print is a wrapper for fmt.Print that treats each argument as if it were 155 | // passed with a Formatter interface returned by c.NewFormatter. It returns 156 | // the number of bytes written and any write error encountered. See 157 | // NewFormatter for formatting details. 158 | // 159 | // This function is shorthand for the following syntax: 160 | // 161 | // fmt.Print(c.NewFormatter(a), c.NewFormatter(b)) 162 | func (c *ConfigState) Print(a ...interface{}) (n int, err error) { 163 | return fmt.Print(c.convertArgs(a)...) 164 | } 165 | 166 | // Printf is a wrapper for fmt.Printf that treats each argument as if it were 167 | // passed with a Formatter interface returned by c.NewFormatter. It returns 168 | // the number of bytes written and any write error encountered. See 169 | // NewFormatter for formatting details. 170 | // 171 | // This function is shorthand for the following syntax: 172 | // 173 | // fmt.Printf(format, c.NewFormatter(a), c.NewFormatter(b)) 174 | func (c *ConfigState) Printf(format string, a ...interface{}) (n int, err error) { 175 | return fmt.Printf(format, c.convertArgs(a)...) 176 | } 177 | 178 | // Println is a wrapper for fmt.Println that treats each argument as if it were 179 | // passed with a Formatter interface returned by c.NewFormatter. It returns 180 | // the number of bytes written and any write error encountered. See 181 | // NewFormatter for formatting details. 182 | // 183 | // This function is shorthand for the following syntax: 184 | // 185 | // fmt.Println(c.NewFormatter(a), c.NewFormatter(b)) 186 | func (c *ConfigState) Println(a ...interface{}) (n int, err error) { 187 | return fmt.Println(c.convertArgs(a)...) 188 | } 189 | 190 | // Sprint is a wrapper for fmt.Sprint that treats each argument as if it were 191 | // passed with a Formatter interface returned by c.NewFormatter. It returns 192 | // the resulting string. See NewFormatter for formatting details. 193 | // 194 | // This function is shorthand for the following syntax: 195 | // 196 | // fmt.Sprint(c.NewFormatter(a), c.NewFormatter(b)) 197 | func (c *ConfigState) Sprint(a ...interface{}) string { 198 | return fmt.Sprint(c.convertArgs(a)...) 199 | } 200 | 201 | // Sprintf is a wrapper for fmt.Sprintf that treats each argument as if it were 202 | // passed with a Formatter interface returned by c.NewFormatter. It returns 203 | // the resulting string. See NewFormatter for formatting details. 204 | // 205 | // This function is shorthand for the following syntax: 206 | // 207 | // fmt.Sprintf(format, c.NewFormatter(a), c.NewFormatter(b)) 208 | func (c *ConfigState) Sprintf(format string, a ...interface{}) string { 209 | return fmt.Sprintf(format, c.convertArgs(a)...) 210 | } 211 | 212 | // Sprintln is a wrapper for fmt.Sprintln that treats each argument as if it 213 | // were passed with a Formatter interface returned by c.NewFormatter. It 214 | // returns the resulting string. See NewFormatter for formatting details. 215 | // 216 | // This function is shorthand for the following syntax: 217 | // 218 | // fmt.Sprintln(c.NewFormatter(a), c.NewFormatter(b)) 219 | func (c *ConfigState) Sprintln(a ...interface{}) string { 220 | return fmt.Sprintln(c.convertArgs(a)...) 221 | } 222 | 223 | /* 224 | NewFormatter returns a custom formatter that satisfies the fmt.Formatter 225 | interface. As a result, it integrates cleanly with standard fmt package 226 | printing functions. The formatter is useful for inline printing of smaller data 227 | types similar to the standard %v format specifier. 228 | 229 | The custom formatter only responds to the %v (most compact), %+v (adds pointer 230 | addresses), %#v (adds types), and %#+v (adds types and pointer addresses) verb 231 | combinations. Any other verbs such as %x and %q will be sent to the the 232 | standard fmt package for formatting. In addition, the custom formatter ignores 233 | the width and precision arguments (however they will still work on the format 234 | specifiers not handled by the custom formatter). 235 | 236 | Typically this function shouldn't be called directly. It is much easier to make 237 | use of the custom formatter by calling one of the convenience functions such as 238 | c.Printf, c.Println, or c.Printf. 239 | */ 240 | func (c *ConfigState) NewFormatter(v interface{}) fmt.Formatter { 241 | return newFormatter(c, v) 242 | } 243 | 244 | // Fdump formats and displays the passed arguments to io.Writer w. It formats 245 | // exactly the same as Dump. 246 | func (c *ConfigState) Fdump(w io.Writer, a ...interface{}) { 247 | fdump(c, w, a...) 248 | } 249 | 250 | /* 251 | Dump displays the passed parameters to standard out with newlines, customizable 252 | indentation, and additional debug information such as complete types and all 253 | pointer addresses used to indirect to the final value. It provides the 254 | following features over the built-in printing facilities provided by the fmt 255 | package: 256 | 257 | * Pointers are dereferenced and followed 258 | * Circular data structures are detected and handled properly 259 | * Custom Stringer/error interfaces are optionally invoked, including 260 | on unexported types 261 | * Custom types which only implement the Stringer/error interfaces via 262 | a pointer receiver are optionally invoked when passing non-pointer 263 | variables 264 | * Byte arrays and slices are dumped like the hexdump -C command which 265 | includes offsets, byte values in hex, and ASCII output 266 | 267 | The configuration options are controlled by modifying the public members 268 | of c. See ConfigState for options documentation. 269 | 270 | See Fdump if you would prefer dumping to an arbitrary io.Writer or Sdump to 271 | get the formatted result as a string. 272 | */ 273 | func (c *ConfigState) Dump(a ...interface{}) { 274 | fdump(c, os.Stdout, a...) 275 | } 276 | 277 | // Sdump returns a string with the passed arguments formatted exactly the same 278 | // as Dump. 279 | func (c *ConfigState) Sdump(a ...interface{}) string { 280 | var buf bytes.Buffer 281 | fdump(c, &buf, a...) 282 | return buf.String() 283 | } 284 | 285 | // convertArgs accepts a slice of arguments and returns a slice of the same 286 | // length with each argument converted to a spew Formatter interface using 287 | // the ConfigState associated with s. 288 | func (c *ConfigState) convertArgs(args []interface{}) (formatters []interface{}) { 289 | formatters = make([]interface{}, len(args)) 290 | for index, arg := range args { 291 | formatters[index] = newFormatter(c, arg) 292 | } 293 | return formatters 294 | } 295 | 296 | // NewDefaultConfig returns a ConfigState with the following default settings. 297 | // 298 | // Indent: " " 299 | // MaxDepth: 0 300 | // DisableMethods: false 301 | // DisablePointerMethods: false 302 | // ContinueOnMethod: false 303 | // SortKeys: false 304 | func NewDefaultConfig() *ConfigState { 305 | return &ConfigState{Indent: " "} 306 | } 307 | -------------------------------------------------------------------------------- /spew/doc.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | /* 18 | Package spew implements a deep pretty printer for Go data structures to aid in 19 | debugging. 20 | 21 | A quick overview of the additional features spew provides over the built-in 22 | printing facilities for Go data types are as follows: 23 | 24 | * Pointers are dereferenced and followed 25 | * Circular data structures are detected and handled properly 26 | * Custom Stringer/error interfaces are optionally invoked, including 27 | on unexported types 28 | * Custom types which only implement the Stringer/error interfaces via 29 | a pointer receiver are optionally invoked when passing non-pointer 30 | variables 31 | * Byte arrays and slices are dumped like the hexdump -C command which 32 | includes offsets, byte values in hex, and ASCII output (only when using 33 | Dump style) 34 | 35 | There are two different approaches spew allows for dumping Go data structures: 36 | 37 | * Dump style which prints with newlines, customizable indentation, 38 | and additional debug information such as types and all pointer addresses 39 | used to indirect to the final value 40 | * A custom Formatter interface that integrates cleanly with the standard fmt 41 | package and replaces %v, %+v, %#v, and %#+v to provide inline printing 42 | similar to the default %v while providing the additional functionality 43 | outlined above and passing unsupported format verbs such as %x and %q 44 | along to fmt 45 | 46 | Quick Start 47 | 48 | This section demonstrates how to quickly get started with spew. See the 49 | sections below for further details on formatting and configuration options. 50 | 51 | To dump a variable with full newlines, indentation, type, and pointer 52 | information use Dump, Fdump, or Sdump: 53 | spew.Dump(myVar1, myVar2, ...) 54 | spew.Fdump(someWriter, myVar1, myVar2, ...) 55 | str := spew.Sdump(myVar1, myVar2, ...) 56 | 57 | Alternatively, if you would prefer to use format strings with a compacted inline 58 | printing style, use the convenience wrappers Printf, Fprintf, etc with 59 | %v (most compact), %+v (adds pointer addresses), %#v (adds types), or 60 | %#+v (adds types and pointer addresses): 61 | spew.Printf("myVar1: %v -- myVar2: %+v", myVar1, myVar2) 62 | spew.Printf("myVar3: %#v -- myVar4: %#+v", myVar3, myVar4) 63 | spew.Fprintf(someWriter, "myVar1: %v -- myVar2: %+v", myVar1, myVar2) 64 | spew.Fprintf(someWriter, "myVar3: %#v -- myVar4: %#+v", myVar3, myVar4) 65 | 66 | Configuration Options 67 | 68 | Configuration of spew is handled by fields in the ConfigState type. For 69 | convenience, all of the top-level functions use a global state available 70 | via the spew.Config global. 71 | 72 | It is also possible to create a ConfigState instance that provides methods 73 | equivalent to the top-level functions. This allows concurrent configuration 74 | options. See the ConfigState documentation for more details. 75 | 76 | The following configuration options are available: 77 | * Indent 78 | String to use for each indentation level for Dump functions. 79 | It is a single space by default. A popular alternative is "\t". 80 | 81 | * MaxDepth 82 | Maximum number of levels to descend into nested data structures. 83 | There is no limit by default. 84 | 85 | * DisableMethods 86 | Disables invocation of error and Stringer interface methods. 87 | Method invocation is enabled by default. 88 | 89 | * DisablePointerMethods 90 | Disables invocation of error and Stringer interface methods on types 91 | which only accept pointer receivers from non-pointer variables. 92 | Pointer method invocation is enabled by default. 93 | 94 | * DisablePointerAddresses 95 | DisablePointerAddresses specifies whether to disable the printing of 96 | pointer addresses. This is useful when diffing data structures in tests. 97 | 98 | * DisableCapacities 99 | DisableCapacities specifies whether to disable the printing of 100 | capacities for arrays, slices, maps and channels. This is useful when 101 | diffing data structures in tests. 102 | 103 | * ContinueOnMethod 104 | Enables recursion into types after invoking error and Stringer interface 105 | methods. Recursion after method invocation is disabled by default. 106 | 107 | * SortKeys 108 | Specifies map keys should be sorted before being printed. Use 109 | this to have a more deterministic, diffable output. Note that 110 | only native types (bool, int, uint, floats, uintptr and string) 111 | and types which implement error or Stringer interfaces are 112 | supported with other types sorted according to the 113 | reflect.Value.String() output which guarantees display 114 | stability. Natural map order is used by default. 115 | 116 | * SpewKeys 117 | Specifies that, as a last resort attempt, map keys should be 118 | spewed to strings and sorted by those strings. This is only 119 | considered if SortKeys is true. 120 | 121 | Dump Usage 122 | 123 | Simply call spew.Dump with a list of variables you want to dump: 124 | 125 | spew.Dump(myVar1, myVar2, ...) 126 | 127 | You may also call spew.Fdump if you would prefer to output to an arbitrary 128 | io.Writer. For example, to dump to standard error: 129 | 130 | spew.Fdump(os.Stderr, myVar1, myVar2, ...) 131 | 132 | A third option is to call spew.Sdump to get the formatted output as a string: 133 | 134 | str := spew.Sdump(myVar1, myVar2, ...) 135 | 136 | Sample Dump Output 137 | 138 | See the Dump example for details on the setup of the types and variables being 139 | shown here. 140 | 141 | (main.Foo) { 142 | unexportedField: (*main.Bar)(0xf84002e210)({ 143 | flag: (main.Flag) flagTwo, 144 | data: (uintptr) 145 | }), 146 | ExportedField: (map[interface {}]interface {}) (len=1) { 147 | (string) (len=3) "one": (bool) true 148 | } 149 | } 150 | 151 | Byte (and uint8) arrays and slices are displayed uniquely like the hexdump -C 152 | command as shown. 153 | ([]uint8) (len=32 cap=32) { 154 | 00000000 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 |............... | 155 | 00000010 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 |!"#$%&'()*+,-./0| 156 | 00000020 31 32 |12| 157 | } 158 | 159 | Custom Formatter 160 | 161 | Spew provides a custom formatter that implements the fmt.Formatter interface 162 | so that it integrates cleanly with standard fmt package printing functions. The 163 | formatter is useful for inline printing of smaller data types similar to the 164 | standard %v format specifier. 165 | 166 | The custom formatter only responds to the %v (most compact), %+v (adds pointer 167 | addresses), %#v (adds types), or %#+v (adds types and pointer addresses) verb 168 | combinations. Any other verbs such as %x and %q will be sent to the the 169 | standard fmt package for formatting. In addition, the custom formatter ignores 170 | the width and precision arguments (however they will still work on the format 171 | specifiers not handled by the custom formatter). 172 | 173 | Custom Formatter Usage 174 | 175 | The simplest way to make use of the spew custom formatter is to call one of the 176 | convenience functions such as spew.Printf, spew.Println, or spew.Printf. The 177 | functions have syntax you are most likely already familiar with: 178 | 179 | spew.Printf("myVar1: %v -- myVar2: %+v", myVar1, myVar2) 180 | spew.Printf("myVar3: %#v -- myVar4: %#+v", myVar3, myVar4) 181 | spew.Println(myVar, myVar2) 182 | spew.Fprintf(os.Stderr, "myVar1: %v -- myVar2: %+v", myVar1, myVar2) 183 | spew.Fprintf(os.Stderr, "myVar3: %#v -- myVar4: %#+v", myVar3, myVar4) 184 | 185 | See the Index for the full list convenience functions. 186 | 187 | Sample Formatter Output 188 | 189 | Double pointer to a uint8: 190 | %v: <**>5 191 | %+v: <**>(0xf8400420d0->0xf8400420c8)5 192 | %#v: (**uint8)5 193 | %#+v: (**uint8)(0xf8400420d0->0xf8400420c8)5 194 | 195 | Pointer to circular struct with a uint8 field and a pointer to itself: 196 | %v: <*>{1 <*>} 197 | %+v: <*>(0xf84003e260){ui8:1 c:<*>(0xf84003e260)} 198 | %#v: (*main.circular){ui8:(uint8)1 c:(*main.circular)} 199 | %#+v: (*main.circular)(0xf84003e260){ui8:(uint8)1 c:(*main.circular)(0xf84003e260)} 200 | 201 | See the Printf example for details on the setup of variables being shown 202 | here. 203 | 204 | Errors 205 | 206 | Since it is possible for custom Stringer/error interfaces to panic, spew 207 | detects them and handles them internally by printing the panic information 208 | inline with the output. Since spew is intended to provide deep pretty printing 209 | capabilities on structures, it intentionally does not return any errors. 210 | */ 211 | package spew 212 | -------------------------------------------------------------------------------- /spew/dump.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | package spew 18 | 19 | import ( 20 | "bytes" 21 | "encoding/hex" 22 | "fmt" 23 | "io" 24 | "os" 25 | "reflect" 26 | "regexp" 27 | "strconv" 28 | "strings" 29 | ) 30 | 31 | var ( 32 | // uint8Type is a reflect.Type representing a uint8. It is used to 33 | // convert cgo types to uint8 slices for hexdumping. 34 | uint8Type = reflect.TypeOf(uint8(0)) 35 | 36 | // cCharRE is a regular expression that matches a cgo char. 37 | // It is used to detect character arrays to hexdump them. 38 | cCharRE = regexp.MustCompile(`^.*\._Ctype_char$`) 39 | 40 | // cUnsignedCharRE is a regular expression that matches a cgo unsigned 41 | // char. It is used to detect unsigned character arrays to hexdump 42 | // them. 43 | cUnsignedCharRE = regexp.MustCompile(`^.*\._Ctype_unsignedchar$`) 44 | 45 | // cUint8tCharRE is a regular expression that matches a cgo uint8_t. 46 | // It is used to detect uint8_t arrays to hexdump them. 47 | cUint8tCharRE = regexp.MustCompile(`^.*\._Ctype_uint8_t$`) 48 | ) 49 | 50 | // dumpState contains information about the state of a dump operation. 51 | type dumpState struct { 52 | w io.Writer 53 | depth int 54 | pointers map[uintptr]int 55 | ignoreNextType bool 56 | ignoreNextIndent bool 57 | cs *ConfigState 58 | } 59 | 60 | // indent performs indentation according to the depth level and cs.Indent 61 | // option. 62 | func (d *dumpState) indent() { 63 | if d.ignoreNextIndent { 64 | d.ignoreNextIndent = false 65 | return 66 | } 67 | d.w.Write(bytes.Repeat([]byte(d.cs.Indent), d.depth)) 68 | } 69 | 70 | // unpackValue returns values inside of non-nil interfaces when possible. 71 | // This is useful for data types like structs, arrays, slices, and maps which 72 | // can contain varying types packed inside an interface. 73 | func (d *dumpState) unpackValue(v reflect.Value) reflect.Value { 74 | if v.Kind() == reflect.Interface && !v.IsNil() { 75 | v = v.Elem() 76 | } 77 | return v 78 | } 79 | 80 | // dumpPtr handles formatting of pointers by indirecting them as necessary. 81 | func (d *dumpState) dumpPtr(v reflect.Value) { 82 | // Remove pointers at or below the current depth from map used to detect 83 | // circular refs. 84 | for k, depth := range d.pointers { 85 | if depth >= d.depth { 86 | delete(d.pointers, k) 87 | } 88 | } 89 | 90 | // Keep list of all dereferenced pointers to show later. 91 | pointerChain := make([]uintptr, 0) 92 | 93 | // Figure out how many levels of indirection there are by dereferencing 94 | // pointers and unpacking interfaces down the chain while detecting circular 95 | // references. 96 | nilFound := false 97 | cycleFound := false 98 | indirects := 0 99 | ve := v 100 | for ve.Kind() == reflect.Ptr { 101 | if ve.IsNil() { 102 | nilFound = true 103 | break 104 | } 105 | indirects++ 106 | addr := ve.Pointer() 107 | pointerChain = append(pointerChain, addr) 108 | if pd, ok := d.pointers[addr]; ok && pd < d.depth { 109 | cycleFound = true 110 | indirects-- 111 | break 112 | } 113 | d.pointers[addr] = d.depth 114 | 115 | ve = ve.Elem() 116 | if ve.Kind() == reflect.Interface { 117 | if ve.IsNil() { 118 | nilFound = true 119 | break 120 | } 121 | ve = ve.Elem() 122 | } 123 | } 124 | 125 | // Display type information. 126 | d.w.Write(openParenBytes) 127 | d.w.Write(bytes.Repeat(asteriskBytes, indirects)) 128 | d.w.Write([]byte(ve.Type().String())) 129 | d.w.Write(closeParenBytes) 130 | 131 | // Display pointer information. 132 | if !d.cs.DisablePointerAddresses && len(pointerChain) > 0 { 133 | d.w.Write(openParenBytes) 134 | for i, addr := range pointerChain { 135 | if i > 0 { 136 | d.w.Write(pointerChainBytes) 137 | } 138 | printHexPtr(d.w, addr) 139 | } 140 | d.w.Write(closeParenBytes) 141 | } 142 | 143 | // Display dereferenced value. 144 | d.w.Write(openParenBytes) 145 | switch { 146 | case nilFound: 147 | d.w.Write(nilAngleBytes) 148 | 149 | case cycleFound: 150 | d.w.Write(circularBytes) 151 | 152 | default: 153 | d.ignoreNextType = true 154 | d.dump(ve) 155 | } 156 | d.w.Write(closeParenBytes) 157 | } 158 | 159 | // dumpSlice handles formatting of arrays and slices. Byte (uint8 under 160 | // reflection) arrays and slices are dumped in hexdump -C fashion. 161 | func (d *dumpState) dumpSlice(v reflect.Value) { 162 | // Determine whether this type should be hex dumped or not. Also, 163 | // for types which should be hexdumped, try to use the underlying data 164 | // first, then fall back to trying to convert them to a uint8 slice. 165 | var buf []uint8 166 | doConvert := false 167 | doHexDump := false 168 | numEntries := v.Len() 169 | if numEntries > 0 { 170 | vt := v.Index(0).Type() 171 | vts := vt.String() 172 | switch { 173 | // C types that need to be converted. 174 | case cCharRE.MatchString(vts): 175 | fallthrough 176 | case cUnsignedCharRE.MatchString(vts): 177 | fallthrough 178 | case cUint8tCharRE.MatchString(vts): 179 | doConvert = true 180 | 181 | // Try to use existing uint8 slices and fall back to converting 182 | // and copying if that fails. 183 | case vt.Kind() == reflect.Uint8: 184 | // We need an addressable interface to convert the type 185 | // to a byte slice. However, the reflect package won't 186 | // give us an interface on certain things like 187 | // unexported struct fields in order to enforce 188 | // visibility rules. We use unsafe, when available, to 189 | // bypass these restrictions since this package does not 190 | // mutate the values. 191 | vs := v 192 | if !vs.CanInterface() || !vs.CanAddr() { 193 | vs = unsafeReflectValue(vs) 194 | } 195 | if !UnsafeDisabled { 196 | vs = vs.Slice(0, numEntries) 197 | 198 | // Use the existing uint8 slice if it can be 199 | // type asserted. 200 | iface := vs.Interface() 201 | if slice, ok := iface.([]uint8); ok { 202 | buf = slice 203 | doHexDump = true 204 | break 205 | } 206 | } 207 | 208 | // The underlying data needs to be converted if it can't 209 | // be type asserted to a uint8 slice. 210 | doConvert = true 211 | } 212 | 213 | // Copy and convert the underlying type if needed. 214 | if doConvert && vt.ConvertibleTo(uint8Type) { 215 | // Convert and copy each element into a uint8 byte 216 | // slice. 217 | buf = make([]uint8, numEntries) 218 | for i := 0; i < numEntries; i++ { 219 | vv := v.Index(i) 220 | buf[i] = uint8(vv.Convert(uint8Type).Uint()) 221 | } 222 | doHexDump = true 223 | } 224 | } 225 | 226 | // Hexdump the entire slice as needed. 227 | if doHexDump { 228 | indent := strings.Repeat(d.cs.Indent, d.depth) 229 | str := indent + hex.Dump(buf) 230 | str = strings.Replace(str, "\n", "\n"+indent, -1) 231 | str = strings.TrimRight(str, d.cs.Indent) 232 | d.w.Write([]byte(str)) 233 | return 234 | } 235 | 236 | // Recursively call dump for each item. 237 | for i := 0; i < numEntries; i++ { 238 | d.dump(d.unpackValue(v.Index(i))) 239 | if i < (numEntries - 1) { 240 | d.w.Write(commaNewlineBytes) 241 | } else { 242 | d.w.Write(newlineBytes) 243 | } 244 | } 245 | } 246 | 247 | // dump is the main workhorse for dumping a value. It uses the passed reflect 248 | // value to figure out what kind of object we are dealing with and formats it 249 | // appropriately. It is a recursive function, however circular data structures 250 | // are detected and handled properly. 251 | func (d *dumpState) dump(v reflect.Value) { 252 | // Handle invalid reflect values immediately. 253 | kind := v.Kind() 254 | if kind == reflect.Invalid { 255 | d.w.Write(invalidAngleBytes) 256 | return 257 | } 258 | 259 | // Handle pointers specially. 260 | if kind == reflect.Ptr { 261 | d.indent() 262 | d.dumpPtr(v) 263 | return 264 | } 265 | 266 | // Print type information unless already handled elsewhere. 267 | if !d.ignoreNextType { 268 | d.indent() 269 | d.w.Write(openParenBytes) 270 | d.w.Write([]byte(v.Type().String())) 271 | d.w.Write(closeParenBytes) 272 | d.w.Write(spaceBytes) 273 | } 274 | d.ignoreNextType = false 275 | 276 | // Display length and capacity if the built-in len and cap functions 277 | // work with the value's kind and the len/cap itself is non-zero. 278 | valueLen, valueCap := 0, 0 279 | switch v.Kind() { 280 | case reflect.Array, reflect.Slice, reflect.Chan: 281 | valueLen, valueCap = v.Len(), v.Cap() 282 | case reflect.Map, reflect.String: 283 | valueLen = v.Len() 284 | } 285 | if valueLen != 0 || !d.cs.DisableCapacities && valueCap != 0 { 286 | d.w.Write(openParenBytes) 287 | if valueLen != 0 { 288 | d.w.Write(lenEqualsBytes) 289 | printInt(d.w, int64(valueLen), 10) 290 | } 291 | if !d.cs.DisableCapacities && valueCap != 0 { 292 | if valueLen != 0 { 293 | d.w.Write(spaceBytes) 294 | } 295 | d.w.Write(capEqualsBytes) 296 | printInt(d.w, int64(valueCap), 10) 297 | } 298 | d.w.Write(closeParenBytes) 299 | d.w.Write(spaceBytes) 300 | } 301 | 302 | // Call Stringer/error interfaces if they exist and the handle methods flag 303 | // is enabled 304 | if !d.cs.DisableMethods { 305 | if (kind != reflect.Invalid) && (kind != reflect.Interface) { 306 | if handled := handleMethods(d.cs, d.w, v); handled { 307 | return 308 | } 309 | } 310 | } 311 | 312 | switch kind { 313 | case reflect.Invalid: 314 | // Do nothing. We should never get here since invalid has already 315 | // been handled above. 316 | 317 | case reflect.Bool: 318 | printBool(d.w, v.Bool()) 319 | 320 | case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int: 321 | printInt(d.w, v.Int(), 10) 322 | 323 | case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint: 324 | printUint(d.w, v.Uint(), 10) 325 | 326 | case reflect.Float32: 327 | printFloat(d.w, v.Float(), 32) 328 | 329 | case reflect.Float64: 330 | printFloat(d.w, v.Float(), 64) 331 | 332 | case reflect.Complex64: 333 | printComplex(d.w, v.Complex(), 32) 334 | 335 | case reflect.Complex128: 336 | printComplex(d.w, v.Complex(), 64) 337 | 338 | case reflect.Slice: 339 | if v.IsNil() { 340 | d.w.Write(nilAngleBytes) 341 | break 342 | } 343 | fallthrough 344 | 345 | case reflect.Array: 346 | d.w.Write(openBraceNewlineBytes) 347 | d.depth++ 348 | if (d.cs.MaxDepth != 0) && (d.depth > d.cs.MaxDepth) { 349 | d.indent() 350 | d.w.Write(maxNewlineBytes) 351 | } else { 352 | d.dumpSlice(v) 353 | } 354 | d.depth-- 355 | d.indent() 356 | d.w.Write(closeBraceBytes) 357 | 358 | case reflect.String: 359 | d.w.Write([]byte(strconv.Quote(v.String()))) 360 | 361 | case reflect.Interface: 362 | // The only time we should get here is for nil interfaces due to 363 | // unpackValue calls. 364 | if v.IsNil() { 365 | d.w.Write(nilAngleBytes) 366 | } 367 | 368 | case reflect.Ptr: 369 | // Do nothing. We should never get here since pointers have already 370 | // been handled above. 371 | 372 | case reflect.Map: 373 | // nil maps should be indicated as different than empty maps 374 | if v.IsNil() { 375 | d.w.Write(nilAngleBytes) 376 | break 377 | } 378 | 379 | d.w.Write(openBraceNewlineBytes) 380 | d.depth++ 381 | if (d.cs.MaxDepth != 0) && (d.depth > d.cs.MaxDepth) { 382 | d.indent() 383 | d.w.Write(maxNewlineBytes) 384 | } else { 385 | numEntries := v.Len() 386 | keys := v.MapKeys() 387 | if d.cs.SortKeys { 388 | sortValues(keys, d.cs) 389 | } 390 | for i, key := range keys { 391 | d.dump(d.unpackValue(key)) 392 | d.w.Write(colonSpaceBytes) 393 | d.ignoreNextIndent = true 394 | d.dump(d.unpackValue(v.MapIndex(key))) 395 | if i < (numEntries - 1) { 396 | d.w.Write(commaNewlineBytes) 397 | } else { 398 | d.w.Write(newlineBytes) 399 | } 400 | } 401 | } 402 | d.depth-- 403 | d.indent() 404 | d.w.Write(closeBraceBytes) 405 | 406 | case reflect.Struct: 407 | d.w.Write(openBraceNewlineBytes) 408 | d.depth++ 409 | if (d.cs.MaxDepth != 0) && (d.depth > d.cs.MaxDepth) { 410 | d.indent() 411 | d.w.Write(maxNewlineBytes) 412 | } else { 413 | vt := v.Type() 414 | numFields := v.NumField() 415 | for i := 0; i < numFields; i++ { 416 | d.indent() 417 | vtf := vt.Field(i) 418 | d.w.Write([]byte(vtf.Name)) 419 | d.w.Write(colonSpaceBytes) 420 | d.ignoreNextIndent = true 421 | d.dump(d.unpackValue(v.Field(i))) 422 | if i < (numFields - 1) { 423 | d.w.Write(commaNewlineBytes) 424 | } else { 425 | d.w.Write(newlineBytes) 426 | } 427 | } 428 | } 429 | d.depth-- 430 | d.indent() 431 | d.w.Write(closeBraceBytes) 432 | 433 | case reflect.Uintptr: 434 | printHexPtr(d.w, uintptr(v.Uint())) 435 | 436 | case reflect.UnsafePointer, reflect.Chan, reflect.Func: 437 | printHexPtr(d.w, v.Pointer()) 438 | 439 | // There were not any other types at the time this code was written, but 440 | // fall back to letting the default fmt package handle it in case any new 441 | // types are added. 442 | default: 443 | if v.CanInterface() { 444 | fmt.Fprintf(d.w, "%v", v.Interface()) 445 | } else { 446 | fmt.Fprintf(d.w, "%v", v.String()) 447 | } 448 | } 449 | } 450 | 451 | // fdump is a helper function to consolidate the logic from the various public 452 | // methods which take varying writers and config states. 453 | func fdump(cs *ConfigState, w io.Writer, a ...interface{}) { 454 | for _, arg := range a { 455 | if arg == nil { 456 | w.Write(interfaceBytes) 457 | w.Write(spaceBytes) 458 | w.Write(nilAngleBytes) 459 | w.Write(newlineBytes) 460 | continue 461 | } 462 | 463 | d := dumpState{w: w, cs: cs} 464 | d.pointers = make(map[uintptr]int) 465 | d.dump(reflect.ValueOf(arg)) 466 | d.w.Write(newlineBytes) 467 | } 468 | } 469 | 470 | // Fdump formats and displays the passed arguments to io.Writer w. It formats 471 | // exactly the same as Dump. 472 | func Fdump(w io.Writer, a ...interface{}) { 473 | fdump(&Config, w, a...) 474 | } 475 | 476 | // Sdump returns a string with the passed arguments formatted exactly the same 477 | // as Dump. 478 | func Sdump(a ...interface{}) string { 479 | var buf bytes.Buffer 480 | fdump(&Config, &buf, a...) 481 | return buf.String() 482 | } 483 | 484 | /* 485 | Dump displays the passed parameters to standard out with newlines, customizable 486 | indentation, and additional debug information such as complete types and all 487 | pointer addresses used to indirect to the final value. It provides the 488 | following features over the built-in printing facilities provided by the fmt 489 | package: 490 | 491 | * Pointers are dereferenced and followed 492 | * Circular data structures are detected and handled properly 493 | * Custom Stringer/error interfaces are optionally invoked, including 494 | on unexported types 495 | * Custom types which only implement the Stringer/error interfaces via 496 | a pointer receiver are optionally invoked when passing non-pointer 497 | variables 498 | * Byte arrays and slices are dumped like the hexdump -C command which 499 | includes offsets, byte values in hex, and ASCII output 500 | 501 | The configuration options are controlled by an exported package global, 502 | spew.Config. See ConfigState for options documentation. 503 | 504 | See Fdump if you would prefer dumping to an arbitrary io.Writer or Sdump to 505 | get the formatted result as a string. 506 | */ 507 | func Dump(a ...interface{}) { 508 | fdump(&Config, os.Stdout, a...) 509 | } 510 | -------------------------------------------------------------------------------- /spew/dump_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | /* 18 | Test Summary: 19 | NOTE: For each test, a nil pointer, a single pointer and double pointer to the 20 | base test element are also tested to ensure proper indirection across all types. 21 | 22 | - Max int8, int16, int32, int64, int 23 | - Max uint8, uint16, uint32, uint64, uint 24 | - Boolean true and false 25 | - Standard complex64 and complex128 26 | - Array containing standard ints 27 | - Array containing type with custom formatter on pointer receiver only 28 | - Array containing interfaces 29 | - Array containing bytes 30 | - Slice containing standard float32 values 31 | - Slice containing type with custom formatter on pointer receiver only 32 | - Slice containing interfaces 33 | - Slice containing bytes 34 | - Nil slice 35 | - Standard string 36 | - Nil interface 37 | - Sub-interface 38 | - Map with string keys and int vals 39 | - Map with custom formatter type on pointer receiver only keys and vals 40 | - Map with interface keys and values 41 | - Map with nil interface value 42 | - Struct with primitives 43 | - Struct that contains another struct 44 | - Struct that contains custom type with Stringer pointer interface via both 45 | exported and unexported fields 46 | - Struct that contains embedded struct and field to same struct 47 | - Uintptr to 0 (null pointer) 48 | - Uintptr address of real variable 49 | - Unsafe.Pointer to 0 (null pointer) 50 | - Unsafe.Pointer to address of real variable 51 | - Nil channel 52 | - Standard int channel 53 | - Function with no params and no returns 54 | - Function with param and no returns 55 | - Function with multiple params and multiple returns 56 | - Struct that is circular through self referencing 57 | - Structs that are circular through cross referencing 58 | - Structs that are indirectly circular 59 | - Type that panics in its Stringer interface 60 | */ 61 | 62 | package spew_test 63 | 64 | import ( 65 | "bytes" 66 | "fmt" 67 | "testing" 68 | "unsafe" 69 | 70 | "github.com/davecgh/go-spew/spew" 71 | ) 72 | 73 | // dumpTest is used to describe a test to be performed against the Dump method. 74 | type dumpTest struct { 75 | in interface{} 76 | wants []string 77 | } 78 | 79 | // dumpTests houses all of the tests to be performed against the Dump method. 80 | var dumpTests = make([]dumpTest, 0) 81 | 82 | // addDumpTest is a helper method to append the passed input and desired result 83 | // to dumpTests 84 | func addDumpTest(in interface{}, wants ...string) { 85 | test := dumpTest{in, wants} 86 | dumpTests = append(dumpTests, test) 87 | } 88 | 89 | func addIntDumpTests() { 90 | // Max int8. 91 | v := int8(127) 92 | nv := (*int8)(nil) 93 | pv := &v 94 | vAddr := fmt.Sprintf("%p", pv) 95 | pvAddr := fmt.Sprintf("%p", &pv) 96 | vt := "int8" 97 | vs := "127" 98 | addDumpTest(v, "("+vt+") "+vs+"\n") 99 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 100 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 101 | addDumpTest(nv, "(*"+vt+")()\n") 102 | 103 | // Max int16. 104 | v2 := int16(32767) 105 | nv2 := (*int16)(nil) 106 | pv2 := &v2 107 | v2Addr := fmt.Sprintf("%p", pv2) 108 | pv2Addr := fmt.Sprintf("%p", &pv2) 109 | v2t := "int16" 110 | v2s := "32767" 111 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 112 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n") 113 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 114 | addDumpTest(nv2, "(*"+v2t+")()\n") 115 | 116 | // Max int32. 117 | v3 := int32(2147483647) 118 | nv3 := (*int32)(nil) 119 | pv3 := &v3 120 | v3Addr := fmt.Sprintf("%p", pv3) 121 | pv3Addr := fmt.Sprintf("%p", &pv3) 122 | v3t := "int32" 123 | v3s := "2147483647" 124 | addDumpTest(v3, "("+v3t+") "+v3s+"\n") 125 | addDumpTest(pv3, "(*"+v3t+")("+v3Addr+")("+v3s+")\n") 126 | addDumpTest(&pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")("+v3s+")\n") 127 | addDumpTest(nv3, "(*"+v3t+")()\n") 128 | 129 | // Max int64. 130 | v4 := int64(9223372036854775807) 131 | nv4 := (*int64)(nil) 132 | pv4 := &v4 133 | v4Addr := fmt.Sprintf("%p", pv4) 134 | pv4Addr := fmt.Sprintf("%p", &pv4) 135 | v4t := "int64" 136 | v4s := "9223372036854775807" 137 | addDumpTest(v4, "("+v4t+") "+v4s+"\n") 138 | addDumpTest(pv4, "(*"+v4t+")("+v4Addr+")("+v4s+")\n") 139 | addDumpTest(&pv4, "(**"+v4t+")("+pv4Addr+"->"+v4Addr+")("+v4s+")\n") 140 | addDumpTest(nv4, "(*"+v4t+")()\n") 141 | 142 | // Max int. 143 | v5 := int(2147483647) 144 | nv5 := (*int)(nil) 145 | pv5 := &v5 146 | v5Addr := fmt.Sprintf("%p", pv5) 147 | pv5Addr := fmt.Sprintf("%p", &pv5) 148 | v5t := "int" 149 | v5s := "2147483647" 150 | addDumpTest(v5, "("+v5t+") "+v5s+"\n") 151 | addDumpTest(pv5, "(*"+v5t+")("+v5Addr+")("+v5s+")\n") 152 | addDumpTest(&pv5, "(**"+v5t+")("+pv5Addr+"->"+v5Addr+")("+v5s+")\n") 153 | addDumpTest(nv5, "(*"+v5t+")()\n") 154 | } 155 | 156 | func addUintDumpTests() { 157 | // Max uint8. 158 | v := uint8(255) 159 | nv := (*uint8)(nil) 160 | pv := &v 161 | vAddr := fmt.Sprintf("%p", pv) 162 | pvAddr := fmt.Sprintf("%p", &pv) 163 | vt := "uint8" 164 | vs := "255" 165 | addDumpTest(v, "("+vt+") "+vs+"\n") 166 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 167 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 168 | addDumpTest(nv, "(*"+vt+")()\n") 169 | 170 | // Max uint16. 171 | v2 := uint16(65535) 172 | nv2 := (*uint16)(nil) 173 | pv2 := &v2 174 | v2Addr := fmt.Sprintf("%p", pv2) 175 | pv2Addr := fmt.Sprintf("%p", &pv2) 176 | v2t := "uint16" 177 | v2s := "65535" 178 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 179 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n") 180 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 181 | addDumpTest(nv2, "(*"+v2t+")()\n") 182 | 183 | // Max uint32. 184 | v3 := uint32(4294967295) 185 | nv3 := (*uint32)(nil) 186 | pv3 := &v3 187 | v3Addr := fmt.Sprintf("%p", pv3) 188 | pv3Addr := fmt.Sprintf("%p", &pv3) 189 | v3t := "uint32" 190 | v3s := "4294967295" 191 | addDumpTest(v3, "("+v3t+") "+v3s+"\n") 192 | addDumpTest(pv3, "(*"+v3t+")("+v3Addr+")("+v3s+")\n") 193 | addDumpTest(&pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")("+v3s+")\n") 194 | addDumpTest(nv3, "(*"+v3t+")()\n") 195 | 196 | // Max uint64. 197 | v4 := uint64(18446744073709551615) 198 | nv4 := (*uint64)(nil) 199 | pv4 := &v4 200 | v4Addr := fmt.Sprintf("%p", pv4) 201 | pv4Addr := fmt.Sprintf("%p", &pv4) 202 | v4t := "uint64" 203 | v4s := "18446744073709551615" 204 | addDumpTest(v4, "("+v4t+") "+v4s+"\n") 205 | addDumpTest(pv4, "(*"+v4t+")("+v4Addr+")("+v4s+")\n") 206 | addDumpTest(&pv4, "(**"+v4t+")("+pv4Addr+"->"+v4Addr+")("+v4s+")\n") 207 | addDumpTest(nv4, "(*"+v4t+")()\n") 208 | 209 | // Max uint. 210 | v5 := uint(4294967295) 211 | nv5 := (*uint)(nil) 212 | pv5 := &v5 213 | v5Addr := fmt.Sprintf("%p", pv5) 214 | pv5Addr := fmt.Sprintf("%p", &pv5) 215 | v5t := "uint" 216 | v5s := "4294967295" 217 | addDumpTest(v5, "("+v5t+") "+v5s+"\n") 218 | addDumpTest(pv5, "(*"+v5t+")("+v5Addr+")("+v5s+")\n") 219 | addDumpTest(&pv5, "(**"+v5t+")("+pv5Addr+"->"+v5Addr+")("+v5s+")\n") 220 | addDumpTest(nv5, "(*"+v5t+")()\n") 221 | } 222 | 223 | func addBoolDumpTests() { 224 | // Boolean true. 225 | v := bool(true) 226 | nv := (*bool)(nil) 227 | pv := &v 228 | vAddr := fmt.Sprintf("%p", pv) 229 | pvAddr := fmt.Sprintf("%p", &pv) 230 | vt := "bool" 231 | vs := "true" 232 | addDumpTest(v, "("+vt+") "+vs+"\n") 233 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 234 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 235 | addDumpTest(nv, "(*"+vt+")()\n") 236 | 237 | // Boolean false. 238 | v2 := bool(false) 239 | pv2 := &v2 240 | v2Addr := fmt.Sprintf("%p", pv2) 241 | pv2Addr := fmt.Sprintf("%p", &pv2) 242 | v2t := "bool" 243 | v2s := "false" 244 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 245 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n") 246 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 247 | } 248 | 249 | func addFloatDumpTests() { 250 | // Standard float32. 251 | v := float32(3.1415) 252 | nv := (*float32)(nil) 253 | pv := &v 254 | vAddr := fmt.Sprintf("%p", pv) 255 | pvAddr := fmt.Sprintf("%p", &pv) 256 | vt := "float32" 257 | vs := "3.1415" 258 | addDumpTest(v, "("+vt+") "+vs+"\n") 259 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 260 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 261 | addDumpTest(nv, "(*"+vt+")()\n") 262 | 263 | // Standard float64. 264 | v2 := float64(3.1415926) 265 | nv2 := (*float64)(nil) 266 | pv2 := &v2 267 | v2Addr := fmt.Sprintf("%p", pv2) 268 | pv2Addr := fmt.Sprintf("%p", &pv2) 269 | v2t := "float64" 270 | v2s := "3.1415926" 271 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 272 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n") 273 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 274 | addDumpTest(nv2, "(*"+v2t+")()\n") 275 | } 276 | 277 | func addComplexDumpTests() { 278 | // Standard complex64. 279 | v := complex(float32(6), -2) 280 | nv := (*complex64)(nil) 281 | pv := &v 282 | vAddr := fmt.Sprintf("%p", pv) 283 | pvAddr := fmt.Sprintf("%p", &pv) 284 | vt := "complex64" 285 | vs := "(6-2i)" 286 | addDumpTest(v, "("+vt+") "+vs+"\n") 287 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 288 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 289 | addDumpTest(nv, "(*"+vt+")()\n") 290 | 291 | // Standard complex128. 292 | v2 := complex(float64(-6), 2) 293 | nv2 := (*complex128)(nil) 294 | pv2 := &v2 295 | v2Addr := fmt.Sprintf("%p", pv2) 296 | pv2Addr := fmt.Sprintf("%p", &pv2) 297 | v2t := "complex128" 298 | v2s := "(-6+2i)" 299 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 300 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n") 301 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 302 | addDumpTest(nv2, "(*"+v2t+")()\n") 303 | } 304 | 305 | func addArrayDumpTests() { 306 | // Array containing standard ints. 307 | v := [3]int{1, 2, 3} 308 | vLen := fmt.Sprintf("%d", len(v)) 309 | vCap := fmt.Sprintf("%d", cap(v)) 310 | nv := (*[3]int)(nil) 311 | pv := &v 312 | vAddr := fmt.Sprintf("%p", pv) 313 | pvAddr := fmt.Sprintf("%p", &pv) 314 | vt := "int" 315 | vs := "(len=" + vLen + " cap=" + vCap + ") {\n (" + vt + ") 1,\n (" + 316 | vt + ") 2,\n (" + vt + ") 3\n}" 317 | addDumpTest(v, "([3]"+vt+") "+vs+"\n") 318 | addDumpTest(pv, "(*[3]"+vt+")("+vAddr+")("+vs+")\n") 319 | addDumpTest(&pv, "(**[3]"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 320 | addDumpTest(nv, "(*[3]"+vt+")()\n") 321 | 322 | // Array containing type with custom formatter on pointer receiver only. 323 | v2i0 := pstringer("1") 324 | v2i1 := pstringer("2") 325 | v2i2 := pstringer("3") 326 | v2 := [3]pstringer{v2i0, v2i1, v2i2} 327 | v2i0Len := fmt.Sprintf("%d", len(v2i0)) 328 | v2i1Len := fmt.Sprintf("%d", len(v2i1)) 329 | v2i2Len := fmt.Sprintf("%d", len(v2i2)) 330 | v2Len := fmt.Sprintf("%d", len(v2)) 331 | v2Cap := fmt.Sprintf("%d", cap(v2)) 332 | nv2 := (*[3]pstringer)(nil) 333 | pv2 := &v2 334 | v2Addr := fmt.Sprintf("%p", pv2) 335 | pv2Addr := fmt.Sprintf("%p", &pv2) 336 | v2t := "spew_test.pstringer" 337 | v2sp := "(len=" + v2Len + " cap=" + v2Cap + ") {\n (" + v2t + 338 | ") (len=" + v2i0Len + ") stringer 1,\n (" + v2t + 339 | ") (len=" + v2i1Len + ") stringer 2,\n (" + v2t + 340 | ") (len=" + v2i2Len + ") " + "stringer 3\n}" 341 | v2s := v2sp 342 | if spew.UnsafeDisabled { 343 | v2s = "(len=" + v2Len + " cap=" + v2Cap + ") {\n (" + v2t + 344 | ") (len=" + v2i0Len + ") \"1\",\n (" + v2t + ") (len=" + 345 | v2i1Len + ") \"2\",\n (" + v2t + ") (len=" + v2i2Len + 346 | ") " + "\"3\"\n}" 347 | } 348 | addDumpTest(v2, "([3]"+v2t+") "+v2s+"\n") 349 | addDumpTest(pv2, "(*[3]"+v2t+")("+v2Addr+")("+v2sp+")\n") 350 | addDumpTest(&pv2, "(**[3]"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2sp+")\n") 351 | addDumpTest(nv2, "(*[3]"+v2t+")()\n") 352 | 353 | // Array containing interfaces. 354 | v3i0 := "one" 355 | v3 := [3]interface{}{v3i0, int(2), uint(3)} 356 | v3i0Len := fmt.Sprintf("%d", len(v3i0)) 357 | v3Len := fmt.Sprintf("%d", len(v3)) 358 | v3Cap := fmt.Sprintf("%d", cap(v3)) 359 | nv3 := (*[3]interface{})(nil) 360 | pv3 := &v3 361 | v3Addr := fmt.Sprintf("%p", pv3) 362 | pv3Addr := fmt.Sprintf("%p", &pv3) 363 | v3t := "[3]interface {}" 364 | v3t2 := "string" 365 | v3t3 := "int" 366 | v3t4 := "uint" 367 | v3s := "(len=" + v3Len + " cap=" + v3Cap + ") {\n (" + v3t2 + ") " + 368 | "(len=" + v3i0Len + ") \"one\",\n (" + v3t3 + ") 2,\n (" + 369 | v3t4 + ") 3\n}" 370 | addDumpTest(v3, "("+v3t+") "+v3s+"\n") 371 | addDumpTest(pv3, "(*"+v3t+")("+v3Addr+")("+v3s+")\n") 372 | addDumpTest(&pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")("+v3s+")\n") 373 | addDumpTest(nv3, "(*"+v3t+")()\n") 374 | 375 | // Array containing bytes. 376 | v4 := [34]byte{ 377 | 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 378 | 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 379 | 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 380 | 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 381 | 0x31, 0x32, 382 | } 383 | v4Len := fmt.Sprintf("%d", len(v4)) 384 | v4Cap := fmt.Sprintf("%d", cap(v4)) 385 | nv4 := (*[34]byte)(nil) 386 | pv4 := &v4 387 | v4Addr := fmt.Sprintf("%p", pv4) 388 | pv4Addr := fmt.Sprintf("%p", &pv4) 389 | v4t := "[34]uint8" 390 | v4s := "(len=" + v4Len + " cap=" + v4Cap + ") " + 391 | "{\n 00000000 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20" + 392 | " |............... |\n" + 393 | " 00000010 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30" + 394 | " |!\"#$%&'()*+,-./0|\n" + 395 | " 00000020 31 32 " + 396 | " |12|\n}" 397 | addDumpTest(v4, "("+v4t+") "+v4s+"\n") 398 | addDumpTest(pv4, "(*"+v4t+")("+v4Addr+")("+v4s+")\n") 399 | addDumpTest(&pv4, "(**"+v4t+")("+pv4Addr+"->"+v4Addr+")("+v4s+")\n") 400 | addDumpTest(nv4, "(*"+v4t+")()\n") 401 | } 402 | 403 | func addSliceDumpTests() { 404 | // Slice containing standard float32 values. 405 | v := []float32{3.14, 6.28, 12.56} 406 | vLen := fmt.Sprintf("%d", len(v)) 407 | vCap := fmt.Sprintf("%d", cap(v)) 408 | nv := (*[]float32)(nil) 409 | pv := &v 410 | vAddr := fmt.Sprintf("%p", pv) 411 | pvAddr := fmt.Sprintf("%p", &pv) 412 | vt := "float32" 413 | vs := "(len=" + vLen + " cap=" + vCap + ") {\n (" + vt + ") 3.14,\n (" + 414 | vt + ") 6.28,\n (" + vt + ") 12.56\n}" 415 | addDumpTest(v, "([]"+vt+") "+vs+"\n") 416 | addDumpTest(pv, "(*[]"+vt+")("+vAddr+")("+vs+")\n") 417 | addDumpTest(&pv, "(**[]"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 418 | addDumpTest(nv, "(*[]"+vt+")()\n") 419 | 420 | // Slice containing type with custom formatter on pointer receiver only. 421 | v2i0 := pstringer("1") 422 | v2i1 := pstringer("2") 423 | v2i2 := pstringer("3") 424 | v2 := []pstringer{v2i0, v2i1, v2i2} 425 | v2i0Len := fmt.Sprintf("%d", len(v2i0)) 426 | v2i1Len := fmt.Sprintf("%d", len(v2i1)) 427 | v2i2Len := fmt.Sprintf("%d", len(v2i2)) 428 | v2Len := fmt.Sprintf("%d", len(v2)) 429 | v2Cap := fmt.Sprintf("%d", cap(v2)) 430 | nv2 := (*[]pstringer)(nil) 431 | pv2 := &v2 432 | v2Addr := fmt.Sprintf("%p", pv2) 433 | pv2Addr := fmt.Sprintf("%p", &pv2) 434 | v2t := "spew_test.pstringer" 435 | v2s := "(len=" + v2Len + " cap=" + v2Cap + ") {\n (" + v2t + ") (len=" + 436 | v2i0Len + ") stringer 1,\n (" + v2t + ") (len=" + v2i1Len + 437 | ") stringer 2,\n (" + v2t + ") (len=" + v2i2Len + ") " + 438 | "stringer 3\n}" 439 | addDumpTest(v2, "([]"+v2t+") "+v2s+"\n") 440 | addDumpTest(pv2, "(*[]"+v2t+")("+v2Addr+")("+v2s+")\n") 441 | addDumpTest(&pv2, "(**[]"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 442 | addDumpTest(nv2, "(*[]"+v2t+")()\n") 443 | 444 | // Slice containing interfaces. 445 | v3i0 := "one" 446 | v3 := []interface{}{v3i0, int(2), uint(3), nil} 447 | v3i0Len := fmt.Sprintf("%d", len(v3i0)) 448 | v3Len := fmt.Sprintf("%d", len(v3)) 449 | v3Cap := fmt.Sprintf("%d", cap(v3)) 450 | nv3 := (*[]interface{})(nil) 451 | pv3 := &v3 452 | v3Addr := fmt.Sprintf("%p", pv3) 453 | pv3Addr := fmt.Sprintf("%p", &pv3) 454 | v3t := "[]interface {}" 455 | v3t2 := "string" 456 | v3t3 := "int" 457 | v3t4 := "uint" 458 | v3t5 := "interface {}" 459 | v3s := "(len=" + v3Len + " cap=" + v3Cap + ") {\n (" + v3t2 + ") " + 460 | "(len=" + v3i0Len + ") \"one\",\n (" + v3t3 + ") 2,\n (" + 461 | v3t4 + ") 3,\n (" + v3t5 + ") \n}" 462 | addDumpTest(v3, "("+v3t+") "+v3s+"\n") 463 | addDumpTest(pv3, "(*"+v3t+")("+v3Addr+")("+v3s+")\n") 464 | addDumpTest(&pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")("+v3s+")\n") 465 | addDumpTest(nv3, "(*"+v3t+")()\n") 466 | 467 | // Slice containing bytes. 468 | v4 := []byte{ 469 | 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 470 | 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 471 | 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 472 | 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 473 | 0x31, 0x32, 474 | } 475 | v4Len := fmt.Sprintf("%d", len(v4)) 476 | v4Cap := fmt.Sprintf("%d", cap(v4)) 477 | nv4 := (*[]byte)(nil) 478 | pv4 := &v4 479 | v4Addr := fmt.Sprintf("%p", pv4) 480 | pv4Addr := fmt.Sprintf("%p", &pv4) 481 | v4t := "[]uint8" 482 | v4s := "(len=" + v4Len + " cap=" + v4Cap + ") " + 483 | "{\n 00000000 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20" + 484 | " |............... |\n" + 485 | " 00000010 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30" + 486 | " |!\"#$%&'()*+,-./0|\n" + 487 | " 00000020 31 32 " + 488 | " |12|\n}" 489 | addDumpTest(v4, "("+v4t+") "+v4s+"\n") 490 | addDumpTest(pv4, "(*"+v4t+")("+v4Addr+")("+v4s+")\n") 491 | addDumpTest(&pv4, "(**"+v4t+")("+pv4Addr+"->"+v4Addr+")("+v4s+")\n") 492 | addDumpTest(nv4, "(*"+v4t+")()\n") 493 | 494 | // Nil slice. 495 | v5 := []int(nil) 496 | nv5 := (*[]int)(nil) 497 | pv5 := &v5 498 | v5Addr := fmt.Sprintf("%p", pv5) 499 | pv5Addr := fmt.Sprintf("%p", &pv5) 500 | v5t := "[]int" 501 | v5s := "" 502 | addDumpTest(v5, "("+v5t+") "+v5s+"\n") 503 | addDumpTest(pv5, "(*"+v5t+")("+v5Addr+")("+v5s+")\n") 504 | addDumpTest(&pv5, "(**"+v5t+")("+pv5Addr+"->"+v5Addr+")("+v5s+")\n") 505 | addDumpTest(nv5, "(*"+v5t+")()\n") 506 | } 507 | 508 | func addStringDumpTests() { 509 | // Standard string. 510 | v := "test" 511 | vLen := fmt.Sprintf("%d", len(v)) 512 | nv := (*string)(nil) 513 | pv := &v 514 | vAddr := fmt.Sprintf("%p", pv) 515 | pvAddr := fmt.Sprintf("%p", &pv) 516 | vt := "string" 517 | vs := "(len=" + vLen + ") \"test\"" 518 | addDumpTest(v, "("+vt+") "+vs+"\n") 519 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 520 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 521 | addDumpTest(nv, "(*"+vt+")()\n") 522 | } 523 | 524 | func addInterfaceDumpTests() { 525 | // Nil interface. 526 | var v interface{} 527 | nv := (*interface{})(nil) 528 | pv := &v 529 | vAddr := fmt.Sprintf("%p", pv) 530 | pvAddr := fmt.Sprintf("%p", &pv) 531 | vt := "interface {}" 532 | vs := "" 533 | addDumpTest(v, "("+vt+") "+vs+"\n") 534 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 535 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 536 | addDumpTest(nv, "(*"+vt+")()\n") 537 | 538 | // Sub-interface. 539 | v2 := interface{}(uint16(65535)) 540 | pv2 := &v2 541 | v2Addr := fmt.Sprintf("%p", pv2) 542 | pv2Addr := fmt.Sprintf("%p", &pv2) 543 | v2t := "uint16" 544 | v2s := "65535" 545 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 546 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n") 547 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 548 | } 549 | 550 | func addMapDumpTests() { 551 | // Map with string keys and int vals. 552 | k := "one" 553 | kk := "two" 554 | m := map[string]int{k: 1, kk: 2} 555 | klen := fmt.Sprintf("%d", len(k)) // not kLen to shut golint up 556 | kkLen := fmt.Sprintf("%d", len(kk)) 557 | mLen := fmt.Sprintf("%d", len(m)) 558 | nilMap := map[string]int(nil) 559 | nm := (*map[string]int)(nil) 560 | pm := &m 561 | mAddr := fmt.Sprintf("%p", pm) 562 | pmAddr := fmt.Sprintf("%p", &pm) 563 | mt := "map[string]int" 564 | mt1 := "string" 565 | mt2 := "int" 566 | ms := "(len=" + mLen + ") {\n (" + mt1 + ") (len=" + klen + ") " + 567 | "\"one\": (" + mt2 + ") 1,\n (" + mt1 + ") (len=" + kkLen + 568 | ") \"two\": (" + mt2 + ") 2\n}" 569 | ms2 := "(len=" + mLen + ") {\n (" + mt1 + ") (len=" + kkLen + ") " + 570 | "\"two\": (" + mt2 + ") 2,\n (" + mt1 + ") (len=" + klen + 571 | ") \"one\": (" + mt2 + ") 1\n}" 572 | addDumpTest(m, "("+mt+") "+ms+"\n", "("+mt+") "+ms2+"\n") 573 | addDumpTest(pm, "(*"+mt+")("+mAddr+")("+ms+")\n", 574 | "(*"+mt+")("+mAddr+")("+ms2+")\n") 575 | addDumpTest(&pm, "(**"+mt+")("+pmAddr+"->"+mAddr+")("+ms+")\n", 576 | "(**"+mt+")("+pmAddr+"->"+mAddr+")("+ms2+")\n") 577 | addDumpTest(nm, "(*"+mt+")()\n") 578 | addDumpTest(nilMap, "("+mt+") \n") 579 | 580 | // Map with custom formatter type on pointer receiver only keys and vals. 581 | k2 := pstringer("one") 582 | v2 := pstringer("1") 583 | m2 := map[pstringer]pstringer{k2: v2} 584 | k2Len := fmt.Sprintf("%d", len(k2)) 585 | v2Len := fmt.Sprintf("%d", len(v2)) 586 | m2Len := fmt.Sprintf("%d", len(m2)) 587 | nilMap2 := map[pstringer]pstringer(nil) 588 | nm2 := (*map[pstringer]pstringer)(nil) 589 | pm2 := &m2 590 | m2Addr := fmt.Sprintf("%p", pm2) 591 | pm2Addr := fmt.Sprintf("%p", &pm2) 592 | m2t := "map[spew_test.pstringer]spew_test.pstringer" 593 | m2t1 := "spew_test.pstringer" 594 | m2t2 := "spew_test.pstringer" 595 | m2s := "(len=" + m2Len + ") {\n (" + m2t1 + ") (len=" + k2Len + ") " + 596 | "stringer one: (" + m2t2 + ") (len=" + v2Len + ") stringer 1\n}" 597 | if spew.UnsafeDisabled { 598 | m2s = "(len=" + m2Len + ") {\n (" + m2t1 + ") (len=" + k2Len + 599 | ") " + "\"one\": (" + m2t2 + ") (len=" + v2Len + 600 | ") \"1\"\n}" 601 | } 602 | addDumpTest(m2, "("+m2t+") "+m2s+"\n") 603 | addDumpTest(pm2, "(*"+m2t+")("+m2Addr+")("+m2s+")\n") 604 | addDumpTest(&pm2, "(**"+m2t+")("+pm2Addr+"->"+m2Addr+")("+m2s+")\n") 605 | addDumpTest(nm2, "(*"+m2t+")()\n") 606 | addDumpTest(nilMap2, "("+m2t+") \n") 607 | 608 | // Map with interface keys and values. 609 | k3 := "one" 610 | k3Len := fmt.Sprintf("%d", len(k3)) 611 | m3 := map[interface{}]interface{}{k3: 1} 612 | m3Len := fmt.Sprintf("%d", len(m3)) 613 | nilMap3 := map[interface{}]interface{}(nil) 614 | nm3 := (*map[interface{}]interface{})(nil) 615 | pm3 := &m3 616 | m3Addr := fmt.Sprintf("%p", pm3) 617 | pm3Addr := fmt.Sprintf("%p", &pm3) 618 | m3t := "map[interface {}]interface {}" 619 | m3t1 := "string" 620 | m3t2 := "int" 621 | m3s := "(len=" + m3Len + ") {\n (" + m3t1 + ") (len=" + k3Len + ") " + 622 | "\"one\": (" + m3t2 + ") 1\n}" 623 | addDumpTest(m3, "("+m3t+") "+m3s+"\n") 624 | addDumpTest(pm3, "(*"+m3t+")("+m3Addr+")("+m3s+")\n") 625 | addDumpTest(&pm3, "(**"+m3t+")("+pm3Addr+"->"+m3Addr+")("+m3s+")\n") 626 | addDumpTest(nm3, "(*"+m3t+")()\n") 627 | addDumpTest(nilMap3, "("+m3t+") \n") 628 | 629 | // Map with nil interface value. 630 | k4 := "nil" 631 | k4Len := fmt.Sprintf("%d", len(k4)) 632 | m4 := map[string]interface{}{k4: nil} 633 | m4Len := fmt.Sprintf("%d", len(m4)) 634 | nilMap4 := map[string]interface{}(nil) 635 | nm4 := (*map[string]interface{})(nil) 636 | pm4 := &m4 637 | m4Addr := fmt.Sprintf("%p", pm4) 638 | pm4Addr := fmt.Sprintf("%p", &pm4) 639 | m4t := "map[string]interface {}" 640 | m4t1 := "string" 641 | m4t2 := "interface {}" 642 | m4s := "(len=" + m4Len + ") {\n (" + m4t1 + ") (len=" + k4Len + ")" + 643 | " \"nil\": (" + m4t2 + ") \n}" 644 | addDumpTest(m4, "("+m4t+") "+m4s+"\n") 645 | addDumpTest(pm4, "(*"+m4t+")("+m4Addr+")("+m4s+")\n") 646 | addDumpTest(&pm4, "(**"+m4t+")("+pm4Addr+"->"+m4Addr+")("+m4s+")\n") 647 | addDumpTest(nm4, "(*"+m4t+")()\n") 648 | addDumpTest(nilMap4, "("+m4t+") \n") 649 | } 650 | 651 | func addStructDumpTests() { 652 | // Struct with primitives. 653 | type s1 struct { 654 | a int8 655 | b uint8 656 | } 657 | v := s1{127, 255} 658 | nv := (*s1)(nil) 659 | pv := &v 660 | vAddr := fmt.Sprintf("%p", pv) 661 | pvAddr := fmt.Sprintf("%p", &pv) 662 | vt := "spew_test.s1" 663 | vt2 := "int8" 664 | vt3 := "uint8" 665 | vs := "{\n a: (" + vt2 + ") 127,\n b: (" + vt3 + ") 255\n}" 666 | addDumpTest(v, "("+vt+") "+vs+"\n") 667 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 668 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 669 | addDumpTest(nv, "(*"+vt+")()\n") 670 | 671 | // Struct that contains another struct. 672 | type s2 struct { 673 | s1 s1 674 | b bool 675 | } 676 | v2 := s2{s1{127, 255}, true} 677 | nv2 := (*s2)(nil) 678 | pv2 := &v2 679 | v2Addr := fmt.Sprintf("%p", pv2) 680 | pv2Addr := fmt.Sprintf("%p", &pv2) 681 | v2t := "spew_test.s2" 682 | v2t2 := "spew_test.s1" 683 | v2t3 := "int8" 684 | v2t4 := "uint8" 685 | v2t5 := "bool" 686 | v2s := "{\n s1: (" + v2t2 + ") {\n a: (" + v2t3 + ") 127,\n b: (" + 687 | v2t4 + ") 255\n },\n b: (" + v2t5 + ") true\n}" 688 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 689 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n") 690 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 691 | addDumpTest(nv2, "(*"+v2t+")()\n") 692 | 693 | // Struct that contains custom type with Stringer pointer interface via both 694 | // exported and unexported fields. 695 | type s3 struct { 696 | s pstringer 697 | S pstringer 698 | } 699 | v3 := s3{"test", "test2"} 700 | nv3 := (*s3)(nil) 701 | pv3 := &v3 702 | v3Addr := fmt.Sprintf("%p", pv3) 703 | pv3Addr := fmt.Sprintf("%p", &pv3) 704 | v3t := "spew_test.s3" 705 | v3t2 := "spew_test.pstringer" 706 | v3s := "{\n s: (" + v3t2 + ") (len=4) stringer test,\n S: (" + v3t2 + 707 | ") (len=5) stringer test2\n}" 708 | v3sp := v3s 709 | if spew.UnsafeDisabled { 710 | v3s = "{\n s: (" + v3t2 + ") (len=4) \"test\",\n S: (" + 711 | v3t2 + ") (len=5) \"test2\"\n}" 712 | v3sp = "{\n s: (" + v3t2 + ") (len=4) \"test\",\n S: (" + 713 | v3t2 + ") (len=5) stringer test2\n}" 714 | } 715 | addDumpTest(v3, "("+v3t+") "+v3s+"\n") 716 | addDumpTest(pv3, "(*"+v3t+")("+v3Addr+")("+v3sp+")\n") 717 | addDumpTest(&pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")("+v3sp+")\n") 718 | addDumpTest(nv3, "(*"+v3t+")()\n") 719 | 720 | // Struct that contains embedded struct and field to same struct. 721 | e := embed{"embedstr"} 722 | eLen := fmt.Sprintf("%d", len("embedstr")) 723 | v4 := embedwrap{embed: &e, e: &e} 724 | nv4 := (*embedwrap)(nil) 725 | pv4 := &v4 726 | eAddr := fmt.Sprintf("%p", &e) 727 | v4Addr := fmt.Sprintf("%p", pv4) 728 | pv4Addr := fmt.Sprintf("%p", &pv4) 729 | v4t := "spew_test.embedwrap" 730 | v4t2 := "spew_test.embed" 731 | v4t3 := "string" 732 | v4s := "{\n embed: (*" + v4t2 + ")(" + eAddr + ")({\n a: (" + v4t3 + 733 | ") (len=" + eLen + ") \"embedstr\"\n }),\n e: (*" + v4t2 + 734 | ")(" + eAddr + ")({\n a: (" + v4t3 + ") (len=" + eLen + ")" + 735 | " \"embedstr\"\n })\n}" 736 | addDumpTest(v4, "("+v4t+") "+v4s+"\n") 737 | addDumpTest(pv4, "(*"+v4t+")("+v4Addr+")("+v4s+")\n") 738 | addDumpTest(&pv4, "(**"+v4t+")("+pv4Addr+"->"+v4Addr+")("+v4s+")\n") 739 | addDumpTest(nv4, "(*"+v4t+")()\n") 740 | } 741 | 742 | func addUintptrDumpTests() { 743 | // Null pointer. 744 | v := uintptr(0) 745 | pv := &v 746 | vAddr := fmt.Sprintf("%p", pv) 747 | pvAddr := fmt.Sprintf("%p", &pv) 748 | vt := "uintptr" 749 | vs := "" 750 | addDumpTest(v, "("+vt+") "+vs+"\n") 751 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 752 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 753 | 754 | // Address of real variable. 755 | i := 1 756 | v2 := uintptr(unsafe.Pointer(&i)) 757 | nv2 := (*uintptr)(nil) 758 | pv2 := &v2 759 | v2Addr := fmt.Sprintf("%p", pv2) 760 | pv2Addr := fmt.Sprintf("%p", &pv2) 761 | v2t := "uintptr" 762 | v2s := fmt.Sprintf("%p", &i) 763 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 764 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n") 765 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 766 | addDumpTest(nv2, "(*"+v2t+")()\n") 767 | } 768 | 769 | func addUnsafePointerDumpTests() { 770 | // Null pointer. 771 | v := unsafe.Pointer(nil) 772 | nv := (*unsafe.Pointer)(nil) 773 | pv := &v 774 | vAddr := fmt.Sprintf("%p", pv) 775 | pvAddr := fmt.Sprintf("%p", &pv) 776 | vt := "unsafe.Pointer" 777 | vs := "" 778 | addDumpTest(v, "("+vt+") "+vs+"\n") 779 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 780 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 781 | addDumpTest(nv, "(*"+vt+")()\n") 782 | 783 | // Address of real variable. 784 | i := 1 785 | v2 := unsafe.Pointer(&i) 786 | pv2 := &v2 787 | v2Addr := fmt.Sprintf("%p", pv2) 788 | pv2Addr := fmt.Sprintf("%p", &pv2) 789 | v2t := "unsafe.Pointer" 790 | v2s := fmt.Sprintf("%p", &i) 791 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 792 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n") 793 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 794 | addDumpTest(nv, "(*"+vt+")()\n") 795 | } 796 | 797 | func addChanDumpTests() { 798 | // Nil channel. 799 | var v chan int 800 | pv := &v 801 | nv := (*chan int)(nil) 802 | vAddr := fmt.Sprintf("%p", pv) 803 | pvAddr := fmt.Sprintf("%p", &pv) 804 | vt := "chan int" 805 | vs := "" 806 | addDumpTest(v, "("+vt+") "+vs+"\n") 807 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 808 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 809 | addDumpTest(nv, "(*"+vt+")()\n") 810 | 811 | // Real channel. 812 | v2 := make(chan int) 813 | pv2 := &v2 814 | v2Addr := fmt.Sprintf("%p", pv2) 815 | pv2Addr := fmt.Sprintf("%p", &pv2) 816 | v2t := "chan int" 817 | v2s := fmt.Sprintf("%p", v2) 818 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 819 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n") 820 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 821 | } 822 | 823 | func addFuncDumpTests() { 824 | // Function with no params and no returns. 825 | v := addIntDumpTests 826 | nv := (*func())(nil) 827 | pv := &v 828 | vAddr := fmt.Sprintf("%p", pv) 829 | pvAddr := fmt.Sprintf("%p", &pv) 830 | vt := "func()" 831 | vs := fmt.Sprintf("%p", v) 832 | addDumpTest(v, "("+vt+") "+vs+"\n") 833 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 834 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 835 | addDumpTest(nv, "(*"+vt+")()\n") 836 | 837 | // Function with param and no returns. 838 | v2 := TestDump 839 | nv2 := (*func(*testing.T))(nil) 840 | pv2 := &v2 841 | v2Addr := fmt.Sprintf("%p", pv2) 842 | pv2Addr := fmt.Sprintf("%p", &pv2) 843 | v2t := "func(*testing.T)" 844 | v2s := fmt.Sprintf("%p", v2) 845 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 846 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s+")\n") 847 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s+")\n") 848 | addDumpTest(nv2, "(*"+v2t+")()\n") 849 | 850 | // Function with multiple params and multiple returns. 851 | var v3 = func(i int, s string) (b bool, err error) { 852 | return true, nil 853 | } 854 | nv3 := (*func(int, string) (bool, error))(nil) 855 | pv3 := &v3 856 | v3Addr := fmt.Sprintf("%p", pv3) 857 | pv3Addr := fmt.Sprintf("%p", &pv3) 858 | v3t := "func(int, string) (bool, error)" 859 | v3s := fmt.Sprintf("%p", v3) 860 | addDumpTest(v3, "("+v3t+") "+v3s+"\n") 861 | addDumpTest(pv3, "(*"+v3t+")("+v3Addr+")("+v3s+")\n") 862 | addDumpTest(&pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")("+v3s+")\n") 863 | addDumpTest(nv3, "(*"+v3t+")()\n") 864 | } 865 | 866 | func addCircularDumpTests() { 867 | // Struct that is circular through self referencing. 868 | type circular struct { 869 | c *circular 870 | } 871 | v := circular{nil} 872 | v.c = &v 873 | pv := &v 874 | vAddr := fmt.Sprintf("%p", pv) 875 | pvAddr := fmt.Sprintf("%p", &pv) 876 | vt := "spew_test.circular" 877 | vs := "{\n c: (*" + vt + ")(" + vAddr + ")({\n c: (*" + vt + ")(" + 878 | vAddr + ")()\n })\n}" 879 | vs2 := "{\n c: (*" + vt + ")(" + vAddr + ")()\n}" 880 | addDumpTest(v, "("+vt+") "+vs+"\n") 881 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs2+")\n") 882 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs2+")\n") 883 | 884 | // Structs that are circular through cross referencing. 885 | v2 := xref1{nil} 886 | ts2 := xref2{&v2} 887 | v2.ps2 = &ts2 888 | pv2 := &v2 889 | ts2Addr := fmt.Sprintf("%p", &ts2) 890 | v2Addr := fmt.Sprintf("%p", pv2) 891 | pv2Addr := fmt.Sprintf("%p", &pv2) 892 | v2t := "spew_test.xref1" 893 | v2t2 := "spew_test.xref2" 894 | v2s := "{\n ps2: (*" + v2t2 + ")(" + ts2Addr + ")({\n ps1: (*" + v2t + 895 | ")(" + v2Addr + ")({\n ps2: (*" + v2t2 + ")(" + ts2Addr + 896 | ")()\n })\n })\n}" 897 | v2s2 := "{\n ps2: (*" + v2t2 + ")(" + ts2Addr + ")({\n ps1: (*" + v2t + 898 | ")(" + v2Addr + ")()\n })\n}" 899 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 900 | addDumpTest(pv2, "(*"+v2t+")("+v2Addr+")("+v2s2+")\n") 901 | addDumpTest(&pv2, "(**"+v2t+")("+pv2Addr+"->"+v2Addr+")("+v2s2+")\n") 902 | 903 | // Structs that are indirectly circular. 904 | v3 := indirCir1{nil} 905 | tic2 := indirCir2{nil} 906 | tic3 := indirCir3{&v3} 907 | tic2.ps3 = &tic3 908 | v3.ps2 = &tic2 909 | pv3 := &v3 910 | tic2Addr := fmt.Sprintf("%p", &tic2) 911 | tic3Addr := fmt.Sprintf("%p", &tic3) 912 | v3Addr := fmt.Sprintf("%p", pv3) 913 | pv3Addr := fmt.Sprintf("%p", &pv3) 914 | v3t := "spew_test.indirCir1" 915 | v3t2 := "spew_test.indirCir2" 916 | v3t3 := "spew_test.indirCir3" 917 | v3s := "{\n ps2: (*" + v3t2 + ")(" + tic2Addr + ")({\n ps3: (*" + v3t3 + 918 | ")(" + tic3Addr + ")({\n ps1: (*" + v3t + ")(" + v3Addr + 919 | ")({\n ps2: (*" + v3t2 + ")(" + tic2Addr + 920 | ")()\n })\n })\n })\n}" 921 | v3s2 := "{\n ps2: (*" + v3t2 + ")(" + tic2Addr + ")({\n ps3: (*" + v3t3 + 922 | ")(" + tic3Addr + ")({\n ps1: (*" + v3t + ")(" + v3Addr + 923 | ")()\n })\n })\n}" 924 | addDumpTest(v3, "("+v3t+") "+v3s+"\n") 925 | addDumpTest(pv3, "(*"+v3t+")("+v3Addr+")("+v3s2+")\n") 926 | addDumpTest(&pv3, "(**"+v3t+")("+pv3Addr+"->"+v3Addr+")("+v3s2+")\n") 927 | } 928 | 929 | func addPanicDumpTests() { 930 | // Type that panics in its Stringer interface. 931 | v := panicer(127) 932 | nv := (*panicer)(nil) 933 | pv := &v 934 | vAddr := fmt.Sprintf("%p", pv) 935 | pvAddr := fmt.Sprintf("%p", &pv) 936 | vt := "spew_test.panicer" 937 | vs := "(PANIC=test panic)127" 938 | addDumpTest(v, "("+vt+") "+vs+"\n") 939 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 940 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 941 | addDumpTest(nv, "(*"+vt+")()\n") 942 | } 943 | 944 | func addErrorDumpTests() { 945 | // Type that has a custom Error interface. 946 | v := customError(127) 947 | nv := (*customError)(nil) 948 | pv := &v 949 | vAddr := fmt.Sprintf("%p", pv) 950 | pvAddr := fmt.Sprintf("%p", &pv) 951 | vt := "spew_test.customError" 952 | vs := "error: 127" 953 | addDumpTest(v, "("+vt+") "+vs+"\n") 954 | addDumpTest(pv, "(*"+vt+")("+vAddr+")("+vs+")\n") 955 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+")("+vs+")\n") 956 | addDumpTest(nv, "(*"+vt+")()\n") 957 | } 958 | 959 | // TestDump executes all of the tests described by dumpTests. 960 | func TestDump(t *testing.T) { 961 | // Setup tests. 962 | addIntDumpTests() 963 | addUintDumpTests() 964 | addBoolDumpTests() 965 | addFloatDumpTests() 966 | addComplexDumpTests() 967 | addArrayDumpTests() 968 | addSliceDumpTests() 969 | addStringDumpTests() 970 | addInterfaceDumpTests() 971 | addMapDumpTests() 972 | addStructDumpTests() 973 | addUintptrDumpTests() 974 | addUnsafePointerDumpTests() 975 | addChanDumpTests() 976 | addFuncDumpTests() 977 | addCircularDumpTests() 978 | addPanicDumpTests() 979 | addErrorDumpTests() 980 | addCgoDumpTests() 981 | 982 | t.Logf("Running %d tests", len(dumpTests)) 983 | for i, test := range dumpTests { 984 | buf := new(bytes.Buffer) 985 | spew.Fdump(buf, test.in) 986 | s := buf.String() 987 | if testFailed(s, test.wants) { 988 | t.Errorf("Dump #%d\n got: %s %s", i, s, stringizeWants(test.wants)) 989 | continue 990 | } 991 | } 992 | } 993 | 994 | func TestDumpSortedKeys(t *testing.T) { 995 | cfg := spew.ConfigState{SortKeys: true} 996 | s := cfg.Sdump(map[int]string{1: "1", 3: "3", 2: "2"}) 997 | expected := "(map[int]string) (len=3) {\n(int) 1: (string) (len=1) " + 998 | "\"1\",\n(int) 2: (string) (len=1) \"2\",\n(int) 3: (string) " + 999 | "(len=1) \"3\"\n" + 1000 | "}\n" 1001 | if s != expected { 1002 | t.Errorf("Sorted keys mismatch:\n %v %v", s, expected) 1003 | } 1004 | 1005 | s = cfg.Sdump(map[stringer]int{"1": 1, "3": 3, "2": 2}) 1006 | expected = "(map[spew_test.stringer]int) (len=3) {\n" + 1007 | "(spew_test.stringer) (len=1) stringer 1: (int) 1,\n" + 1008 | "(spew_test.stringer) (len=1) stringer 2: (int) 2,\n" + 1009 | "(spew_test.stringer) (len=1) stringer 3: (int) 3\n" + 1010 | "}\n" 1011 | if s != expected { 1012 | t.Errorf("Sorted keys mismatch:\n %v %v", s, expected) 1013 | } 1014 | 1015 | s = cfg.Sdump(map[pstringer]int{pstringer("1"): 1, pstringer("3"): 3, pstringer("2"): 2}) 1016 | expected = "(map[spew_test.pstringer]int) (len=3) {\n" + 1017 | "(spew_test.pstringer) (len=1) stringer 1: (int) 1,\n" + 1018 | "(spew_test.pstringer) (len=1) stringer 2: (int) 2,\n" + 1019 | "(spew_test.pstringer) (len=1) stringer 3: (int) 3\n" + 1020 | "}\n" 1021 | if spew.UnsafeDisabled { 1022 | expected = "(map[spew_test.pstringer]int) (len=3) {\n" + 1023 | "(spew_test.pstringer) (len=1) \"1\": (int) 1,\n" + 1024 | "(spew_test.pstringer) (len=1) \"2\": (int) 2,\n" + 1025 | "(spew_test.pstringer) (len=1) \"3\": (int) 3\n" + 1026 | "}\n" 1027 | } 1028 | if s != expected { 1029 | t.Errorf("Sorted keys mismatch:\n %v %v", s, expected) 1030 | } 1031 | 1032 | s = cfg.Sdump(map[customError]int{customError(1): 1, customError(3): 3, customError(2): 2}) 1033 | expected = "(map[spew_test.customError]int) (len=3) {\n" + 1034 | "(spew_test.customError) error: 1: (int) 1,\n" + 1035 | "(spew_test.customError) error: 2: (int) 2,\n" + 1036 | "(spew_test.customError) error: 3: (int) 3\n" + 1037 | "}\n" 1038 | if s != expected { 1039 | t.Errorf("Sorted keys mismatch:\n %v %v", s, expected) 1040 | } 1041 | 1042 | } 1043 | -------------------------------------------------------------------------------- /spew/dumpcgo_test.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013-2016 Dave Collins 2 | // 3 | // Permission to use, copy, modify, and distribute this software for any 4 | // purpose with or without fee is hereby granted, provided that the above 5 | // copyright notice and this permission notice appear in all copies. 6 | // 7 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | 15 | // NOTE: Due to the following build constraints, this file will only be compiled 16 | // when both cgo is supported and "-tags testcgo" is added to the go test 17 | // command line. This means the cgo tests are only added (and hence run) when 18 | // specifially requested. This configuration is used because spew itself 19 | // does not require cgo to run even though it does handle certain cgo types 20 | // specially. Rather than forcing all clients to require cgo and an external 21 | // C compiler just to run the tests, this scheme makes them optional. 22 | // +build cgo,testcgo 23 | 24 | package spew_test 25 | 26 | import ( 27 | "fmt" 28 | 29 | "github.com/davecgh/go-spew/spew/testdata" 30 | ) 31 | 32 | func addCgoDumpTests() { 33 | // C char pointer. 34 | v := testdata.GetCgoCharPointer() 35 | nv := testdata.GetCgoNullCharPointer() 36 | pv := &v 37 | vcAddr := fmt.Sprintf("%p", v) 38 | vAddr := fmt.Sprintf("%p", pv) 39 | pvAddr := fmt.Sprintf("%p", &pv) 40 | vt := "*testdata._Ctype_char" 41 | vs := "116" 42 | addDumpTest(v, "("+vt+")("+vcAddr+")("+vs+")\n") 43 | addDumpTest(pv, "(*"+vt+")("+vAddr+"->"+vcAddr+")("+vs+")\n") 44 | addDumpTest(&pv, "(**"+vt+")("+pvAddr+"->"+vAddr+"->"+vcAddr+")("+vs+")\n") 45 | addDumpTest(nv, "("+vt+")()\n") 46 | 47 | // C char array. 48 | v2, v2l, v2c := testdata.GetCgoCharArray() 49 | v2Len := fmt.Sprintf("%d", v2l) 50 | v2Cap := fmt.Sprintf("%d", v2c) 51 | v2t := "[6]testdata._Ctype_char" 52 | v2s := "(len=" + v2Len + " cap=" + v2Cap + ") " + 53 | "{\n 00000000 74 65 73 74 32 00 " + 54 | " |test2.|\n}" 55 | addDumpTest(v2, "("+v2t+") "+v2s+"\n") 56 | 57 | // C unsigned char array. 58 | v3, v3l, v3c := testdata.GetCgoUnsignedCharArray() 59 | v3Len := fmt.Sprintf("%d", v3l) 60 | v3Cap := fmt.Sprintf("%d", v3c) 61 | v3t := "[6]testdata._Ctype_unsignedchar" 62 | v3t2 := "[6]testdata._Ctype_uchar" 63 | v3s := "(len=" + v3Len + " cap=" + v3Cap + ") " + 64 | "{\n 00000000 74 65 73 74 33 00 " + 65 | " |test3.|\n}" 66 | addDumpTest(v3, "("+v3t+") "+v3s+"\n", "("+v3t2+") "+v3s+"\n") 67 | 68 | // C signed char array. 69 | v4, v4l, v4c := testdata.GetCgoSignedCharArray() 70 | v4Len := fmt.Sprintf("%d", v4l) 71 | v4Cap := fmt.Sprintf("%d", v4c) 72 | v4t := "[6]testdata._Ctype_schar" 73 | v4t2 := "testdata._Ctype_schar" 74 | v4s := "(len=" + v4Len + " cap=" + v4Cap + ") " + 75 | "{\n (" + v4t2 + ") 116,\n (" + v4t2 + ") 101,\n (" + v4t2 + 76 | ") 115,\n (" + v4t2 + ") 116,\n (" + v4t2 + ") 52,\n (" + v4t2 + 77 | ") 0\n}" 78 | addDumpTest(v4, "("+v4t+") "+v4s+"\n") 79 | 80 | // C uint8_t array. 81 | v5, v5l, v5c := testdata.GetCgoUint8tArray() 82 | v5Len := fmt.Sprintf("%d", v5l) 83 | v5Cap := fmt.Sprintf("%d", v5c) 84 | v5t := "[6]testdata._Ctype_uint8_t" 85 | v5t2 := "[6]testdata._Ctype_uchar" 86 | v5s := "(len=" + v5Len + " cap=" + v5Cap + ") " + 87 | "{\n 00000000 74 65 73 74 35 00 " + 88 | " |test5.|\n}" 89 | addDumpTest(v5, "("+v5t+") "+v5s+"\n", "("+v5t2+") "+v5s+"\n") 90 | 91 | // C typedefed unsigned char array. 92 | v6, v6l, v6c := testdata.GetCgoTypdefedUnsignedCharArray() 93 | v6Len := fmt.Sprintf("%d", v6l) 94 | v6Cap := fmt.Sprintf("%d", v6c) 95 | v6t := "[6]testdata._Ctype_custom_uchar_t" 96 | v6t2 := "[6]testdata._Ctype_uchar" 97 | v6s := "(len=" + v6Len + " cap=" + v6Cap + ") " + 98 | "{\n 00000000 74 65 73 74 36 00 " + 99 | " |test6.|\n}" 100 | addDumpTest(v6, "("+v6t+") "+v6s+"\n", "("+v6t2+") "+v6s+"\n") 101 | } 102 | -------------------------------------------------------------------------------- /spew/dumpnocgo_test.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 Dave Collins 2 | // 3 | // Permission to use, copy, modify, and distribute this software for any 4 | // purpose with or without fee is hereby granted, provided that the above 5 | // copyright notice and this permission notice appear in all copies. 6 | // 7 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | 15 | // NOTE: Due to the following build constraints, this file will only be compiled 16 | // when either cgo is not supported or "-tags testcgo" is not added to the go 17 | // test command line. This file intentionally does not setup any cgo tests in 18 | // this scenario. 19 | // +build !cgo !testcgo 20 | 21 | package spew_test 22 | 23 | func addCgoDumpTests() { 24 | // Don't add any tests for cgo since this file is only compiled when 25 | // there should not be any cgo tests. 26 | } 27 | -------------------------------------------------------------------------------- /spew/example_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | package spew_test 18 | 19 | import ( 20 | "fmt" 21 | 22 | "github.com/davecgh/go-spew/spew" 23 | ) 24 | 25 | type Flag int 26 | 27 | const ( 28 | flagOne Flag = iota 29 | flagTwo 30 | ) 31 | 32 | var flagStrings = map[Flag]string{ 33 | flagOne: "flagOne", 34 | flagTwo: "flagTwo", 35 | } 36 | 37 | func (f Flag) String() string { 38 | if s, ok := flagStrings[f]; ok { 39 | return s 40 | } 41 | return fmt.Sprintf("Unknown flag (%d)", int(f)) 42 | } 43 | 44 | type Bar struct { 45 | data uintptr 46 | } 47 | 48 | type Foo struct { 49 | unexportedField Bar 50 | ExportedField map[interface{}]interface{} 51 | } 52 | 53 | // This example demonstrates how to use Dump to dump variables to stdout. 54 | func ExampleDump() { 55 | // The following package level declarations are assumed for this example: 56 | /* 57 | type Flag int 58 | 59 | const ( 60 | flagOne Flag = iota 61 | flagTwo 62 | ) 63 | 64 | var flagStrings = map[Flag]string{ 65 | flagOne: "flagOne", 66 | flagTwo: "flagTwo", 67 | } 68 | 69 | func (f Flag) String() string { 70 | if s, ok := flagStrings[f]; ok { 71 | return s 72 | } 73 | return fmt.Sprintf("Unknown flag (%d)", int(f)) 74 | } 75 | 76 | type Bar struct { 77 | data uintptr 78 | } 79 | 80 | type Foo struct { 81 | unexportedField Bar 82 | ExportedField map[interface{}]interface{} 83 | } 84 | */ 85 | 86 | // Setup some sample data structures for the example. 87 | bar := Bar{uintptr(0)} 88 | s1 := Foo{bar, map[interface{}]interface{}{"one": true}} 89 | f := Flag(5) 90 | b := []byte{ 91 | 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 92 | 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 93 | 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 94 | 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 95 | 0x31, 0x32, 96 | } 97 | 98 | // Dump! 99 | spew.Dump(s1, f, b) 100 | 101 | // Output: 102 | // (spew_test.Foo) { 103 | // unexportedField: (spew_test.Bar) { 104 | // data: (uintptr) 105 | // }, 106 | // ExportedField: (map[interface {}]interface {}) (len=1) { 107 | // (string) (len=3) "one": (bool) true 108 | // } 109 | // } 110 | // (spew_test.Flag) Unknown flag (5) 111 | // ([]uint8) (len=34 cap=34) { 112 | // 00000000 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 |............... | 113 | // 00000010 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 |!"#$%&'()*+,-./0| 114 | // 00000020 31 32 |12| 115 | // } 116 | // 117 | } 118 | 119 | // This example demonstrates how to use Printf to display a variable with a 120 | // format string and inline formatting. 121 | func ExamplePrintf() { 122 | // Create a double pointer to a uint 8. 123 | ui8 := uint8(5) 124 | pui8 := &ui8 125 | ppui8 := &pui8 126 | 127 | // Create a circular data type. 128 | type circular struct { 129 | ui8 uint8 130 | c *circular 131 | } 132 | c := circular{ui8: 1} 133 | c.c = &c 134 | 135 | // Print! 136 | spew.Printf("ppui8: %v\n", ppui8) 137 | spew.Printf("circular: %v\n", c) 138 | 139 | // Output: 140 | // ppui8: <**>5 141 | // circular: {1 <*>{1 <*>}} 142 | } 143 | 144 | // This example demonstrates how to use a ConfigState. 145 | func ExampleConfigState() { 146 | // Modify the indent level of the ConfigState only. The global 147 | // configuration is not modified. 148 | scs := spew.ConfigState{Indent: "\t"} 149 | 150 | // Output using the ConfigState instance. 151 | v := map[string]int{"one": 1} 152 | scs.Printf("v: %v\n", v) 153 | scs.Dump(v) 154 | 155 | // Output: 156 | // v: map[one:1] 157 | // (map[string]int) (len=1) { 158 | // (string) (len=3) "one": (int) 1 159 | // } 160 | } 161 | 162 | // This example demonstrates how to use ConfigState.Dump to dump variables to 163 | // stdout 164 | func ExampleConfigState_Dump() { 165 | // See the top-level Dump example for details on the types used in this 166 | // example. 167 | 168 | // Create two ConfigState instances with different indentation. 169 | scs := spew.ConfigState{Indent: "\t"} 170 | scs2 := spew.ConfigState{Indent: " "} 171 | 172 | // Setup some sample data structures for the example. 173 | bar := Bar{uintptr(0)} 174 | s1 := Foo{bar, map[interface{}]interface{}{"one": true}} 175 | 176 | // Dump using the ConfigState instances. 177 | scs.Dump(s1) 178 | scs2.Dump(s1) 179 | 180 | // Output: 181 | // (spew_test.Foo) { 182 | // unexportedField: (spew_test.Bar) { 183 | // data: (uintptr) 184 | // }, 185 | // ExportedField: (map[interface {}]interface {}) (len=1) { 186 | // (string) (len=3) "one": (bool) true 187 | // } 188 | // } 189 | // (spew_test.Foo) { 190 | // unexportedField: (spew_test.Bar) { 191 | // data: (uintptr) 192 | // }, 193 | // ExportedField: (map[interface {}]interface {}) (len=1) { 194 | // (string) (len=3) "one": (bool) true 195 | // } 196 | // } 197 | // 198 | } 199 | 200 | // This example demonstrates how to use ConfigState.Printf to display a variable 201 | // with a format string and inline formatting. 202 | func ExampleConfigState_Printf() { 203 | // See the top-level Dump example for details on the types used in this 204 | // example. 205 | 206 | // Create two ConfigState instances and modify the method handling of the 207 | // first ConfigState only. 208 | scs := spew.NewDefaultConfig() 209 | scs2 := spew.NewDefaultConfig() 210 | scs.DisableMethods = true 211 | 212 | // Alternatively 213 | // scs := spew.ConfigState{Indent: " ", DisableMethods: true} 214 | // scs2 := spew.ConfigState{Indent: " "} 215 | 216 | // This is of type Flag which implements a Stringer and has raw value 1. 217 | f := flagTwo 218 | 219 | // Dump using the ConfigState instances. 220 | scs.Printf("f: %v\n", f) 221 | scs2.Printf("f: %v\n", f) 222 | 223 | // Output: 224 | // f: 1 225 | // f: flagTwo 226 | } 227 | -------------------------------------------------------------------------------- /spew/format.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | package spew 18 | 19 | import ( 20 | "bytes" 21 | "fmt" 22 | "reflect" 23 | "strconv" 24 | "strings" 25 | ) 26 | 27 | // supportedFlags is a list of all the character flags supported by fmt package. 28 | const supportedFlags = "0-+# " 29 | 30 | // formatState implements the fmt.Formatter interface and contains information 31 | // about the state of a formatting operation. The NewFormatter function can 32 | // be used to get a new Formatter which can be used directly as arguments 33 | // in standard fmt package printing calls. 34 | type formatState struct { 35 | value interface{} 36 | fs fmt.State 37 | depth int 38 | pointers map[uintptr]int 39 | ignoreNextType bool 40 | cs *ConfigState 41 | } 42 | 43 | // buildDefaultFormat recreates the original format string without precision 44 | // and width information to pass in to fmt.Sprintf in the case of an 45 | // unrecognized type. Unless new types are added to the language, this 46 | // function won't ever be called. 47 | func (f *formatState) buildDefaultFormat() (format string) { 48 | buf := bytes.NewBuffer(percentBytes) 49 | 50 | for _, flag := range supportedFlags { 51 | if f.fs.Flag(int(flag)) { 52 | buf.WriteRune(flag) 53 | } 54 | } 55 | 56 | buf.WriteRune('v') 57 | 58 | format = buf.String() 59 | return format 60 | } 61 | 62 | // constructOrigFormat recreates the original format string including precision 63 | // and width information to pass along to the standard fmt package. This allows 64 | // automatic deferral of all format strings this package doesn't support. 65 | func (f *formatState) constructOrigFormat(verb rune) (format string) { 66 | buf := bytes.NewBuffer(percentBytes) 67 | 68 | for _, flag := range supportedFlags { 69 | if f.fs.Flag(int(flag)) { 70 | buf.WriteRune(flag) 71 | } 72 | } 73 | 74 | if width, ok := f.fs.Width(); ok { 75 | buf.WriteString(strconv.Itoa(width)) 76 | } 77 | 78 | if precision, ok := f.fs.Precision(); ok { 79 | buf.Write(precisionBytes) 80 | buf.WriteString(strconv.Itoa(precision)) 81 | } 82 | 83 | buf.WriteRune(verb) 84 | 85 | format = buf.String() 86 | return format 87 | } 88 | 89 | // unpackValue returns values inside of non-nil interfaces when possible and 90 | // ensures that types for values which have been unpacked from an interface 91 | // are displayed when the show types flag is also set. 92 | // This is useful for data types like structs, arrays, slices, and maps which 93 | // can contain varying types packed inside an interface. 94 | func (f *formatState) unpackValue(v reflect.Value) reflect.Value { 95 | if v.Kind() == reflect.Interface { 96 | f.ignoreNextType = false 97 | if !v.IsNil() { 98 | v = v.Elem() 99 | } 100 | } 101 | return v 102 | } 103 | 104 | // formatPtr handles formatting of pointers by indirecting them as necessary. 105 | func (f *formatState) formatPtr(v reflect.Value) { 106 | // Display nil if top level pointer is nil. 107 | showTypes := f.fs.Flag('#') 108 | if v.IsNil() && (!showTypes || f.ignoreNextType) { 109 | f.fs.Write(nilAngleBytes) 110 | return 111 | } 112 | 113 | // Remove pointers at or below the current depth from map used to detect 114 | // circular refs. 115 | for k, depth := range f.pointers { 116 | if depth >= f.depth { 117 | delete(f.pointers, k) 118 | } 119 | } 120 | 121 | // Keep list of all dereferenced pointers to possibly show later. 122 | pointerChain := make([]uintptr, 0) 123 | 124 | // Figure out how many levels of indirection there are by derferencing 125 | // pointers and unpacking interfaces down the chain while detecting circular 126 | // references. 127 | nilFound := false 128 | cycleFound := false 129 | indirects := 0 130 | ve := v 131 | for ve.Kind() == reflect.Ptr { 132 | if ve.IsNil() { 133 | nilFound = true 134 | break 135 | } 136 | indirects++ 137 | addr := ve.Pointer() 138 | pointerChain = append(pointerChain, addr) 139 | if pd, ok := f.pointers[addr]; ok && pd < f.depth { 140 | cycleFound = true 141 | indirects-- 142 | break 143 | } 144 | f.pointers[addr] = f.depth 145 | 146 | ve = ve.Elem() 147 | if ve.Kind() == reflect.Interface { 148 | if ve.IsNil() { 149 | nilFound = true 150 | break 151 | } 152 | ve = ve.Elem() 153 | } 154 | } 155 | 156 | // Display type or indirection level depending on flags. 157 | if showTypes && !f.ignoreNextType { 158 | f.fs.Write(openParenBytes) 159 | f.fs.Write(bytes.Repeat(asteriskBytes, indirects)) 160 | f.fs.Write([]byte(ve.Type().String())) 161 | f.fs.Write(closeParenBytes) 162 | } else { 163 | if nilFound || cycleFound { 164 | indirects += strings.Count(ve.Type().String(), "*") 165 | } 166 | f.fs.Write(openAngleBytes) 167 | f.fs.Write([]byte(strings.Repeat("*", indirects))) 168 | f.fs.Write(closeAngleBytes) 169 | } 170 | 171 | // Display pointer information depending on flags. 172 | if f.fs.Flag('+') && (len(pointerChain) > 0) { 173 | f.fs.Write(openParenBytes) 174 | for i, addr := range pointerChain { 175 | if i > 0 { 176 | f.fs.Write(pointerChainBytes) 177 | } 178 | printHexPtr(f.fs, addr) 179 | } 180 | f.fs.Write(closeParenBytes) 181 | } 182 | 183 | // Display dereferenced value. 184 | switch { 185 | case nilFound: 186 | f.fs.Write(nilAngleBytes) 187 | 188 | case cycleFound: 189 | f.fs.Write(circularShortBytes) 190 | 191 | default: 192 | f.ignoreNextType = true 193 | f.format(ve) 194 | } 195 | } 196 | 197 | // format is the main workhorse for providing the Formatter interface. It 198 | // uses the passed reflect value to figure out what kind of object we are 199 | // dealing with and formats it appropriately. It is a recursive function, 200 | // however circular data structures are detected and handled properly. 201 | func (f *formatState) format(v reflect.Value) { 202 | // Handle invalid reflect values immediately. 203 | kind := v.Kind() 204 | if kind == reflect.Invalid { 205 | f.fs.Write(invalidAngleBytes) 206 | return 207 | } 208 | 209 | // Handle pointers specially. 210 | if kind == reflect.Ptr { 211 | f.formatPtr(v) 212 | return 213 | } 214 | 215 | // Print type information unless already handled elsewhere. 216 | if !f.ignoreNextType && f.fs.Flag('#') { 217 | f.fs.Write(openParenBytes) 218 | f.fs.Write([]byte(v.Type().String())) 219 | f.fs.Write(closeParenBytes) 220 | } 221 | f.ignoreNextType = false 222 | 223 | // Call Stringer/error interfaces if they exist and the handle methods 224 | // flag is enabled. 225 | if !f.cs.DisableMethods { 226 | if (kind != reflect.Invalid) && (kind != reflect.Interface) { 227 | if handled := handleMethods(f.cs, f.fs, v); handled { 228 | return 229 | } 230 | } 231 | } 232 | 233 | switch kind { 234 | case reflect.Invalid: 235 | // Do nothing. We should never get here since invalid has already 236 | // been handled above. 237 | 238 | case reflect.Bool: 239 | printBool(f.fs, v.Bool()) 240 | 241 | case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int: 242 | printInt(f.fs, v.Int(), 10) 243 | 244 | case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint: 245 | printUint(f.fs, v.Uint(), 10) 246 | 247 | case reflect.Float32: 248 | printFloat(f.fs, v.Float(), 32) 249 | 250 | case reflect.Float64: 251 | printFloat(f.fs, v.Float(), 64) 252 | 253 | case reflect.Complex64: 254 | printComplex(f.fs, v.Complex(), 32) 255 | 256 | case reflect.Complex128: 257 | printComplex(f.fs, v.Complex(), 64) 258 | 259 | case reflect.Slice: 260 | if v.IsNil() { 261 | f.fs.Write(nilAngleBytes) 262 | break 263 | } 264 | fallthrough 265 | 266 | case reflect.Array: 267 | f.fs.Write(openBracketBytes) 268 | f.depth++ 269 | if (f.cs.MaxDepth != 0) && (f.depth > f.cs.MaxDepth) { 270 | f.fs.Write(maxShortBytes) 271 | } else { 272 | numEntries := v.Len() 273 | for i := 0; i < numEntries; i++ { 274 | if i > 0 { 275 | f.fs.Write(spaceBytes) 276 | } 277 | f.ignoreNextType = true 278 | f.format(f.unpackValue(v.Index(i))) 279 | } 280 | } 281 | f.depth-- 282 | f.fs.Write(closeBracketBytes) 283 | 284 | case reflect.String: 285 | f.fs.Write([]byte(v.String())) 286 | 287 | case reflect.Interface: 288 | // The only time we should get here is for nil interfaces due to 289 | // unpackValue calls. 290 | if v.IsNil() { 291 | f.fs.Write(nilAngleBytes) 292 | } 293 | 294 | case reflect.Ptr: 295 | // Do nothing. We should never get here since pointers have already 296 | // been handled above. 297 | 298 | case reflect.Map: 299 | // nil maps should be indicated as different than empty maps 300 | if v.IsNil() { 301 | f.fs.Write(nilAngleBytes) 302 | break 303 | } 304 | 305 | f.fs.Write(openMapBytes) 306 | f.depth++ 307 | if (f.cs.MaxDepth != 0) && (f.depth > f.cs.MaxDepth) { 308 | f.fs.Write(maxShortBytes) 309 | } else { 310 | keys := v.MapKeys() 311 | if f.cs.SortKeys { 312 | sortValues(keys, f.cs) 313 | } 314 | for i, key := range keys { 315 | if i > 0 { 316 | f.fs.Write(spaceBytes) 317 | } 318 | f.ignoreNextType = true 319 | f.format(f.unpackValue(key)) 320 | f.fs.Write(colonBytes) 321 | f.ignoreNextType = true 322 | f.format(f.unpackValue(v.MapIndex(key))) 323 | } 324 | } 325 | f.depth-- 326 | f.fs.Write(closeMapBytes) 327 | 328 | case reflect.Struct: 329 | numFields := v.NumField() 330 | f.fs.Write(openBraceBytes) 331 | f.depth++ 332 | if (f.cs.MaxDepth != 0) && (f.depth > f.cs.MaxDepth) { 333 | f.fs.Write(maxShortBytes) 334 | } else { 335 | vt := v.Type() 336 | for i := 0; i < numFields; i++ { 337 | if i > 0 { 338 | f.fs.Write(spaceBytes) 339 | } 340 | vtf := vt.Field(i) 341 | if f.fs.Flag('+') || f.fs.Flag('#') { 342 | f.fs.Write([]byte(vtf.Name)) 343 | f.fs.Write(colonBytes) 344 | } 345 | f.format(f.unpackValue(v.Field(i))) 346 | } 347 | } 348 | f.depth-- 349 | f.fs.Write(closeBraceBytes) 350 | 351 | case reflect.Uintptr: 352 | printHexPtr(f.fs, uintptr(v.Uint())) 353 | 354 | case reflect.UnsafePointer, reflect.Chan, reflect.Func: 355 | printHexPtr(f.fs, v.Pointer()) 356 | 357 | // There were not any other types at the time this code was written, but 358 | // fall back to letting the default fmt package handle it if any get added. 359 | default: 360 | format := f.buildDefaultFormat() 361 | if v.CanInterface() { 362 | fmt.Fprintf(f.fs, format, v.Interface()) 363 | } else { 364 | fmt.Fprintf(f.fs, format, v.String()) 365 | } 366 | } 367 | } 368 | 369 | // Format satisfies the fmt.Formatter interface. See NewFormatter for usage 370 | // details. 371 | func (f *formatState) Format(fs fmt.State, verb rune) { 372 | f.fs = fs 373 | 374 | // Use standard formatting for verbs that are not v. 375 | if verb != 'v' { 376 | format := f.constructOrigFormat(verb) 377 | fmt.Fprintf(fs, format, f.value) 378 | return 379 | } 380 | 381 | if f.value == nil { 382 | if fs.Flag('#') { 383 | fs.Write(interfaceBytes) 384 | } 385 | fs.Write(nilAngleBytes) 386 | return 387 | } 388 | 389 | f.format(reflect.ValueOf(f.value)) 390 | } 391 | 392 | // newFormatter is a helper function to consolidate the logic from the various 393 | // public methods which take varying config states. 394 | func newFormatter(cs *ConfigState, v interface{}) fmt.Formatter { 395 | fs := &formatState{value: v, cs: cs} 396 | fs.pointers = make(map[uintptr]int) 397 | return fs 398 | } 399 | 400 | /* 401 | NewFormatter returns a custom formatter that satisfies the fmt.Formatter 402 | interface. As a result, it integrates cleanly with standard fmt package 403 | printing functions. The formatter is useful for inline printing of smaller data 404 | types similar to the standard %v format specifier. 405 | 406 | The custom formatter only responds to the %v (most compact), %+v (adds pointer 407 | addresses), %#v (adds types), or %#+v (adds types and pointer addresses) verb 408 | combinations. Any other verbs such as %x and %q will be sent to the the 409 | standard fmt package for formatting. In addition, the custom formatter ignores 410 | the width and precision arguments (however they will still work on the format 411 | specifiers not handled by the custom formatter). 412 | 413 | Typically this function shouldn't be called directly. It is much easier to make 414 | use of the custom formatter by calling one of the convenience functions such as 415 | Printf, Println, or Fprintf. 416 | */ 417 | func NewFormatter(v interface{}) fmt.Formatter { 418 | return newFormatter(&Config, v) 419 | } 420 | -------------------------------------------------------------------------------- /spew/internal_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | /* 18 | This test file is part of the spew package rather than than the spew_test 19 | package because it needs access to internals to properly test certain cases 20 | which are not possible via the public interface since they should never happen. 21 | */ 22 | 23 | package spew 24 | 25 | import ( 26 | "bytes" 27 | "reflect" 28 | "testing" 29 | ) 30 | 31 | // dummyFmtState implements a fake fmt.State to use for testing invalid 32 | // reflect.Value handling. This is necessary because the fmt package catches 33 | // invalid values before invoking the formatter on them. 34 | type dummyFmtState struct { 35 | bytes.Buffer 36 | } 37 | 38 | func (dfs *dummyFmtState) Flag(f int) bool { 39 | return f == int('+') 40 | } 41 | 42 | func (dfs *dummyFmtState) Precision() (int, bool) { 43 | return 0, false 44 | } 45 | 46 | func (dfs *dummyFmtState) Width() (int, bool) { 47 | return 0, false 48 | } 49 | 50 | // TestInvalidReflectValue ensures the dump and formatter code handles an 51 | // invalid reflect value properly. This needs access to internal state since it 52 | // should never happen in real code and therefore can't be tested via the public 53 | // API. 54 | func TestInvalidReflectValue(t *testing.T) { 55 | i := 1 56 | 57 | // Dump invalid reflect value. 58 | v := new(reflect.Value) 59 | buf := new(bytes.Buffer) 60 | d := dumpState{w: buf, cs: &Config} 61 | d.dump(*v) 62 | s := buf.String() 63 | want := "" 64 | if s != want { 65 | t.Errorf("InvalidReflectValue #%d\n got: %s want: %s", i, s, want) 66 | } 67 | i++ 68 | 69 | // Formatter invalid reflect value. 70 | buf2 := new(dummyFmtState) 71 | f := formatState{value: *v, cs: &Config, fs: buf2} 72 | f.format(*v) 73 | s = buf2.String() 74 | want = "" 75 | if s != want { 76 | t.Errorf("InvalidReflectValue #%d got: %s want: %s", i, s, want) 77 | } 78 | } 79 | 80 | // SortValues makes the internal sortValues function available to the test 81 | // package. 82 | func SortValues(values []reflect.Value, cs *ConfigState) { 83 | sortValues(values, cs) 84 | } 85 | -------------------------------------------------------------------------------- /spew/internalunsafe_test.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013-2016 Dave Collins 2 | 3 | // Permission to use, copy, modify, and distribute this software for any 4 | // purpose with or without fee is hereby granted, provided that the above 5 | // copyright notice and this permission notice appear in all copies. 6 | 7 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | 15 | // NOTE: Due to the following build constraints, this file will only be compiled 16 | // when the code is not running on Google App Engine, compiled by GopherJS, and 17 | // "-tags safe" is not added to the go build command line. The "disableunsafe" 18 | // tag is deprecated and thus should not be used. 19 | // +build !js,!appengine,!safe,!disableunsafe,go1.4 20 | 21 | /* 22 | This test file is part of the spew package rather than than the spew_test 23 | package because it needs access to internals to properly test certain cases 24 | which are not possible via the public interface since they should never happen. 25 | */ 26 | 27 | package spew 28 | 29 | import ( 30 | "bytes" 31 | "reflect" 32 | "testing" 33 | ) 34 | 35 | // changeKind uses unsafe to intentionally change the kind of a reflect.Value to 36 | // the maximum kind value which does not exist. This is needed to test the 37 | // fallback code which punts to the standard fmt library for new types that 38 | // might get added to the language. 39 | func changeKind(v *reflect.Value, readOnly bool) { 40 | flags := flagField(v) 41 | if readOnly { 42 | *flags |= flagRO 43 | } else { 44 | *flags &^= flagRO 45 | } 46 | *flags |= flagKindMask 47 | } 48 | 49 | // TestAddedReflectValue tests functionaly of the dump and formatter code which 50 | // falls back to the standard fmt library for new types that might get added to 51 | // the language. 52 | func TestAddedReflectValue(t *testing.T) { 53 | i := 1 54 | 55 | // Dump using a reflect.Value that is exported. 56 | v := reflect.ValueOf(int8(5)) 57 | changeKind(&v, false) 58 | buf := new(bytes.Buffer) 59 | d := dumpState{w: buf, cs: &Config} 60 | d.dump(v) 61 | s := buf.String() 62 | want := "(int8) 5" 63 | if s != want { 64 | t.Errorf("TestAddedReflectValue #%d\n got: %s want: %s", i, s, want) 65 | } 66 | i++ 67 | 68 | // Dump using a reflect.Value that is not exported. 69 | changeKind(&v, true) 70 | buf.Reset() 71 | d.dump(v) 72 | s = buf.String() 73 | want = "(int8) " 74 | if s != want { 75 | t.Errorf("TestAddedReflectValue #%d\n got: %s want: %s", i, s, want) 76 | } 77 | i++ 78 | 79 | // Formatter using a reflect.Value that is exported. 80 | changeKind(&v, false) 81 | buf2 := new(dummyFmtState) 82 | f := formatState{value: v, cs: &Config, fs: buf2} 83 | f.format(v) 84 | s = buf2.String() 85 | want = "5" 86 | if s != want { 87 | t.Errorf("TestAddedReflectValue #%d got: %s want: %s", i, s, want) 88 | } 89 | i++ 90 | 91 | // Formatter using a reflect.Value that is not exported. 92 | changeKind(&v, true) 93 | buf2.Reset() 94 | f = formatState{value: v, cs: &Config, fs: buf2} 95 | f.format(v) 96 | s = buf2.String() 97 | want = "" 98 | if s != want { 99 | t.Errorf("TestAddedReflectValue #%d got: %s want: %s", i, s, want) 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /spew/spew.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | package spew 18 | 19 | import ( 20 | "fmt" 21 | "io" 22 | ) 23 | 24 | // Errorf is a wrapper for fmt.Errorf that treats each argument as if it were 25 | // passed with a default Formatter interface returned by NewFormatter. It 26 | // returns the formatted string as a value that satisfies error. See 27 | // NewFormatter for formatting details. 28 | // 29 | // This function is shorthand for the following syntax: 30 | // 31 | // fmt.Errorf(format, spew.NewFormatter(a), spew.NewFormatter(b)) 32 | func Errorf(format string, a ...interface{}) (err error) { 33 | return fmt.Errorf(format, convertArgs(a)...) 34 | } 35 | 36 | // Fprint is a wrapper for fmt.Fprint that treats each argument as if it were 37 | // passed with a default Formatter interface returned by NewFormatter. It 38 | // returns the number of bytes written and any write error encountered. See 39 | // NewFormatter for formatting details. 40 | // 41 | // This function is shorthand for the following syntax: 42 | // 43 | // fmt.Fprint(w, spew.NewFormatter(a), spew.NewFormatter(b)) 44 | func Fprint(w io.Writer, a ...interface{}) (n int, err error) { 45 | return fmt.Fprint(w, convertArgs(a)...) 46 | } 47 | 48 | // Fprintf is a wrapper for fmt.Fprintf that treats each argument as if it were 49 | // passed with a default Formatter interface returned by NewFormatter. It 50 | // returns the number of bytes written and any write error encountered. See 51 | // NewFormatter for formatting details. 52 | // 53 | // This function is shorthand for the following syntax: 54 | // 55 | // fmt.Fprintf(w, format, spew.NewFormatter(a), spew.NewFormatter(b)) 56 | func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) { 57 | return fmt.Fprintf(w, format, convertArgs(a)...) 58 | } 59 | 60 | // Fprintln is a wrapper for fmt.Fprintln that treats each argument as if it 61 | // passed with a default Formatter interface returned by NewFormatter. See 62 | // NewFormatter for formatting details. 63 | // 64 | // This function is shorthand for the following syntax: 65 | // 66 | // fmt.Fprintln(w, spew.NewFormatter(a), spew.NewFormatter(b)) 67 | func Fprintln(w io.Writer, a ...interface{}) (n int, err error) { 68 | return fmt.Fprintln(w, convertArgs(a)...) 69 | } 70 | 71 | // Print is a wrapper for fmt.Print that treats each argument as if it were 72 | // passed with a default Formatter interface returned by NewFormatter. It 73 | // returns the number of bytes written and any write error encountered. See 74 | // NewFormatter for formatting details. 75 | // 76 | // This function is shorthand for the following syntax: 77 | // 78 | // fmt.Print(spew.NewFormatter(a), spew.NewFormatter(b)) 79 | func Print(a ...interface{}) (n int, err error) { 80 | return fmt.Print(convertArgs(a)...) 81 | } 82 | 83 | // Printf is a wrapper for fmt.Printf that treats each argument as if it were 84 | // passed with a default Formatter interface returned by NewFormatter. It 85 | // returns the number of bytes written and any write error encountered. See 86 | // NewFormatter for formatting details. 87 | // 88 | // This function is shorthand for the following syntax: 89 | // 90 | // fmt.Printf(format, spew.NewFormatter(a), spew.NewFormatter(b)) 91 | func Printf(format string, a ...interface{}) (n int, err error) { 92 | return fmt.Printf(format, convertArgs(a)...) 93 | } 94 | 95 | // Println is a wrapper for fmt.Println that treats each argument as if it were 96 | // passed with a default Formatter interface returned by NewFormatter. It 97 | // returns the number of bytes written and any write error encountered. See 98 | // NewFormatter for formatting details. 99 | // 100 | // This function is shorthand for the following syntax: 101 | // 102 | // fmt.Println(spew.NewFormatter(a), spew.NewFormatter(b)) 103 | func Println(a ...interface{}) (n int, err error) { 104 | return fmt.Println(convertArgs(a)...) 105 | } 106 | 107 | // Sprint is a wrapper for fmt.Sprint that treats each argument as if it were 108 | // passed with a default Formatter interface returned by NewFormatter. It 109 | // returns the resulting string. See NewFormatter for formatting details. 110 | // 111 | // This function is shorthand for the following syntax: 112 | // 113 | // fmt.Sprint(spew.NewFormatter(a), spew.NewFormatter(b)) 114 | func Sprint(a ...interface{}) string { 115 | return fmt.Sprint(convertArgs(a)...) 116 | } 117 | 118 | // Sprintf is a wrapper for fmt.Sprintf that treats each argument as if it were 119 | // passed with a default Formatter interface returned by NewFormatter. It 120 | // returns the resulting string. See NewFormatter for formatting details. 121 | // 122 | // This function is shorthand for the following syntax: 123 | // 124 | // fmt.Sprintf(format, spew.NewFormatter(a), spew.NewFormatter(b)) 125 | func Sprintf(format string, a ...interface{}) string { 126 | return fmt.Sprintf(format, convertArgs(a)...) 127 | } 128 | 129 | // Sprintln is a wrapper for fmt.Sprintln that treats each argument as if it 130 | // were passed with a default Formatter interface returned by NewFormatter. It 131 | // returns the resulting string. See NewFormatter for formatting details. 132 | // 133 | // This function is shorthand for the following syntax: 134 | // 135 | // fmt.Sprintln(spew.NewFormatter(a), spew.NewFormatter(b)) 136 | func Sprintln(a ...interface{}) string { 137 | return fmt.Sprintln(convertArgs(a)...) 138 | } 139 | 140 | // convertArgs accepts a slice of arguments and returns a slice of the same 141 | // length with each argument converted to a default spew Formatter interface. 142 | func convertArgs(args []interface{}) (formatters []interface{}) { 143 | formatters = make([]interface{}, len(args)) 144 | for index, arg := range args { 145 | formatters[index] = NewFormatter(arg) 146 | } 147 | return formatters 148 | } 149 | -------------------------------------------------------------------------------- /spew/spew_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013-2016 Dave Collins 3 | * 4 | * Permission to use, copy, modify, and distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | package spew_test 18 | 19 | import ( 20 | "bytes" 21 | "fmt" 22 | "io/ioutil" 23 | "os" 24 | "testing" 25 | 26 | "github.com/davecgh/go-spew/spew" 27 | ) 28 | 29 | // spewFunc is used to identify which public function of the spew package or 30 | // ConfigState a test applies to. 31 | type spewFunc int 32 | 33 | const ( 34 | fCSFdump spewFunc = iota 35 | fCSFprint 36 | fCSFprintf 37 | fCSFprintln 38 | fCSPrint 39 | fCSPrintln 40 | fCSSdump 41 | fCSSprint 42 | fCSSprintf 43 | fCSSprintln 44 | fCSErrorf 45 | fCSNewFormatter 46 | fErrorf 47 | fFprint 48 | fFprintln 49 | fPrint 50 | fPrintln 51 | fSdump 52 | fSprint 53 | fSprintf 54 | fSprintln 55 | ) 56 | 57 | // Map of spewFunc values to names for pretty printing. 58 | var spewFuncStrings = map[spewFunc]string{ 59 | fCSFdump: "ConfigState.Fdump", 60 | fCSFprint: "ConfigState.Fprint", 61 | fCSFprintf: "ConfigState.Fprintf", 62 | fCSFprintln: "ConfigState.Fprintln", 63 | fCSSdump: "ConfigState.Sdump", 64 | fCSPrint: "ConfigState.Print", 65 | fCSPrintln: "ConfigState.Println", 66 | fCSSprint: "ConfigState.Sprint", 67 | fCSSprintf: "ConfigState.Sprintf", 68 | fCSSprintln: "ConfigState.Sprintln", 69 | fCSErrorf: "ConfigState.Errorf", 70 | fCSNewFormatter: "ConfigState.NewFormatter", 71 | fErrorf: "spew.Errorf", 72 | fFprint: "spew.Fprint", 73 | fFprintln: "spew.Fprintln", 74 | fPrint: "spew.Print", 75 | fPrintln: "spew.Println", 76 | fSdump: "spew.Sdump", 77 | fSprint: "spew.Sprint", 78 | fSprintf: "spew.Sprintf", 79 | fSprintln: "spew.Sprintln", 80 | } 81 | 82 | func (f spewFunc) String() string { 83 | if s, ok := spewFuncStrings[f]; ok { 84 | return s 85 | } 86 | return fmt.Sprintf("Unknown spewFunc (%d)", int(f)) 87 | } 88 | 89 | // spewTest is used to describe a test to be performed against the public 90 | // functions of the spew package or ConfigState. 91 | type spewTest struct { 92 | cs *spew.ConfigState 93 | f spewFunc 94 | format string 95 | in interface{} 96 | want string 97 | } 98 | 99 | // spewTests houses the tests to be performed against the public functions of 100 | // the spew package and ConfigState. 101 | // 102 | // These tests are only intended to ensure the public functions are exercised 103 | // and are intentionally not exhaustive of types. The exhaustive type 104 | // tests are handled in the dump and format tests. 105 | var spewTests []spewTest 106 | 107 | // redirStdout is a helper function to return the standard output from f as a 108 | // byte slice. 109 | func redirStdout(f func()) ([]byte, error) { 110 | tempFile, err := ioutil.TempFile("", "ss-test") 111 | if err != nil { 112 | return nil, err 113 | } 114 | fileName := tempFile.Name() 115 | defer os.Remove(fileName) // Ignore error 116 | 117 | origStdout := os.Stdout 118 | os.Stdout = tempFile 119 | f() 120 | os.Stdout = origStdout 121 | tempFile.Close() 122 | 123 | return ioutil.ReadFile(fileName) 124 | } 125 | 126 | func initSpewTests() { 127 | // Config states with various settings. 128 | scsDefault := spew.NewDefaultConfig() 129 | scsNoMethods := &spew.ConfigState{Indent: " ", DisableMethods: true} 130 | scsNoPmethods := &spew.ConfigState{Indent: " ", DisablePointerMethods: true} 131 | scsMaxDepth := &spew.ConfigState{Indent: " ", MaxDepth: 1} 132 | scsContinue := &spew.ConfigState{Indent: " ", ContinueOnMethod: true} 133 | scsNoPtrAddr := &spew.ConfigState{DisablePointerAddresses: true} 134 | scsNoCap := &spew.ConfigState{DisableCapacities: true} 135 | 136 | // Variables for tests on types which implement Stringer interface with and 137 | // without a pointer receiver. 138 | ts := stringer("test") 139 | tps := pstringer("test") 140 | 141 | type ptrTester struct { 142 | s *struct{} 143 | } 144 | tptr := &ptrTester{s: &struct{}{}} 145 | 146 | // depthTester is used to test max depth handling for structs, array, slices 147 | // and maps. 148 | type depthTester struct { 149 | ic indirCir1 150 | arr [1]string 151 | slice []string 152 | m map[string]int 153 | } 154 | dt := depthTester{indirCir1{nil}, [1]string{"arr"}, []string{"slice"}, 155 | map[string]int{"one": 1}} 156 | 157 | // Variable for tests on types which implement error interface. 158 | te := customError(10) 159 | 160 | spewTests = []spewTest{ 161 | {scsDefault, fCSFdump, "", int8(127), "(int8) 127\n"}, 162 | {scsDefault, fCSFprint, "", int16(32767), "32767"}, 163 | {scsDefault, fCSFprintf, "%v", int32(2147483647), "2147483647"}, 164 | {scsDefault, fCSFprintln, "", int(2147483647), "2147483647\n"}, 165 | {scsDefault, fCSPrint, "", int64(9223372036854775807), "9223372036854775807"}, 166 | {scsDefault, fCSPrintln, "", uint8(255), "255\n"}, 167 | {scsDefault, fCSSdump, "", uint8(64), "(uint8) 64\n"}, 168 | {scsDefault, fCSSprint, "", complex(1, 2), "(1+2i)"}, 169 | {scsDefault, fCSSprintf, "%v", complex(float32(3), 4), "(3+4i)"}, 170 | {scsDefault, fCSSprintln, "", complex(float64(5), 6), "(5+6i)\n"}, 171 | {scsDefault, fCSErrorf, "%#v", uint16(65535), "(uint16)65535"}, 172 | {scsDefault, fCSNewFormatter, "%v", uint32(4294967295), "4294967295"}, 173 | {scsDefault, fErrorf, "%v", uint64(18446744073709551615), "18446744073709551615"}, 174 | {scsDefault, fFprint, "", float32(3.14), "3.14"}, 175 | {scsDefault, fFprintln, "", float64(6.28), "6.28\n"}, 176 | {scsDefault, fPrint, "", true, "true"}, 177 | {scsDefault, fPrintln, "", false, "false\n"}, 178 | {scsDefault, fSdump, "", complex(-10, -20), "(complex128) (-10-20i)\n"}, 179 | {scsDefault, fSprint, "", complex(-1, -2), "(-1-2i)"}, 180 | {scsDefault, fSprintf, "%v", complex(float32(-3), -4), "(-3-4i)"}, 181 | {scsDefault, fSprintln, "", complex(float64(-5), -6), "(-5-6i)\n"}, 182 | {scsNoMethods, fCSFprint, "", ts, "test"}, 183 | {scsNoMethods, fCSFprint, "", &ts, "<*>test"}, 184 | {scsNoMethods, fCSFprint, "", tps, "test"}, 185 | {scsNoMethods, fCSFprint, "", &tps, "<*>test"}, 186 | {scsNoPmethods, fCSFprint, "", ts, "stringer test"}, 187 | {scsNoPmethods, fCSFprint, "", &ts, "<*>stringer test"}, 188 | {scsNoPmethods, fCSFprint, "", tps, "test"}, 189 | {scsNoPmethods, fCSFprint, "", &tps, "<*>stringer test"}, 190 | {scsMaxDepth, fCSFprint, "", dt, "{{} [] [] map[]}"}, 191 | {scsMaxDepth, fCSFdump, "", dt, "(spew_test.depthTester) {\n" + 192 | " ic: (spew_test.indirCir1) {\n \n },\n" + 193 | " arr: ([1]string) (len=1 cap=1) {\n \n },\n" + 194 | " slice: ([]string) (len=1 cap=1) {\n \n },\n" + 195 | " m: (map[string]int) (len=1) {\n \n }\n}\n"}, 196 | {scsContinue, fCSFprint, "", ts, "(stringer test) test"}, 197 | {scsContinue, fCSFdump, "", ts, "(spew_test.stringer) " + 198 | "(len=4) (stringer test) \"test\"\n"}, 199 | {scsContinue, fCSFprint, "", te, "(error: 10) 10"}, 200 | {scsContinue, fCSFdump, "", te, "(spew_test.customError) " + 201 | "(error: 10) 10\n"}, 202 | {scsNoPtrAddr, fCSFprint, "", tptr, "<*>{<*>{}}"}, 203 | {scsNoPtrAddr, fCSSdump, "", tptr, "(*spew_test.ptrTester)({\ns: (*struct {})({\n})\n})\n"}, 204 | {scsNoCap, fCSSdump, "", make([]string, 0, 10), "([]string) {\n}\n"}, 205 | {scsNoCap, fCSSdump, "", make([]string, 1, 10), "([]string) (len=1) {\n(string) \"\"\n}\n"}, 206 | } 207 | } 208 | 209 | // TestSpew executes all of the tests described by spewTests. 210 | func TestSpew(t *testing.T) { 211 | initSpewTests() 212 | 213 | t.Logf("Running %d tests", len(spewTests)) 214 | for i, test := range spewTests { 215 | buf := new(bytes.Buffer) 216 | switch test.f { 217 | case fCSFdump: 218 | test.cs.Fdump(buf, test.in) 219 | 220 | case fCSFprint: 221 | test.cs.Fprint(buf, test.in) 222 | 223 | case fCSFprintf: 224 | test.cs.Fprintf(buf, test.format, test.in) 225 | 226 | case fCSFprintln: 227 | test.cs.Fprintln(buf, test.in) 228 | 229 | case fCSPrint: 230 | b, err := redirStdout(func() { test.cs.Print(test.in) }) 231 | if err != nil { 232 | t.Errorf("%v #%d %v", test.f, i, err) 233 | continue 234 | } 235 | buf.Write(b) 236 | 237 | case fCSPrintln: 238 | b, err := redirStdout(func() { test.cs.Println(test.in) }) 239 | if err != nil { 240 | t.Errorf("%v #%d %v", test.f, i, err) 241 | continue 242 | } 243 | buf.Write(b) 244 | 245 | case fCSSdump: 246 | str := test.cs.Sdump(test.in) 247 | buf.WriteString(str) 248 | 249 | case fCSSprint: 250 | str := test.cs.Sprint(test.in) 251 | buf.WriteString(str) 252 | 253 | case fCSSprintf: 254 | str := test.cs.Sprintf(test.format, test.in) 255 | buf.WriteString(str) 256 | 257 | case fCSSprintln: 258 | str := test.cs.Sprintln(test.in) 259 | buf.WriteString(str) 260 | 261 | case fCSErrorf: 262 | err := test.cs.Errorf(test.format, test.in) 263 | buf.WriteString(err.Error()) 264 | 265 | case fCSNewFormatter: 266 | fmt.Fprintf(buf, test.format, test.cs.NewFormatter(test.in)) 267 | 268 | case fErrorf: 269 | err := spew.Errorf(test.format, test.in) 270 | buf.WriteString(err.Error()) 271 | 272 | case fFprint: 273 | spew.Fprint(buf, test.in) 274 | 275 | case fFprintln: 276 | spew.Fprintln(buf, test.in) 277 | 278 | case fPrint: 279 | b, err := redirStdout(func() { spew.Print(test.in) }) 280 | if err != nil { 281 | t.Errorf("%v #%d %v", test.f, i, err) 282 | continue 283 | } 284 | buf.Write(b) 285 | 286 | case fPrintln: 287 | b, err := redirStdout(func() { spew.Println(test.in) }) 288 | if err != nil { 289 | t.Errorf("%v #%d %v", test.f, i, err) 290 | continue 291 | } 292 | buf.Write(b) 293 | 294 | case fSdump: 295 | str := spew.Sdump(test.in) 296 | buf.WriteString(str) 297 | 298 | case fSprint: 299 | str := spew.Sprint(test.in) 300 | buf.WriteString(str) 301 | 302 | case fSprintf: 303 | str := spew.Sprintf(test.format, test.in) 304 | buf.WriteString(str) 305 | 306 | case fSprintln: 307 | str := spew.Sprintln(test.in) 308 | buf.WriteString(str) 309 | 310 | default: 311 | t.Errorf("%v #%d unrecognized function", test.f, i) 312 | continue 313 | } 314 | s := buf.String() 315 | if test.want != s { 316 | t.Errorf("ConfigState #%d\n got: %s want: %s", i, s, test.want) 317 | continue 318 | } 319 | } 320 | } 321 | -------------------------------------------------------------------------------- /spew/testdata/dumpcgo.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 Dave Collins 2 | // 3 | // Permission to use, copy, modify, and distribute this software for any 4 | // purpose with or without fee is hereby granted, provided that the above 5 | // copyright notice and this permission notice appear in all copies. 6 | // 7 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | 15 | // NOTE: Due to the following build constraints, this file will only be compiled 16 | // when both cgo is supported and "-tags testcgo" is added to the go test 17 | // command line. This code should really only be in the dumpcgo_test.go file, 18 | // but unfortunately Go will not allow cgo in test files, so this is a 19 | // workaround to allow cgo types to be tested. This configuration is used 20 | // because spew itself does not require cgo to run even though it does handle 21 | // certain cgo types specially. Rather than forcing all clients to require cgo 22 | // and an external C compiler just to run the tests, this scheme makes them 23 | // optional. 24 | // +build cgo,testcgo 25 | 26 | package testdata 27 | 28 | /* 29 | #include 30 | typedef unsigned char custom_uchar_t; 31 | 32 | char *ncp = 0; 33 | char *cp = "test"; 34 | char ca[6] = {'t', 'e', 's', 't', '2', '\0'}; 35 | unsigned char uca[6] = {'t', 'e', 's', 't', '3', '\0'}; 36 | signed char sca[6] = {'t', 'e', 's', 't', '4', '\0'}; 37 | uint8_t ui8ta[6] = {'t', 'e', 's', 't', '5', '\0'}; 38 | custom_uchar_t tuca[6] = {'t', 'e', 's', 't', '6', '\0'}; 39 | */ 40 | import "C" 41 | 42 | // GetCgoNullCharPointer returns a null char pointer via cgo. This is only 43 | // used for tests. 44 | func GetCgoNullCharPointer() interface{} { 45 | return C.ncp 46 | } 47 | 48 | // GetCgoCharPointer returns a char pointer via cgo. This is only used for 49 | // tests. 50 | func GetCgoCharPointer() interface{} { 51 | return C.cp 52 | } 53 | 54 | // GetCgoCharArray returns a char array via cgo and the array's len and cap. 55 | // This is only used for tests. 56 | func GetCgoCharArray() (interface{}, int, int) { 57 | return C.ca, len(C.ca), cap(C.ca) 58 | } 59 | 60 | // GetCgoUnsignedCharArray returns an unsigned char array via cgo and the 61 | // array's len and cap. This is only used for tests. 62 | func GetCgoUnsignedCharArray() (interface{}, int, int) { 63 | return C.uca, len(C.uca), cap(C.uca) 64 | } 65 | 66 | // GetCgoSignedCharArray returns a signed char array via cgo and the array's len 67 | // and cap. This is only used for tests. 68 | func GetCgoSignedCharArray() (interface{}, int, int) { 69 | return C.sca, len(C.sca), cap(C.sca) 70 | } 71 | 72 | // GetCgoUint8tArray returns a uint8_t array via cgo and the array's len and 73 | // cap. This is only used for tests. 74 | func GetCgoUint8tArray() (interface{}, int, int) { 75 | return C.ui8ta, len(C.ui8ta), cap(C.ui8ta) 76 | } 77 | 78 | // GetCgoTypdefedUnsignedCharArray returns a typedefed unsigned char array via 79 | // cgo and the array's len and cap. This is only used for tests. 80 | func GetCgoTypdefedUnsignedCharArray() (interface{}, int, int) { 81 | return C.tuca, len(C.tuca), cap(C.tuca) 82 | } 83 | -------------------------------------------------------------------------------- /test_coverage.txt: -------------------------------------------------------------------------------- 1 | 2 | github.com/davecgh/go-spew/spew/dump.go dumpState.dump 100.00% (88/88) 3 | github.com/davecgh/go-spew/spew/format.go formatState.format 100.00% (82/82) 4 | github.com/davecgh/go-spew/spew/format.go formatState.formatPtr 100.00% (52/52) 5 | github.com/davecgh/go-spew/spew/dump.go dumpState.dumpPtr 100.00% (44/44) 6 | github.com/davecgh/go-spew/spew/dump.go dumpState.dumpSlice 100.00% (39/39) 7 | github.com/davecgh/go-spew/spew/common.go handleMethods 100.00% (30/30) 8 | github.com/davecgh/go-spew/spew/common.go printHexPtr 100.00% (18/18) 9 | github.com/davecgh/go-spew/spew/common.go unsafeReflectValue 100.00% (13/13) 10 | github.com/davecgh/go-spew/spew/format.go formatState.constructOrigFormat 100.00% (12/12) 11 | github.com/davecgh/go-spew/spew/dump.go fdump 100.00% (11/11) 12 | github.com/davecgh/go-spew/spew/format.go formatState.Format 100.00% (11/11) 13 | github.com/davecgh/go-spew/spew/common.go init 100.00% (10/10) 14 | github.com/davecgh/go-spew/spew/common.go printComplex 100.00% (9/9) 15 | github.com/davecgh/go-spew/spew/common.go valuesSorter.Less 100.00% (8/8) 16 | github.com/davecgh/go-spew/spew/format.go formatState.buildDefaultFormat 100.00% (7/7) 17 | github.com/davecgh/go-spew/spew/format.go formatState.unpackValue 100.00% (5/5) 18 | github.com/davecgh/go-spew/spew/dump.go dumpState.indent 100.00% (4/4) 19 | github.com/davecgh/go-spew/spew/common.go catchPanic 100.00% (4/4) 20 | github.com/davecgh/go-spew/spew/config.go ConfigState.convertArgs 100.00% (4/4) 21 | github.com/davecgh/go-spew/spew/spew.go convertArgs 100.00% (4/4) 22 | github.com/davecgh/go-spew/spew/format.go newFormatter 100.00% (3/3) 23 | github.com/davecgh/go-spew/spew/dump.go Sdump 100.00% (3/3) 24 | github.com/davecgh/go-spew/spew/common.go printBool 100.00% (3/3) 25 | github.com/davecgh/go-spew/spew/common.go sortValues 100.00% (3/3) 26 | github.com/davecgh/go-spew/spew/config.go ConfigState.Sdump 100.00% (3/3) 27 | github.com/davecgh/go-spew/spew/dump.go dumpState.unpackValue 100.00% (3/3) 28 | github.com/davecgh/go-spew/spew/spew.go Printf 100.00% (1/1) 29 | github.com/davecgh/go-spew/spew/spew.go Println 100.00% (1/1) 30 | github.com/davecgh/go-spew/spew/spew.go Sprint 100.00% (1/1) 31 | github.com/davecgh/go-spew/spew/spew.go Sprintf 100.00% (1/1) 32 | github.com/davecgh/go-spew/spew/spew.go Sprintln 100.00% (1/1) 33 | github.com/davecgh/go-spew/spew/common.go printFloat 100.00% (1/1) 34 | github.com/davecgh/go-spew/spew/config.go NewDefaultConfig 100.00% (1/1) 35 | github.com/davecgh/go-spew/spew/common.go printInt 100.00% (1/1) 36 | github.com/davecgh/go-spew/spew/common.go printUint 100.00% (1/1) 37 | github.com/davecgh/go-spew/spew/common.go valuesSorter.Len 100.00% (1/1) 38 | github.com/davecgh/go-spew/spew/common.go valuesSorter.Swap 100.00% (1/1) 39 | github.com/davecgh/go-spew/spew/config.go ConfigState.Errorf 100.00% (1/1) 40 | github.com/davecgh/go-spew/spew/config.go ConfigState.Fprint 100.00% (1/1) 41 | github.com/davecgh/go-spew/spew/config.go ConfigState.Fprintf 100.00% (1/1) 42 | github.com/davecgh/go-spew/spew/config.go ConfigState.Fprintln 100.00% (1/1) 43 | github.com/davecgh/go-spew/spew/config.go ConfigState.Print 100.00% (1/1) 44 | github.com/davecgh/go-spew/spew/config.go ConfigState.Printf 100.00% (1/1) 45 | github.com/davecgh/go-spew/spew/config.go ConfigState.Println 100.00% (1/1) 46 | github.com/davecgh/go-spew/spew/config.go ConfigState.Sprint 100.00% (1/1) 47 | github.com/davecgh/go-spew/spew/config.go ConfigState.Sprintf 100.00% (1/1) 48 | github.com/davecgh/go-spew/spew/config.go ConfigState.Sprintln 100.00% (1/1) 49 | github.com/davecgh/go-spew/spew/config.go ConfigState.NewFormatter 100.00% (1/1) 50 | github.com/davecgh/go-spew/spew/config.go ConfigState.Fdump 100.00% (1/1) 51 | github.com/davecgh/go-spew/spew/config.go ConfigState.Dump 100.00% (1/1) 52 | github.com/davecgh/go-spew/spew/dump.go Fdump 100.00% (1/1) 53 | github.com/davecgh/go-spew/spew/dump.go Dump 100.00% (1/1) 54 | github.com/davecgh/go-spew/spew/spew.go Fprintln 100.00% (1/1) 55 | github.com/davecgh/go-spew/spew/format.go NewFormatter 100.00% (1/1) 56 | github.com/davecgh/go-spew/spew/spew.go Errorf 100.00% (1/1) 57 | github.com/davecgh/go-spew/spew/spew.go Fprint 100.00% (1/1) 58 | github.com/davecgh/go-spew/spew/spew.go Fprintf 100.00% (1/1) 59 | github.com/davecgh/go-spew/spew/spew.go Print 100.00% (1/1) 60 | github.com/davecgh/go-spew/spew ------------------------------- 100.00% (505/505) 61 | 62 | --------------------------------------------------------------------------------