feat: Implemented Delete method for context metadata management as proposed by the ISSUE: #4397

This commit is contained in:
suhan 2025-10-15 00:43:32 +05:30
parent c221133ee8
commit b014358efb
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)
}
// 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 ************/
/************************************/

View File

@ -404,6 +404,19 @@ func TestContextSetGetBool(t *testing.T) {
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) {
c, _ := CreateTestContext(httptest.NewRecorder())
c.Set("int", 1)