mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-24 02:32:17 +08:00
use interface{} in map
This commit is contained in:
parent
9e895470dd
commit
a64352db17
20
context.go
20
context.go
@ -49,7 +49,7 @@ type Context struct {
|
|||||||
engine *Engine
|
engine *Engine
|
||||||
|
|
||||||
// Keys is a key/value pair exclusively for the context of each request.
|
// Keys is a key/value pair exclusively for the context of each request.
|
||||||
Keys map[string]interface{}
|
Keys map[interface{}]interface{}
|
||||||
|
|
||||||
// Errors is a list of errors attached to all the handlers/middlewares who used this context.
|
// Errors is a list of errors attached to all the handlers/middlewares who used this context.
|
||||||
Errors errorMsgs
|
Errors errorMsgs
|
||||||
@ -178,30 +178,32 @@ func (c *Context) Error(err error) *Error {
|
|||||||
|
|
||||||
// Set is used to store a new key/value pair exclusively for this context.
|
// Set is used to store a new key/value pair exclusively for this context.
|
||||||
// It also lazy initializes c.Keys if it was not used previously.
|
// It also lazy initializes c.Keys if it was not used previously.
|
||||||
func (c *Context) Set(key string, value interface{}) {
|
func (c *Context) Set(key, value interface{}) {
|
||||||
if c.Keys == nil {
|
if c.Keys == nil {
|
||||||
c.Keys = make(map[string]interface{})
|
c.Keys = make(map[interface{}]interface{})
|
||||||
}
|
}
|
||||||
c.Keys[key] = value
|
c.Keys[key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the value for the given key, ie: (value, true).
|
// Get returns the value for the given key, ie: (value, true).
|
||||||
// If the value does not exists it returns (nil, false)
|
// If the value does not exists it returns (nil, false)
|
||||||
func (c *Context) Get(key string) (value interface{}, exists bool) {
|
func (c *Context) Get(key interface{}) (value interface{}, exists bool) {
|
||||||
value, exists = c.Keys[key]
|
if c.Keys != nil {
|
||||||
|
value, exists = c.Keys[key]
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// MustGet returns the value for the given key if it exists, otherwise it panics.
|
// MustGet returns the value for the given key if it exists, otherwise it panics.
|
||||||
func (c *Context) MustGet(key string) interface{} {
|
func (c *Context) MustGet(key interface{}) interface{} {
|
||||||
if value, exists := c.Get(key); exists {
|
if value, exists := c.Get(key); exists {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
panic("Key \"" + key + "\" does not exist")
|
panic(fmt.Sprintf("Key %v does not exist", key))
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetString returns the value associated with the key as a string.
|
// GetString returns the value associated with the key as a string.
|
||||||
func (c *Context) GetString(key string) (s string) {
|
func (c *Context) GetString(key interface{}) (s string) {
|
||||||
if val, ok := c.Get(key); ok && val != nil {
|
if val, ok := c.Get(key); ok && val != nil {
|
||||||
s, _ = val.(string)
|
s, _ = val.(string)
|
||||||
}
|
}
|
||||||
@ -209,7 +211,7 @@ func (c *Context) GetString(key string) (s string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetBool returns the value associated with the key as a boolean.
|
// GetBool returns the value associated with the key as a boolean.
|
||||||
func (c *Context) GetBool(key string) (b bool) {
|
func (c *Context) GetBool(key interface{}) (b bool) {
|
||||||
if val, ok := c.Get(key); ok && val != nil {
|
if val, ok := c.Get(key); ok && val != nil {
|
||||||
b, _ = val.(bool)
|
b, _ = val.(bool)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user