Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dump uintptr as hex #32

Merged
merged 3 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
}
}
32 changes: 29 additions & 3 deletions dumper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -74,6 +76,8 @@ func TestCanDumpPrimitives(t *testing.T) {
Bool2 bool
String string

Uintptr uintptr

IntPtr *int
Int8Ptr *int8
Int16Ptr *int16
Expand All @@ -92,6 +96,8 @@ func TestCanDumpPrimitives(t *testing.T) {
Bool2Ptr *bool
StringPtr *string

UintptrPtr *uintptr

TypedInt IntType
TypedInt8 Int8Type
TypedInt16 Int16Type
Expand All @@ -110,6 +116,8 @@ func TestCanDumpPrimitives(t *testing.T) {
TypedBool2 Bool2Type
TypedString StringType

TypedUintptr UintptrType

TypedIntPtr IntPtrType
TypedInt8Ptr Int8PtrType
TypedInt16Ptr Int16PtrType
Expand All @@ -128,6 +136,8 @@ func TestCanDumpPrimitives(t *testing.T) {
TypedBool2Ptr Bool2PtrType
TypedStringPtr StringPtrType

TypedUintptrPtr UintptrPtrType

PtrTypedInt *IntType
PtrTypedInt8 *Int8Type
PtrTypedInt16 *Int16Type
Expand All @@ -146,6 +156,8 @@ func TestCanDumpPrimitives(t *testing.T) {
PtrTypedBool2 *Bool2Type
PtrTypedString *StringType

PtrTypedUintptr *UintptrType

Nil *any

Func func()
Expand Down Expand Up @@ -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),
Expand All @@ -225,6 +239,8 @@ func TestCanDumpPrimitives(t *testing.T) {
TypedBool2: Bool2Type(false),
TypedString: StringType("foo bar"),

TypedUintptr: UintptrType(1234567890),

Nil: nil,

UnsafePointer1: nil,
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -387,6 +412,7 @@ func TestCanDumpStructes(t *testing.T) {
},
},
},
Ptr: &numaddr,
}

node.Inline.Field2.Field2.Field2 = node.Inline.Field2.Field2
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion testdata/primitives.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ godump.Node {
Bool1: true,
Bool2: false,
String: "foo bar",
Uintptr: 0x499602d2,
IntPtr: &123,
Int8Ptr: &-45,
Int16Ptr: &6789,
Expand All @@ -33,6 +34,7 @@ godump.Node {
Bool1Ptr: &true,
Bool2Ptr: &false,
StringPtr: &"foo bar",
UintptrPtr: &0x499602d2,
TypedInt: 123,
TypedInt8: -45,
TypedInt16: 6789,
Expand All @@ -50,6 +52,7 @@ godump.Node {
TypedBool1: true,
TypedBool2: false,
TypedString: "foo bar",
TypedUintptr: 0x499602d2,
TypedIntPtr: &123,
TypedInt8Ptr: &-45,
TypedInt16Ptr: &6789,
Expand All @@ -67,6 +70,7 @@ godump.Node {
TypedBool1Ptr: &true,
TypedBool2Ptr: &false,
TypedStringPtr: &"foo bar",
TypedUintptrPtr: &0x499602d2,
PtrTypedInt: &123,
PtrTypedInt8: &-45,
PtrTypedInt16: &6789,
Expand All @@ -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 {},
Expand Down
2 changes: 2 additions & 0 deletions testdata/structs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ godump.Node {
Field2: &@1,
},
},
Ptr: &&123,
Empty: struct {},
Ref: &godump.Node {#3
Inline: struct {
Expand All @@ -61,6 +62,7 @@ godump.Node {
},
Field2: &@2,
},
Ptr: &&123,
Empty: struct {},
Ref: &@3,
},
Expand Down