Ver código fonte

Fix incorrect val display on packed nil interfaces.

This was reported by shurcooL as issue #12.
Dave Collins 12 anos atrás
pai
commit
173295b96e
2 arquivos alterados com 14 adições e 4 exclusões
  1. 5 1
      spew/dump.go
  2. 9 3
      spew/format.go

+ 5 - 1
spew/dump.go

@@ -262,7 +262,11 @@ func (d *dumpState) dump(v reflect.Value) {
 		d.w.Write([]byte(strconv.Quote(v.String())))
 
 	case reflect.Interface:
-		// Do nothing.  We should never get here due to unpackValue calls.
+		// The only time we should get here is for nil interfaces due to
+		// unpackValue calls.
+		if v.IsNil() {
+			d.w.Write(nilAngleBytes)
+		}
 
 	case reflect.Ptr:
 		// Do nothing.  We should never get here since pointers have already

+ 9 - 3
spew/format.go

@@ -92,9 +92,11 @@ func (f *formatState) constructOrigFormat(verb rune) (format string) {
 // This is useful for data types like structs, arrays, slices, and maps which
 // can contain varying types packed inside an interface.
 func (f *formatState) unpackValue(v reflect.Value) reflect.Value {
-	if v.Kind() == reflect.Interface && !v.IsNil() {
+	if v.Kind() == reflect.Interface {
 		f.ignoreNextType = false
-		v = v.Elem()
+		if !v.IsNil() {
+			v = v.Elem()
+		}
 	}
 	return v
 }
@@ -276,7 +278,11 @@ func (f *formatState) format(v reflect.Value) {
 		f.fs.Write([]byte(v.String()))
 
 	case reflect.Interface:
-		// Do nothing.  We should never get here due to unpackValue calls
+		// The only time we should get here is for nil interfaces due to
+		// unpackValue calls.
+		if v.IsNil() {
+			f.fs.Write(nilAngleBytes)
+		}
 
 	case reflect.Ptr:
 		// Do nothing.  We should never get here since pointers have already