mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 13:22:09 +08:00
feat: Engine.AppendKeys
Allow c.Keys data to be appended into obj if obj has map[string]any data type
This commit is contained in:
parent
f1e942889a
commit
b728bcac15
21
context.go
21
context.go
@ -823,6 +823,22 @@ func bodyAllowedForStatus(status int) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func appendKeysData(data map[string]any, obj any) any {
|
||||||
|
copyObj, ok := obj.(map[string]any)
|
||||||
|
if !ok {
|
||||||
|
return obj
|
||||||
|
}
|
||||||
|
|
||||||
|
for key, value := range data {
|
||||||
|
_, exist := copyObj[key]
|
||||||
|
if !exist {
|
||||||
|
copyObj[key] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return copyObj
|
||||||
|
}
|
||||||
|
|
||||||
// Status sets the HTTP response code.
|
// Status sets the HTTP response code.
|
||||||
func (c *Context) Status(code int) {
|
func (c *Context) Status(code int) {
|
||||||
c.Writer.WriteHeader(code)
|
c.Writer.WriteHeader(code)
|
||||||
@ -905,6 +921,11 @@ func (c *Context) Render(code int, r render.Render) {
|
|||||||
// It also updates the HTTP code and sets the Content-Type as "text/html".
|
// It also updates the HTTP code and sets the Content-Type as "text/html".
|
||||||
// See http://golang.org/doc/articles/wiki/
|
// See http://golang.org/doc/articles/wiki/
|
||||||
func (c *Context) HTML(code int, name string, obj any) {
|
func (c *Context) HTML(code int, name string, obj any) {
|
||||||
|
// Append c.Set() data into object if object is map[string]any
|
||||||
|
if c.engine.AppendKeys {
|
||||||
|
obj = appendKeysData(c.Keys, obj)
|
||||||
|
}
|
||||||
|
|
||||||
instance := c.engine.HTMLRender.Instance(name, obj)
|
instance := c.engine.HTMLRender.Instance(name, obj)
|
||||||
c.Render(code, instance)
|
c.Render(code, instance)
|
||||||
}
|
}
|
||||||
|
3
gin.go
3
gin.go
@ -147,6 +147,9 @@ type Engine struct {
|
|||||||
// UseH2C enable h2c support.
|
// UseH2C enable h2c support.
|
||||||
UseH2C bool
|
UseH2C bool
|
||||||
|
|
||||||
|
// AppendKeys if true, Gin will merge c.Set() data with user obj data
|
||||||
|
AppendKeys bool
|
||||||
|
|
||||||
delims render.Delims
|
delims render.Delims
|
||||||
secureJSONPrefix string
|
secureJSONPrefix string
|
||||||
HTMLRender render.HTMLRender
|
HTMLRender render.HTMLRender
|
||||||
|
Loading…
x
Reference in New Issue
Block a user