Skip to content

Commit

Permalink
use deep pointer checks
Browse files Browse the repository at this point in the history
  • Loading branch information
yassinebenaid committed Jun 11, 2024
1 parent ccc41a2 commit 39e72c6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
24 changes: 16 additions & 8 deletions dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
}
}
10 changes: 7 additions & 3 deletions dumper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

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

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

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

0 comments on commit 39e72c6

Please sign in to comment.