Fixes Content.Negotiate API
This commit is contained in:
parent
ffea7e88a2
commit
275bdc194e
42
context.go
42
context.go
@ -82,6 +82,7 @@ func (engine *Engine) createContext(w http.ResponseWriter, req *http.Request, pa
|
|||||||
c.handlers = handlers
|
c.handlers = handlers
|
||||||
c.Keys = nil
|
c.Keys = nil
|
||||||
c.index = -1
|
c.index = -1
|
||||||
|
c.accepted = nil
|
||||||
c.Errors = c.Errors[0:0]
|
c.Errors = c.Errors[0:0]
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
@ -280,47 +281,34 @@ func (c *Context) File(filepath string) {
|
|||||||
/************************************/
|
/************************************/
|
||||||
/******** CONTENT NEGOTIATION *******/
|
/******** CONTENT NEGOTIATION *******/
|
||||||
/************************************/
|
/************************************/
|
||||||
|
|
||||||
type Negotiate struct {
|
type Negotiate struct {
|
||||||
Offered []string
|
Offered []string
|
||||||
Data interface{}
|
|
||||||
JsonData interface{}
|
|
||||||
XMLData interface{}
|
|
||||||
HTMLData interface{}
|
|
||||||
HTMLPath string
|
HTMLPath string
|
||||||
|
HTMLData interface{}
|
||||||
|
JSONData interface{}
|
||||||
|
XMLData interface{}
|
||||||
|
Data interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) Negotiate2(code int, config Negotiate) {
|
func (c *Context) Negotiate(code int, config Negotiate) {
|
||||||
result := c.NegotiateFormat(config.Offered...)
|
result := c.NegotiateFormat(config.Offered...)
|
||||||
switch result {
|
switch result {
|
||||||
case MIMEJSON:
|
case MIMEJSON:
|
||||||
c.JSON(code, config.Data)
|
data := chooseData(config.JSONData, config.Data)
|
||||||
|
|
||||||
case MIMEHTML:
|
|
||||||
name := config.HTMLPath
|
|
||||||
c.HTML(code, name, config.Data)
|
|
||||||
|
|
||||||
case MIMEXML:
|
|
||||||
c.XML(code, config.Data)
|
|
||||||
default:
|
|
||||||
c.Fail(400, errors.New("m"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Context) Negotiate(code int, config map[string]interface{}, offerts ...string) {
|
|
||||||
result := c.NegotiateFormat(offerts...)
|
|
||||||
switch result {
|
|
||||||
case MIMEJSON:
|
|
||||||
data := readData("json.data", config)
|
|
||||||
c.JSON(code, data)
|
c.JSON(code, data)
|
||||||
|
|
||||||
case MIMEHTML:
|
case MIMEHTML:
|
||||||
data := readData("html.data", config)
|
data := chooseData(config.HTMLData, config.Data)
|
||||||
name := config["html.path"].(string)
|
if len(config.HTMLPath) == 0 {
|
||||||
c.HTML(code, name, data)
|
panic("negotiate config is wrong. html path is needed")
|
||||||
|
}
|
||||||
|
c.HTML(code, config.HTMLPath, data)
|
||||||
|
|
||||||
case MIMEXML:
|
case MIMEXML:
|
||||||
data := readData("xml.data", config)
|
data := chooseData(config.XMLData, config.Data)
|
||||||
c.XML(code, data)
|
c.XML(code, data)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
c.Fail(400, errors.New("m"))
|
c.Fail(400, errors.New("m"))
|
||||||
}
|
}
|
||||||
|
14
utils.go
14
utils.go
@ -46,16 +46,14 @@ func filterFlags(content string) string {
|
|||||||
return content
|
return content
|
||||||
}
|
}
|
||||||
|
|
||||||
func readData(key string, config map[string]interface{}) interface{} {
|
func chooseData(custom, wildcard interface{}) interface{} {
|
||||||
data, ok := config[key]
|
if custom == nil {
|
||||||
if ok {
|
if wildcard == nil {
|
||||||
return data
|
|
||||||
}
|
|
||||||
data, ok = config["*.data"]
|
|
||||||
if !ok {
|
|
||||||
panic("negotiation config is invalid")
|
panic("negotiation config is invalid")
|
||||||
}
|
}
|
||||||
return data
|
return wildcard
|
||||||
|
}
|
||||||
|
return custom
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseAccept(accept string) []string {
|
func parseAccept(accept string) []string {
|
||||||
|
Loading…
Reference in New Issue
Block a user