Documentation
¶
Overview ¶
Package dapper is a deterministic pretty-printer with minimal output.
Index ¶
- Constants
- Variables
- func DurationFilter(w io.Writer, v Value, _ Config, p FilterPrinter) error
- func Format(v interface{}) string
- func Print(values ...interface{})
- func ProtobufFilter(w io.Writer, v Value, c Config, p FilterPrinter) error
- func ReflectTypeFilter(w io.Writer, v Value, _ Config, p FilterPrinter) error
- func StringerFilter(w io.Writer, v Value, c Config, p FilterPrinter) error
- func SyncFilter(w io.Writer, v Value, c Config, p FilterPrinter) error
- func TimeFilter(w io.Writer, v Value, _ Config, p FilterPrinter) error
- func Write(w io.Writer, v interface{}) (int, error)
- type Config
- type Filter
- type FilterPrinter
- type Printer
- type Stringer
- type Value
Examples ¶
Constants ¶
const ( // DefaultZeroValueMarker is the default string to display when rendering a // zero-value struct. DefaultZeroValueMarker = "<zero>" // DefaultRecursionMarker is the default string to display when recursion // is detected within a Go value. DefaultRecursionMarker = "<recursion>" )
Variables ¶
var DefaultIndent = []byte(" ")
DefaultIndent is the default indent string used to indent nested values.
var DefaultPrinter = Printer{ Config: Config{ Filters: []Filter{ DurationFilter, ProtobufFilter, ReflectTypeFilter, StringerFilter, SyncFilter, TimeFilter, }, }, }
DefaultPrinter is the printer used by Write(), Format() and Print().
Functions ¶
func DurationFilter ¶ added in v0.3.4
DurationFilter is a filter that formats time.Duration values.
func Format ¶
func Format(v interface{}) string
Format returns a pretty-printed representation of v.
Example ¶
s := Format(123) fmt.Println(s)
Output: int(123)
func Print ¶ added in v0.2.0
func Print(values ...interface{})
Print writes a pretty-printed representation of v to os.Stdout.
Example ¶
Print(123, 456.0)
Output: int(123) float64(456)
func ProtobufFilter ¶ added in v0.4.3
ProtobufFilter is a filter for proto.Message types.
It shows the field names as defined in the .proto file and hides implementation-specific internal state.
func ReflectTypeFilter ¶ added in v0.3.0
ReflectTypeFilter is a filter that formats reflect.Type values.
func StringerFilter ¶ added in v0.4.4
StringerFilter is a filter that formats implementations of dapper.Stringer.
func SyncFilter ¶ added in v0.3.4
SyncFilter is a filter that formats various types from the sync package.
func TimeFilter ¶ added in v0.3.4
TimeFilter is a filter that formats time.Time values.
Types ¶
type Config ¶ added in v0.4.0
type Config struct {
// Filters is the set of filters to apply when formatting values.
Filters []Filter
// Indent is the string used to indent nested values.
// If it is empty, DefaultIndent is used.
Indent []byte
// ZeroValueMarker is a string that is displayed instead of a structs field
// list when it is the zero-value. If it is empty, DefaultZeroValueMarker is
// used.
ZeroValueMarker string
// RecursionMarker is a string that is displayed instead of a value's
// representation when recursion has been detected.
// If it is empty, DefaultRecursionMarker is used.
RecursionMarker string
// OmitPackagePaths, when true, causes the printer to omit the
// fully-qualified package path from the rendered type names.
OmitPackagePaths bool
// OmitUnexportedFields omits unexported struct fields when set to true
OmitUnexportedFields bool
}
Config holds the configuration for a printer.
type Filter ¶ added in v0.3.0
Filter is a function that provides custom formatting logic for specific values.
It optionally writes a formatted representation of v to w. If the filter does not produce any output the default formatting logic is used.
c is the configuration used by the Printer that is invoking the filter.
p is used to render values and type names according to the printer configuration.
Particular attention should be paid to the v.IsUnexported field. If this flag is true, many operations on v.Value are unavailable.
type FilterPrinter ¶ added in v0.4.0
type FilterPrinter interface {
// Write writes a pretty-printed representation of v to w.
Write(w io.Writer, v Value)
// FormatTypeName returns the name of v's dynamic type, rendered as per the
// printer's configuration.
FormatTypeName(v Value) string
// Fallback writes the filtered value using the standard pretty-printer.
Fallback(w io.Writer, c Config)
}
FilterPrinter is an interface used by filters to render values and types.
type Printer ¶
type Printer struct {
// Config is the configuration for the printer.
Config Config
}
Printer generates human-readable representations of Go values.
The output format is intended to be as minimal as possible, without being ambiguous. To that end, type information is only included where it can not be reliably inferred from the structure of the value.
Example ¶
type TreeNode struct {
Name string
Value interface{}
Children []*TreeNode
}
type NodeValue struct{}
v := TreeNode{
Name: "root",
Children: []*TreeNode{
{
Name: "branch #1",
Value: 100,
},
{
Name: "branch #2",
Value: NodeValue{},
},
},
}
p := Printer{}
s := p.Format(v)
fmt.Println(s)
Output: github.com/dogmatiq/dapper_test.TreeNode{ Name: "root" Value: nil Children: { { Name: "branch #1" Value: int(100) Children: nil } { Name: "branch #2" Value: github.com/dogmatiq/dapper_test.NodeValue{} Children: nil } } }
type Stringer ¶ added in v0.4.4
type Stringer interface {
DapperString() string
}
Stringer is an interface for types that produce their own Dapper representation.
type Value ¶ added in v0.3.0
type Value struct {
// Value is the value to be formatted.
Value reflect.Value
// DynamicType is the value's type.
DynamicType reflect.Type
// StaticType is the type of the "variable" that the value is stored in, which
// may not be the same as its dynamic type.
//
// For example, when formatting the values within a slice of interface{}
// containing integers, such as []interface{}{1, 2, 3}, the DynamicType will be
// "int", but the static type will be "interface{}".
StaticType reflect.Type
// IsAmbiguousDynamicType is true if the value's dynamic type is not clear from
// the context of what has already been rendered.
IsAmbiguousDynamicType bool
// IsAmbiguousStaticType is true if the value's static type is not clear from
// the context of what has already been rendered.
IsAmbiguousStaticType bool
// IsUnexported is true if this value was obtained from an unexported struct
// field. If so, it is not possible to extract the underlying value.
IsUnexported bool
}
Value contains information about a Go value that is to be formatted.
func (*Value) IsAmbiguousType ¶ added in v0.3.0
IsAmbiguousType returns true if either the dynamic type or the static type is ambiguous.
func (*Value) IsAnonymousType ¶ added in v0.3.0
IsAnonymousType returns true if the value has an anonymous type.