Debian Codegraph @

internal/spew/common.go

raw
1 /*
2 * Copyright (c) 2013-2016 Dave Collins <[email protected]>
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("<nil>")
50 maxNewlineBytes = []byte("<max depth reached>\n")
51 maxShortBytes = []byte("<max>")
52 circularBytes = []byte("<already shown>")
53 circularShortBytes = []byte("<shown>")
54 invalidAngleBytes = []byte("<invalid>")
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 v := vs.values[i]
248 // Keys taken from unexported struct fields are read-only,
249 // so Interface would panic. Bypass like handleMethods does;
250 // under "-tags safe" fall back to String.
251 if !v.CanInterface() {
252 v = unsafeReflectValue(v)
253 }
254 if v.CanInterface() {
255 vs.strings[i] = Sprintf("%#v", v.Interface())
256 } else {
257 vs.strings[i] = v.String()
258 }
259 }
260 }
261 return vs
262 }
263
264 // canSortSimply tests whether a reflect.Kind is a primitive that can be sorted
265 // directly, or whether it should be considered for sorting by surrogate keys
266 // (if the ConfigState allows it).
267 func canSortSimply(kind reflect.Kind) bool {
268 // This switch parallels valueSortLess, except for the default case.
269 switch kind {
270 case reflect.Bool:
271 return true
272 case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
273 return true
274 case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
275 return true
276 case reflect.Float32, reflect.Float64:
277 return true
278 case reflect.String:
279 return true
280 case reflect.Uintptr:
281 return true
282 case reflect.Array:
283 return true
284 }
285 return false
286 }
287
288 // Len returns the number of values in the slice. It is part of the
289 // sort.Interface implementation.
290 func (s *valuesSorter) Len() int {
291 return len(s.values)
292 }
293
294 // Swap swaps the values at the passed indices. It is part of the
295 // sort.Interface implementation.
296 func (s *valuesSorter) Swap(i, j int) {
297 s.values[i], s.values[j] = s.values[j], s.values[i]
298 if s.strings != nil {
299 s.strings[i], s.strings[j] = s.strings[j], s.strings[i]
300 }
301 }
302
303 // valueSortLess returns whether the first value should sort before the second
304 // value. It is used by valueSorter.Less as part of the sort.Interface
305 // implementation.
306 func valueSortLess(a, b reflect.Value) bool {
307 switch a.Kind() {
308 case reflect.Bool:
309 return !a.Bool() && b.Bool()
310 case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
311 return a.Int() < b.Int()
312 case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
313 return a.Uint() < b.Uint()
314 case reflect.Float32, reflect.Float64:
315 return a.Float() < b.Float()
316 case reflect.String:
317 return a.String() < b.String()
318 case reflect.Uintptr:
319 return a.Uint() < b.Uint()
320 case reflect.Array:
321 // Compare the contents of both arrays.
322 l := a.Len()
323 for i := 0; i < l; i++ {
324 av := a.Index(i)
325 bv := b.Index(i)
326 // Array elements taken from unexported struct fields are
327 // flagged read-only, so Interface would panic. Bypass the
328 // restriction the same way handleMethods does. Under
329 // "-tags safe" the bypass is a no-op, so guard with
330 // CanInterface and fall back to the String comparison below.
331 if !av.CanInterface() {
332 av = unsafeReflectValue(av)
333 bv = unsafeReflectValue(bv)
334 }
335 if !av.CanInterface() {
336 break
337 }
338 if av.Interface() == bv.Interface() {
339 continue
340 }
341 return valueSortLess(av, bv)
342 }
343 }
344 return a.String() < b.String()
345 }
346
347 // Less returns whether the value at index i should sort before the
348 // value at index j. It is part of the sort.Interface implementation.
349 func (s *valuesSorter) Less(i, j int) bool {
350 if s.strings == nil {
351 return valueSortLess(s.values[i], s.values[j])
352 }
353 return s.strings[i] < s.strings[j]
354 }
355
356 // sortValues is a sort function that handles both native types and any type that
357 // can be converted to error or Stringer. Other inputs are sorted according to
358 // their Value.String() value to ensure display stability.
359 func sortValues(values []reflect.Value, cs *ConfigState) {
360 if len(values) == 0 {
361 return
362 }
363 sort.Sort(newValuesSorter(values, cs))
364 }