Skip to content

Commit 8e289a6

Browse files
authored
Merge pull request #18 from jaavier/feature/delete-key-list
Add functions to delete keys and lists
2 parents b208ea5 + e7e9051 commit 8e289a6

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

keys.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,12 @@ func Get(key string) (string, error) {
2525
}
2626
return "", fmt.Errorf("key '%s' not found", key)
2727
}
28+
29+
func DeleteKey(key string) error {
30+
value, _ := Get(key)
31+
if len(value) > 0 {
32+
delete(keys, key)
33+
return nil
34+
}
35+
return fmt.Errorf("key '%s' doesn't exist", key)
36+
}

lists.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,14 @@ func ReplaceList(listName string, index int, newElement string) (bool, error) {
144144
func CountList(listName string) (int, error) {
145145
list, err := GetList(listName)
146146
return len(list), err
147+
}
148+
149+
func DeleteList(listName string) error {
150+
for key, _ := range lists {
151+
if key == listName {
152+
delete(lists, listName)
153+
return nil
154+
}
155+
}
156+
return fmt.Errorf("list '%s' doesn't exist", listName)
147157
}

tests/keys_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,11 @@ func TestKeys(t *testing.T) {
2323
}
2424
})
2525

26+
t.Run("Delete Key", func(t *testing.T) {
27+
var newKey string = "delete-key"
28+
sider.Set(newKey, "value")
29+
if err := sider.DeleteKey(newKey); err != nil {
30+
t.Errorf("Error deleting key '%s'", newKey)
31+
}
32+
})
2633
}

tests/lists_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,12 @@ func TestLists(t *testing.T) {
168168
}
169169
}
170170
})
171+
172+
t.Run("Delete List", func(t *testing.T) {
173+
var newList string = "my-list-to-delete"
174+
sider.LPush(newList, "element")
175+
if err := sider.DeleteList(newList); err != nil {
176+
t.Errorf("error: %v", err)
177+
}
178+
})
171179
}

0 commit comments

Comments
 (0)