Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic when input is array #73

Merged
merged 2 commits into from
Nov 17, 2024
Merged
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
18 changes: 11 additions & 7 deletions dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,22 +240,26 @@ 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() {
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
}

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++
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 {
1,
2,
},
[2]interface {} {
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 {
1,
2,
},
[2]interface {} {
nil,
nil,
},
&@2,
},
}