mirror of
https://github.com/gin-gonic/gin.git
synced 2026-01-11 09:06:56 +08:00
feat(context): add cast helpers to c.Keys
This commit is contained in:
parent
ad2dacedd6
commit
fb11dc25fd
88
context.go
88
context.go
@ -186,6 +186,94 @@ func (c *Context) MustGet(key string) interface{} {
|
|||||||
panic("Key \"" + key + "\" does not exist")
|
panic("Key \"" + key + "\" does not exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetString returns the value associated with the key as a string.
|
||||||
|
func (c *Context) GetString(key string) (s string) {
|
||||||
|
if val, ok := c.Get(key); ok && val != nil {
|
||||||
|
s, _ = val.(string)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetBool returns the value associated with the key as a boolean.
|
||||||
|
func (c *Context) GetBool(key string) (b bool) {
|
||||||
|
if val, ok := c.Get(key); ok && val != nil {
|
||||||
|
b, _ = val.(bool)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetInt returns the value associated with the key as an integer.
|
||||||
|
func (c *Context) GetInt(key string) (i int) {
|
||||||
|
if val, ok := c.Get(key); ok && val != nil {
|
||||||
|
i, _ = val.(int)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetInt64 returns the value associated with the key as an integer.
|
||||||
|
func (c *Context) GetInt64(key string) (i64 int64) {
|
||||||
|
if val, ok := c.Get(key); ok && val != nil {
|
||||||
|
i64, _ = val.(int64)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetFloat64 returns the value associated with the key as a float64.
|
||||||
|
func (c *Context) GetFloat64(key string) (f64 float64) {
|
||||||
|
if val, ok := c.Get(key); ok && val != nil {
|
||||||
|
f64, _ = val.(float64)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetTime returns the value associated with the key as time.
|
||||||
|
func (c *Context) GetTime(key string) (t time.Time) {
|
||||||
|
if val, ok := c.Get(key); ok && val != nil {
|
||||||
|
t, _ = val.(time.Time)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDuration returns the value associated with the key as a duration.
|
||||||
|
func (c *Context) GetDuration(key string) (d time.Duration) {
|
||||||
|
if val, ok := c.Get(key); ok && val != nil {
|
||||||
|
d, _ = val.(time.Duration)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetStringSlice returns the value associated with the key as a slice of strings.
|
||||||
|
func (c *Context) GetStringSlice(key string) (ss []string) {
|
||||||
|
if val, ok := c.Get(key); ok && val != nil {
|
||||||
|
ss, _ = val.([]string)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetStringMap returns the value associated with the key as a map of interfaces.
|
||||||
|
func (c *Context) GetStringMap(key string) (sm map[string]interface{}) {
|
||||||
|
if val, ok := c.Get(key); ok && val != nil {
|
||||||
|
sm, _ = val.(map[string]interface{})
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetStringMapString returns the value associated with the key as a map of strings.
|
||||||
|
func (c *Context) GetStringMapString(key string) (sms map[string]string) {
|
||||||
|
if val, ok := c.Get(key); ok && val != nil {
|
||||||
|
sms, _ = val.(map[string]string)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings.
|
||||||
|
func (c *Context) GetStringMapStringSlice(key string) (smss map[string][]string) {
|
||||||
|
if val, ok := c.Get(key); ok && val != nil {
|
||||||
|
smss, _ = val.(map[string][]string)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
/************************************/
|
/************************************/
|
||||||
/************ INPUT DATA ************/
|
/************ INPUT DATA ************/
|
||||||
/************************************/
|
/************************************/
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user