Skip to content

Commit 3630c7d

Browse files
authored
Use github.com/rogpeppe/go-internal/fmtsort for stable map output (#67)
github.com/rogpeppe/go-internal factors out an opinionated selection of internal packages and functionality from the Go standard library. One such package is fmtsort: Package fmtsort provides a general stable ordering mechanism for maps, on behalf of the fmt and text/template packages. It is not guaranteed to be efficient and works only for types that are valid map keys. Use this package to ensure the Formatter output for maps is stable. Fixes #47
1 parent 814ac30 commit 3630c7d

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

formatter.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"text/tabwriter"
99

1010
"github.com/kr/text"
11+
"github.com/rogpeppe/go-internal/fmtsort"
1112
)
1213

1314
type formatter struct {
@@ -123,10 +124,10 @@ func (p *printer) printValue(v reflect.Value, showType, quote bool) {
123124
writeByte(p, '\n')
124125
pp = p.indent()
125126
}
126-
keys := v.MapKeys()
127+
sm := fmtsort.Sort(v)
127128
for i := 0; i < v.Len(); i++ {
128-
k := keys[i]
129-
mv := v.MapIndex(k)
129+
k := sm.Key[i]
130+
mv := sm.Value[i]
130131
pp.printValue(k, false, true)
131132
writeByte(pp, ':')
132133
if expand {

formatter_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (f F) Format(s fmt.State, c rune) {
3838
fmt.Fprintf(s, "F(%d)", int(f))
3939
}
4040

41-
type Stringer struct { i int }
41+
type Stringer struct{ i int }
4242

4343
func (s *Stringer) String() string { return "foo" }
4444

@@ -74,6 +74,7 @@ var gosyntax = []test{
7474
//{make(chan int), "(chan int)(0x1234)"},
7575
{unsafe.Pointer(uintptr(unsafe.Pointer(&long))), fmt.Sprintf("unsafe.Pointer(0x%02x)", uintptr(unsafe.Pointer(&long)))},
7676
{func(int) {}, "func(int) {...}"},
77+
{map[string]string{"a": "a", "b": "b"}, "map[string]string{\"a\":\"a\", \"b\":\"b\"}"},
7778
{map[int]int{1: 1}, "map[int]int{1:1}"},
7879
{int32(1), "int32(1)"},
7980
{io.EOF, `&errors.errorString{s:"EOF"}`},

go.mod

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/kr/pretty
22

33
go 1.12
44

5-
require github.com/kr/text v0.1.0
5+
require (
6+
github.com/kr/text v0.1.0
7+
github.com/rogpeppe/go-internal v1.6.1
8+
)

go.sum

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
12
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
23
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
34
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
5+
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
6+
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
7+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
8+
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=

0 commit comments

Comments
 (0)