Skip to content

Commit eb5498b

Browse files
committed
Add explanatory comment
1 parent 142fb45 commit eb5498b

4 files changed

Lines changed: 6 additions & 194 deletions

File tree

spew/bypass.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ var (
4949
// value may be taken.
5050
flagAddr flag
5151

52+
// flagPrivate indicates whether the reflect.Value is exported
53+
// or private.
5254
flagPrivate flag
5355
)
5456

spew/config.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,6 @@ type ConfigState struct {
9898
// be spewed to strings and sorted by those strings. This is only
9999
// considered if SortKeys is true.
100100
SpewKeys bool
101-
102-
// NoDuplicates specifies that any given pointer should have
103-
// its dereference dumped only once. This is similar to
104-
// circularity detection, but applies to all pointers across a
105-
// given dump action. This can consume much memory.
106-
NoDuplicates bool
107-
108-
// UseOrdinals specifies that pointer values are to be
109-
// replaced with monotonically increasing integers. It has no
110-
// effect if DisablePointerAddresses is true; else, it
111-
// provides some degree of stability across runs versus
112-
// printing out raw pointers. This can consume much memory.
113-
UseOrdinals bool
114101
}
115102

116103
// Config is the active configuration of the top-level functions.

spew/dump.go

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,9 @@ type dumpState struct {
5252
w io.Writer
5353
depth int
5454
pointers map[uintptr]int
55-
allPointers map[uintptr]uintptr
5655
ignoreNextType bool
5756
ignoreNextIndent bool
5857
cs *ConfigState
59-
nextOrdinal uintptr
6058
}
6159

6260
// indent performs indentation according to the depth level and cs.Indent
@@ -79,50 +77,6 @@ func (d *dumpState) unpackValue(v reflect.Value) reflect.Value {
7977
return v
8078
}
8179

