Skip to content

Commit

Permalink
Merge pull request #868 from k1LoW/deletekv
Browse files Browse the repository at this point in the history
Add DelKV for delete key-value pair by key
  • Loading branch information
k1LoW authored Apr 12, 2024
2 parents e10c420 + 44a6555 commit 449b1bc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
6 changes: 6 additions & 0 deletions kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ func (kv *kv) get(k string) any { //nostyle:getters
return v
}

func (kv *kv) del(k string) {
kv.mu.RLock()
defer kv.mu.RUnlock()
delete(kv.m, k)
}

func (kv *kv) clear() {
kv.mu.Lock()
defer kv.mu.Unlock()
Expand Down
19 changes: 18 additions & 1 deletion kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,24 @@ func TestKV(t *testing.T) {
{4.5},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%v", tt.in), func(t *testing.T) {
t.Run(fmt.Sprintf("set/get/del %v", tt.in), func(t *testing.T) {
kv := newKV()
kv.set("key", tt.in)
got := kv.get("key")
if diff := cmp.Diff(got, tt.in); diff != "" {
t.Error(diff)
}

{
kv.del("key")
got := kv.get("key")
if got != nil {
t.Errorf("got %v, want %v", got, nil)
}
}
})

t.Run(fmt.Sprintf("set/get/clear %v", tt.in), func(t *testing.T) {
kv := newKV()
kv.set("key", tt.in)
got := kv.get("key")
Expand Down
5 changes: 5 additions & 0 deletions operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,11 @@ func (ops *operators) GetKV(k string) any { //nostyle:getters
return ops.kv.get(k)
}

// DelKV deletes a key-value pair from runn.kv.
func (ops *operators) DelKV(k string) {
ops.kv.del(k)
}

// ClearKV clears all key-value pairs in runn.kv.
func (ops *operators) Clear() {
ops.kv.clear()
Expand Down

0 comments on commit 449b1bc

Please sign in to comment.