make zero value of LatestMap valid

This commit is contained in:
Paul Bellamy
2016-01-25 17:33:24 +00:00
parent d1c0fcec2d
commit b809f94272
2 changed files with 76 additions and 4 deletions

View File

@@ -47,6 +47,9 @@ func (m LatestMap) Copy() LatestMap {
// Size returns the number of elements
func (m LatestMap) Size() int {
if m.Map == nil {
return 0
}
return m.Map.Size()
}
@@ -83,6 +86,9 @@ func (m LatestMap) Merge(other LatestMap) LatestMap {
// Lookup the value for the given key.
func (m LatestMap) Lookup(key string) (string, bool) {
if m.Map == nil {
return "", false
}
value, ok := m.Map.Lookup(key)
if !ok {
return "", false
@@ -92,16 +98,25 @@ func (m LatestMap) Lookup(key string) (string, bool) {
// Set the value for the given key.
func (m LatestMap) Set(key string, timestamp time.Time, value string) LatestMap {
if m.Map == nil {
m = EmptyLatestMap
}
return LatestMap{m.Map.Set(key, LatestEntry{timestamp, value})}
}
// Delete the value for the given key.
func (m LatestMap) Delete(key string) LatestMap {
if m.Map == nil {
m = EmptyLatestMap
}
return LatestMap{m.Map.Delete(key)}
}
// ForEach executes f on each key value pair in the map
func (m LatestMap) ForEach(fn func(k, v string)) {
if m.Map == nil {
return
}
m.Map.ForEach(func(key string, value interface{}) {
fn(key, value.(LatestEntry).Value)
})
@@ -109,6 +124,9 @@ func (m LatestMap) ForEach(fn func(k, v string)) {
func (m LatestMap) String() string {
keys := []string{}
if m.Map == nil {
m = EmptyLatestMap
}
for _, k := range m.Map.Keys() {
keys = append(keys, k)
}
@@ -130,9 +148,12 @@ func (m LatestMap) DeepEqual(i interface{}) bool {
return false
}
if m.Map.Size() != n.Map.Size() {
if m.Size() != n.Size() {
return false
}
if m.Size() == 0 {
return true
}
equal := true
m.Map.ForEach(func(k string, val interface{}) {
@@ -147,9 +168,11 @@ func (m LatestMap) DeepEqual(i interface{}) bool {
func (m LatestMap) toIntermediate() map[string]LatestEntry {
intermediate := map[string]LatestEntry{}
m.Map.ForEach(func(key string, val interface{}) {
intermediate[key] = val.(LatestEntry)
})
if m.Map != nil {
m.Map.ForEach(func(key string, val interface{}) {
intermediate[key] = val.(LatestEntry)
})
}
return intermediate
}

View File

@@ -26,6 +26,14 @@ func TestLatestMapAdd(t *testing.T) {
})
}
func TestLatestMapAddNil(t *testing.T) {
now := time.Now()
have := LatestMap{}.Set("foo", now, "Bar")
if v, ok := have.Lookup("foo"); !ok || v != "Bar" {
t.Errorf("v != Bar")
}
}
func TestLatestMapDeepEquals(t *testing.T) {
now := time.Now()
want := EmptyLatestMap.
@@ -48,6 +56,14 @@ func TestLatestMapDelete(t *testing.T) {
}
}
func TestLatestMapDeleteNil(t *testing.T) {
want := LatestMap{}
have := LatestMap{}.Delete("foo")
if !reflect.DeepEqual(want, have) {
t.Errorf(test.Diff(want, have))
}
}
func TestLatestMapMerge(t *testing.T) {
now := time.Now()
then := now.Add(-1)
@@ -55,6 +71,11 @@ func TestLatestMapMerge(t *testing.T) {
for name, c := range map[string]struct {
a, b, want LatestMap
}{
"nils": {
a: LatestMap{},
b: LatestMap{},
want: LatestMap{},
},
"Empty a": {
a: EmptyLatestMap,
b: EmptyLatestMap.
@@ -123,3 +144,31 @@ func TestLatestMapEncoding(t *testing.T) {
}
}
}
func TestLatestMapEncodingNil(t *testing.T) {
want := LatestMap{}
{
gobs, err := want.GobEncode()
if err != nil {
t.Fatal(err)
}
have := EmptyLatestMap
have.GobDecode(gobs)
if have.Map == nil {
t.Error("Decoded LatestMap.psMap should not be nil")
}
}
{
json, err := want.MarshalJSON()
if err != nil {
t.Fatal(err)
}
have := EmptyLatestMap
have.UnmarshalJSON(json)
if have.Map == nil {
t.Error("Decoded LatestMap.psMap should not be nil")
}
}
}