forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/davecgh/go-spew/spew/config.go

550 lines
13 KiB
Go
Raw Normal View History

2024-04-26 19:30:35 +00:00
/*
2024-04-26 19:30:35 +00:00
* Copyright (c) 2013-2016 Dave Collins <dave@davec.name>
2024-04-26 19:30:35 +00:00
*
2024-04-26 19:30:35 +00:00
* Permission to use, copy, modify, and distribute this software for any
2024-04-26 19:30:35 +00:00
* purpose with or without fee is hereby granted, provided that the above
2024-04-26 19:30:35 +00:00
* copyright notice and this permission notice appear in all copies.
2024-04-26 19:30:35 +00:00
*
2024-04-26 19:30:35 +00:00
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
2024-04-26 19:30:35 +00:00
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
2024-04-26 19:30:35 +00:00
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
2024-04-26 19:30:35 +00:00
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2024-04-26 19:30:35 +00:00
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2024-04-26 19:30:35 +00:00
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
2024-04-26 19:30:35 +00:00
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2024-04-26 19:30:35 +00:00
*/
package spew
import (
"bytes"
"fmt"
"io"
"os"
)
// ConfigState houses the configuration options used by spew to format and
2024-04-26 19:30:35 +00:00
// display values. There is a global instance, Config, that is used to control
2024-04-26 19:30:35 +00:00
// all top-level Formatter and Dump functionality. Each ConfigState instance
2024-04-26 19:30:35 +00:00
// provides methods equivalent to the top-level functions.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// The zero value for ConfigState provides no indentation. You would typically
2024-04-26 19:30:35 +00:00
// want to set it to a space or a tab.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// Alternatively, you can use NewDefaultConfig to get a ConfigState instance
2024-04-26 19:30:35 +00:00
// with default settings. See the documentation of NewDefaultConfig for default
2024-04-26 19:30:35 +00:00
// values.
2024-04-26 19:30:35 +00:00
type ConfigState struct {
2024-04-26 19:30:35 +00:00
// Indent specifies the string to use for each indentation level. The
2024-04-26 19:30:35 +00:00
// global config instance that all top-level functions use set this to a
2024-04-26 19:30:35 +00:00
// single space by default. If you would like more indentation, you might
2024-04-26 19:30:35 +00:00
// set this to a tab with "\t" or perhaps two spaces with " ".
2024-04-26 19:30:35 +00:00
Indent string
// MaxDepth controls the maximum number of levels to descend into nested
2024-04-26 19:30:35 +00:00
// data structures. The default, 0, means there is no limit.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// NOTE: Circular data structures are properly detected, so it is not
2024-04-26 19:30:35 +00:00
// necessary to set this value unless you specifically want to limit deeply
2024-04-26 19:30:35 +00:00
// nested data structures.
2024-04-26 19:30:35 +00:00
MaxDepth int
// DisableMethods specifies whether or not error and Stringer interfaces are
2024-04-26 19:30:35 +00:00
// invoked for types that implement them.
2024-04-26 19:30:35 +00:00
DisableMethods bool
// DisablePointerMethods specifies whether or not to check for and invoke
2024-04-26 19:30:35 +00:00
// error and Stringer interfaces on types which only accept a pointer
2024-04-26 19:30:35 +00:00
// receiver when the current type is not a pointer.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// NOTE: This might be an unsafe action since calling one of these methods
2024-04-26 19:30:35 +00:00
// with a pointer receiver could technically mutate the value, however,
2024-04-26 19:30:35 +00:00
// in practice, types which choose to satisify an error or Stringer
2024-04-26 19:30:35 +00:00
// interface with a pointer receiver should not be mutating their state
2024-04-26 19:30:35 +00:00
// inside these interface methods. As a result, this option relies on
2024-04-26 19:30:35 +00:00
// access to the unsafe package, so it will not have any effect when
2024-04-26 19:30:35 +00:00
// running in environments without access to the unsafe package such as
2024-04-26 19:30:35 +00:00
// Google App Engine or with the "safe" build tag specified.
2024-04-26 19:30:35 +00:00
DisablePointerMethods bool
// DisablePointerAddresses specifies whether to disable the printing of
2024-04-26 19:30:35 +00:00
// pointer addresses. This is useful when diffing data structures in tests.
2024-04-26 19:30:35 +00:00
DisablePointerAddresses bool
// DisableCapacities specifies whether to disable the printing of capacities
2024-04-26 19:30:35 +00:00
// for arrays, slices, maps and channels. This is useful when diffing
2024-04-26 19:30:35 +00:00
// data structures in tests.
2024-04-26 19:30:35 +00:00
DisableCapacities bool
// ContinueOnMethod specifies whether or not recursion should continue once
2024-04-26 19:30:35 +00:00
// a custom error or Stringer interface is invoked. The default, false,
2024-04-26 19:30:35 +00:00
// means it will print the results of invoking the custom error or Stringer
2024-04-26 19:30:35 +00:00
// interface and return immediately instead of continuing to recurse into
2024-04-26 19:30:35 +00:00
// the internals of the data type.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// NOTE: This flag does not have any effect if method invocation is disabled
2024-04-26 19:30:35 +00:00
// via the DisableMethods or DisablePointerMethods options.
2024-04-26 19:30:35 +00:00
ContinueOnMethod bool
// SortKeys specifies map keys should be sorted before being printed. Use
2024-04-26 19:30:35 +00:00
// this to have a more deterministic, diffable output. Note that only
2024-04-26 19:30:35 +00:00
// native types (bool, int, uint, floats, uintptr and string) and types
2024-04-26 19:30:35 +00:00
// that support the error or Stringer interfaces (if methods are
2024-04-26 19:30:35 +00:00
// enabled) are supported, with other types sorted according to the
2024-04-26 19:30:35 +00:00
// reflect.Value.String() output which guarantees display stability.
2024-04-26 19:30:35 +00:00
SortKeys bool
// SpewKeys specifies that, as a last resort attempt, map keys should
2024-04-26 19:30:35 +00:00
// be spewed to strings and sorted by those strings. This is only
2024-04-26 19:30:35 +00:00
// considered if SortKeys is true.
2024-04-26 19:30:35 +00:00
SpewKeys bool
}
// Config is the active configuration of the top-level functions.
2024-04-26 19:30:35 +00:00
// The configuration can be changed by modifying the contents of spew.Config.
2024-04-26 19:30:35 +00:00
var Config = ConfigState{Indent: " "}
// Errorf is a wrapper for fmt.Errorf that treats each argument as if it were
2024-04-26 19:30:35 +00:00
// passed with a Formatter interface returned by c.NewFormatter. It returns
2024-04-26 19:30:35 +00:00
// the formatted string as a value that satisfies error. See NewFormatter
2024-04-26 19:30:35 +00:00
// for formatting details.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// This function is shorthand for the following syntax:
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// fmt.Errorf(format, c.NewFormatter(a), c.NewFormatter(b))
2024-04-26 19:30:35 +00:00
func (c *ConfigState) Errorf(format string, a ...interface{}) (err error) {
2024-04-26 19:30:35 +00:00
return fmt.Errorf(format, c.convertArgs(a)...)
2024-04-26 19:30:35 +00:00
}
// Fprint is a wrapper for fmt.Fprint that treats each argument as if it were
2024-04-26 19:30:35 +00:00
// passed with a Formatter interface returned by c.NewFormatter. It returns
2024-04-26 19:30:35 +00:00
// the number of bytes written and any write error encountered. See
2024-04-26 19:30:35 +00:00
// NewFormatter for formatting details.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// This function is shorthand for the following syntax:
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// fmt.Fprint(w, c.NewFormatter(a), c.NewFormatter(b))
2024-04-26 19:30:35 +00:00
func (c *ConfigState) Fprint(w io.Writer, a ...interface{}) (n int, err error) {
2024-04-26 19:30:35 +00:00
return fmt.Fprint(w, c.convertArgs(a)...)
2024-04-26 19:30:35 +00:00
}
// Fprintf is a wrapper for fmt.Fprintf that treats each argument as if it were
2024-04-26 19:30:35 +00:00
// passed with a Formatter interface returned by c.NewFormatter. It returns
2024-04-26 19:30:35 +00:00
// the number of bytes written and any write error encountered. See
2024-04-26 19:30:35 +00:00
// NewFormatter for formatting details.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// This function is shorthand for the following syntax:
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// fmt.Fprintf(w, format, c.NewFormatter(a), c.NewFormatter(b))
2024-04-26 19:30:35 +00:00
func (c *ConfigState) Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
2024-04-26 19:30:35 +00:00
return fmt.Fprintf(w, format, c.convertArgs(a)...)
2024-04-26 19:30:35 +00:00
}
// Fprintln is a wrapper for fmt.Fprintln that treats each argument as if it
2024-04-26 19:30:35 +00:00
// passed with a Formatter interface returned by c.NewFormatter. See
2024-04-26 19:30:35 +00:00
// NewFormatter for formatting details.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// This function is shorthand for the following syntax:
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// fmt.Fprintln(w, c.NewFormatter(a), c.NewFormatter(b))
2024-04-26 19:30:35 +00:00
func (c *ConfigState) Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
2024-04-26 19:30:35 +00:00
return fmt.Fprintln(w, c.convertArgs(a)...)
2024-04-26 19:30:35 +00:00
}
// Print is a wrapper for fmt.Print that treats each argument as if it were
2024-04-26 19:30:35 +00:00
// passed with a Formatter interface returned by c.NewFormatter. It returns
2024-04-26 19:30:35 +00:00
// the number of bytes written and any write error encountered. See
2024-04-26 19:30:35 +00:00
// NewFormatter for formatting details.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// This function is shorthand for the following syntax:
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// fmt.Print(c.NewFormatter(a), c.NewFormatter(b))
2024-04-26 19:30:35 +00:00
func (c *ConfigState) Print(a ...interface{}) (n int, err error) {
2024-04-26 19:30:35 +00:00
return fmt.Print(c.convertArgs(a)...)
2024-04-26 19:30:35 +00:00
}
// Printf is a wrapper for fmt.Printf that treats each argument as if it were
2024-04-26 19:30:35 +00:00
// passed with a Formatter interface returned by c.NewFormatter. It returns
2024-04-26 19:30:35 +00:00
// the number of bytes written and any write error encountered. See
2024-04-26 19:30:35 +00:00
// NewFormatter for formatting details.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// This function is shorthand for the following syntax:
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// fmt.Printf(format, c.NewFormatter(a), c.NewFormatter(b))
2024-04-26 19:30:35 +00:00
func (c *ConfigState) Printf(format string, a ...interface{}) (n int, err error) {
2024-04-26 19:30:35 +00:00
return fmt.Printf(format, c.convertArgs(a)...)
2024-04-26 19:30:35 +00:00
}
// Println is a wrapper for fmt.Println that treats each argument as if it were
2024-04-26 19:30:35 +00:00
// passed with a Formatter interface returned by c.NewFormatter. It returns
2024-04-26 19:30:35 +00:00
// the number of bytes written and any write error encountered. See
2024-04-26 19:30:35 +00:00
// NewFormatter for formatting details.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// This function is shorthand for the following syntax:
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// fmt.Println(c.NewFormatter(a), c.NewFormatter(b))
2024-04-26 19:30:35 +00:00
func (c *ConfigState) Println(a ...interface{}) (n int, err error) {
2024-04-26 19:30:35 +00:00
return fmt.Println(c.convertArgs(a)...)
2024-04-26 19:30:35 +00:00
}
// Sprint is a wrapper for fmt.Sprint that treats each argument as if it were
2024-04-26 19:30:35 +00:00
// passed with a Formatter interface returned by c.NewFormatter. It returns
2024-04-26 19:30:35 +00:00
// the resulting string. See NewFormatter for formatting details.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// This function is shorthand for the following syntax:
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// fmt.Sprint(c.NewFormatter(a), c.NewFormatter(b))
2024-04-26 19:30:35 +00:00
func (c *ConfigState) Sprint(a ...interface{}) string {
2024-04-26 19:30:35 +00:00
return fmt.Sprint(c.convertArgs(a)...)
2024-04-26 19:30:35 +00:00
}
// Sprintf is a wrapper for fmt.Sprintf that treats each argument as if it were
2024-04-26 19:30:35 +00:00
// passed with a Formatter interface returned by c.NewFormatter. It returns
2024-04-26 19:30:35 +00:00
// the resulting string. See NewFormatter for formatting details.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// This function is shorthand for the following syntax:
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// fmt.Sprintf(format, c.NewFormatter(a), c.NewFormatter(b))
2024-04-26 19:30:35 +00:00
func (c *ConfigState) Sprintf(format string, a ...interface{}) string {
2024-04-26 19:30:35 +00:00
return fmt.Sprintf(format, c.convertArgs(a)...)
2024-04-26 19:30:35 +00:00
}
// Sprintln is a wrapper for fmt.Sprintln that treats each argument as if it
2024-04-26 19:30:35 +00:00
// were passed with a Formatter interface returned by c.NewFormatter. It
2024-04-26 19:30:35 +00:00
// returns the resulting string. See NewFormatter for formatting details.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// This function is shorthand for the following syntax:
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// fmt.Sprintln(c.NewFormatter(a), c.NewFormatter(b))
2024-04-26 19:30:35 +00:00
func (c *ConfigState) Sprintln(a ...interface{}) string {
2024-04-26 19:30:35 +00:00
return fmt.Sprintln(c.convertArgs(a)...)
2024-04-26 19:30:35 +00:00
}
/*
2024-04-26 19:30:35 +00:00
NewFormatter returns a custom formatter that satisfies the fmt.Formatter
2024-04-26 19:30:35 +00:00
interface. As a result, it integrates cleanly with standard fmt package
2024-04-26 19:30:35 +00:00
printing functions. The formatter is useful for inline printing of smaller data
2024-04-26 19:30:35 +00:00
types similar to the standard %v format specifier.
2024-04-26 19:30:35 +00:00
The custom formatter only responds to the %v (most compact), %+v (adds pointer
2024-04-26 19:30:35 +00:00
addresses), %#v (adds types), and %#+v (adds types and pointer addresses) verb
2024-04-26 19:30:35 +00:00
combinations. Any other verbs such as %x and %q will be sent to the the
2024-04-26 19:30:35 +00:00
standard fmt package for formatting. In addition, the custom formatter ignores
2024-04-26 19:30:35 +00:00
the width and precision arguments (however they will still work on the format
2024-04-26 19:30:35 +00:00
specifiers not handled by the custom formatter).
2024-04-26 19:30:35 +00:00
Typically this function shouldn't be called directly. It is much easier to make
2024-04-26 19:30:35 +00:00
use of the custom formatter by calling one of the convenience functions such as
2024-04-26 19:30:35 +00:00
c.Printf, c.Println, or c.Printf.
2024-04-26 19:30:35 +00:00
*/
2024-04-26 19:30:35 +00:00
func (c *ConfigState) NewFormatter(v interface{}) fmt.Formatter {
2024-04-26 19:30:35 +00:00
return newFormatter(c, v)
2024-04-26 19:30:35 +00:00
}
// Fdump formats and displays the passed arguments to io.Writer w. It formats
2024-04-26 19:30:35 +00:00
// exactly the same as Dump.
2024-04-26 19:30:35 +00:00
func (c *ConfigState) Fdump(w io.Writer, a ...interface{}) {
2024-04-26 19:30:35 +00:00
fdump(c, w, a...)
2024-04-26 19:30:35 +00:00
}
/*
2024-04-26 19:30:35 +00:00
Dump displays the passed parameters to standard out with newlines, customizable
2024-04-26 19:30:35 +00:00
indentation, and additional debug information such as complete types and all
2024-04-26 19:30:35 +00:00
pointer addresses used to indirect to the final value. It provides the
2024-04-26 19:30:35 +00:00
following features over the built-in printing facilities provided by the fmt
2024-04-26 19:30:35 +00:00
package:
2024-06-14 08:41:36 +00:00
- Pointers are dereferenced and followed
2024-06-14 08:41:36 +00:00
- Circular data structures are detected and handled properly
2024-06-14 08:41:36 +00:00
- Custom Stringer/error interfaces are optionally invoked, including
2024-06-14 08:41:36 +00:00
on unexported types
2024-06-14 08:41:36 +00:00
- Custom types which only implement the Stringer/error interfaces via
2024-06-14 08:41:36 +00:00
a pointer receiver are optionally invoked when passing non-pointer
2024-06-14 08:41:36 +00:00
variables
2024-06-14 08:41:36 +00:00
- Byte arrays and slices are dumped like the hexdump -C command which
2024-06-14 08:41:36 +00:00
includes offsets, byte values in hex, and ASCII output
2024-04-26 19:30:35 +00:00
2024-04-26 19:30:35 +00:00
The configuration options are controlled by modifying the public members
2024-04-26 19:30:35 +00:00
of c. See ConfigState for options documentation.
2024-04-26 19:30:35 +00:00
See Fdump if you would prefer dumping to an arbitrary io.Writer or Sdump to
2024-04-26 19:30:35 +00:00
get the formatted result as a string.
2024-04-26 19:30:35 +00:00
*/
2024-04-26 19:30:35 +00:00
func (c *ConfigState) Dump(a ...interface{}) {
2024-04-26 19:30:35 +00:00
fdump(c, os.Stdout, a...)
2024-04-26 19:30:35 +00:00
}
// Sdump returns a string with the passed arguments formatted exactly the same
2024-04-26 19:30:35 +00:00
// as Dump.
2024-04-26 19:30:35 +00:00
func (c *ConfigState) Sdump(a ...interface{}) string {
2024-04-26 19:30:35 +00:00
var buf bytes.Buffer
2024-04-26 19:30:35 +00:00
fdump(c, &buf, a...)
2024-04-26 19:30:35 +00:00
return buf.String()
2024-04-26 19:30:35 +00:00
}
// convertArgs accepts a slice of arguments and returns a slice of the same
2024-04-26 19:30:35 +00:00
// length with each argument converted to a spew Formatter interface using
2024-04-26 19:30:35 +00:00
// the ConfigState associated with s.
2024-04-26 19:30:35 +00:00
func (c *ConfigState) convertArgs(args []interface{}) (formatters []interface{}) {
2024-04-26 19:30:35 +00:00
formatters = make([]interface{}, len(args))
2024-04-26 19:30:35 +00:00
for index, arg := range args {
2024-04-26 19:30:35 +00:00
formatters[index] = newFormatter(c, arg)
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
return formatters
2024-04-26 19:30:35 +00:00
}
// NewDefaultConfig returns a ConfigState with the following default settings.
2024-04-26 19:30:35 +00:00
//
2024-06-14 08:41:36 +00:00
// Indent: " "
2024-06-14 08:41:36 +00:00
// MaxDepth: 0
2024-06-14 08:41:36 +00:00
// DisableMethods: false
2024-06-14 08:41:36 +00:00
// DisablePointerMethods: false
2024-06-14 08:41:36 +00:00
// ContinueOnMethod: false
2024-06-14 08:41:36 +00:00
// SortKeys: false
2024-04-26 19:30:35 +00:00
func NewDefaultConfig() *ConfigState {
2024-04-26 19:30:35 +00:00
return &ConfigState{Indent: " "}
2024-04-26 19:30:35 +00:00
}