Skip to content

Commit

Permalink
handle address annotation of slices
Browse files Browse the repository at this point in the history
  • Loading branch information
kortschak committed Jul 29, 2018
1 parent 364ec7d commit b1e6c15
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ func (d *dumpState) indent() {
func (d *dumpState) unpackValue(v reflect.Value) (val reflect.Value, wasPtr, static bool, addr uintptr) {
if v.CanAddr() {
addr = v.Addr().Pointer()
} else if v.Kind() == reflect.Slice && v.Len() != 0 {
elem := v.Index(0)
if elem.CanAddr() {
addr = elem.Addr().Pointer()
}
}
if v.Kind() == reflect.Interface && !v.IsNil() {
return v.Elem(), v.Kind() == reflect.Ptr, false, addr
Expand Down
23 changes: 23 additions & 0 deletions dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1045,11 +1045,34 @@ var mapElementCycles = []struct {
},
},
}
`,
},
// The following test is to confirm that the recursion detection
// is not overly zealous by missing identifying the address of slices.
// This is https://github.com/kortschak/utter/issues/12.
{
v: map[interface{}][]interface{}{
"outer": []interface{}{
map[interface{}]interface{}{
"inner": []interface{}{"value"},
},
},
},
want: `map[interface{}][]interface{}{
string("outer"): []interface{}{
map[interface{}]interface{}{
string("inner"): []interface{}{
string("value"),
},
},
},
}
`,
},
}

// https://github.com/kortschak/utter/issues/5
// https://github.com/kortschak/utter/issues/12
func TestIssue5Maps(t *testing.T) {
for _, test := range mapElementCycles {
w := newLimitedWriter(512)
Expand Down
2 changes: 1 addition & 1 deletion spew_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func initSpewTests() {
" int(1): []interface{}{\n"+
" int( /*%p*/ 5),\n"+
" int( /*%p*/ 5),\n"+
" &int /*%[1]p*/ (5),\n"+
" (*interface{}) /*%[1]p*/ (<already shown>),\n"+
" &int /*%p*/ (5),\n },\n}\n", &c[0], &c[1])},
{bs8Default, fCSFdump, []byte{1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3}, "[]uint8{\n" +
" 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x01, 0x02, // |........|\n" +
Expand Down

0 comments on commit b1e6c15

Please sign in to comment.