Skip to content

Commit

Permalink
introduce mapKeys helper
Browse files Browse the repository at this point in the history
  • Loading branch information
rade committed May 29, 2017
1 parent c060468 commit 2f81145
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 54 deletions.
22 changes: 4 additions & 18 deletions report/counters.go
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"reflect"
"sort"

"github.com/ugorji/go/codec"
"github.com/weaveworks/ps"
Expand Down Expand Up @@ -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()
}
Expand Down
19 changes: 10 additions & 9 deletions report/map_helpers.go
Expand Up @@ -63,23 +63,24 @@ 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)
}
fmt.Fprintf(buf, "}")
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
Expand Down
30 changes: 3 additions & 27 deletions report/node_set.go
Expand Up @@ -3,7 +3,6 @@ package report
import (
"bytes"
"fmt"
"sort"

"github.com/davecgh/go-spew/spew"
"github.com/ugorji/go/codec"
Expand Down Expand Up @@ -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 {
Expand All @@ -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))
}
Expand All @@ -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, "}")
Expand Down

0 comments on commit 2f81145

Please sign in to comment.