Skip to content

Commit

Permalink
treat non-nil interface fields as nonzero
Browse files Browse the repository at this point in the history
Fixes kr#97.
  • Loading branch information
ydnar committed Sep 19, 2023
1 parent 3cd153a commit 0880c4e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 9 additions & 0 deletions formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func (s StructWithPrivateFields) GoString() string {
return fmt.Sprintf("NewStructWithPrivateFields(%q)", s.A)
}

type StructWithInterfaceField struct {
V interface{}
}

var gosyntax = []test{
{nil, `nil`},
{"", `""`},
Expand Down Expand Up @@ -184,6 +188,11 @@ var gosyntax = []test{
{&PointerGoString{"pgs"}, `PGS pgs`},
{(*PointerGoString)(nil), "(*pretty.PointerGoString)(nil)"},
{&PanicGoString{"oops!"}, `(*pretty.PanicGoString)(PANIC=calling method "GoString": oops!)`},
{&StructWithInterfaceField{}, "&pretty.StructWithInterfaceField{}"},
{&StructWithInterfaceField{nil}, "&pretty.StructWithInterfaceField{}"},
{&StructWithInterfaceField{(*struct{})(nil)}, "&pretty.StructWithInterfaceField{\n V: (*struct {})(nil),\n}"},
{&StructWithInterfaceField{""}, "&pretty.StructWithInterfaceField{\n V: \"\",\n}"},
{&StructWithInterfaceField{struct{}{}}, "&pretty.StructWithInterfaceField{\n V: struct {}{},\n}"},
}

type ValueGoString struct {
Expand Down
2 changes: 1 addition & 1 deletion zero.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func nonzero(v reflect.Value) bool {
return v.String() != ""
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
if nonzero(getField(v, i)) {
if nonzero(v.Field(i)) {
return true
}
}
Expand Down

0 comments on commit 0880c4e

Please sign in to comment.