Skip to content

Commit e61cc06

Browse files
authored
feat(context): return GIN Context from Value method (#2825)
1 parent d8e053d commit e61cc06

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

context.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ const (
3939
// BodyBytesKey indicates a default body bytes key.
4040
const BodyBytesKey = "_gin-gonic/gin/bodybyteskey"
4141

42+
// ContextKey is the key that a Context returns itself for.
43+
const ContextKey = "_gin-gonic/gin/contextkey"
44+
4245
// abortIndex represents a typical value used in abort functions.
4346
const abortIndex int8 = math.MaxInt8 >> 1
4447

@@ -1163,6 +1166,9 @@ func (c *Context) Value(key any) any {
11631166
if key == 0 {
11641167
return c.Request
11651168
}
1169+
if key == ContextKey {
1170+
return c
1171+
}
11661172
if keyAsString, ok := key.(string); ok {
11671173
if val, exists := c.Get(keyAsString); exists {
11681174
return val

context_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,6 +1880,7 @@ func TestContextGolangContext(t *testing.T) {
18801880
assert.Equal(t, ti, time.Time{})
18811881
assert.False(t, ok)
18821882
assert.Equal(t, c.Value(0), c.Request)
1883+
assert.Equal(t, c.Value(ContextKey), c)
18831884
assert.Nil(t, c.Value("foo"))
18841885

18851886
c.Set("foo", "bar")

0 commit comments

Comments
 (0)