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

fix wrong cycle detect by clone the map[visit]int #95

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
steps:
- uses: actions/setup-go@v2
with:
go-version: 1.13.x
go-version: 1.17
- uses: actions/checkout@v3
- name: Build
run: go build .
Expand Down
21 changes: 17 additions & 4 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type formatter struct {
// breaks and tabs. Object f responds to the "%v" formatting verb when both the
// "#" and " " (space) flags are set, for example:
//
// fmt.Sprintf("%# v", Formatter(x))
// fmt.Sprintf("%# v", Formatter(x))
//
// If one of these two flags is not set, or any other verb is used, f will
// format x according to the usual rules of package fmt.
Expand Down Expand Up @@ -69,8 +69,21 @@ type printer struct {
depth int
}

func (p *printer) clone() printer {
visited := make(map[visit]int, len(p.visited))
for k, v := range p.visited {
visited[k] = v
}
return printer{
Writer: p.Writer,
tw: p.tw,
visited: visited,
depth: p.depth,
}
}

func (p *printer) indent() *printer {
q := *p
q := p.clone()
q.tw = tabwriter.NewWriter(p.Writer, 4, 4, 1, ' ', 0)
q.Writer = text.NewIndentWriter(q.tw, []byte{'\t'})
return &q
Expand Down Expand Up @@ -223,7 +236,7 @@ func (p *printer) printValue(v reflect.Value, showType, quote bool) {
case e.Kind() == reflect.Invalid:
io.WriteString(p, "nil")
case e.IsValid():
pp := *p
pp := p.clone()
pp.depth++
pp.printValue(e, showType, true)
default:
Expand Down Expand Up @@ -270,7 +283,7 @@ func (p *printer) printValue(v reflect.Value, showType, quote bool) {
io.WriteString(p, v.Type().String())
io.WriteString(p, ")(nil)")
} else {
pp := *p
pp := p.clone()
pp.depth++
writeByte(pp, '&')
pp.printValue(e, true, true)
Expand Down
59 changes: 59 additions & 0 deletions formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,62 @@ func TestCycle(t *testing.T) {
*iv = *i
t.Logf("Example long interface cycle:\n%# v", Formatter(i))
}

type AValue struct {
ID int
Name string
}

type ComplexValue struct {
AValues []*AValue
Values []interface{}
ByName map[string]interface{}
}

func TestReuseVisitMap(t *testing.T) {
var a = &AValue{ID: 1, Name: "A"}
var c = ComplexValue{
AValues: []*AValue{a},
Values: []interface{}{a},
ByName: map[string]interface{}{
"A": a,
},
}

var s = Sprint(c)
if strings.Contains(s, "CYCLIC") {
t.Error("there should not cycle in ComplexValue ", s)
}
}

type Tree struct {
Left *Tree
Value interface{}
Right *Tree
}

func TestCycleRefer(t *testing.T) {
var tree = &Tree{
Left: nil,
Value: 1,
Right: &Tree{
Left: nil,
Value: 2,
Right: nil,
},
}
var s = Sprint(tree)
if strings.Contains(s, "CYCLIC") {
t.Error("tree should have no cycle in Tree", s)
}

tree.Right.Value = []interface{}{
map[string]interface{}{
"refer": tree,
},
}
var s2 = Sprint(tree)
if !strings.Contains(s2, "CYCLIC") {
t.Error("tree should have no cycle in Tree", s2)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/kr/pretty

go 1.12
go 1.17

require (
github.com/kr/text v0.2.0
Expand Down