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

Change format of zero types #71

Merged
merged 17 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
25 changes: 25 additions & 0 deletions dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,16 @@ func (d *Dumper) dump(val reflect.Value, ignoreDepth ...bool) {
d.dumpMap(val)
case reflect.Func:
d.buf.WriteString(__(d.Theme.Func, val.Type().String()))
if val.IsNil() {
d.buf.WriteString(__(d.Theme.Braces, "(") + __(d.Theme.Nil, "nil") + __(d.Theme.Braces, ")"))
yassinebenaid marked this conversation as resolved.
Show resolved Hide resolved
}
case reflect.Chan:
d.buf.WriteString(__(d.Theme.Chan, val.Type().String()))

if val.IsNil() {
d.buf.WriteString(__(d.Theme.Braces, "(") + __(d.Theme.Nil, "nil") + __(d.Theme.Braces, ")"))
}

if vCap := val.Cap(); vCap > 0 {
d.buf.WriteString(__(d.Theme.Chan, fmt.Sprintf("<%d>", vCap)))
}
Expand Down Expand Up @@ -240,6 +248,12 @@ func (d *Dumper) dumpSlice(v reflect.Value) {
d.ptrTag = 0
}

if v.IsNil() {
d.buf.WriteString(__(d.Theme.Types, v.Type().String()) + __(d.Theme.Braces, "(") +
__(d.Theme.Nil, "nil") + __(d.Theme.Braces, ")"+tag))
return
}

d.buf.WriteString(__(d.Theme.Types, fmt.Sprintf("%s:%d:%d", v.Type(), length, v.Cap())))
d.buf.WriteString(__(d.Theme.Braces, fmt.Sprintf(" {%s", tag)))

Expand Down Expand Up @@ -268,6 +282,12 @@ func (d *Dumper) dumpMap(v reflect.Value) {
d.ptrTag = 0
}

if v.IsNil() {
d.buf.WriteString(__(d.Theme.Types, v.Type().String()) + __(d.Theme.Braces, "(") +
__(d.Theme.Nil, "nil") + __(d.Theme.Braces, ")"+tag))
return
}

d.buf.WriteString(__(d.Theme.Types, fmt.Sprintf("%s:%d", v.Type(), len(keys))))
d.buf.WriteString(__(d.Theme.Braces, fmt.Sprintf(" {%s", tag)))

Expand All @@ -290,6 +310,11 @@ func (d *Dumper) dumpMap(v reflect.Value) {
}

func (d *Dumper) dumpPointer(v reflect.Value) {
if v.IsNil() {
d.buf.WriteString(__(d.Theme.Types, v.Type().String()) + __(d.Theme.Braces, "(") + __(d.Theme.Nil, "nil") + __(d.Theme.Braces, ")"))
return
}

elem := v.Elem()

if isPrimitive(elem) {
Expand Down
26 changes: 23 additions & 3 deletions dumper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func TestCanDumpPrimitives(t *testing.T) {

PtrTypedUintptr *UintptrType

Nil *any
NilPointer *int

Func func()
Func2 func(int) float64
Expand All @@ -183,6 +183,8 @@ func TestCanDumpPrimitives(t *testing.T) {
PtrTypedFunc3 *Func3Type
PtrTypedFunc4 *Func4Type

NilFunc func()

Chan chan struct{}
Chan1 <-chan struct{}
Chan2 chan<- struct{}
Expand All @@ -200,6 +202,7 @@ func TestCanDumpPrimitives(t *testing.T) {
PtrTypedChan2 *Chan2Type

BufferedChan chan struct{}
NilChan chan struct{}

UnsafePointer1 unsafe.Pointer
UnsafePointer2 *unsafe.Pointer
Expand Down Expand Up @@ -247,11 +250,12 @@ func TestCanDumpPrimitives(t *testing.T) {

TypedUintptr: UintptrType(1234567890),

Nil: nil,

UnsafePointer1: nil,
NamedUnsafePointer: nil,

Chan: make(chan struct{}),
Chan1: make(chan struct{}),
Chan2: make(chan struct{}),
BufferedChan: make(chan struct{}, 255),
}

Expand Down Expand Up @@ -315,6 +319,16 @@ func TestCanDumpPrimitives(t *testing.T) {

node.PtrTypedUintptr = &node.TypedUintptr

node.Func = func() {}
node.Func2 = func(int) float64 { return 0 }
node.Func3 = func(...*any) any { return nil }
node.Func4 = func(byte, ...[]*complex128) bool { return false }

node.TypedFunc = func() {}
node.TypedFunc2 = func(int) float64 { return 0 }
node.TypedFunc3 = func(...*any) any { return nil }
node.TypedFunc4 = func(byte, ...[]*complex128) bool { return false }

node.FuncPtr = &node.Func
node.Func2Ptr = &node.Func2
node.Func3Ptr = &node.Func3
Expand Down Expand Up @@ -714,6 +728,8 @@ func TestCanDumpPrivateStructs(t *testing.T) {
func TestCanDumpSlices(t *testing.T) {
type Slice []any

var nilSLice []Slice
yassinebenaid marked this conversation as resolved.
Show resolved Hide resolved

foo := "foo"
bar := "bar"
baz := "baz"
Expand All @@ -735,6 +751,7 @@ func TestCanDumpSlices(t *testing.T) {
false,
},
make([]any, 3, 8),
nilSLice,
}
s = append(s, &s)

Expand All @@ -748,6 +765,8 @@ func TestCanDumpMaps(t *testing.T) {
type SomeMap map[*SomeMap]*SomeMap
sm := &SomeMap{}

var nilMap SomeMap

m := map[any]any{12: 34}
maps := []any{
make(map[string]string),
Expand All @@ -760,6 +779,7 @@ func TestCanDumpMaps(t *testing.T) {
SomeMap{
&SomeMap{}: &SomeMap{sm: sm},
},
nilMap,
}

var d godump.Dumper
Expand Down
3 changes: 2 additions & 1 deletion testdata/maps.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[]interface {}:4:4 {
[]interface {}:5:5 {
map[string]string:0 {},
map[interface {}]int:1 {
&map[interface {}]interface {}:1 {#1
Expand All @@ -13,4 +13,5 @@
&godump_test.SomeMap:0 {#4}: &@4,
},
},
godump_test.SomeMap(nil),
}
4 changes: 3 additions & 1 deletion testdata/primitives.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ godump_test.Node {
PtrTypedBool2: &false,
PtrTypedString: &"foo bar",
PtrTypedUintptr: &0x499602d2,
Nil: nil,
NilPointer: *int(nil),
Func: func(),
Func2: func(int) float64,
Func3: func(...*interface {}) interface {},
Expand All @@ -106,6 +106,7 @@ godump_test.Node {
PtrTypedFunc2: &godump_test.Func2Type,
PtrTypedFunc3: &godump_test.Func3Type,
PtrTypedFunc4: &godump_test.Func4Type,
NilFunc: func()(nil),
Chan: chan struct {},
Chan1: <-chan struct {},
Chan2: chan<- struct {},
Expand All @@ -119,6 +120,7 @@ godump_test.Node {
PtrTypedChan1: &godump_test.Chan1Type,
PtrTypedChan2: &godump_test.Chan2Type,
BufferedChan: chan struct {}<255>,
NilChan: chan struct {}(nil),
UnsafePointer1: unsafe.Pointer(0x0),
UnsafePointer2: &unsafe.Pointer(0x7b),
NamedUnsafePointer: godump_test.UnsafePointer(0x0),
Expand Down
6 changes: 4 additions & 2 deletions testdata/slices.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
godump_test.Slice:10:18 {
godump_test.Slice:11:20 {
1,
2.3,
true,
Expand All @@ -19,7 +19,8 @@ godump_test.Slice:10:18 {
nil,
nil,
},
&godump_test.Slice:10:18 {#2
[]godump_test.Slice(nil),
&godump_test.Slice:11:20 {#2
1,
2.3,
true,
Expand All @@ -37,6 +38,7 @@ godump_test.Slice:10:18 {
nil,
nil,
},
[]godump_test.Slice(nil),
&@2,
},
}