Skip to content

Commit

Permalink
Merge Upstream (#3)
Browse files Browse the repository at this point in the history
* Update Dependencies  (kr#75)

This updates github.com/rogpeppe/go-internal from 1.6.1 to 1.8.0
and the github action 'setup-go' from v1 to v2.

It also adds a config file for dependabot to automatically check
this repo for new dependency versions weekly.

* Bump actions/checkout from 2 to 3 (kr#84)

Bumps [actions/checkout](https://github.com/actions/checkout) from 2 to 3.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](actions/checkout@v2...v3)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <[email protected]>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Catch panics when calling GoString like fmt %#v does (kr#87)

This handles a few cases (similar to how fmt %#v does):

- A GoString method on a value receiver, called with a nil pointer
- A GoString method on a pointer receiver that doesn't check for nil
- A GoString method that panics in some other way

Because Go 1.17 added a method Time.GoString with value receiver, this
broke structs that had *time.Time fields with nil values (which is
common!).

Also added a bunch of tests for these cases.

Fixes kr#77

Co-authored-by: Jordan Barrett <[email protected]>

* Bump github.com/rogpeppe/go-internal from 1.8.0 to 1.9.0 (kr#88)

This change bumps github.com/rogpeppe/go-internal from version 1.8.0 to 1.9.0.

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Fix failing test

* bump go version to same as zk, and update workflow

* update go.sum

* update go.sum - again

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: Matthieu MOREL <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Ben Hoyt <[email protected]>
Co-authored-by: Jordan Barrett <[email protected]>
Co-authored-by: tjex <[email protected]>
  • Loading branch information
6 people authored Jan 10, 2024
1 parent 7506f7d commit 7ca3601
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 14 deletions.
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2
updates:
- package-ecosystem: gomod
directory: /
schedule:
interval: weekly
- package-ecosystem: github-actions
directory: /
schedule:
interval: weekly
6 changes: 3 additions & 3 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v1
- uses: actions/setup-go@v2
with:
go-version: 1.13.x
- uses: actions/checkout@v2
go-version: 1.18.x
- uses: actions/checkout@v3
- name: Build
run: go build .
- name: Test
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
_go*
_test*
_obj
/.idea
19 changes: 19 additions & 0 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,24 @@ type visit struct {
typ reflect.Type
}

func (p *printer) catchPanic(v reflect.Value, method string) {
if r := recover(); r != nil {
if v.Kind() == reflect.Ptr && v.IsNil() {
writeByte(p, '(')
io.WriteString(p, v.Type().String())
io.WriteString(p, ")(nil)")
return
}
writeByte(p, '(')
io.WriteString(p, v.Type().String())
io.WriteString(p, ")(PANIC=calling method ")
io.WriteString(p, strconv.Quote(method))
io.WriteString(p, ": ")
fmt.Fprint(p, r)
writeByte(p, ')')
}
}

func (p *printer) printValue(v reflect.Value, showType, quote bool) {
if p.depth > 10 {
io.WriteString(p, "!%v(DEPTH EXCEEDED)")
Expand All @@ -102,6 +120,7 @@ func (p *printer) printValue(v reflect.Value, showType, quote bool) {
if v.IsValid() && v.CanInterface() {
i := v.Interface()
if goStringer, ok := i.(fmt.GoStringer); ok {
defer p.catchPanic(v, "GoString")
io.WriteString(p, goStringer.GoString())
return
}
Expand Down
37 changes: 36 additions & 1 deletion formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ var gosyntax = []test{
`[]string{"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"}`,
},
{F(5), "pretty.F(5)"},
{ NewStructWithPrivateFields("foo"), `NewStructWithPrivateFields("foo")`},
{NewStructWithPrivateFields("foo"), `NewStructWithPrivateFields("foo")`},
{
SA{&T{1, 2}, T{3, 4}},
`pretty.SA{
Expand Down Expand Up @@ -181,6 +181,41 @@ var gosyntax = []test{
time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC),
"time.Date(2009, time.November, 17, 20, 34, 58, 651387237, time.UTC)",
},
{(*time.Time)(nil), "(*time.Time)(nil)"},
{&ValueGoString{"vgs"}, `VGS vgs`},
{(*ValueGoString)(nil), `(*pretty.ValueGoString)(nil)`},
{(*VGSWrapper)(nil), `(*pretty.VGSWrapper)(nil)`},
{&PointerGoString{"pgs"}, `PGS pgs`},
{(*PointerGoString)(nil), "(*pretty.PointerGoString)(nil)"},
{&PanicGoString{"oops!"}, `(*pretty.PanicGoString)(PANIC=calling method "GoString": oops!)`},
}

type ValueGoString struct {
s string
}

func (g ValueGoString) GoString() string {
return "VGS " + g.s
}

type VGSWrapper struct {
ValueGoString
}

type PointerGoString struct {
s string
}

func (g *PointerGoString) GoString() string {
return "PGS " + g.s
}

type PanicGoString struct {
s string
}

func (g *PanicGoString) GoString() string {
panic(g.s)
}

func TestGoSyntax(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module github.com/zk-org/pretty

go 1.12
go 1.18

require (
github.com/kr/text v0.2.0
github.com/rogpeppe/go-internal v1.6.1
github.com/rogpeppe/go-internal v1.9.0
)
10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=

0 comments on commit 7ca3601

Please sign in to comment.