Skip to content

Commit

Permalink
fix panic when input is an array
Browse files Browse the repository at this point in the history
  • Loading branch information
yassinebenaid committed Nov 17, 2024
1 parent 2caa654 commit dbfec3c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
6 changes: 3 additions & 3 deletions dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand Down
3 changes: 3 additions & 0 deletions dumper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,7 @@ func TestCanDumpSlices(t *testing.T) {
type Slice []any

var nilSlice []Slice
var zeroArray [2]any

foo := "foo"
bar := "bar"
Expand All @@ -752,6 +753,8 @@ func TestCanDumpSlices(t *testing.T) {
},
make([]any, 3, 8),
nilSlice,
[2]int{1, 2},
zeroArray,
}
s = append(s, &s)

Expand Down
20 changes: 18 additions & 2 deletions testdata/slices.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
godump_test.Slice:11:20 {
godump_test.Slice:13:24 {
1,
2.3,
true,
Expand All @@ -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,
Expand All @@ -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,
},
}

0 comments on commit dbfec3c

Please sign in to comment.