Skip to content

Commit 7628a47

Browse files
authored
set: Allow nil/expired items
Fixes #397
1 parent 7413539 commit 7628a47

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

set/set.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ var ErrCollision = errors.New("key already exists")
1212
// Returned when a requested item does not exist in the set.
1313
var ErrMissing = errors.New("item does not exist")
1414

15-
// Returned when a nil item is added. Nil values are considered expired and invalid.
16-
var ErrNil = errors.New("item value must not be nil")
17-
1815
// ZeroValue can be used when we only care about the key, not about the value.
1916
var ZeroValue = struct{}{}
2017

@@ -100,17 +97,14 @@ func (s *Set) Get(key string) (Item, error) {
10097
func (s *Set) cleanup(key string) {
10198
s.Lock()
10299
item, ok := s.lookup[key]
103-
if ok && item == nil {
100+
if ok && item.Value() == nil {
104101
delete(s.lookup, key)
105102
}
106103
s.Unlock()
107104
}
108105

109106
// Add item to this set if it does not exist already.
110107
func (s *Set) Add(item Item) error {
111-
if item.Value() == nil {
112-
return ErrNil
113-
}
114108
key := s.normalize(item.Key())
115109

116110
s.Lock()
@@ -127,9 +121,6 @@ func (s *Set) Add(item Item) error {
127121

128122
// Set item to this set, even if it already exists.
129123
func (s *Set) Set(item Item) error {
130-
if item.Value() == nil {
131-
return ErrNil
132-
}
133124
key := s.normalize(item.Key())
134125

135126
s.Lock()
@@ -156,9 +147,6 @@ func (s *Set) Remove(key string) error {
156147
// Replace oldKey with a new item, which might be a new key.
157148
// Can be used to rename items.
158149
func (s *Set) Replace(oldKey string, item Item) error {
159-
if item.Value() == nil {
160-
return ErrNil
161-
}
162150
newKey := s.normalize(item.Key())
163151
oldKey = s.normalize(oldKey)
164152

set/set_test.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,23 @@ func TestSetExpiring(t *testing.T) {
2121
t.Error("not len 1 after set")
2222
}
2323

24-
item := &ExpiringItem{nil, time.Now().Add(-time.Nanosecond * 1)}
24+
item := Expire(StringItem("asdf"), -time.Nanosecond).(*ExpiringItem)
2525
if !item.Expired() {
2626
t.Errorf("ExpiringItem a nanosec ago is not expiring")
2727
}
28+
if err := s.Add(item); err != nil {
29+
t.Error("Error adding expired item to set: ", err)
30+
}
31+
if s.In("asdf") {
32+
t.Error("Expired item in set")
33+
}
34+
if s.Len() != 1 {
35+
t.Error("not len 1 after expired item")
36+
}
2837

2938
item = &ExpiringItem{nil, time.Now().Add(time.Minute * 5)}
3039
if item.Expired() {
31-
t.Errorf("ExpiringItem in 2 minutes is expiring now")
40+
t.Errorf("ExpiringItem in 5 minutes is expiring now")
3241
}
3342

3443
item = Expire(StringItem("bar"), time.Minute*5).(*ExpiringItem)
@@ -42,11 +51,13 @@ func TestSetExpiring(t *testing.T) {
4251
if err := s.Add(item); err != nil {
4352
t.Fatalf("failed to add item: %s", err)
4453
}
45-
_, ok := s.lookup["bar"]
54+
itemInLookup, ok := s.lookup["bar"]
4655
if !ok {
47-
t.Fatalf("expired bar added to lookup")
56+
t.Fatalf("bar not present in lookup even though it's not expired")
57+
}
58+
if itemInLookup != item {
59+
t.Fatalf("present item %#v != %#v original item", itemInLookup, item)
4860
}
49-
s.lookup["bar"] = item
5061

5162
if !s.In("bar") {
5263
t.Errorf("not matched after timed set")

0 commit comments

Comments
 (0)