82-
func (d *dumpState) isPointerSeen(addr uintptr) (seen bool, ordinal uintptr) {
83-
ordinal = uintptr(addr)
84-
85-
if d.allPointers == nil {
86-
return false, ordinal
87-
}
88-
89-
// d.cs.NoDuplicates || d.cs.UseOrdinals
90-
duplicateFound := false
91-
if ord, ok := d.allPointers[addr]; ok {
92-
if d.cs.UseOrdinals {
93-
ordinal = ord
94-
}
95-
if d.cs.NoDuplicates {
96-
duplicateFound = true
97-
}
98-
} else {
99-
if d.cs.UseOrdinals {
100-
d.nextOrdinal++
101-
ordinal = d.nextOrdinal
102-
}
103-
d.allPointers[addr] = ordinal
104-
}
105-
106-
return duplicateFound, ordinal
107-
}
108-
109-
func (d *dumpState) printPtrOrOrdinal(addr uintptr) {
110-
if d.cs.UseOrdinals {
111-
printOrdinal(d.w, addr)
112-
} else {
113-
printHexPtr(d.w, addr)
114-
}
115-
}
116-
117-
func (d *dumpState) printPtr(addr uintptr) {
118-
if d.cs.UseOrdinals {
119-
_, addr := d.isPointerSeen(addr)
120-
printOrdinal(d.w, addr)
121-
} else {
122-
printHexPtr(d.w, addr)
123-
}
124-
}
125-
12680
// dumpPtr handles formatting of pointers by indirecting them as necessary.
12781
func (d *dumpState) dumpPtr(v reflect.Value) {
12882
// Remove pointers at or below the current depth from map used to detect
@@ -141,7 +95,6 @@ func (d *dumpState) dumpPtr(v reflect.Value) {
14195
// references.
14296
nilFound := false
14397
cycleFound := false
144-
duplicateFound := false
14598
indirects := 0
14699
ve := v
147100
for ve.Kind() == reflect.Ptr {
@@ -151,21 +104,12 @@ func (d *dumpState) dumpPtr(v reflect.Value) {
151104
}
152105
indirects++
153106
addr := ve.Pointer()
154-
107+
pointerChain = append(pointerChain, addr)
155108
if pd, ok := d.pointers[addr]; ok && pd < d.depth {
156109
cycleFound = true
157-
}
158-
159-
dup, ordinal := d.isPointerSeen(addr)
160-
161-
pointerChain = append(pointerChain, ordinal)
162-
163-
if cycleFound || dup {
164110
indirects--
165-
duplicateFound = true
166111
break
167112
}
168-
169113
d.pointers[addr] = d.depth
170114

171115
ve = ve.Elem()
@@ -191,7 +135,7 @@ func (d *dumpState) dumpPtr(v reflect.Value) {
191135
if i > 0 {
192136
d.w.Write(pointerChainBytes)
193137
}
194-
d.printPtrOrOrdinal(addr)
138+
printHexPtr(d.w, addr)
195139
}
196140
d.w.Write(closeParenBytes)
197141
}
@@ -205,9 +149,6 @@ func (d *dumpState) dumpPtr(v reflect.Value) {
205149
case cycleFound:
206150
d.w.Write(circularBytes)
207151

208-
case duplicateFound:
209-
d.w.Write(duplicateBytes)
210-
211152
default:
212153
d.ignoreNextType = true
213154
d.dump(ve)
@@ -441,7 +382,6 @@ func (d *dumpState) dump(v reflect.Value) {
441382
d.indent()
442383
d.w.Write(maxNewlineBytes)
443384
} else {
444-
v = unsafeReflectValue(v)
445385
numEntries := v.Len()
446386
keys := v.MapKeys()
447387
if d.cs.SortKeys {
@@ -491,10 +431,10 @@ func (d *dumpState) dump(v reflect.Value) {
491431
d.w.Write(closeBraceBytes)
492432

493433
case reflect.Uintptr:
494-
d.printPtr(uintptr(v.Uint()))
434+
printHexPtr(d.w, uintptr(v.Uint()))
495435

496436
case reflect.UnsafePointer, reflect.Chan, reflect.Func:
497-
d.printPtr(v.Pointer())
437+
printHexPtr(d.w, v.Pointer())
498438

499439
// There were not any other types at the time this code was written, but
500440
// fall back to letting the default fmt package handle it in case any new
@@ -522,9 +462,6 @@ func fdump(cs *ConfigState, w io.Writer, a ...interface{}) {
522462

523463
d := dumpState{w: w, cs: cs}
524464
d.pointers = make(map[uintptr]int)
525-
if cs.NoDuplicates || cs.UseOrdinals {
526-
d.allPointers = make(map[uintptr]uintptr)
527-
}
528465
d.dump(reflect.ValueOf(arg))
529466
d.w.Write(newlineBytes)
530467
}

spew/dump_test.go

Lines changed: 0 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,117 +1040,3 @@ func TestDumpSortedKeys(t *testing.T) {
10401040
}
10411041

10421042
}
1043-
1044-
func TestDumpDuplicatePointers(t *testing.T) {
1045-
cfg := spew.ConfigState{NoDuplicates: true}
1046-
type info struct {
1047-
a int
1048-
b int
1049-
}
1050-
type twice struct {
1051-
info1 *info
1052-
info2 *info
1053-
info3 *info
1054-
}
1055-
i1 := &info{a: 1, b: 2}
1056-
ip1 := fmt.Sprintf("%p", i1)
1057-
i2 := &info{a: 3, b: 4}
1058-
ip2 := fmt.Sprintf("%p", i2)
1059-
v := twice{info1: i1, info2: i2, info3: i1}
1060-
// vt := "spew_test.twice"
1061-
s := cfg.Sdump(v)
1062-
expected := `(spew_test.twice) {
1063-
info1: (*spew_test.info)(` + ip1 + `)({
1064-
a: (int) 1,
1065-
b: (int) 2
1066-
}),
1067-
info2: (*spew_test.info)(` + ip2 + `)({
1068-
a: (int) 3,
1069-
b: (int) 4
1070-
}),
1071-
info3: (*spew_test.info)(` + ip1 + `)(<already seen>)
1072-
}
1073-
`
1074-
if s != expected {
1075-
t.Errorf("Duplicate-pointers mismatch:\n %v %v", s, expected)
1076-
}
1077-
}
1078-
1079-
func TestDumpOrdinals(t *testing.T) {
1080-
cfg := spew.ConfigState{UseOrdinals: true}
1081-
type info struct {
1082-
a int
1083-
b int
1084-
}
1085-
type twice struct {
1086-
info1 *info
1087-
info2 *info
1088-
info3 *info
1089-
}
1090-
i := &info{a: 1, b: 2}
1091-
v := twice{info1: i, info2: &info{a: 3, b: 4}, info3: i}
1092-
// vt := "spew_test.twice"
1093-
s := cfg.Sdump(v)
1094-
expected := `(spew_test.twice) {
1095-
info1: (*spew_test.info)(#1)({
1096-
a: (int) 1,
1097-
b: (int) 2
1098-
}),
1099-
info2: (*spew_test.info)(#2)({
1100-
a: (int) 3,
1101-
b: (int) 4
1102-
}),
1103-
info3: (*spew_test.info)(#1)({
1104-
a: (int) 1,
1105-
b: (int) 2
1106-
})
1107-
}
1108-
`
1109-
if s != expected {
1110-
t.Errorf("Ordinals mismatch:\n %v %v", s, expected)
1111-
}
1112-
}
1113-
1114-
func a() {
1115-
}
1116-
1117-
func b() {
1118-
}
1119-
1120-
func TestDumpDuplicateOrdinals(t *testing.T) {
1121-
cfg := spew.ConfigState{NoDuplicates: true, UseOrdinals: true}
1122-
type info struct {
1123-
a int
1124-
b int
1125-
}
1126-
type twice struct {
1127-
info1 *info
1128-
info2 *info
1129-
info3 *info
1130-
fn1 func()
1131-
fn2 func()
1132-
fn3 func()
1133-
}
1134-
i := &info{a: 1, b: 2}
1135-
v := twice{info1: i, info2: &info{a: 3, b: 4}, info3: i, fn1: a, fn2: b, fn3: a}
1136-
// vt := "spew_test.twice"
1137-
s := cfg.Sdump(v)
1138-
expected := `(spew_test.twice) {
1139-
info1: (*spew_test.info)(#1)({
1140-
a: (int) 1,
1141-
b: (int) 2
1142-
}),
1143-
info2: (*spew_test.info)(#2)({
1144-
a: (int) 3,
1145-
b: (int) 4
1146-
}),
1147-
info3: (*spew_test.info)(#1)(<already seen>),
1148-
fn1: (func()) #3,
1149-
fn2: (func()) #4,
1150-
fn3: (func()) #3
1151-
}
1152-
`
1153-
if s != expected {
1154-
t.Errorf("Duplicate-pointers mismatch:\n %v %v", s, expected)
1155-
}
1156-
}

0 commit comments

Comments
 (0)