Skip to content

Commit

Permalink
Merge pull request #2546 from weaveworks/use_map_helpers
Browse files Browse the repository at this point in the history
Use map helpers
  • Loading branch information
rade authored May 29, 2017
2 parents b703f4d + 29f2af1 commit e2bccd1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 164 deletions.
42 changes: 5 additions & 37 deletions report/counters.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e2bccd1

Please sign in to comment.