From 39e72c6815c17910767d48f2b7e17ca56d071d96 Mon Sep 17 00:00:00 2001 From: yassinebenaid Date: Tue, 11 Jun 2024 21:40:45 +0100 Subject: [PATCH] use deep pointer checks --- dumper.go | 24 ++++++++++++++++-------- dumper_test.go | 10 +++++++--- testdata/structs.txt | 2 ++ 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/dumper.go b/dumper.go index 66ce73f..1ab069f 100644 --- a/dumper.go +++ b/dumper.go @@ -144,6 +144,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) { @@ -202,13 +203,20 @@ 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, reflect.UnsafePointer: - 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: + fmt.Println(v.Kind()) + + return false + } } } diff --git a/dumper_test.go b/dumper_test.go index a253236..5ff8ec3 100644 --- a/dumper_test.go +++ b/dumper_test.go @@ -341,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 @@ -370,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 { @@ -409,6 +412,7 @@ func TestCanDumpStructes(t *testing.T) { }, }, }, + Ptr: &numaddr, } node.Inline.Field2.Field2.Field2 = node.Inline.Field2.Field2 @@ -422,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 @@ -504,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/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, },