Skip to content

Commit 588e564

Browse files
committed
feat: add Fdump(w io.Writer, vs ...any)
1 parent c483de2 commit 588e564

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

godump.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ func Dump(vs ...any) {
7575
tw.Flush()
7676
}
7777

78+
// Fdump writes the formatted dump of values to the given io.Writer.
79+
func Fdump(w io.Writer, vs ...any) {
80+
printDumpHeader(w, 3)
81+
tw := tabwriter.NewWriter(w, 0, 0, 1, ' ', 0)
82+
writeDump(tw, vs...)
83+
tw.Flush()
84+
}
85+
7886
// DumpStr dumps the values as a string with colorized output.
7987
func DumpStr(vs ...any) string {
8088
var sb strings.Builder

godump_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,3 +740,37 @@ func TestMakeAddressable_CanAddr(t *testing.T) {
740740

741741
assert.Equal(t, v.Interface(), out.Interface()) // compare by value
742742
}
743+
744+
func TestFdump_WritesToWriter(t *testing.T) {
745+
var buf strings.Builder
746+
747+
type Inner struct {
748+
Field string
749+
}
750+
type Outer struct {
751+
InnerField Inner
752+
Number int
753+
}
754+
755+
val := Outer{
756+
InnerField: Inner{Field: "hello"},
757+
Number: 42,
758+
}
759+
760+
Fdump(&buf, val)
761+
762+
out := buf.String()
763+
764+
if !strings.Contains(out, "Outer") {
765+
t.Errorf("expected output to contain type name 'Outer', got: %s", out)
766+
}
767+
if !strings.Contains(out, "InnerField") || !strings.Contains(out, "hello") {
768+
t.Errorf("expected nested struct and field to appear, got: %s", out)
769+
}
770+
if !strings.Contains(out, "Number") || !strings.Contains(out, "42") {
771+
t.Errorf("expected field 'Number' with value '42', got: %s", out)
772+
}
773+
if !strings.Contains(out, "<#dump //") {
774+
t.Errorf("expected dump header with file and line, got: %s", out)
775+
}
776+
}

0 commit comments

Comments
 (0)