From 0880c4e00e5699725fc42d329b2a9b5d72c59a2a Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Tue, 19 Sep 2023 14:55:28 -0700 Subject: [PATCH 1/3] treat non-nil interface fields as nonzero Fixes kr/pretty#97. --- formatter_test.go | 9 +++++++++ zero.go | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/formatter_test.go b/formatter_test.go index 6e27b27..cb7d977 100644 --- a/formatter_test.go +++ b/formatter_test.go @@ -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`}, {"", `""`}, @@ -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 { diff --git a/zero.go b/zero.go index abb5b6f..b8cb18f 100644 --- a/zero.go +++ b/zero.go @@ -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 } } From a17193eda441c3f5e366900d71585d0c2bc4f79e Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Mon, 22 Apr 2024 11:41:13 -0700 Subject: [PATCH 2/3] fix go vet and go test -race issues --- diff_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/diff_test.go b/diff_test.go index 08ec64b..6a0af24 100644 --- a/diff_test.go +++ b/diff_test.go @@ -117,14 +117,14 @@ var diffs = []difftest{ {struct{ x N }{N{0}}, struct{ x N }{N{0}}, nil}, {struct{ x N }{N{0}}, struct{ x N }{N{1}}, []string{`x.N: 0 != 1`}}, { - struct{ x unsafe.Pointer }{unsafe.Pointer(uintptr(0))}, - struct{ x unsafe.Pointer }{unsafe.Pointer(uintptr(0))}, + struct{ x unsafe.Pointer }{unsafe.Pointer(nil)}, + struct{ x unsafe.Pointer }{unsafe.Pointer(nil)}, nil, }, { - struct{ x unsafe.Pointer }{unsafe.Pointer(uintptr(0))}, - struct{ x unsafe.Pointer }{unsafe.Pointer(uintptr(1))}, - []string{`x: 0x0 != 0x1`}, + struct{ x unsafe.Pointer }{unsafe.Pointer(nil)}, + struct{ x unsafe.Pointer }{unsafe.Pointer(&i0)}, + []string{fmt.Sprintf("x: %p != %p", unsafe.Pointer(nil), unsafe.Pointer(&i0))}, }, } From e3fc7545b45eda0498cb341564a683718d67b788 Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Mon, 22 Apr 2024 11:41:30 -0700 Subject: [PATCH 3/3] enable tests under TinyGo --- formatter_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/formatter_test.go b/formatter_test.go index cb7d977..77c273d 100644 --- a/formatter_test.go +++ b/formatter_test.go @@ -3,6 +3,7 @@ package pretty import ( "fmt" "io" + "runtime" "strings" "testing" "time" @@ -224,6 +225,9 @@ func (g *PanicGoString) GoString() string { } func TestGoSyntax(t *testing.T) { + if runtime.Compiler == "tinygo" { + return + } for _, tt := range gosyntax { s := fmt.Sprintf("%# v", Formatter(tt.v)) if tt.s != s {