From 61c7adc866cfdd1b90678e61afb4b91cfd414d94 Mon Sep 17 00:00:00 2001 From: yassinebenaid <yassinebenaide3@gmail.com> Date: Mon, 26 Aug 2024 20:58:48 +0100 Subject: [PATCH 1/5] chore: added pimitive type --- dumper.go | 35 +++++++++++++----- testdata/primitives.txt | 72 ++++++++++++++++++------------------ testdata/private-structs.txt | 16 ++++---- testdata/structs.txt | 16 ++++---- 4 files changed, 77 insertions(+), 62 deletions(-) diff --git a/dumper.go b/dumper.go index 2005270..bfa9298 100644 --- a/dumper.go +++ b/dumper.go @@ -186,11 +186,9 @@ func (d *Dumper) dump(val reflect.Value, ignoreDepth ...bool) { switch val.Kind() { case reflect.String: - d.buf.WriteString(__(d.Theme.Quotes, `"`) + - __(d.Theme.String, val.String()) + - __(d.Theme.Quotes, `"`)) + d.wrapType(val, __(d.Theme.Quotes, `"`)+__(d.Theme.String, val.String())+__(d.Theme.Quotes, `"`)) case reflect.Bool: - d.buf.WriteString(__(d.Theme.Bool, fmt.Sprintf("%t", val.Bool()))) + d.wrapType(val, __(d.Theme.Bool, fmt.Sprintf("%t", val.Bool()))) case reflect.Slice, reflect.Array: d.dumpSlice(val) case reflect.Map: @@ -207,21 +205,30 @@ func (d *Dumper) dump(val reflect.Value, ignoreDepth ...bool) { case reflect.Pointer: d.dumpPointer(val) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - d.buf.WriteString(__(d.Theme.Number, fmt.Sprint(val))) + d.wrapType(val, __(d.Theme.Number, fmt.Sprint(val))) case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - d.buf.WriteString(__(d.Theme.Number, fmt.Sprint(val))) + d.wrapType(val, __(d.Theme.Number, fmt.Sprint(val))) case reflect.Float32, reflect.Float64: - d.buf.WriteString(__(d.Theme.Number, fmt.Sprint(val))) + d.wrapType(val, __(d.Theme.Number, fmt.Sprint(val))) case reflect.Complex64, reflect.Complex128: - d.buf.WriteString(__(d.Theme.Number, fmt.Sprint(val))) + d.wrapType(val, __(d.Theme.Number, fmt.Sprint(val))) case reflect.Uintptr: - d.buf.WriteString(__(d.Theme.Number, fmt.Sprintf("0x%x", val.Uint()))) + d.wrapType(val, __(d.Theme.Number, fmt.Sprintf("0x%x", val.Uint()))) case reflect.Invalid: d.buf.WriteString(__(d.Theme.Nil, "nil")) case reflect.Interface: d.dump(val.Elem(), true) case reflect.UnsafePointer: - d.buf.WriteString(__(d.Theme.UnsafePointer, fmt.Sprintf("unsafe.Pointer(0x%x)", uintptr(val.UnsafePointer())))) + if t := val.Type(); t.PkgPath() != "" { + d.buf.WriteString( + __(d.Theme.Types, t.String()) + + __(d.Theme.Braces, "(") + + __(d.Theme.UnsafePointer, fmt.Sprintf("0x%x", uintptr(val.UnsafePointer()))) + + __(d.Theme.Braces, ")"), + ) + } else { + d.buf.WriteString(__(d.Theme.UnsafePointer, fmt.Sprintf("unsafe.Pointer(0x%x)", uintptr(val.UnsafePointer())))) + } } } @@ -374,3 +381,11 @@ func isPrimitive(val reflect.Value) bool { } } } + +func (d *Dumper) wrapType(v reflect.Value, str string) { + if t := v.Type(); t.PkgPath() != "" { + str = __(d.Theme.Types, t.String()) + __(d.Theme.Braces, "(") + str + __(d.Theme.Braces, ")") + } + + d.buf.WriteString(str) +} diff --git a/testdata/primitives.txt b/testdata/primitives.txt index 964e71f..235919d 100644 --- a/testdata/primitives.txt +++ b/testdata/primitives.txt @@ -35,24 +35,24 @@ godump_test.Node { Bool2Ptr: &false, StringPtr: &"foo bar", UintptrPtr: &0x499602d2, - TypedInt: 123, - TypedInt8: -45, - TypedInt16: 6789, - TypedInt32: -987, - TypedInt64: 3849876543247876432, - TypedUint: 837, - TypedUint8: 38, - TypedUint16: 3847, - TypedUint32: 9843, - TypedUint64: 2834, - TypedFloat32: 123.475, - TypedFloat64: -12345.09876, - TypedComplex64: (0+12.987i), - TypedComplex128: (0-473i), - TypedBool1: true, - TypedBool2: false, - TypedString: "foo bar", - TypedUintptr: 0x499602d2, + TypedInt: godump_test.IntType(123), + TypedInt8: godump_test.Int8Type(-45), + TypedInt16: godump_test.Int16Type(6789), + TypedInt32: godump_test.Int32Type(-987), + TypedInt64: godump_test.Int64Type(3849876543247876432), + TypedUint: godump_test.UintType(837), + TypedUint8: godump_test.Uint8Type(38), + TypedUint16: godump_test.Uint16Type(3847), + TypedUint32: godump_test.Uint32Type(9843), + TypedUint64: godump_test.Uint64Type(2834), + TypedFloat32: godump_test.Float32Type(123.475), + TypedFloat64: godump_test.Float64Type(-12345.09876), + TypedComplex64: godump_test.Complex64Type((0+12.987i)), + TypedComplex128: godump_test.Complex128Type((0-473i)), + TypedBool1: godump_test.Bool1Type(true), + TypedBool2: godump_test.Bool2Type(false), + TypedString: godump_test.StringType("foo bar"), + TypedUintptr: godump_test.UintptrType(0x499602d2), TypedIntPtr: &123, TypedInt8Ptr: &-45, TypedInt16Ptr: &6789, @@ -71,24 +71,24 @@ godump_test.Node { TypedBool2Ptr: &false, TypedStringPtr: &"foo bar", TypedUintptrPtr: &0x499602d2, - PtrTypedInt: &123, - PtrTypedInt8: &-45, - PtrTypedInt16: &6789, - PtrTypedInt32: &-987, - PtrTypedInt64: &3849876543247876432, - PtrTypedUint: &837, - PtrTypedUint8: &38, - PtrTypedUint16: &3847, - PtrTypedUint32: &9843, - PtrTypedUint64: &2834, - PtrTypedFloat32: &123.475, - PtrTypedFloat64: &-12345.09876, - PtrTypedComplex64: &(0+12.987i), - PtrTypedComplex128: &(0-473i), - PtrTypedBool1: &true, - PtrTypedBool2: &false, - PtrTypedString: &"foo bar", - PtrTypedUintptr: &0x499602d2, + PtrTypedInt: &godump_test.IntType(123), + PtrTypedInt8: &godump_test.Int8Type(-45), + PtrTypedInt16: &godump_test.Int16Type(6789), + PtrTypedInt32: &godump_test.Int32Type(-987), + PtrTypedInt64: &godump_test.Int64Type(3849876543247876432), + PtrTypedUint: &godump_test.UintType(837), + PtrTypedUint8: &godump_test.Uint8Type(38), + PtrTypedUint16: &godump_test.Uint16Type(3847), + PtrTypedUint32: &godump_test.Uint32Type(9843), + PtrTypedUint64: &godump_test.Uint64Type(2834), + PtrTypedFloat32: &godump_test.Float32Type(123.475), + PtrTypedFloat64: &godump_test.Float64Type(-12345.09876), + PtrTypedComplex64: &godump_test.Complex64Type((0+12.987i)), + PtrTypedComplex128: &godump_test.Complex128Type((0-473i)), + PtrTypedBool1: &godump_test.Bool1Type(true), + PtrTypedBool2: &godump_test.Bool2Type(false), + PtrTypedString: &godump_test.StringType("foo bar"), + PtrTypedUintptr: &godump_test.UintptrType(0x499602d2), Nil: nil, Func: func(), Func2: func(int) float64, diff --git a/testdata/private-structs.txt b/testdata/private-structs.txt index 05166d2..628e2b5 100644 --- a/testdata/private-structs.txt +++ b/testdata/private-structs.txt @@ -3,19 +3,19 @@ godump_test.node { field1: struct { x: 123, y: 123.456, - z: 987, + z: godump_test.number(987), }, field2: godump_test.child { field1: godump_test.child1 { x: 12344, y: 578, - z: 9876543, + z: godump_test.number(9876543), }, field2: &godump_test.child {#1 field1: godump_test.child1 { x: 12344, y: 578, - z: 9876543, + z: godump_test.number(9876543), }, field2: &@1, }, @@ -25,13 +25,13 @@ godump_test.node { field1: godump_test.child1 { x: 0, y: 0, - z: 0, + z: godump_test.number(0), }, field2: &godump_test.child {#2 field1: godump_test.child1 { x: 12344, y: 578, - z: 9876543, + z: godump_test.number(9876543), }, field2: &@1, }, @@ -42,13 +42,13 @@ godump_test.node { field1: struct { x: 123, y: 123.456, - z: 987, + z: godump_test.number(987), }, field2: godump_test.child { field1: godump_test.child1 { x: 12344, y: 578, - z: 9876543, + z: godump_test.number(9876543), }, field2: &@1, }, @@ -57,7 +57,7 @@ godump_test.node { field1: godump_test.child1 { x: 0, y: 0, - z: 0, + z: godump_test.number(0), }, field2: &@2, }, diff --git a/testdata/structs.txt b/testdata/structs.txt index 0c0083e..8f54b41 100644 --- a/testdata/structs.txt +++ b/testdata/structs.txt @@ -3,19 +3,19 @@ godump_test.Node { Field1: struct { X: 123, Y: 123.456, - Z: 987, + Z: godump_test.Number(987), }, Field2: godump_test.Child { Field1: godump_test.Child1 { X: 12344, Y: 578, - Z: 9876543, + Z: godump_test.Number(9876543), }, Field2: &godump_test.Child {#1 Field1: godump_test.Child1 { X: 12344, Y: 578, - Z: 9876543, + Z: godump_test.Number(9876543), }, Field2: &@1, }, @@ -25,13 +25,13 @@ godump_test.Node { Field1: godump_test.Child1 { X: 0, Y: 0, - Z: 0, + Z: godump_test.Number(0), }, Field2: &godump_test.Child {#2 Field1: godump_test.Child1 { X: 12344, Y: 578, - Z: 9876543, + Z: godump_test.Number(9876543), }, Field2: &@1, }, @@ -43,13 +43,13 @@ godump_test.Node { Field1: struct { X: 123, Y: 123.456, - Z: 987, + Z: godump_test.Number(987), }, Field2: godump_test.Child { Field1: godump_test.Child1 { X: 12344, Y: 578, - Z: 9876543, + Z: godump_test.Number(9876543), }, Field2: &@1, }, @@ -58,7 +58,7 @@ godump_test.Node { Field1: godump_test.Child1 { X: 0, Y: 0, - Z: 0, + Z: godump_test.Number(0), }, Field2: &@2, }, From 343df3aad631f0d39fd4afa0fe339d275e455dd8 Mon Sep 17 00:00:00 2001 From: yassinebenaid <yassinebenaide3@gmail.com> Date: Tue, 27 Aug 2024 21:06:12 +0100 Subject: [PATCH 2/5] feat: added option to dump primitive named types --- dumper.go | 9 ++++- dumper_test.go | 10 +++-- testdata/primitives.txt | 73 ++++++++++++++++++------------------ testdata/private-structs.txt | 16 ++++---- testdata/structs.txt | 16 ++++---- 5 files changed, 67 insertions(+), 57 deletions(-) diff --git a/dumper.go b/dumper.go index bfa9298..e78e21b 100644 --- a/dumper.go +++ b/dumper.go @@ -105,6 +105,9 @@ type Dumper struct { // The default value is a string of three spaces. Indentation string + // ShowPrimitiveNamedTypes determines whether to show primitive named types. + ShowPrimitiveNamedTypes bool + // HidePrivateFields allows you to optionally hide struct's unexported fields from being printed. HidePrivateFields bool @@ -383,8 +386,10 @@ func isPrimitive(val reflect.Value) bool { } func (d *Dumper) wrapType(v reflect.Value, str string) { - if t := v.Type(); t.PkgPath() != "" { - str = __(d.Theme.Types, t.String()) + __(d.Theme.Braces, "(") + str + __(d.Theme.Braces, ")") + if d.ShowPrimitiveNamedTypes { + if t := v.Type(); t.PkgPath() != "" { + str = __(d.Theme.Types, t.String()) + __(d.Theme.Braces, "(") + str + __(d.Theme.Braces, ")") + } } d.buf.WriteString(str) diff --git a/dumper_test.go b/dumper_test.go index 5673d43..af09549 100644 --- a/dumper_test.go +++ b/dumper_test.go @@ -58,6 +58,8 @@ func TestCanDumpPrimitives(t *testing.T) { type Chan1Type <-chan struct{} type Chan2Type chan<- struct{} + type UnsafePointer unsafe.Pointer + type Node struct { Int int Int8 int8 @@ -199,8 +201,9 @@ func TestCanDumpPrimitives(t *testing.T) { BufferedChan chan struct{} - UnsafePointer1 unsafe.Pointer - UnsafePointer2 *unsafe.Pointer + UnsafePointer1 unsafe.Pointer + UnsafePointer2 *unsafe.Pointer + NamedUnsafePointer UnsafePointer } node := Node{ @@ -246,7 +249,8 @@ func TestCanDumpPrimitives(t *testing.T) { Nil: nil, - UnsafePointer1: nil, + UnsafePointer1: nil, + NamedUnsafePointer: nil, BufferedChan: make(chan struct{}, 255), } diff --git a/testdata/primitives.txt b/testdata/primitives.txt index 235919d..fc9cb75 100644 --- a/testdata/primitives.txt +++ b/testdata/primitives.txt @@ -35,24 +35,24 @@ godump_test.Node { Bool2Ptr: &false, StringPtr: &"foo bar", UintptrPtr: &0x499602d2, - TypedInt: godump_test.IntType(123), - TypedInt8: godump_test.Int8Type(-45), - TypedInt16: godump_test.Int16Type(6789), - TypedInt32: godump_test.Int32Type(-987), - TypedInt64: godump_test.Int64Type(3849876543247876432), - TypedUint: godump_test.UintType(837), - TypedUint8: godump_test.Uint8Type(38), - TypedUint16: godump_test.Uint16Type(3847), - TypedUint32: godump_test.Uint32Type(9843), - TypedUint64: godump_test.Uint64Type(2834), - TypedFloat32: godump_test.Float32Type(123.475), - TypedFloat64: godump_test.Float64Type(-12345.09876), - TypedComplex64: godump_test.Complex64Type((0+12.987i)), - TypedComplex128: godump_test.Complex128Type((0-473i)), - TypedBool1: godump_test.Bool1Type(true), - TypedBool2: godump_test.Bool2Type(false), - TypedString: godump_test.StringType("foo bar"), - TypedUintptr: godump_test.UintptrType(0x499602d2), + TypedInt: 123, + TypedInt8: -45, + TypedInt16: 6789, + TypedInt32: -987, + TypedInt64: 3849876543247876432, + TypedUint: 837, + TypedUint8: 38, + TypedUint16: 3847, + TypedUint32: 9843, + TypedUint64: 2834, + TypedFloat32: 123.475, + TypedFloat64: -12345.09876, + TypedComplex64: (0+12.987i), + TypedComplex128: (0-473i), + TypedBool1: true, + TypedBool2: false, + TypedString: "foo bar", + TypedUintptr: 0x499602d2, TypedIntPtr: &123, TypedInt8Ptr: &-45, TypedInt16Ptr: &6789, @@ -71,24 +71,24 @@ godump_test.Node { TypedBool2Ptr: &false, TypedStringPtr: &"foo bar", TypedUintptrPtr: &0x499602d2, - PtrTypedInt: &godump_test.IntType(123), - PtrTypedInt8: &godump_test.Int8Type(-45), - PtrTypedInt16: &godump_test.Int16Type(6789), - PtrTypedInt32: &godump_test.Int32Type(-987), - PtrTypedInt64: &godump_test.Int64Type(3849876543247876432), - PtrTypedUint: &godump_test.UintType(837), - PtrTypedUint8: &godump_test.Uint8Type(38), - PtrTypedUint16: &godump_test.Uint16Type(3847), - PtrTypedUint32: &godump_test.Uint32Type(9843), - PtrTypedUint64: &godump_test.Uint64Type(2834), - PtrTypedFloat32: &godump_test.Float32Type(123.475), - PtrTypedFloat64: &godump_test.Float64Type(-12345.09876), - PtrTypedComplex64: &godump_test.Complex64Type((0+12.987i)), - PtrTypedComplex128: &godump_test.Complex128Type((0-473i)), - PtrTypedBool1: &godump_test.Bool1Type(true), - PtrTypedBool2: &godump_test.Bool2Type(false), - PtrTypedString: &godump_test.StringType("foo bar"), - PtrTypedUintptr: &godump_test.UintptrType(0x499602d2), + PtrTypedInt: &123, + PtrTypedInt8: &-45, + PtrTypedInt16: &6789, + PtrTypedInt32: &-987, + PtrTypedInt64: &3849876543247876432, + PtrTypedUint: &837, + PtrTypedUint8: &38, + PtrTypedUint16: &3847, + PtrTypedUint32: &9843, + PtrTypedUint64: &2834, + PtrTypedFloat32: &123.475, + PtrTypedFloat64: &-12345.09876, + PtrTypedComplex64: &(0+12.987i), + PtrTypedComplex128: &(0-473i), + PtrTypedBool1: &true, + PtrTypedBool2: &false, + PtrTypedString: &"foo bar", + PtrTypedUintptr: &0x499602d2, Nil: nil, Func: func(), Func2: func(int) float64, @@ -121,4 +121,5 @@ godump_test.Node { BufferedChan: chan struct {}<255>, UnsafePointer1: unsafe.Pointer(0x0), UnsafePointer2: &unsafe.Pointer(0x7b), + NamedUnsafePointer: godump_test.UnsafePointer(0x0), } \ No newline at end of file diff --git a/testdata/private-structs.txt b/testdata/private-structs.txt index 628e2b5..05166d2 100644 --- a/testdata/private-structs.txt +++ b/testdata/private-structs.txt @@ -3,19 +3,19 @@ godump_test.node { field1: struct { x: 123, y: 123.456, - z: godump_test.number(987), + z: 987, }, field2: godump_test.child { field1: godump_test.child1 { x: 12344, y: 578, - z: godump_test.number(9876543), + z: 9876543, }, field2: &godump_test.child {#1 field1: godump_test.child1 { x: 12344, y: 578, - z: godump_test.number(9876543), + z: 9876543, }, field2: &@1, }, @@ -25,13 +25,13 @@ godump_test.node { field1: godump_test.child1 { x: 0, y: 0, - z: godump_test.number(0), + z: 0, }, field2: &godump_test.child {#2 field1: godump_test.child1 { x: 12344, y: 578, - z: godump_test.number(9876543), + z: 9876543, }, field2: &@1, }, @@ -42,13 +42,13 @@ godump_test.node { field1: struct { x: 123, y: 123.456, - z: godump_test.number(987), + z: 987, }, field2: godump_test.child { field1: godump_test.child1 { x: 12344, y: 578, - z: godump_test.number(9876543), + z: 9876543, }, field2: &@1, }, @@ -57,7 +57,7 @@ godump_test.node { field1: godump_test.child1 { x: 0, y: 0, - z: godump_test.number(0), + z: 0, }, field2: &@2, }, diff --git a/testdata/structs.txt b/testdata/structs.txt index 8f54b41..0c0083e 100644 --- a/testdata/structs.txt +++ b/testdata/structs.txt @@ -3,19 +3,19 @@ godump_test.Node { Field1: struct { X: 123, Y: 123.456, - Z: godump_test.Number(987), + Z: 987, }, Field2: godump_test.Child { Field1: godump_test.Child1 { X: 12344, Y: 578, - Z: godump_test.Number(9876543), + Z: 9876543, }, Field2: &godump_test.Child {#1 Field1: godump_test.Child1 { X: 12344, Y: 578, - Z: godump_test.Number(9876543), + Z: 9876543, }, Field2: &@1, }, @@ -25,13 +25,13 @@ godump_test.Node { Field1: godump_test.Child1 { X: 0, Y: 0, - Z: godump_test.Number(0), + Z: 0, }, Field2: &godump_test.Child {#2 Field1: godump_test.Child1 { X: 12344, Y: 578, - Z: godump_test.Number(9876543), + Z: 9876543, }, Field2: &@1, }, @@ -43,13 +43,13 @@ godump_test.Node { Field1: struct { X: 123, Y: 123.456, - Z: godump_test.Number(987), + Z: 987, }, Field2: godump_test.Child { Field1: godump_test.Child1 { X: 12344, Y: 578, - Z: godump_test.Number(9876543), + Z: 9876543, }, Field2: &@1, }, @@ -58,7 +58,7 @@ godump_test.Node { Field1: godump_test.Child1 { X: 0, Y: 0, - Z: godump_test.Number(0), + Z: 0, }, Field2: &@2, }, From 1bd0468bf9b387b5b82117fb0f2d4af8198e1295 Mon Sep 17 00:00:00 2001 From: yassinebenaid <yassinebenaide3@gmail.com> Date: Tue, 27 Aug 2024 21:15:04 +0100 Subject: [PATCH 3/5] feat: added named types tests --- dumper_test.go | 111 ++++++++++++++++++++++++++++++++++ testdata/named-primitives.txt | 38 ++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 testdata/named-primitives.txt diff --git a/dumper_test.go b/dumper_test.go index af09549..0e30b2c 100644 --- a/dumper_test.go +++ b/dumper_test.go @@ -350,6 +350,117 @@ func TestCanDumpPrimitives(t *testing.T) { checkFromFeed(t, []byte(result), "./testdata/primitives.txt") } +func TestCanDumpNamedPrimitives(t *testing.T) { + type IntType int + type Int8Type int8 + type Int16Type int16 + type Int32Type int32 + type Int64Type int64 + type UintType uint + type Uint8Type uint8 + type Uint16Type uint16 + type Uint32Type uint32 + type Uint64Type uint64 + type Float32Type float32 + type Float64Type float64 + type Complex64Type complex64 + type Complex128Type complex128 + type Bool1Type bool + type Bool2Type bool + type StringType string + type UintptrType uintptr + + type Node struct { + Int IntType + Int8 Int8Type + Int16 Int16Type + Int32 Int32Type + Int64 Int64Type + Uint UintType + Uint8 Uint8Type + Uint16 Uint16Type + Uint32 Uint32Type + Uint64 Uint64Type + Float32 Float32Type + Float64 Float64Type + Complex64 Complex64Type + Complex128 Complex128Type + Bool1 Bool1Type + Bool2 Bool2Type + String StringType + + Uintptr UintptrType + + PtrInt *IntType + PtrInt8 *Int8Type + PtrInt16 *Int16Type + PtrInt32 *Int32Type + PtrInt64 *Int64Type + PtrUint *UintType + PtrUint8 *Uint8Type + PtrUint16 *Uint16Type + PtrUint32 *Uint32Type + PtrUint64 *Uint64Type + PtrFloat32 *Float32Type + PtrFloat64 *Float64Type + PtrComplex64 *Complex64Type + PtrComplex128 *Complex128Type + PtrBool1 *Bool1Type + PtrBool2 *Bool2Type + PtrString *StringType + + PtrUintptr *UintptrType + } + + node := Node{ + Int: IntType(123), + Int8: Int8Type(-45), + Int16: Int16Type(6789), + Int32: Int32Type(-987), + Int64: Int64Type(3849876543247876432), + Uint: UintType(837), + Uint8: Uint8Type(38), + Uint16: Uint16Type(3847), + Uint32: Uint32Type(9843), + Uint64: Uint64Type(2834), + Float32: Float32Type(123.475), + Float64: Float64Type(-12345.09876), + Complex64: Complex64Type(12.987i), + Complex128: Complex128Type(-473i), + Bool1: Bool1Type(true), + Bool2: Bool2Type(false), + String: StringType("foo bar"), + + Uintptr: UintptrType(1234567890), + } + + node.PtrInt = &node.Int + node.PtrInt8 = &node.Int8 + node.PtrInt16 = &node.Int16 + node.PtrInt32 = &node.Int32 + node.PtrInt64 = &node.Int64 + node.PtrUint = &node.Uint + node.PtrUint8 = &node.Uint8 + node.PtrUint16 = &node.Uint16 + node.PtrUint32 = &node.Uint32 + node.PtrUint64 = &node.Uint64 + node.PtrFloat32 = &node.Float32 + node.PtrFloat64 = &node.Float64 + node.PtrComplex64 = &node.Complex64 + node.PtrComplex128 = &node.Complex128 + node.PtrBool1 = &node.Bool1 + node.PtrBool2 = &node.Bool2 + node.PtrString = &node.String + + node.PtrUintptr = &node.Uintptr + + var d godump.Dumper + d.ShowPrimitiveNamedTypes = true + result := d.Sprint(node) + + checkFromFeed(t, []byte(result), "./testdata/named-primitives.txt") +} + func TestCanDumpStructs(t *testing.T) { type Number int diff --git a/testdata/named-primitives.txt b/testdata/named-primitives.txt new file mode 100644 index 0000000..1710f19 --- /dev/null +++ b/testdata/named-primitives.txt @@ -0,0 +1,38 @@ +godump_test.Node { + Int: godump_test.IntType(123), + Int8: godump_test.Int8Type(-45), + Int16: godump_test.Int16Type(6789), + Int32: godump_test.Int32Type(-987), + Int64: godump_test.Int64Type(3849876543247876432), + Uint: godump_test.UintType(837), + Uint8: godump_test.Uint8Type(38), + Uint16: godump_test.Uint16Type(3847), + Uint32: godump_test.Uint32Type(9843), + Uint64: godump_test.Uint64Type(2834), + Float32: godump_test.Float32Type(123.475), + Float64: godump_test.Float64Type(-12345.09876), + Complex64: godump_test.Complex64Type((0+12.987i)), + Complex128: godump_test.Complex128Type((0-473i)), + Bool1: godump_test.Bool1Type(true), + Bool2: godump_test.Bool2Type(false), + String: godump_test.StringType("foo bar"), + Uintptr: godump_test.UintptrType(0x499602d2), + PtrInt: &godump_test.IntType(123), + PtrInt8: &godump_test.Int8Type(-45), + PtrInt16: &godump_test.Int16Type(6789), + PtrInt32: &godump_test.Int32Type(-987), + PtrInt64: &godump_test.Int64Type(3849876543247876432), + PtrUint: &godump_test.UintType(837), + PtrUint8: &godump_test.Uint8Type(38), + PtrUint16: &godump_test.Uint16Type(3847), + PtrUint32: &godump_test.Uint32Type(9843), + PtrUint64: &godump_test.Uint64Type(2834), + PtrFloat32: &godump_test.Float32Type(123.475), + PtrFloat64: &godump_test.Float64Type(-12345.09876), + PtrComplex64: &godump_test.Complex64Type((0+12.987i)), + PtrComplex128: &godump_test.Complex128Type((0-473i)), + PtrBool1: &godump_test.Bool1Type(true), + PtrBool2: &godump_test.Bool2Type(false), + PtrString: &godump_test.StringType("foo bar"), + PtrUintptr: &godump_test.UintptrType(0x499602d2), +} \ No newline at end of file From 35c3e2a952ef9b290201e30f6b7cf280d6fd0f91 Mon Sep 17 00:00:00 2001 From: Yassine Benaid <yassinebenaide3@gmail.com> Date: Wed, 28 Aug 2024 09:23:17 +0100 Subject: [PATCH 4/5] Remove Unnecessary boilerplate --- dumper.go | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/dumper.go b/dumper.go index e78e21b..1d411c0 100644 --- a/dumper.go +++ b/dumper.go @@ -222,16 +222,12 @@ func (d *Dumper) dump(val reflect.Value, ignoreDepth ...bool) { case reflect.Interface: d.dump(val.Elem(), true) case reflect.UnsafePointer: - if t := val.Type(); t.PkgPath() != "" { - d.buf.WriteString( - __(d.Theme.Types, t.String()) + - __(d.Theme.Braces, "(") + - __(d.Theme.UnsafePointer, fmt.Sprintf("0x%x", uintptr(val.UnsafePointer()))) + - __(d.Theme.Braces, ")"), - ) - } else { - d.buf.WriteString(__(d.Theme.UnsafePointer, fmt.Sprintf("unsafe.Pointer(0x%x)", uintptr(val.UnsafePointer())))) - } + d.buf.WriteString( + __(d.Theme.Types, val.Type().String()) + + __(d.Theme.Braces, "(") + + __(d.Theme.UnsafePointer, fmt.Sprintf("0x%x", uintptr(val.UnsafePointer()))) + + __(d.Theme.Braces, ")"), + ) } } From 2e7584099b429c2698196259fa100358c6beb8ba Mon Sep 17 00:00:00 2001 From: Yassine Benaid <yassinebenaide3@gmail.com> Date: Wed, 28 Aug 2024 09:28:08 +0100 Subject: [PATCH 5/5] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4845779..2ade0c1 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,7 @@ func main() { var d = godump.Dumper{ Indentation: " ", HidePrivateFields: false, + ShowPrimitiveNamedTypes = false Theme: godump.Theme{ String: godump.RGB{R: 138, G: 201, B: 38}, // ...