Documentation
¶
Overview ¶
Package dump can render a string representation of any value.
Package dump.
nolint: forbidigo
Index ¶
- Constants
- Variables
- func ArrayDumper(dmp Dump, lvl int, val reflect.Value) string
- func ChanDumper(dmp Dump, lvl int, val reflect.Value) string
- func ComplexDumper(dmp Dump, lvl int, val reflect.Value) string
- func DurDumperSeconds(dmp Dump, lvl int, val reflect.Value) string
- func DurDumperString(dmp Dump, lvl int, val reflect.Value) string
- func FuncDumper(dmp Dump, lvl int, val reflect.Value) string
- func HexPtrDumper(dmp Dump, lvl int, val reflect.Value) string
- func MapDumper(dmp Dump, lvl int, val reflect.Value) string
- func RegisterTypeDumper(typ any, dmp Dumper)
- func SimpleDumper(dmp Dump, lvl int, val reflect.Value) string
- func SliceDumper(dmp Dump, lvl int, val reflect.Value) string
- func StructDumper(dmp Dump, lvl int, val reflect.Value) string
- func TimeDumperDate(dmp Dump, lvl int, val reflect.Value) string
- func TimeDumperUnix(dmp Dump, lvl int, val reflect.Value) string
- func WithCompact(dmp *Dump)
- func WithFlat(dmp *Dump)
- func WithNoPrivate(dmp *Dump)
- func WithPtrAddr(dmp *Dump)
- func ZoneDumper(dmp Dump, lvl int, val reflect.Value) string
- type Dump
- type Dumper
- type Option
- type Printer
- func (prn Printer) Comma(last bool) Printer
- func (prn Printer) NL() Printer
- func (prn Printer) NLI(cnt int) Printer
- func (prn Printer) Sep(last bool) Printer
- func (prn Printer) Space() Printer
- func (prn Printer) String() string
- func (prn Printer) Tab(n int) Printer
- func (prn Printer) Write(s string) Printer
Examples ¶
Constants ¶
const ( ValNil = "nil" // The [reflect.Value] is nil. ValAddr = "<addr>" // The [reflect.Value] is an address. ValFunc = "<func>" // The [reflect.Value] is a function. ValChan = "<chan>" // The [reflect.Value] is a channel. ValInvalid = "<invalid>" // The [reflect.Value] is invalid. ValMaxNesting = "<...>" // The maximum nesting reached. ValEmpty = "<empty>" // Empty value. ValErrUsage = "<dump-usage-error>" // The [reflect.Value] is unexpected in the given context. )
Strings used by dump package to indicate special values.
const ( // DefaultTimeFormat is default format for parsing time strings. DefaultTimeFormat = time.RFC3339Nano // DefaultDepth is the default depth when dumping values recursively. DefaultDepth = 6 // DefaultIndent is default additional indent when dumping values. DefaultIndent = 0 // DefaultTabWith is the default tab width in spaces. DefaultTabWith = 2 )
Package wide default configuration.
const ( DurAsString = "" // Same format as [time.Duration.String]. DurAsSeconds = "<seconds>" // Duration as seconds float. )
Formats used by GetDurDumper.
const ( TimeAsRFC3339 = "" // Formats time as [time.RFC3339Nano]. TimeAsUnix = "<unix>" // Formats time as Unix timestamp (seconds). TimeAsGoString = "<go-str>" // Formats time the same way as [time.GoString]. )
Formats used by GetTimeDumper.
Variables ¶
var ( // TimeFormat is configurable format for dumping [time.Time] values. TimeFormat = DefaultTimeFormat // Depth is configurable depth when dumping values recursively. Depth = DefaultDepth // Indent is a configurable additional indent when dumping values. Indent = DefaultIndent // TabWidth is a configurable tab width in spaces. TabWidth = DefaultTabWith )
Package-wide configuration.
Functions ¶
func ArrayDumper ¶ added in v0.12.0
ArrayDumper is a generic dumper for arrays. It expects val to represent one of the kinds:
Returns [valErrUsage] ("<dump-usage-error>") string if the kind cannot be matched. It returns string representation in the format defined by Dump configuration.
func ChanDumper ¶ added in v0.12.0
ChanDumper is a generic dumper for channels. It expects val to represent one of the kinds:
Returns [valErrUsage] ("<dump-usage-error>") string if the kind cannot be matched. It returns string representation in the format defined by Dump configuration.
func ComplexDumper ¶ added in v0.12.0
ComplexDumper is a generic dumper for complex values. It expects val to represent one of the kinds:
Returns [valErrUsage] ("<dump-usage-error>") string if kind cannot be matched. It returns string representation in the format defined by Dump configuration.
func DurDumperSeconds ¶
DurDumperSeconds requires val to be dereferenced representation of time.Duration. Returns [valErrUsage] ("<dump-usage-error>") string if the type cannot be matched. It returns string representation in the format // defined by Dump configuration.
func DurDumperString ¶
DurDumperString requires val to be a value representing time.Duration. Returns [valErrUsage] ("<dump-usage-error>") string if the type cannot be matched. It returns string representation in the format defined by Dump configuration.
func FuncDumper ¶ added in v0.12.0
FuncDumper is a generic dumper for functions. It expects val to represent one of the kinds:
Returns [valErrUsage] ("<dump-usage-error>") string if kind cannot be matched. It returns string representation in the format defined by Dump configuration.
func HexPtrDumper ¶ added in v0.12.0
HexPtrDumper is a generic hex dumper for pointers. It expects val to represent one of the kinds:
Returns [valErrUsage] ("<dump-usage-error>") string if kind cannot be matched. It returns string representation in the format defined by Dump configuration.
func MapDumper ¶ added in v0.12.0
MapDumper is a generic dumper for maps. It expects val to represent the reflect.Map kind. Returns [valErrUsage] ("<dump-usage-error>") string if the kind cannot be matched. It returns string representation in the format defined by Dump configuration.
nolint: cyclop
func RegisterTypeDumper ¶ added in v0.15.0
RegisterTypeDumper globally registers a custom dumper for a given type. It panics if a dumper for the same type is already registered.
func SimpleDumper ¶ added in v0.12.0
SimpleDumper is a generic dumper for simple types. It expects val to represent one of the kinds:
- reflect.Bool
- reflect.Int
- reflect.Int8
- reflect.Int16
- reflect.Int32
- reflect.Int64
- reflect.Uint
- reflect.Uint8
- reflect.Uint16
- reflect.Uint32
- reflect.Uint64
- reflect.Float32
- reflect.Float64
- reflect.String
It returns string representation in the format defined by Dump configuration.
nolint: cyclop
func SliceDumper ¶ added in v0.12.0
SliceDumper is a generic dumper for slices. It expects val to represent the reflect.Slice kind. Returns [valErrUsage] ("<dump-usage-error>") string if the kind cannot be matched. It returns string representation in the format defined by Dump configuration.
func StructDumper ¶ added in v0.12.0
StructDumper is a generic dumper for maps. It expects val to represent the reflect.Struct kind. Returns [valErrUsage] ("<dump-usage-error>") string if the kind cannot be matched. It returns string representation in the format defined by Dump configuration.
func TimeDumperDate ¶
TimeDumperDate requires val to be a value representing time.Time and returns its representation using time.Time.GoString method. Returns [valErrUsage] ("<dump-usage-error>") string if the type cannot be matched.
func TimeDumperUnix ¶
TimeDumperUnix requires val to be a value representing time.Time and returns its string representation as a Unix timestamp. Returns [valErrUsage] ("<dump-usage-error>") string if the type cannot be matched.
func WithCompact ¶
func WithCompact(dmp *Dump)
WithCompact is an option for New which makes Dump display values without unnecessary whitespaces.
func WithFlat ¶
func WithFlat(dmp *Dump)
WithFlat is an option for New which makes Dump display values in one line.
func WithNoPrivate ¶ added in v0.13.0
func WithNoPrivate(dmp *Dump)
WithNoPrivate is an option for New which makes Dump skip displaying values for not exported fields.
func WithPtrAddr ¶
func WithPtrAddr(dmp *Dump)
WithPtrAddr is an option for New which makes Dump display pointer addresses.
func ZoneDumper ¶ added in v0.12.0
ZoneDumper requires val to be dereferenced representation of time.Location. Returns [valErrUsage] ("<dump-usage-error>") string if the kind cannot be matched. It returns string representation in the format defined by Dump configuration.
Types ¶
type Dump ¶
type Dump struct {
// Display values on one line.
Flat bool
// Display strings shorter that given value as with Flat.
FlatStrings int
// Do not use any indents or whitespace separators.
Compact bool
// Controls how [time.Time] is formated.
//
// Aside from Go time formating layouts, the following custom formats are
// available:
//
// - [TimeAsUnix] - Unix timestamp,
//
// By default (empty value) [time.RFC3339Nano] is used.
TimeFormat string
// Controls how [time.Duration] is formated.
//
// Supports formats:
//
// - [DurAsString]
// - [DurAsSeconds]
DurationFormat string
// Show pointer addresses.
PtrAddr bool
// Print types.
PrintType bool
// Controls if the not exported field values should be printed.
PrintPrivate bool
// Use "any" instead of "interface{}".
UseAny bool
// Custom type dumpers.
//
// By default, dumpers for types:
// - [time.Time]
// - [time.Duration]
// - [time.Location]
//
// are automatically registered.
Dumpers map[reflect.Type]Dumper
// Controls maximum nesting when dumping recursive types.
// The depth is also used to properly indent values being dumped.
MaxDepth int
// How much additional indentation to apply to values being dumped.
Indent int
// Default tab with in spaces.
TabWidth int
// contains filtered or unexported fields
}
Dump implements logic for dumping values and types.
func (Dump) Any ¶
Any dumps any value to its string representation.
Example ¶
package main
import (
"fmt"
"time"
"github.com/ctx42/testing/internal/types"
"github.com/ctx42/testing/pkg/dump"
)
func main() {
val := types.TA{
Dur: 3,
Int: 42,
Loc: types.WAW,
Str: "abc",
Tim: time.Date(2000, 1, 2, 3, 4, 5, 0, time.UTC),
TAp: nil,
}
have := dump.New().Any(val)
fmt.Println(have)
}
Output: { Int: 42, Str: "abc", Tim: "2000-01-02T03:04:05Z", Dur: "3ns", Loc: "Europe/Warsaw", TAp: nil, private: 0, }
Example (CustomDumper) ¶
package main
import (
"fmt"
"reflect"
"github.com/ctx42/testing/pkg/dump"
)
func main() {
var i int
dumper := func(dmp dump.Dump, lvl int, val reflect.Value) string {
switch val.Kind() {
case reflect.Int:
return fmt.Sprintf("%X", val.Int())
default:
panic("unexpected kind")
}
}
opts := []dump.Option{
dump.WithFlat,
dump.WithCompact,
dump.WithDumper(i, dumper),
}
have := dump.New(opts...).Any(42)
fmt.Println(have)
}
Output: 2A
Example (CustomTimeFormat) ¶
package main
import (
"fmt"
"time"
"github.com/ctx42/testing/pkg/dump"
)
func main() {
val := map[time.Time]int{time.Date(2000, 1, 2, 3, 4, 5, 0, time.UTC): 42}
have := dump.New(dump.WithFlat, dump.WithTimeFormat(time.Kitchen)).Any(val)
fmt.Println(have)
}
Output: map[time.Time]int{"3:04AM": 42}
Example (FlatCompact) ¶
package main
import (
"fmt"
"github.com/ctx42/testing/internal/types"
"github.com/ctx42/testing/pkg/dump"
)
func main() {
val := map[string]any{
"int": 42,
"loc": types.WAW,
"nil": nil,
}
have := dump.New(dump.WithFlat).Any(val)
fmt.Println(have)
}
Output: map[string]any{"int": 42, "loc": "Europe/Warsaw", "nil": nil}
Example (Recursive) ¶
package main
import (
"fmt"
"github.com/ctx42/testing/pkg/dump"
)
func main() {
type Node struct {
Value int
Children []*Node
}
val := &Node{
Value: 1,
Children: []*Node{
{
Value: 2,
Children: nil,
},
{
Value: 3,
Children: []*Node{
{
Value: 4,
Children: nil,
},
},
},
},
}
have := dump.New().Any(val)
fmt.Println(have)
}
Output: { Value: 1, Children: []*dump_test.Node{ { Value: 2, Children: nil, }, { Value: 3, Children: []*dump_test.Node{ { Value: 4, Children: nil, }, }, }, }, }
func (Dump) Diff ¶ added in v0.9.0
Diff compares two values and returns their formatted representations and diff. The first result is the formatted "want" value, the second is the formatted "have" value, and the third is the unified diff if they differ. If the values are identical, the diff result will be an empty string.
type Dumper ¶
Dumper represents function signature for value dumpers.
func GetDurDumper ¶
GetDurDumper returns time.Duration dumper based on format.
func GetTimeDumper ¶
GetTimeDumper returns time.Time dumper based on format.
func TimeDumperFmt ¶
TimeDumperFmt returns Dumper for time.Time using the given format. The returned function requires val to be a value representing time.Time and returns its string representation in the format defined by Dump configuration. Returns [valErrUsage] ("<dump-usage-error>") string if the type cannot be matched.
type Option ¶
type Option func(*Dump)
Option represents a [NewConfig] option.
func WithDumper ¶
WithDumper adds custom Dumper to the config.
func WithFlatStrings ¶ added in v0.6.0
WithFlatStrings configures the maximum length of strings to be represented as flat in the output. Strings longer than the specified length may be formatted differently, depending on the configuration. This option is similar to WithFlat but applies specifically to strings based on their length. Set to zero to turn this feature off.
func WithIndent ¶
WithIndent is an option for New which sets additional indentation to apply to dumped values.
func WithMaxDepth ¶
WithMaxDepth is an option for New which controls maximum nesting when bumping recursive types.
func WithTabWidth ¶
WithTabWidth is an option for New setting tab width in spaces.
type Printer ¶
type Printer struct {
// contains filtered or unexported fields
}
Printer represents code printer.