From 29f2af11d90ac5c185d80447840bf58274f3014b Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Mon, 29 May 2017 13:48:31 +0100 Subject: [PATCH] introduce mapKeys helper --- report/counters.go | 22 ++++------------------ report/map_helpers.go | 19 ++++++++++--------- report/node_set.go | 30 +++--------------------------- 3 files changed, 17 insertions(+), 54 deletions(-) diff --git a/report/counters.go b/report/counters.go index a110330dd..660c32af4 100644 --- a/report/counters.go +++ b/report/counters.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "reflect" - "sort" "github.com/ugorji/go/codec" "github.com/weaveworks/ps" @@ -87,28 +86,15 @@ func (c Counters) Merge(other Counters) Counters { return Counters{output} } -// ForEach calls f for each k/v pair of counters. Keys are iterated in -// lexicographical order. -func (c Counters) ForEach(f func(key string, val int)) { - if c.psMap != nil { - keys := c.psMap.Keys() - sort.Strings(keys) - for _, key := range keys { - if val, ok := c.psMap.Lookup(key); ok { - f(key, val.(int)) - } - } - } -} - // String serializes Counters into a string. func (c Counters) String() string { buf := bytes.NewBufferString("{") prefix := "" - c.ForEach(func(k string, v int) { - fmt.Fprintf(buf, "%s%s: %d", prefix, k, v) + for _, key := range mapKeys(c.psMap) { + val, _ := c.psMap.Lookup(key) + fmt.Fprintf(buf, "%s%s: %d", prefix, key, val.(int)) prefix = ", " - }) + } fmt.Fprintf(buf, "}") return buf.String() } diff --git a/report/map_helpers.go b/report/map_helpers.go index f42978af4..3d6bc7a05 100644 --- a/report/map_helpers.go +++ b/report/map_helpers.go @@ -63,16 +63,8 @@ func mapEqual(m, n ps.Map, equalf func(a, b interface{}) bool) bool { // very similar to ps.Map.String() but with keys sorted func mapToString(m ps.Map) string { - keys := []string{} - if m != nil { - for _, k := range m.Keys() { - keys = append(keys, k) - } - sort.Strings(keys) - } - buf := bytes.NewBufferString("{") - for _, key := range keys { + for _, key := range mapKeys(m) { val, _ := m.Lookup(key) fmt.Fprintf(buf, "%s: %s,\n", key, val) } @@ -80,6 +72,15 @@ func mapToString(m ps.Map) string { return buf.String() } +func mapKeys(m ps.Map) []string { + if m == nil { + return nil + } + keys := m.Keys() + sort.Strings(keys) + return keys +} + // constants from https://github.com/ugorji/go/blob/master/codec/helper.go#L207 const ( containerMapKey = 2 diff --git a/report/node_set.go b/report/node_set.go index b302488a2..5a749ca18 100644 --- a/report/node_set.go +++ b/report/node_set.go @@ -3,7 +3,6 @@ package report import ( "bytes" "fmt" - "sort" "github.com/davecgh/go-spew/spew" "github.com/ugorji/go/codec" @@ -85,16 +84,6 @@ func (n NodeSet) Lookup(key string) (Node, bool) { return Node{}, false } -// Keys is a list of all the keys in this set. -func (n NodeSet) Keys() []string { - if n.psMap == nil { - return nil - } - k := n.psMap.Keys() - sort.Strings(k) - return k -} - // Size is the number of nodes in the set func (n NodeSet) Size() int { if n.psMap == nil { @@ -106,7 +95,7 @@ func (n NodeSet) Size() int { // ForEach executes f for each node in the set. Nodes are traversed in sorted // order. func (n NodeSet) ForEach(f func(Node)) { - for _, key := range n.Keys() { + for _, key := range mapKeys(n.psMap) { if val, ok := n.psMap.Lookup(key); ok { f(val.(Node)) } @@ -119,22 +108,9 @@ func (n NodeSet) Copy() NodeSet { } func (n NodeSet) String() string { - keys := []string{} - if n.psMap == nil { - n = EmptyNodeSet - } - psMap := n.psMap - if psMap == nil { - psMap = ps.NewMap() - } - for _, k := range psMap.Keys() { - keys = append(keys, k) - } - sort.Strings(keys) - buf := bytes.NewBufferString("{") - for _, key := range keys { - val, _ := psMap.Lookup(key) + for _, key := range mapKeys(n.psMap) { + val, _ := n.psMap.Lookup(key) fmt.Fprintf(buf, "%s: %s, ", key, spew.Sdump(val)) } fmt.Fprintf(buf, "}")