Skip to content

Commit

Permalink
Merge pull request #18 from jaavier/feature/delete-key-list
Browse files Browse the repository at this point in the history
Add functions to delete keys and lists
  • Loading branch information
jaavier authored Nov 5, 2022
2 parents b208ea5 + e7e9051 commit 8e289a6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
9 changes: 9 additions & 0 deletions keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,12 @@ func Get(key string) (string, error) {
}
return "", fmt.Errorf("key '%s' not found", key)
}

func DeleteKey(key string) error {
value, _ := Get(key)
if len(value) > 0 {
delete(keys, key)
return nil
}
return fmt.Errorf("key '%s' doesn't exist", key)
}
10 changes: 10 additions & 0 deletions lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,14 @@ func ReplaceList(listName string, index int, newElement string) (bool, error) {
func CountList(listName string) (int, error) {
list, err := GetList(listName)
return len(list), err
}

func DeleteList(listName string) error {
for key, _ := range lists {
if key == listName {
delete(lists, listName)
return nil
}
}
return fmt.Errorf("list '%s' doesn't exist", listName)
}
7 changes: 7 additions & 0 deletions tests/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@ func TestKeys(t *testing.T) {
}
})

t.Run("Delete Key", func(t *testing.T) {
var newKey string = "delete-key"
sider.Set(newKey, "value")
if err := sider.DeleteKey(newKey); err != nil {
t.Errorf("Error deleting key '%s'", newKey)
}
})
}
8 changes: 8 additions & 0 deletions tests/lists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,12 @@ func TestLists(t *testing.T) {
}
}
})

t.Run("Delete List", func(t *testing.T) {
var newList string = "my-list-to-delete"
sider.LPush(newList, "element")
if err := sider.DeleteList(newList); err != nil {
t.Errorf("error: %v", err)
}
})
}

0 comments on commit 8e289a6

Please sign in to comment.