Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use map helpers #2546

Merged
merged 3 commits into from May 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 5 additions & 37 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,53 +86,22 @@ 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()
}

// DeepEqual tests equality with other Counters
func (c Counters) DeepEqual(d Counters) bool {
if (c.psMap == nil) != (d.psMap == nil) {
return false
} else if c.psMap == nil && d.psMap == nil {
return true
}

if c.psMap.Size() != d.psMap.Size() {
return false
}

equal := true
c.psMap.ForEach(func(k string, val interface{}) {
if otherValue, ok := d.psMap.Lookup(k); !ok {
equal = false
} else {
equal = equal && reflect.DeepEqual(val, otherValue)
}
})
return equal
return mapEqual(c.psMap, d.psMap, reflect.DeepEqual)
}

func (c Counters) fromIntermediate(in map[string]int) Counters {
Expand Down
36 changes: 2 additions & 34 deletions report/edge_metadatas.go
@@ -1,10 +1,8 @@
package report

import (
"bytes"
"fmt"
"reflect"
"sort"
"strconv"

"github.com/ugorji/go/codec"
Expand Down Expand Up @@ -109,42 +107,12 @@ func (c EdgeMetadatas) ForEach(fn func(k string, v EdgeMetadata)) {
}

func (c EdgeMetadatas) String() string {
keys := []string{}
if c.psMap == nil {
c = EmptyEdgeMetadatas
}
for _, k := range c.psMap.Keys() {
keys = append(keys, k)
}
sort.Strings(keys)

buf := bytes.NewBufferString("{")
for _, key := range keys {
val, _ := c.psMap.Lookup(key)
fmt.Fprintf(buf, "%s: %v, ", key, val)
}
fmt.Fprintf(buf, "}")
return buf.String()
return mapToString(c.psMap)
}

// DeepEqual tests equality with other Counters
func (c EdgeMetadatas) DeepEqual(d EdgeMetadatas) bool {
if c.Size() != d.Size() {
return false
}
if c.Size() == 0 {
return true
}

equal := true
c.psMap.ForEach(func(k string, val interface{}) {
if otherValue, ok := d.psMap.Lookup(k); !ok {
equal = false
} else {
equal = equal && reflect.DeepEqual(val, otherValue)
}
})
return equal
return mapEqual(c.psMap, d.psMap, reflect.DeepEqual)
}

// CodecEncodeSelf implements codec.Selfer
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
54 changes: 5 additions & 49 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,51 +108,18 @@ 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, "}")
return buf.String()
}

// DeepEqual tests equality with other NodeSets
func (n NodeSet) DeepEqual(i interface{}) bool {
d, ok := i.(NodeSet)
if !ok {
return false
}

if n.Size() != d.Size() {
return false
}
if n.Size() == 0 {
return true
}

equal := true
n.psMap.ForEach(func(k string, val interface{}) {
if otherValue, ok := d.psMap.Lookup(k); !ok {
equal = false
} else {
equal = equal && reflect.DeepEqual(val, otherValue)
}
})
return equal
func (n NodeSet) DeepEqual(o NodeSet) bool {
return mapEqual(n.psMap, o.psMap, reflect.DeepEqual)
}

func (n NodeSet) toIntermediate() []Node {
Expand Down
37 changes: 2 additions & 35 deletions report/sets.go
@@ -1,10 +1,7 @@
package report

import (
"bytes"
"fmt"
"reflect"
"sort"

"github.com/ugorji/go/codec"
"github.com/weaveworks/ps"
Expand Down Expand Up @@ -109,42 +106,12 @@ func (s Sets) Copy() Sets {
}

func (s Sets) String() string {
if s.psMap == nil {
s = EmptySets
}
keys := []string{}
for _, k := range s.psMap.Keys() {
keys = append(keys, k)
}
sort.Strings(keys)

buf := bytes.NewBufferString("{")
for _, key := range keys {
val, _ := s.psMap.Lookup(key)
fmt.Fprintf(buf, "%s: %v, ", key, val)
}
fmt.Fprintf(buf, "}")
return buf.String()
return mapToString(s.psMap)
}

// DeepEqual tests equality with other Sets
func (s Sets) DeepEqual(t Sets) bool {
if s.Size() != t.Size() {
return false
}
if s.Size() == 0 {
return true
}

equal := true
s.psMap.ForEach(func(k string, val interface{}) {
if otherValue, ok := t.psMap.Lookup(k); !ok {
equal = false
} else {
equal = equal && reflect.DeepEqual(val, otherValue)
}
})
return equal
return mapEqual(s.psMap, t.psMap, reflect.DeepEqual)
}

// CodecEncodeSelf implements codec.Selfer
Expand Down