Explorar o código

Fix incorrect type display on nil pointers.

This was reported by korschak as issue #4 who also pinpointed the issue
and provided a patch.
Dave Collins %!s(int64=13) %!d(string=hai) anos
pai
achega
389ea44d6a
Modificáronse 2 ficheiros con 10 adicións e 8 borrados
  1. 9 7
      spew/dump.go
  2. 1 1
      spew/format.go

+ 9 - 7
spew/dump.go

@@ -66,11 +66,11 @@ func (d *dumpState) dumpPtr(v reflect.Value) {
 	indirects := 0
 	ve := v
 	for ve.Kind() == reflect.Ptr {
-		indirects++
 		if ve.IsNil() {
 			nilFound = true
 			break
 		}
+		indirects++
 		addr := ve.Pointer()
 		pointerChain = append(pointerChain, addr)
 		if pd, ok := d.pointers[addr]; ok && pd < d.depth {
@@ -97,14 +97,16 @@ func (d *dumpState) dumpPtr(v reflect.Value) {
 	d.w.Write(closeParenBytes)
 
 	// Display pointer information.
-	d.w.Write(openParenBytes)
-	for i, addr := range pointerChain {
-		if i > 0 {
-			d.w.Write(pointerChainBytes)
+	if len(pointerChain) > 0 {
+		d.w.Write(openParenBytes)
+		for i, addr := range pointerChain {
+			if i > 0 {
+				d.w.Write(pointerChainBytes)
+			}
+			printHexPtr(d.w, addr)
 		}
-		printHexPtr(d.w, addr)
+		d.w.Write(closeParenBytes)
 	}
-	d.w.Write(closeParenBytes)
 
 	// Display dereferenced value.
 	d.w.Write(openParenBytes)

+ 1 - 1
spew/format.go

@@ -115,11 +115,11 @@ func (f *formatState) formatPtr(v reflect.Value) {
 	indirects := 0
 	ve := v
 	for ve.Kind() == reflect.Ptr {
-		indirects++
 		if ve.IsNil() {
 			nilFound = true
 			break
 		}
+		indirects++
 		addr := ve.Pointer()
 		pointerChain = append(pointerChain, addr)
 		if pd, ok := f.pointers[addr]; ok && pd < f.depth {