mirror of https://github.com/gin-gonic/gin.git
feat(context): return GIN Context from Value method (#2825)
This commit is contained in:
parent
d8e053d15f
commit
e61cc06955
|
@ -39,6 +39,9 @@ const (
|
|||
// BodyBytesKey indicates a default body bytes key.
|
||||
const BodyBytesKey = "_gin-gonic/gin/bodybyteskey"
|
||||
|
||||
// ContextKey is the key that a Context returns itself for.
|
||||
const ContextKey = "_gin-gonic/gin/contextkey"
|
||||
|
||||
// abortIndex represents a typical value used in abort functions.
|
||||
const abortIndex int8 = math.MaxInt8 >> 1
|
||||
|
||||
|
@ -1163,6 +1166,9 @@ func (c *Context) Value(key any) any {
|
|||
if key == 0 {
|
||||
return c.Request
|
||||
}
|
||||
if key == ContextKey {
|
||||
return c
|
||||
}
|
||||
if keyAsString, ok := key.(string); ok {
|
||||
if val, exists := c.Get(keyAsString); exists {
|
||||
return val
|
||||
|
|
|
@ -1880,6 +1880,7 @@ func TestContextGolangContext(t *testing.T) {
|
|||
assert.Equal(t, ti, time.Time{})
|
||||
assert.False(t, ok)
|
||||
assert.Equal(t, c.Value(0), c.Request)
|
||||
assert.Equal(t, c.Value(ContextKey), c)
|
||||
assert.Nil(t, c.Value("foo"))
|
||||
|
||||
c.Set("foo", "bar")
|
||||
|
|
Loading…
Reference in New Issue