mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-17 05:42:09 +08:00
feat(context): return GIN Context from Value method
This commit is contained in:
parent
b463b1c2a1
commit
2f7c905ab8
@ -40,6 +40,9 @@ const (
|
|||||||
// BodyBytesKey indicates a default body bytes key.
|
// BodyBytesKey indicates a default body bytes key.
|
||||||
const BodyBytesKey = "_gin-gonic/gin/bodybyteskey"
|
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.
|
// abortIndex represents a typical value used in abort functions.
|
||||||
const abortIndex int8 = math.MaxInt8 >> 1
|
const abortIndex int8 = math.MaxInt8 >> 1
|
||||||
|
|
||||||
@ -1192,6 +1195,9 @@ func (c *Context) Value(key interface{}) interface{} {
|
|||||||
if key == 0 {
|
if key == 0 {
|
||||||
return c.Request
|
return c.Request
|
||||||
}
|
}
|
||||||
|
if key == ContextKey {
|
||||||
|
return c
|
||||||
|
}
|
||||||
if keyAsString, ok := key.(string); ok {
|
if keyAsString, ok := key.(string); ok {
|
||||||
if val, exists := c.Get(keyAsString); exists {
|
if val, exists := c.Get(keyAsString); exists {
|
||||||
return val
|
return val
|
||||||
|
@ -1861,6 +1861,7 @@ func TestContextGolangContext(t *testing.T) {
|
|||||||
assert.Equal(t, ti, time.Time{})
|
assert.Equal(t, ti, time.Time{})
|
||||||
assert.False(t, ok)
|
assert.False(t, ok)
|
||||||
assert.Equal(t, c.Value(0), c.Request)
|
assert.Equal(t, c.Value(0), c.Request)
|
||||||
|
assert.Equal(t, c.Value(ContextKey), c)
|
||||||
assert.Nil(t, c.Value("foo"))
|
assert.Nil(t, c.Value("foo"))
|
||||||
|
|
||||||
c.Set("foo", "bar")
|
c.Set("foo", "bar")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user