Documentation
¶
Overview ¶
Package dump can render string representation of any value.
Package dump.
nolint: forbidigo
Index ¶
- Constants
- Variables
- func DurDumperSeconds(dmp Dump, lvl int, val reflect.Value) string
- func DurDumperString(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 WithPtrAddr(dmp *Dump)
- 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 ( // DefaultTimeFormat is default format for parsing time strings. DefaultTimeFormat = time.RFC3339Nano // DefaultDepth is default depth when dumping values recursively. DefaultDepth = 6 // DefaultIndent is default additional indent when dumping values. DefaultIndent = 0 // DefaultTabWith is 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 to 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 to 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 configurable additional indent when dumping values. Indent = DefaultIndent // TabWidth is configurable tab width in spaces. TabWidth = DefaultTabWith )
Package wide configuration.
Functions ¶
func DurDumperSeconds ¶
DurDumperSeconds requires val to be dereferenced representation of reflect.Duration and returns its string representation in format defined by Dump configuration.
func DurDumperString ¶
DurDumperString requires val to be dereferenced representation of reflect.Duration and returns its string representation in format defined by Dump configuration.
func TimeDumperDate ¶
TimeDumperDate requires val to be dereferenced representation of reflect.Struct which can be cast to time.Time and returns its representation using time.Time.GoString method.
func TimeDumperUnix ¶
TimeDumperUnix requires val to be dereferenced representation of reflect.Struct which can be cast to time.Time and returns its string representation as Unix timestamp.
func WithCompact ¶
func WithCompact(dmp *Dump)
WithCompact is option for New which makes Dump display values without unnecessary whitespaces.
func WithFlat ¶
func WithFlat(dmp *Dump)
WithFlat is option for New which makes Dump display values in one line.
func WithPtrAddr ¶
func WithPtrAddr(dmp *Dump)
WithPtrAddr is option for New which makes Dump display pointer addresses.
Types ¶
type Dump ¶
type Dump struct { // Display values on one line. Flat bool // Display strings shorter that given value as with Flat. FlatStings 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 // 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 }
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, }
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, }, }, }, }, }
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 given format. The returned function requires val to be dereferenced representation of reflect.Struct which can be cast to time.Time and returns its string representation in format defined by Dump configuration.
type Option ¶
type Option func(*Dump)
Option represents [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.
func WithIndent ¶
WithIndent is option for New which sets additional indentation to apply to dumped values.
func WithMaxDepth ¶
WithMaxDepth is option for New which controls maximum nesting when bumping recursive types.
func WithTabWidth ¶
WithTabWidth is option for New setting tab width in spaces.
type Printer ¶
type Printer struct {
// contains filtered or unexported fields
}
Printer represents code printer.