Skip to content

Commit

Permalink
Merge pull request #71 from yassinebenaid/change-format-of-zero-types
Browse files Browse the repository at this point in the history
Change format of zero types
  • Loading branch information
yassinebenaid authored Nov 5, 2024
2 parents 840b087 + ef1fad9 commit 2c21843
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 8 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
push:
branches: [master]
pull_request:
types: [opened, reopened]
branches: [master]

permissions: read-all
Expand Down
32 changes: 32 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.writeNil()
}
case reflect.Chan:
d.buf.WriteString(__(d.Theme.Chan, val.Type().String()))

if val.IsNil() {
d.writeNil()
}

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

if v.IsNil() {
d.buf.WriteString(__(d.Theme.Types, v.Type().String()))
d.writeNil()
d.buf.WriteString(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 +283,13 @@ func (d *Dumper) dumpMap(v reflect.Value) {
d.ptrTag = 0
}

if v.IsNil() {
d.buf.WriteString(__(d.Theme.Types, v.Type().String()))
d.writeNil()
d.buf.WriteString(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 +312,12 @@ 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.writeNil()
return
}

elem := v.Elem()

if isPrimitive(elem) {
Expand Down Expand Up @@ -390,3 +418,7 @@ func (d *Dumper) wrapType(v reflect.Value, str string) {

d.buf.WriteString(str)
}

func (d *Dumper) writeNil() {
d.buf.WriteString(__(d.Theme.Braces, "(") + __(d.Theme.Nil, "nil") + __(d.Theme.Braces, ")"))
}
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

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,
},
}

0 comments on commit 2c21843

Please sign in to comment.