dumper

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 5, 2024 License: AGPL-3.0 Imports: 13 Imported by: 0

README

Go Dumper

When debugging Go code, being able to dump values with nice formatting and colors can go a long way:

package main

import (
    "time"

    . "github.com/symfony-cli/dumper"
)

func main() {
    now := time.Now().Local()
    Dump(now)
}

Output:

time.Time{{
  wall: 507838786, // uint64
  ext: 63649250679, // int64
  loc: &time.Location { // (0x0014c3a0)
    name: "",
    zone: nil, // []time.zone
    tx: nil, // []time.zoneTrans
    cacheStart: 0, // int64,
    cacheEnd: 0, // int64,
    cacheZone: nil, // &time.zone
  },
}

Custom Dumpers

Implement the Dumpable interface on your types to take control of how your type is dumped:

type Dumpable interface {
    Dump(State)
}

Use the available helpers on the State object to write your custom dump. Alternatively, you can write directly to the provided stream. In both cases, your type name (and eventually the pointer comment) will be automatically added.

Read he test suite for some examples.

Another alternative, useful for package you don't maintain is to register a function of type DumpFunc:

func init() {
    RegisterCustomDumper(http.Request{}, dumpHttpRequest)
}

func dumpHttpRequest(s State, v reflect.Value) {
    for _, f := range []string{"Status", "Proto"} {
        s.DumpStructField(f, v.FieldByName(f))
    }
    s.AddComment("Hello World!")
}

Documentation

Index

Constants

View Source
const ElementsPerLine = 30

Variables

View Source
var Dump = defaultDump

Dump points to the default dumper

Functions

func DumpStructWithPrivateFields

func DumpStructWithPrivateFields(s State, v reflect.Value)

func Fdump

func Fdump(out io.Writer, values ...interface{})

Fdump prints to the writer the value with indentation.

func FdumpColor

func FdumpColor(out io.Writer, values ...interface{})

FdumpColor prints to the writer the value with indentation and color.

func RegisterCustomDumper

func RegisterCustomDumper(v interface{}, f DumpFunc)

func Sdump

func Sdump(values ...interface{}) string

Sdump dumps the values into a string with indentation.

func UnregisterCustomDumper

func UnregisterCustomDumper(v interface{})

Types

type DumpFunc

type DumpFunc func(State, reflect.Value)

type Dumpable

type Dumpable interface {
	Dump(State)
}

Dumpable is the interface for implementing custom dumper for your types.

type State

type State interface {
	io.Writer
	WithTempBuffer(func(buf *bytes.Buffer)) string

	AddComment(string)
	ResetComments() []string

	ForceNewLines(bool) bool

	DepthUp()
	DepthDown()

	Pad()
	Dump(value interface{})
	DumpString(str string)
	DumpScalar(v interface{}, t reflect.Type, dumpTypeInstantiation bool)
	DumpComplex(v complex128, t reflect.Kind)
	DumpStructType(t reflect.Type)
	DumpStructField(string, reflect.Value)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL