feat(context): implemented Delete method

Co-authored-by: suhan <s.bangera@castsoftware.com>
This commit is contained in:
Spyder01 2025-10-17 08:51:34 +05:30 committed by GitHub
parent c221133ee8
commit 38e7651192
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 0 deletions

View File

@ -465,6 +465,16 @@ func (c *Context) GetStringMapStringSlice(key any) (smss map[string][]string) {
return getTyped[map[string][]string](c, key) return getTyped[map[string][]string](c, key)
} }
// Delete deletes the key from the Context's Key map, if it exists.
// This operation is safe to be used by concurrent go-routines
func (c *Context) Delete(key any) {
c.mu.Lock()
defer c.mu.Unlock()
if c.Keys != nil {
delete(c.Keys, key)
}
}
/************************************/ /************************************/
/************ INPUT DATA ************/ /************ INPUT DATA ************/
/************************************/ /************************************/

View File

@ -404,6 +404,19 @@ func TestContextSetGetBool(t *testing.T) {
assert.True(t, c.GetBool("bool")) assert.True(t, c.GetBool("bool"))
} }
func TestSetGetDelete(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
key := "example-key"
value := "example-value"
c.Set(key, value)
val, exists := c.Get(key)
assert.True(t, exists)
assert.Equal(t, val, value)
c.Delete(key)
_, exists = c.Get(key)
assert.False(t, exists)
}
func TestContextGetInt(t *testing.T) { func TestContextGetInt(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder()) c, _ := CreateTestContext(httptest.NewRecorder())
c.Set("int", 1) c.Set("int", 1)