From d8159ca20075ecf718f6bc868ac46332ddd08a50 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Mon, 1 Apr 2024 15:02:39 +0900 Subject: [PATCH] Add DelKV for delete key-value pair by key --- kv.go | 6 ++++++ kv_test.go | 19 ++++++++++++++++++- operator.go | 5 +++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/kv.go b/kv.go index 5943293a..29b66f4c 100644 --- a/kv.go +++ b/kv.go @@ -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() diff --git a/kv_test.go b/kv_test.go index 0e307b7e..bf0a027e 100644 --- a/kv_test.go +++ b/kv_test.go @@ -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") diff --git a/operator.go b/operator.go index dfe2a2e9..402b0aae 100644 --- a/operator.go +++ b/operator.go @@ -1507,6 +1507,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()