Skip to content

Commit 38e7651

Browse files
feat(context): implemented Delete method
Co-authored-by: suhan <s.bangera@castsoftware.com>
1 parent c221133 commit 38e7651

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

context.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,16 @@ func (c *Context) GetStringMapStringSlice(key any) (smss map[string][]string) {
465465
return getTyped[map[string][]string](c, key)
466466
}
467467

468+
// Delete deletes the key from the Context's Key map, if it exists.
469+
// This operation is safe to be used by concurrent go-routines
470+
func (c *Context) Delete(key any) {
471+
c.mu.Lock()
472+
defer c.mu.Unlock()
473+
if c.Keys != nil {
474+
delete(c.Keys, key)
475+
}
476+
}
477+
468478
/************************************/
469479
/************ INPUT DATA ************/
470480
/************************************/

context_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,19 @@ func TestContextSetGetBool(t *testing.T) {
404404
assert.True(t, c.GetBool("bool"))
405405
}
406406

407+
func TestSetGetDelete(t *testing.T) {
408+
c, _ := CreateTestContext(httptest.NewRecorder())
409+
key := "example-key"
410+
value := "example-value"
411+
c.Set(key, value)
412+
val, exists := c.Get(key)
413+
assert.True(t, exists)
414+
assert.Equal(t, val, value)
415+
c.Delete(key)
416+
_, exists = c.Get(key)
417+
assert.False(t, exists)
418+
}
419+
407420
func TestContextGetInt(t *testing.T) {
408421
c, _ := CreateTestContext(httptest.NewRecorder())
409422
c.Set("int", 1)

0 commit comments

Comments
 (0)