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

skip pointer tags on primitives #27

Merged
merged 1 commit into from
Jun 11, 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
22 changes: 21 additions & 1 deletion dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ func (d *dumper) dumpPointer(v reflect.Value) {
d.ptrs = make(map[uintptr]*pointer)
}

elem := v.Elem()

if isPrimitive(elem) {
d.write(d.theme.PointerSign.apply("&"))
d.dump(elem, true)
return
}

addr := uintptr(v.UnsafePointer())

if p, ok := d.ptrs[addr]; ok {
Expand All @@ -130,7 +138,7 @@ func (d *dumper) dumpPointer(v reflect.Value) {
}

d.write(d.theme.PointerSign.apply("&"))
d.dump(v.Elem(), true)
d.dump(elem, true)
}

func (d *dumper) tagPtr(ptr *pointer) {
Expand Down Expand Up @@ -195,3 +203,15 @@ func (d *dumper) indent() {

d.write(strings.Repeat(d.indentation, d.depth))
}

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
}
}
35 changes: 0 additions & 35 deletions dumper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,38 +611,3 @@ func TestDumperWithComplexDataStructure(t *testing.T) {
}
}
}

func TestDumperWithComplexDataStructureAndPrivateFieldsDumpingEnabled(t *testing.T) {
cyclicNode = root

expectedOutput, err := os.ReadFile("./testdata/private-fields-output.txt")
if err != nil {
t.Fatal(err)
}

var d dumper
d.dumpPrivateFields = true
d.dump(reflect.ValueOf(root))
returned := d.buf

r_lines := bytes.Split(returned, []byte("\n"))
e_lines := bytes.Split(expectedOutput, []byte("\n"))

if len(r_lines) != len(e_lines) {
t.Fatalf("expected %d lines, got %d", len(e_lines), len(r_lines))
}

for i, line := range e_lines {
if len(line) != len(r_lines[i]) {
t.Fatalf(`mismatche at line %d:
--- "%s"
+++ "%s"`, i+1, line, r_lines[i])
}

for j, ch := range line {
if ch != r_lines[i][j] {
t.Fatalf(`expected "%c", got "%c" at line %d:%d"`, ch, r_lines[i][j], i+1, j)
}
}
}
}
72 changes: 36 additions & 36 deletions testdata/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
3.3,
},
},
IntPointer: #3&42,
StringPointer: #4&"example",
MapPointer: #5&map[string]int:1 {
IntPointer: &42,
StringPointer: &"example",
MapPointer: #3&map[string]int:1 {
"key1": 1,
},
ChannelInt: chan int,
Expand All @@ -58,7 +58,7 @@
},
SubNode: &godump.SubNode {
Code: 100,
Parent: #7&nil,
Parent: &nil,
Data: []*godump.Node:0:0 {},
SubDetails: []godump.Detail:1:1 {
godump.Detail {
Expand All @@ -67,7 +67,7 @@
Next: &godump.Detail {
Info: "Detail2",
Count: 2,
Next: &@7,
Next: &nil,
DetailMap: map[string]bool:1 {
"key1": true,
},
Expand All @@ -88,7 +88,7 @@
ChannelSub: chan *godump.SubNode,
},
privateSubNode: *godump.SubNode,
CyclicReference: &@7,
CyclicReference: &nil,
NamedTypes: godump.NamedTypes {
NamedInt: 0,
NamedFloat: 0,
Expand Down Expand Up @@ -122,9 +122,9 @@
"c",
},
},
IntPointer: &@3,
StringPointer: &@4,
MapPointer: &@5,
IntPointer: &42,
StringPointer: &"example",
MapPointer: &@3,
ChannelInt: chan int,
ChannelStr: chan string,
ChannelStruct: chan godump.Detail,
Expand All @@ -135,7 +135,7 @@
},
SubNode: &godump.SubNode {
Code: 200,
Parent: &@7,
Parent: &nil,
Data: []*godump.Node:0:0 {},
SubDetails: []godump.Detail:1:1 {
godump.Detail {
Expand All @@ -144,7 +144,7 @@
Next: &godump.Detail {
Info: "DetailB",
Count: 20,
Next: &@7,
Next: &nil,
DetailMap: map[string]bool:1 {
"key3": true,
},
Expand All @@ -165,7 +165,7 @@
ChannelSub: chan *godump.SubNode,
},
privateSubNode: *godump.SubNode,
CyclicReference: &@7,
CyclicReference: &nil,
NamedTypes: godump.NamedTypes {
NamedInt: 0,
NamedFloat: 0,
Expand All @@ -181,9 +181,9 @@
300,
},
},
IntPointer: &@3,
StringPointer: &@4,
MapPointer: &@5,
IntPointer: &42,
StringPointer: &"example",
MapPointer: &@3,
ChannelInt: chan int,
ChannelStr: chan string,
ChannelStruct: chan godump.Detail,
Expand All @@ -194,7 +194,7 @@
},
SubNode: &godump.SubNode {
Code: 999,
Parent: &@7,
Parent: &nil,
Data: []*godump.Node:2:2 {
&godump.Node {
ID: 4,
Expand Down Expand Up @@ -237,9 +237,9 @@
Attributes: map[string]interface {}:1 {
"greatAttr": "greatValue",
},
IntPointer: &@3,
StringPointer: &@4,
MapPointer: &@5,
IntPointer: &42,
StringPointer: &"example",
MapPointer: &@3,
ChannelInt: chan int,
ChannelStr: chan string,
ChannelStruct: chan godump.Detail,
Expand All @@ -248,9 +248,9 @@
14,
15,
},
SubNode: &@7,
SubNode: &nil,
privateSubNode: *godump.SubNode,
CyclicReference: &@7,
CyclicReference: &nil,
NamedTypes: godump.NamedTypes {
NamedInt: 0,
NamedFloat: 0,
Expand All @@ -262,9 +262,9 @@
Attributes: map[string]interface {}:1 {
"grandAttr": "grandValue1",
},
IntPointer: &@3,
StringPointer: &@4,
MapPointer: &@5,
IntPointer: &42,
StringPointer: &"example",
MapPointer: &@3,
ChannelInt: chan int,
ChannelStr: chan string,
ChannelStruct: chan godump.Detail,
Expand All @@ -273,9 +273,9 @@
11,
12,
},
SubNode: &@7,
SubNode: &nil,
privateSubNode: *godump.SubNode,
CyclicReference: &@7,
CyclicReference: &nil,
NamedTypes: godump.NamedTypes {
NamedInt: 0,
NamedFloat: 0,
Expand Down Expand Up @@ -324,9 +324,9 @@
Attributes: map[string]interface {}:1 {
"greatAttr": "greatValue2",
},
IntPointer: &@3,
StringPointer: &@4,
MapPointer: &@5,
IntPointer: &42,
StringPointer: &"example",
MapPointer: &@3,
ChannelInt: chan int,
ChannelStr: chan string,
ChannelStruct: chan godump.Detail,
Expand All @@ -335,9 +335,9 @@
20,
21,
},
SubNode: &@7,
SubNode: &nil,
privateSubNode: *godump.SubNode,
CyclicReference: &@7,
CyclicReference: &nil,
NamedTypes: godump.NamedTypes {
NamedInt: 0,
NamedFloat: 0,
Expand All @@ -349,9 +349,9 @@
Attributes: map[string]interface {}:1 {
"grandAttr": "grandValue2",
},
IntPointer: &@3,
StringPointer: &@4,
MapPointer: &@5,
IntPointer: &42,
StringPointer: &"example",
MapPointer: &@3,
ChannelInt: chan int,
ChannelStr: chan string,
ChannelStruct: chan godump.Detail,
Expand All @@ -360,9 +360,9 @@
17,
18,
},
SubNode: &@7,
SubNode: &nil,
privateSubNode: *godump.SubNode,
CyclicReference: &@7,
CyclicReference: &nil,
NamedTypes: godump.NamedTypes {
NamedInt: 0,
NamedFloat: 0,
Expand Down
Loading