2 * Copyright (c) 2013-2016 Dave Collins <dave@davec.name>
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.
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.
27 // supportedFlags is a list of all the character flags supported by fmt package.
28 const supportedFlags = "0-+# "
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 {
38 pointers map[uintptr]int
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)
50 for _, flag := range supportedFlags {
51 if f.fs.Flag(int(flag)) {
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)
68 for _, flag := range supportedFlags {
69 if f.fs.Flag(int(flag)) {
74 if width, ok := f.fs.Width(); ok {
75 buf.WriteString(strconv.Itoa(width))
78 if precision, ok := f.fs.Precision(); ok {
79 buf.Write(precisionBytes)
80 buf.WriteString(strconv.Itoa(precision))
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
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)
113 // Remove pointers at or below the current depth from map used to detect
115 for k, depth := range f.pointers {
116 if depth >= f.depth {
117 delete(f.pointers, k)
121 // Keep list of all dereferenced pointers to possibly show later.
122 pointerChain := make([]uintptr, 0)
124 // Figure out how many levels of indirection there are by derferencing
125 // pointers and unpacking interfaces down the chain while detecting circular
131 for ve.Kind() == reflect.Ptr {
138 pointerChain = append(pointerChain, addr)
139 if pd, ok := f.pointers[addr]; ok && pd < f.depth {
144 f.pointers[addr] = f.depth
147 if ve.Kind() == reflect.Interface {
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)
163 if nilFound || cycleFound {
164 indirects += strings.Count(ve.Type().String(), "*")
166 f.fs.Write(openAngleBytes)
167 f.fs.Write([]byte(strings.Repeat("*", indirects)))
168 f.fs.Write(closeAngleBytes)
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 {
176 f.fs.Write(pointerChainBytes)
178 printHexPtr(f.fs, addr)
180 f.fs.Write(closeParenBytes)
183 // Display dereferenced value.
186 f.fs.Write(nilAngleBytes)
189 f.fs.Write(circularShortBytes)
192 f.ignoreNextType = true
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.
204 if kind == reflect.Invalid {
205 f.fs.Write(invalidAngleBytes)
209 // Handle pointers specially.
210 if kind == reflect.Ptr {
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)
221 f.ignoreNextType = false
223 // Call Stringer/error interfaces if they exist and the handle methods
225 if !f.cs.DisableMethods {
226 if (kind != reflect.Invalid) && (kind != reflect.Interface) {
227 if handled := handleMethods(f.cs, f.fs, v); handled {
234 case reflect.Invalid:
235 // Do nothing. We should never get here since invalid has already
236 // been handled above.
239 printBool(f.fs, v.Bool())
241 case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
242 printInt(f.fs, v.Int(), 10)
244 case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
245 printUint(f.fs, v.Uint(), 10)
247 case reflect.Float32:
248 printFloat(f.fs, v.Float(), 32)
250 case reflect.Float64:
251 printFloat(f.fs, v.Float(), 64)
253 case reflect.Complex64:
254 printComplex(f.fs, v.Complex(), 32)
256 case reflect.Complex128:
257 printComplex(f.fs, v.Complex(), 64)
261 f.fs.Write(nilAngleBytes)
267 f.fs.Write(openBracketBytes)
269 if (f.cs.MaxDepth != 0) && (f.depth > f.cs.MaxDepth) {
270 f.fs.Write(maxShortBytes)
272 numEntries := v.Len()
273 for i := 0; i < numEntries; i++ {
275 f.fs.Write(spaceBytes)
277 f.ignoreNextType = true
278 f.format(f.unpackValue(v.Index(i)))
282 f.fs.Write(closeBracketBytes)
285 f.fs.Write([]byte(v.String()))
287 case reflect.Interface:
288 // The only time we should get here is for nil interfaces due to
289 // unpackValue calls.
291 f.fs.Write(nilAngleBytes)
295 // Do nothing. We should never get here since pointers have already
296 // been handled above.
299 // nil maps should be indicated as different than empty maps
301 f.fs.Write(nilAngleBytes)
305 f.fs.Write(openMapBytes)
307 if (f.cs.MaxDepth != 0) && (f.depth > f.cs.MaxDepth) {
308 f.fs.Write(maxShortBytes)
312 sortValues(keys, f.cs)
314 for i, key := range keys {
316 f.fs.Write(spaceBytes)
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)))
326 f.fs.Write(closeMapBytes)
329 numFields := v.NumField()
330 f.fs.Write(openBraceBytes)
332 if (f.cs.MaxDepth != 0) && (f.depth > f.cs.MaxDepth) {
333 f.fs.Write(maxShortBytes)
336 for i := 0; i < numFields; i++ {
338 f.fs.Write(spaceBytes)
341 if f.fs.Flag('+') || f.fs.Flag('#') {
342 f.fs.Write([]byte(vtf.Name))
343 f.fs.Write(colonBytes)
345 f.format(f.unpackValue(v.Field(i)))
349 f.fs.Write(closeBraceBytes)
351 case reflect.Uintptr:
352 printHexPtr(f.fs, uintptr(v.Uint()))
354 case reflect.UnsafePointer, reflect.Chan, reflect.Func:
355 printHexPtr(f.fs, v.Pointer())
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.
360 format := f.buildDefaultFormat()
361 if v.CanInterface() {
362 fmt.Fprintf(f.fs, format, v.Interface())
364 fmt.Fprintf(f.fs, format, v.String())
369 // Format satisfies the fmt.Formatter interface. See NewFormatter for usage
371 func (f *formatState) Format(fs fmt.State, verb rune) {
374 // Use standard formatting for verbs that are not v.
376 format := f.constructOrigFormat(verb)
377 fmt.Fprintf(fs, format, f.value)
383 fs.Write(interfaceBytes)
385 fs.Write(nilAngleBytes)
389 f.format(reflect.ValueOf(f.value))
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)
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.
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).
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.
417 func NewFormatter(v interface{}) fmt.Formatter {
418 return newFormatter(&Config, v)