mirror of
https://github.com/weaveworks/scope.git
synced 2026-08-18 03:46:45 +00:00
introduce mapKeys helper
This commit is contained in:
+4
-18
@@ -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()
|
||||
}
|
||||
|
||||
+10
-9
@@ -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
|
||||
|
||||
+3
-27
@@ -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, "}")
|
||||
|
||||
Reference in New Issue
Block a user