diff --git a/dumper.go b/dumper.go index 8e7d640..f610ead 100644 --- a/dumper.go +++ b/dumper.go @@ -45,12 +45,14 @@ func (d *dumper) dump(val reflect.Value, ignore_depth ...bool) { d.dumpPointer(val) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: d.write(d.theme.Number.apply(fmt.Sprint(val))) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: d.write(d.theme.Number.apply(fmt.Sprint(val))) case reflect.Float32, reflect.Float64: d.write(d.theme.Number.apply(fmt.Sprint(val))) case reflect.Complex64, reflect.Complex128: d.write(d.theme.Number.apply(fmt.Sprint(val))) + case reflect.Uintptr: + d.write(d.theme.Number.apply(fmt.Sprintf("0x%x", val.Uint()))) case reflect.Invalid: d.write(d.theme.Nil.apply("nil")) case reflect.Interface: @@ -124,7 +126,9 @@ func (d *dumper) dumpPointer(v reflect.Value) { elem := v.Elem() if isPrimitive(elem) { - d.write(d.theme.PointerSign.apply("&")) + if elem.IsValid() { + d.write(d.theme.PointerSign.apply("&")) + } d.dump(elem, true) return } @@ -142,6 +146,7 @@ func (d *dumper) dumpPointer(v reflect.Value) { d.ptrTag = uint(len(d.ptrs)) d.write(d.theme.PointerSign.apply("&")) d.dump(elem, true) + d.ptrTag = 0 } func (d *dumper) dumpStruct(v reflect.Value) { @@ -200,13 +205,18 @@ func (d *dumper) indent() { } func isPrimitive(val reflect.Value) bool { - switch val.Kind() { - case reflect.String, reflect.Bool, reflect.Func, reflect.Chan, - reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, - reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, - reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128, reflect.Invalid: - return true - default: - return false + v := val + for { + switch v.Kind() { + case reflect.String, reflect.Bool, reflect.Func, reflect.Chan, + reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, + reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128, reflect.Invalid, reflect.UnsafePointer: + return true + case reflect.Pointer: + v = v.Elem() + default: + return false + } } } diff --git a/dumper_test.go b/dumper_test.go index 6bdac40..5ff8ec3 100644 --- a/dumper_test.go +++ b/dumper_test.go @@ -27,6 +27,7 @@ func TestCanDumpPrimitives(t *testing.T) { type Bool1Type bool type Bool2Type bool type StringType string + type UintptrType uintptr type IntPtrType *int type Int8PtrType *int8 @@ -45,6 +46,7 @@ func TestCanDumpPrimitives(t *testing.T) { type Bool1PtrType *bool type Bool2PtrType *bool type StringPtrType *string + type UintptrPtrType *uintptr type FuncType func() type Func2Type func(int) float64 @@ -74,6 +76,8 @@ func TestCanDumpPrimitives(t *testing.T) { Bool2 bool String string + Uintptr uintptr + IntPtr *int Int8Ptr *int8 Int16Ptr *int16 @@ -92,6 +96,8 @@ func TestCanDumpPrimitives(t *testing.T) { Bool2Ptr *bool StringPtr *string + UintptrPtr *uintptr + TypedInt IntType TypedInt8 Int8Type TypedInt16 Int16Type @@ -110,6 +116,8 @@ func TestCanDumpPrimitives(t *testing.T) { TypedBool2 Bool2Type TypedString StringType + TypedUintptr UintptrType + TypedIntPtr IntPtrType TypedInt8Ptr Int8PtrType TypedInt16Ptr Int16PtrType @@ -128,6 +136,8 @@ func TestCanDumpPrimitives(t *testing.T) { TypedBool2Ptr Bool2PtrType TypedStringPtr StringPtrType + TypedUintptrPtr UintptrPtrType + PtrTypedInt *IntType PtrTypedInt8 *Int8Type PtrTypedInt16 *Int16Type @@ -146,6 +156,8 @@ func TestCanDumpPrimitives(t *testing.T) { PtrTypedBool2 *Bool2Type PtrTypedString *StringType + PtrTypedUintptr *UintptrType + Nil *any Func func() @@ -207,6 +219,8 @@ func TestCanDumpPrimitives(t *testing.T) { Bool2: false, String: "foo bar", + Uintptr: 1234567890, + TypedInt: IntType(123), TypedInt8: Int8Type(-45), TypedInt16: Int16Type(6789), @@ -225,6 +239,8 @@ func TestCanDumpPrimitives(t *testing.T) { TypedBool2: Bool2Type(false), TypedString: StringType("foo bar"), + TypedUintptr: UintptrType(1234567890), + Nil: nil, UnsafePointer1: nil, @@ -248,6 +264,8 @@ func TestCanDumpPrimitives(t *testing.T) { node.Bool2Ptr = &node.Bool2 node.StringPtr = &node.String + node.UintptrPtr = &node.Uintptr + node.TypedIntPtr = node.IntPtr node.TypedInt8Ptr = node.Int8Ptr node.TypedInt16Ptr = node.Int16Ptr @@ -266,6 +284,8 @@ func TestCanDumpPrimitives(t *testing.T) { node.TypedBool2Ptr = node.Bool2Ptr node.TypedStringPtr = node.StringPtr + node.TypedUintptrPtr = node.UintptrPtr + node.PtrTypedInt = &node.TypedInt node.PtrTypedInt8 = &node.TypedInt8 node.PtrTypedInt16 = &node.TypedInt16 @@ -284,6 +304,8 @@ func TestCanDumpPrimitives(t *testing.T) { node.PtrTypedBool2 = &node.TypedBool2 node.PtrTypedString = &node.TypedString + node.PtrTypedUintptr = &node.TypedUintptr + node.FuncPtr = &node.Func node.Func2Ptr = &node.Func2 node.Func3Ptr = &node.Func3 @@ -319,7 +341,7 @@ func TestCanDumpPrimitives(t *testing.T) { checkFromFeed(t, d.buf, "./testdata/primitives.txt") } -func TestCanDumpStructes(t *testing.T) { +func TestCanDumpStructs(t *testing.T) { type Number int @@ -348,11 +370,14 @@ func TestCanDumpStructes(t *testing.T) { Typed Child + Ptr **int Empty struct{} Ref *Node } + var num = 123 + var numaddr = &num node := Node{ Inline: struct { Field1 struct { @@ -387,6 +412,7 @@ func TestCanDumpStructes(t *testing.T) { }, }, }, + Ptr: &numaddr, } node.Inline.Field2.Field2.Field2 = node.Inline.Field2.Field2 @@ -400,7 +426,7 @@ func TestCanDumpStructes(t *testing.T) { checkFromFeed(t, d.buf, "./testdata/structs.txt") } -func TestCanDumpPrivateStructes(t *testing.T) { +func TestCanDumpPrivateStructs(t *testing.T) { type number int @@ -482,7 +508,7 @@ func TestCanDumpPrivateStructes(t *testing.T) { checkFromFeed(t, d.buf, "./testdata/private-structs.txt") } -func TestCanDumpPrivateStructesWhenPrivateFieldsDumpingIsEnabled(t *testing.T) { +func TestCanDumpPrivateStructsWhenPrivateFieldsDumpingIsEnabled(t *testing.T) { type number int diff --git a/testdata/primitives.txt b/testdata/primitives.txt index 9bb3b88..861f213 100644 --- a/testdata/primitives.txt +++ b/testdata/primitives.txt @@ -16,6 +16,7 @@ godump.Node { Bool1: true, Bool2: false, String: "foo bar", + Uintptr: 0x499602d2, IntPtr: &123, Int8Ptr: &-45, Int16Ptr: &6789, @@ -33,6 +34,7 @@ godump.Node { Bool1Ptr: &true, Bool2Ptr: &false, StringPtr: &"foo bar", + UintptrPtr: &0x499602d2, TypedInt: 123, TypedInt8: -45, TypedInt16: 6789, @@ -50,6 +52,7 @@ godump.Node { TypedBool1: true, TypedBool2: false, TypedString: "foo bar", + TypedUintptr: 0x499602d2, TypedIntPtr: &123, TypedInt8Ptr: &-45, TypedInt16Ptr: &6789, @@ -67,6 +70,7 @@ godump.Node { TypedBool1Ptr: &true, TypedBool2Ptr: &false, TypedStringPtr: &"foo bar", + TypedUintptrPtr: &0x499602d2, PtrTypedInt: &123, PtrTypedInt8: &-45, PtrTypedInt16: &6789, @@ -84,7 +88,8 @@ godump.Node { PtrTypedBool1: &true, PtrTypedBool2: &false, PtrTypedString: &"foo bar", - Nil: &nil, + PtrTypedUintptr: &0x499602d2, + Nil: nil, Func: func(), Func2: func(int) float64, Func3: func(...*interface {}) interface {}, diff --git a/testdata/structs.txt b/testdata/structs.txt index 03a173f..bbd45a0 100644 --- a/testdata/structs.txt +++ b/testdata/structs.txt @@ -36,6 +36,7 @@ godump.Node { Field2: &@1, }, }, + Ptr: &&123, Empty: struct {}, Ref: &godump.Node {#3 Inline: struct { @@ -61,6 +62,7 @@ godump.Node { }, Field2: &@2, }, + Ptr: &&123, Empty: struct {}, Ref: &@3, },