From 15f90ba1f0694f5b9b27358c6ef53ae332a692e9 Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Thu, 20 Aug 2020 15:27:43 +0100 Subject: [PATCH] Use github.com/rogpeppe/go-internal/fmtsort for stable map output 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 --- formatter.go | 7 ++++--- formatter_test.go | 3 ++- go.mod | 5 ++++- go.sum | 5 +++++ 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/formatter.go b/formatter.go index bf4b598..f3718ea 100644 --- a/formatter.go +++ b/formatter.go @@ -8,6 +8,7 @@ import ( "text/tabwriter" "github.com/kr/text" + "github.com/rogpeppe/go-internal/fmtsort" ) type formatter struct { @@ -123,10 +124,10 @@ func (p *printer) printValue(v reflect.Value, showType, quote bool) { writeByte(p, '\n') pp = p.indent() } - keys := v.MapKeys() + sm := fmtsort.Sort(v) for i := 0; i < v.Len(); i++ { - k := keys[i] - mv := v.MapIndex(k) + k := sm.Key[i] + mv := sm.Value[i] pp.printValue(k, false, true) writeByte(pp, ':') if expand { diff --git a/formatter_test.go b/formatter_test.go index c8c0b51..10ce57a 100644 --- a/formatter_test.go +++ b/formatter_test.go @@ -38,7 +38,7 @@ func (f F) Format(s fmt.State, c rune) { fmt.Fprintf(s, "F(%d)", int(f)) } -type Stringer struct { i int } +type Stringer struct{ i int } func (s *Stringer) String() string { return "foo" } @@ -74,6 +74,7 @@ var gosyntax = []test{ //{make(chan int), "(chan int)(0x1234)"}, {unsafe.Pointer(uintptr(unsafe.Pointer(&long))), fmt.Sprintf("unsafe.Pointer(0x%02x)", uintptr(unsafe.Pointer(&long)))}, {func(int) {}, "func(int) {...}"}, + {map[string]string{"a": "a", "b": "b"}, "map[string]string{\"a\":\"a\", \"b\":\"b\"}"}, {map[int]int{1: 1}, "map[int]int{1:1}"}, {int32(1), "int32(1)"}, {io.EOF, `&errors.errorString{s:"EOF"}`}, diff --git a/go.mod b/go.mod index 9a27b6e..579209a 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/kr/pretty go 1.12 -require github.com/kr/text v0.1.0 +require ( + github.com/kr/text v0.1.0 + github.com/rogpeppe/go-internal v1.6.1 +) diff --git a/go.sum b/go.sum index 714f82a..a0e55fc 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,8 @@ +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/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=