Skip to content

Commit 1a3ba57

Browse files
authored
feat: Implement WithoutHeader (#58)
* feat: field redaction * fix: naming
1 parent c7ad293 commit 1a3ba57

File tree

6 files changed

+68
-2
lines changed

6 files changed

+68
-2
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<a href="https://goreportcard.com/report/github.com/goforj/godump"><img src="https://goreportcard.com/badge/github.com/goforj/godump" alt="Go Report Card"></a>
1212
<a href="https://codecov.io/gh/goforj/godump" ><img src="https://codecov.io/gh/goforj/godump/graph/badge.svg?token=ULUTXL03XC"/></a>
1313
<!-- test-count:embed:start -->
14-
<img src="https://img.shields.io/badge/tests-127-brightgreen" alt="Tests">
14+
<img src="https://img.shields.io/badge/tests-130-brightgreen" alt="Tests">
1515
<!-- test-count:embed:end -->
1616
<a href="https://github.com/avelino/awesome-go?tab=readme-ov-file#parsersencodersdecoders"><img src="https://awesome.re/mentioned-badge-flat.svg" alt="Mentioned in Awesome Go"></a>
1717
</p>
@@ -236,7 +236,7 @@ If a pointer has already been printed:
236236
| **Dump** | [Dd](#dd) [Dump](#dump) [DumpStr](#dumpstr) [Fdump](#fdump) |
237237
| **HTML** | [DumpHTML](#dumphtml) |
238238
| **JSON** | [DumpJSON](#dumpjson) [DumpJSONStr](#dumpjsonstr) |
239-
| **Options** | [WithDisableStringer](#withdisablestringer) [WithMaxDepth](#withmaxdepth) [WithMaxItems](#withmaxitems) [WithMaxStringLen](#withmaxstringlen) [WithSkipStackFrames](#withskipstackframes) [WithWriter](#withwriter) [WithoutColor](#withoutcolor) |
239+
| **Options** | [WithDisableStringer](#withdisablestringer) [WithMaxDepth](#withmaxdepth) [WithMaxItems](#withmaxitems) [WithMaxStringLen](#withmaxstringlen) [WithoutHeader](#withoutheader) [WithSkipStackFrames](#withskipstackframes) [WithWriter](#withwriter) [WithoutColor](#withoutcolor) |
240240
241241
242242
## Builder
@@ -581,6 +581,17 @@ d.Dump(v)
581581
// "hello…" #string
582582
```
583583
584+
### <a id="withoutheader"></a>WithoutHeader
585+
586+
WithoutHeader disables printing the source location header.
587+
588+
```go
589+
// Default: false
590+
d := godump.NewDumper(godump.WithoutHeader())
591+
d.Dump("hello")
592+
// "hello" #string
593+
```
594+
584595
### <a id="withskipstackframes"></a>WithSkipStackFrames
585596
586597
WithSkipStackFrames skips additional stack frames for header reporting.

diff.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ func (d *Dumper) dumpStrNoHeader(vs ...any) string {
178178

179179
// printDiffHeader writes the diff header line when a caller frame is available.
180180
func (d *Dumper) printDiffHeader(out io.Writer) {
181+
if d.disableHeader {
182+
return
183+
}
181184
file, line := d.findFirstNonInternalFrame(d.skippedStackFrames)
182185
if file == "" {
183186
return

diff_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ func TestDiffStrNoColor(t *testing.T) {
104104
assert.Contains(t, out, `+ "b" #string`)
105105
}
106106

107+
func TestDiffStrNoHeader(t *testing.T) {
108+
d := NewDumper(WithoutHeader())
109+
d.colorizer = colorizeUnstyled
110+
out := d.DiffStr(1, 2)
111+
out = stripANSI(out)
112+
assert.NotContains(t, out, "<#diff")
113+
assert.Contains(t, out, "- 1")
114+
assert.Contains(t, out, "+ 2")
115+
}
116+
107117
func TestDiffHTMLNoColor(t *testing.T) {
108118
out := NewDumper(WithoutColor()).DiffHTML("a", "b")
109119
assert.NotContains(t, out, `<span style="color:`)

examples/withoutheader/main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//go:build ignore
2+
// +build ignore
3+
4+
package main
5+
6+
import "github.com/goforj/godump"
7+
8+
func main() {
9+
// WithoutHeader disables printing the source location header.
10+
11+
// Example: disable header
12+
// Default: false
13+
d := godump.NewDumper(godump.WithoutHeader())
14+
d.Dump("hello")
15+
// "hello" #string
16+
}

godump.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ type Dumper struct {
9494
skippedStackFrames int
9595
disableStringer bool
9696
disableColor bool
97+
disableHeader bool
9798

9899
// callerFn is used to get the caller information.
99100
// It defaults to [runtime.Caller], it is here to be overridden for testing purposes.
@@ -270,6 +271,22 @@ func WithoutColor() Option {
270271
}
271272
}
272273

274+
// WithoutHeader disables printing the source location header.
275+
// @group Options
276+
//
277+
// Example: disable header
278+
//
279+
// // Default: false
280+
// d := godump.NewDumper(godump.WithoutHeader())
281+
// d.Dump("hello")
282+
// // "hello" #string
283+
func WithoutHeader() Option {
284+
return func(d *Dumper) *Dumper {
285+
d.disableHeader = true
286+
return d
287+
}
288+
}
289+
273290
// NewDumper creates a new Dumper with the given options applied.
274291
// Defaults are used for any setting not overridden.
275292
// @group Builder
@@ -553,6 +570,9 @@ func (d *Dumper) ensureColorizer() {
553570

554571
// printDumpHeader prints the header for the dump output, including the file and line number.
555572
func (d *Dumper) printDumpHeader(out io.Writer) {
573+
if d.disableHeader {
574+
return
575+
}
556576
file, line := d.findFirstNonInternalFrame(d.skippedStackFrames)
557577
if file == "" {
558578
return

godump_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,12 @@ func TestDumpStrNoColor(t *testing.T) {
220220
assert.Contains(t, out, `"x"`)
221221
}
222222

223+
func TestDumpStrNoHeader(t *testing.T) {
224+
out := newDumperT(t, WithoutHeader()).DumpStr("x")
225+
assert.NotContains(t, out, "<#dump")
226+
assert.Contains(t, out, `"x"`)
227+
}
228+
223229
func TestDiffStr(t *testing.T) {
224230
type User struct {
225231
Name string

0 commit comments

Comments
 (0)