Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,13 @@ func main() {

// Custom Dumper with all options set explicitly
d := godump.NewDumper(
godump.WithMaxDepth(15), // default: 15
godump.WithMaxItems(100), // default: 100
godump.WithMaxStringLen(100000), // default: 100000
godump.WithWriter(os.Stdout), // default: os.Stdout
godump.WithMaxDepth(15), // default: 15
godump.WithMaxItems(100), // default: 100
godump.WithMaxStringLen(100000), // default: 100000
godump.WithWriter(os.Stdout), // default: os.Stdout
godump.WithSkipStackFrames(0), // default: 0
godump.WithDisableStringer(false), // default: false
godump.WithHeader(true), // default: true
)

// Use the custom dumper
Expand Down
15 changes: 14 additions & 1 deletion godump.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
defaultMaxItems = 100
defaultMaxStringLen = 100000
defaultMaxStackDepth = 10
defaultShowHeader = true
initialCallerSkip = 2
)

Expand Down Expand Up @@ -92,6 +93,7 @@ type Dumper struct {
writer io.Writer
skippedStackFrames int
disableStringer bool
showHeader bool

// callerFn is used to get the caller information.
// It defaults to [runtime.Caller], it is here to be overridden for testing purposes.
Expand Down Expand Up @@ -167,6 +169,14 @@ func WithDisableStringer(b bool) Option {
}
}

// WithHeader configures whether to print the header with the call location.
func WithHeader(show bool) Option {
return func(d *Dumper) *Dumper {
d.showHeader = show
return d
}
}

// NewDumper creates a new Dumper with the given options applied.
// Defaults are used for any setting not overridden.
func NewDumper(opts ...Option) *Dumper {
Expand All @@ -175,6 +185,7 @@ func NewDumper(opts ...Option) *Dumper {
maxItems: defaultMaxItems,
maxStringLen: defaultMaxStringLen,
disableStringer: defaultDisableStringer,
showHeader: defaultShowHeader,
writer: os.Stdout,
colorizer: nil, // ensure no detection is made if we don't need it
callerFn: runtime.Caller,
Expand Down Expand Up @@ -208,7 +219,9 @@ func DumpStr(vs ...any) string {
// DumpStr returns a string representation of the values with colorized output.
func (d *Dumper) DumpStr(vs ...any) string {
var sb strings.Builder
d.printDumpHeader(&sb)
if d.showHeader {
d.printDumpHeader(&sb)
}
tw := tabwriter.NewWriter(&sb, 0, 0, 1, ' ', 0)
d.writeDump(tw, vs...)
tw.Flush()
Expand Down
9 changes: 9 additions & 0 deletions godump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1059,3 +1059,12 @@ func TestDisableStringer(t *testing.T) {
v = d.DumpStr(data)
assert.Contains(t, v, `-secret => 👻 hidden stringer`)
}

func TestWithHeader(t *testing.T) {
out := dumpStrT(t, "x")
assert.Contains(t, out, "<#dump //", "expected header to be present")

d := newDumperT(t, WithHeader(false))
out = d.DumpStr("x")
assert.NotContains(t, out, "<#dump //", "expected header to be disabled")
}