From b1e6c159315b62330a9f9b723c6a154888e9013c Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Sun, 29 Jul 2018 17:04:56 +0930 Subject: [PATCH] handle address annotation of slices --- dump.go | 5 +++++ dump_test.go | 23 +++++++++++++++++++++++ spew_test.go | 2 +- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/dump.go b/dump.go index 1099f1d..7e54e77 100644 --- a/dump.go +++ b/dump.go @@ -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 diff --git a/dump_test.go b/dump_test.go index e8d9fa0..bca849e 100644 --- a/dump_test.go +++ b/dump_test.go @@ -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) diff --git a/spew_test.go b/spew_test.go index 720bfd7..bb0cd62 100644 --- a/spew_test.go +++ b/spew_test.go @@ -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*/ (),\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" +