From dbfec3c09d829ea2e82c846fd17ff64897b1d706 Mon Sep 17 00:00:00 2001 From: yassinebenaid Date: Sun, 17 Nov 2024 08:06:25 +0100 Subject: [PATCH 1/2] fix panic when input is an array --- dumper.go | 6 +++--- dumper_test.go | 3 +++ testdata/slices.txt | 20 ++++++++++++++++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/dumper.go b/dumper.go index 45968c1..c39b55e 100644 --- a/dumper.go +++ b/dumper.go @@ -240,21 +240,21 @@ func (d *Dumper) dump(val reflect.Value, ignoreDepth ...bool) { } func (d *Dumper) dumpSlice(v reflect.Value) { - length := v.Len() - var tag string if d.ptrTag != 0 { tag = __(d.Theme.PointerTag, fmt.Sprintf("#%d", d.ptrTag)) d.ptrTag = 0 } - if v.IsNil() { + if v.Kind() == reflect.Slice && v.IsNil() { d.buf.WriteString(__(d.Theme.Types, v.Type().String())) d.writeNil() d.buf.WriteString(tag) return } + length := v.Len() + d.buf.WriteString(__(d.Theme.Types, fmt.Sprintf("%s:%d:%d", v.Type(), length, v.Cap()))) d.buf.WriteString(__(d.Theme.Braces, fmt.Sprintf(" {%s", tag))) diff --git a/dumper_test.go b/dumper_test.go index af016b9..c77f813 100644 --- a/dumper_test.go +++ b/dumper_test.go @@ -729,6 +729,7 @@ func TestCanDumpSlices(t *testing.T) { type Slice []any var nilSlice []Slice + var zeroArray [2]any foo := "foo" bar := "bar" @@ -752,6 +753,8 @@ func TestCanDumpSlices(t *testing.T) { }, make([]any, 3, 8), nilSlice, + [2]int{1, 2}, + zeroArray, } s = append(s, &s) diff --git a/testdata/slices.txt b/testdata/slices.txt index a337c74..dbf044d 100644 --- a/testdata/slices.txt +++ b/testdata/slices.txt @@ -1,4 +1,4 @@ -godump_test.Slice:11:20 { +godump_test.Slice:13:24 { 1, 2.3, true, @@ -20,7 +20,15 @@ godump_test.Slice:11:20 { nil, }, []godump_test.Slice(nil), - &godump_test.Slice:11:20 {#2 + [2]int:2:2 { + 1, + 2, + }, + [2]interface {}:2:2 { + nil, + nil, + }, + &godump_test.Slice:13:24 {#2 1, 2.3, true, @@ -39,6 +47,14 @@ godump_test.Slice:11:20 { nil, }, []godump_test.Slice(nil), + [2]int:2:2 { + 1, + 2, + }, + [2]interface {}:2:2 { + nil, + nil, + }, &@2, }, } \ No newline at end of file From 39f1f00ca486bae8673d205bfcca74385422d03a Mon Sep 17 00:00:00 2001 From: yassinebenaid Date: Sun, 17 Nov 2024 08:10:38 +0100 Subject: [PATCH 2/2] remove the capacity and length part if input is an array --- dumper.go | 18 +++++++++++------- testdata/slices.txt | 8 ++++---- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/dumper.go b/dumper.go index c39b55e..b92d47e 100644 --- a/dumper.go +++ b/dumper.go @@ -246,16 +246,20 @@ func (d *Dumper) dumpSlice(v reflect.Value) { d.ptrTag = 0 } - if v.Kind() == reflect.Slice && v.IsNil() { + length := v.Len() + + if v.Kind() == reflect.Slice { + if v.IsNil() { + d.buf.WriteString(__(d.Theme.Types, v.Type().String())) + d.writeNil() + d.buf.WriteString(tag) + return + } + d.buf.WriteString(__(d.Theme.Types, fmt.Sprintf("%s:%d:%d", v.Type(), length, v.Cap()))) + } else { d.buf.WriteString(__(d.Theme.Types, v.Type().String())) - d.writeNil() - d.buf.WriteString(tag) - return } - length := v.Len() - - d.buf.WriteString(__(d.Theme.Types, fmt.Sprintf("%s:%d:%d", v.Type(), length, v.Cap()))) d.buf.WriteString(__(d.Theme.Braces, fmt.Sprintf(" {%s", tag))) d.depth++ diff --git a/testdata/slices.txt b/testdata/slices.txt index dbf044d..f4faead 100644 --- a/testdata/slices.txt +++ b/testdata/slices.txt @@ -20,11 +20,11 @@ godump_test.Slice:13:24 { nil, }, []godump_test.Slice(nil), - [2]int:2:2 { + [2]int { 1, 2, }, - [2]interface {}:2:2 { + [2]interface {} { nil, nil, }, @@ -47,11 +47,11 @@ godump_test.Slice:13:24 { nil, }, []godump_test.Slice(nil), - [2]int:2:2 { + [2]int { 1, 2, }, - [2]interface {}:2:2 { + [2]interface {} { nil, nil, },