mirror of
https://github.com/weaveworks/scope.git
synced 2026-03-06 03:31:00 +00:00
fixing panic on EdgeMetadatas deserialization by making valid zero value
This commit is contained in:
@@ -32,6 +32,9 @@ func (c EdgeMetadatas) Copy() EdgeMetadatas {
|
||||
|
||||
// Add value to the counter 'key'
|
||||
func (c EdgeMetadatas) Add(key string, value EdgeMetadata) EdgeMetadatas {
|
||||
if c.psMap == nil {
|
||||
c = EmptyEdgeMetadatas
|
||||
}
|
||||
if existingValue, ok := c.psMap.Lookup(key); ok {
|
||||
value = value.Merge(existingValue.(EdgeMetadata))
|
||||
}
|
||||
@@ -42,15 +45,20 @@ func (c EdgeMetadatas) Add(key string, value EdgeMetadata) EdgeMetadatas {
|
||||
|
||||
// Lookup the counter 'key'
|
||||
func (c EdgeMetadatas) Lookup(key string) (EdgeMetadata, bool) {
|
||||
existingValue, ok := c.psMap.Lookup(key)
|
||||
if ok {
|
||||
return existingValue.(EdgeMetadata), true
|
||||
if c.psMap != nil {
|
||||
existingValue, ok := c.psMap.Lookup(key)
|
||||
if ok {
|
||||
return existingValue.(EdgeMetadata), true
|
||||
}
|
||||
}
|
||||
return EdgeMetadata{}, false
|
||||
}
|
||||
|
||||
// Size is the number of elements
|
||||
func (c EdgeMetadatas) Size() int {
|
||||
if c.psMap == nil {
|
||||
return 0
|
||||
}
|
||||
return c.psMap.Size()
|
||||
}
|
||||
|
||||
@@ -85,21 +93,26 @@ func (c EdgeMetadatas) Merge(other EdgeMetadatas) EdgeMetadatas {
|
||||
// The original is not modified.
|
||||
func (c EdgeMetadatas) Flatten() EdgeMetadata {
|
||||
result := EdgeMetadata{}
|
||||
c.psMap.ForEach(func(_ string, v interface{}) {
|
||||
result = result.Flatten(v.(EdgeMetadata))
|
||||
c.ForEach(func(_ string, e EdgeMetadata) {
|
||||
result = result.Flatten(e)
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
// ForEach executes f on each key value pair in the map
|
||||
func (c EdgeMetadatas) ForEach(fn func(k string, v EdgeMetadata)) {
|
||||
c.psMap.ForEach(func(key string, value interface{}) {
|
||||
fn(key, value.(EdgeMetadata))
|
||||
})
|
||||
if c.psMap != nil {
|
||||
c.psMap.ForEach(func(key string, value interface{}) {
|
||||
fn(key, value.(EdgeMetadata))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (c EdgeMetadatas) String() string {
|
||||
keys := []string{}
|
||||
if c.psMap == nil {
|
||||
c = EmptyEdgeMetadatas
|
||||
}
|
||||
for _, k := range c.psMap.Keys() {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
@@ -121,9 +134,12 @@ func (c EdgeMetadatas) DeepEqual(i interface{}) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
if c.psMap.Size() != d.psMap.Size() {
|
||||
if c.Size() != d.Size() {
|
||||
return false
|
||||
}
|
||||
if c.Size() == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
equal := true
|
||||
c.psMap.ForEach(func(k string, val interface{}) {
|
||||
@@ -138,9 +154,11 @@ func (c EdgeMetadatas) DeepEqual(i interface{}) bool {
|
||||
|
||||
func (c EdgeMetadatas) toIntermediate() map[string]EdgeMetadata {
|
||||
intermediate := map[string]EdgeMetadata{}
|
||||
c.psMap.ForEach(func(key string, val interface{}) {
|
||||
intermediate[key] = val.(EdgeMetadata)
|
||||
})
|
||||
if c.psMap != nil {
|
||||
c.psMap.ForEach(func(key string, val interface{}) {
|
||||
intermediate[key] = val.(EdgeMetadata)
|
||||
})
|
||||
}
|
||||
return intermediate
|
||||
}
|
||||
|
||||
|
||||
@@ -38,10 +38,22 @@ func TestEdgeMetadatasAdd(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestEdgeMetadatasAddNil(t *testing.T) {
|
||||
have := EdgeMetadatas{}.Add("foo", EdgeMetadata{EgressPacketCount: newu64(1)})
|
||||
if have.Size() != 1 {
|
||||
t.Errorf("Adding to a zero-value EdgeMetadatas failed, got: %v", have)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEdgeMetadatasMerge(t *testing.T) {
|
||||
for name, c := range map[string]struct {
|
||||
a, b, want EdgeMetadatas
|
||||
}{
|
||||
"nils": {
|
||||
a: EdgeMetadatas{},
|
||||
b: EdgeMetadatas{},
|
||||
want: EdgeMetadatas{},
|
||||
},
|
||||
"Empty a": {
|
||||
a: EmptyEdgeMetadatas,
|
||||
b: EmptyEdgeMetadatas.
|
||||
@@ -171,6 +183,15 @@ func TestEdgeMetadataFlatten(t *testing.T) {
|
||||
t.Error(test.Diff(want, have))
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// Should not panic on nil
|
||||
have := EdgeMetadatas{}.Flatten()
|
||||
want := EdgeMetadata{}
|
||||
if !reflect.DeepEqual(want, have) {
|
||||
t.Error(test.Diff(want, have))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEdgeMetadataReversed(t *testing.T) {
|
||||
@@ -185,7 +206,7 @@ func TestEdgeMetadataReversed(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestEdgeMetadataEncoding(t *testing.T) {
|
||||
func TestEdgeMetadatasEncoding(t *testing.T) {
|
||||
want := EmptyEdgeMetadatas.
|
||||
Add("foo", EdgeMetadata{
|
||||
EgressPacketCount: newu64(1),
|
||||
@@ -221,4 +242,32 @@ func TestEdgeMetadataEncoding(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestEdgeMetadatasEncodingNil(t *testing.T) {
|
||||
want := EdgeMetadatas{}
|
||||
|
||||
{
|
||||
gobs, err := want.GobEncode()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
have := EmptyEdgeMetadatas
|
||||
have.GobDecode(gobs)
|
||||
if have.psMap == nil {
|
||||
t.Error("needed to get back a non-nil psMap for EdgeMetadata")
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
json, err := want.MarshalJSON()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
have := EmptyEdgeMetadatas
|
||||
have.UnmarshalJSON(json)
|
||||
if have.psMap == nil {
|
||||
t.Error("needed to get back a non-nil psMap for EdgeMetadata")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newu64(value uint64) *uint64 { return &value }
|
||||
|
||||
Reference in New Issue
Block a user