|
@@ -22,7 +22,7 @@ import (
|
|
|
"github.com/gin-gonic/gin/render"
|
|
"github.com/gin-gonic/gin/render"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
-// Content-Type MIME of the most common data formats
|
|
|
|
|
|
|
+// Content-Type MIME of the most common data formats.
|
|
|
const (
|
|
const (
|
|
|
MIMEJSON = binding.MIMEJSON
|
|
MIMEJSON = binding.MIMEJSON
|
|
|
MIMEHTML = binding.MIMEHTML
|
|
MIMEHTML = binding.MIMEHTML
|
|
@@ -51,13 +51,13 @@ 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[string]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
|
|
|
|
|
|
|
|
- // Accepted defines a list of manually accepted formats for content negotiation
|
|
|
|
|
|
|
+ // Accepted defines a list of manually accepted formats for content negotiation.
|
|
|
Accepted []string
|
|
Accepted []string
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -87,7 +87,7 @@ func (c *Context) Copy() *Context {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// HandlerName returns the main handler's name. For example if the handler is "handleGetUsers()",
|
|
// HandlerName returns the main handler's name. For example if the handler is "handleGetUsers()",
|
|
|
-// this function will return "main.handleGetUsers"
|
|
|
|
|
|
|
+// this function will return "main.handleGetUsers".
|
|
|
func (c *Context) HandlerName() string {
|
|
func (c *Context) HandlerName() string {
|
|
|
return nameOfFunction(c.handlers.Last())
|
|
return nameOfFunction(c.handlers.Last())
|
|
|
}
|
|
}
|
|
@@ -462,18 +462,18 @@ func (c *Context) Bind(obj interface{}) error {
|
|
|
return c.MustBindWith(obj, b)
|
|
return c.MustBindWith(obj, b)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// BindJSON is a shortcut for c.MustBindWith(obj, binding.JSON)
|
|
|
|
|
|
|
+// BindJSON is a shortcut for c.MustBindWith(obj, binding.JSON).
|
|
|
func (c *Context) BindJSON(obj interface{}) error {
|
|
func (c *Context) BindJSON(obj interface{}) error {
|
|
|
return c.MustBindWith(obj, binding.JSON)
|
|
return c.MustBindWith(obj, binding.JSON)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// BindQuery is a shortcut for c.MustBindWith(obj, binding.Query)
|
|
|
|
|
|
|
+// BindQuery is a shortcut for c.MustBindWith(obj, binding.Query).
|
|
|
func (c *Context) BindQuery(obj interface{}) error {
|
|
func (c *Context) BindQuery(obj interface{}) error {
|
|
|
return c.MustBindWith(obj, binding.Query)
|
|
return c.MustBindWith(obj, binding.Query)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// MustBindWith binds the passed struct pointer using the specified binding
|
|
|
|
|
-// engine. It will abort the request with HTTP 400 if any error ocurrs.
|
|
|
|
|
|
|
+// MustBindWith binds the passed struct pointer using the specified binding engine.
|
|
|
|
|
+// It will abort the request with HTTP 400 if any error ocurrs.
|
|
|
// See the binding package.
|
|
// See the binding package.
|
|
|
func (c *Context) MustBindWith(obj interface{}, b binding.Binding) (err error) {
|
|
func (c *Context) MustBindWith(obj interface{}, b binding.Binding) (err error) {
|
|
|
if err = c.ShouldBindWith(obj, b); err != nil {
|
|
if err = c.ShouldBindWith(obj, b); err != nil {
|
|
@@ -483,8 +483,7 @@ func (c *Context) MustBindWith(obj interface{}, b binding.Binding) (err error) {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// ShouldBindWith binds the passed struct pointer using the specified binding
|
|
|
|
|
-// engine.
|
|
|
|
|
|
|
+// ShouldBindWith binds the passed struct pointer using the specified binding engine.
|
|
|
// See the binding package.
|
|
// See the binding package.
|
|
|
func (c *Context) ShouldBindWith(obj interface{}, b binding.Binding) error {
|
|
func (c *Context) ShouldBindWith(obj interface{}, b binding.Binding) error {
|
|
|
return b.Bind(c.Request, obj)
|
|
return b.Bind(c.Request, obj)
|
|
@@ -548,7 +547,7 @@ func (c *Context) requestHeader(key string) string {
|
|
|
/******** RESPONSE RENDERING ********/
|
|
/******** RESPONSE RENDERING ********/
|
|
|
/************************************/
|
|
/************************************/
|
|
|
|
|
|
|
|
-// bodyAllowedForStatus is a copy of http.bodyAllowedForStatus non-exported function
|
|
|
|
|
|
|
+// bodyAllowedForStatus is a copy of http.bodyAllowedForStatus non-exported function.
|
|
|
func bodyAllowedForStatus(status int) bool {
|
|
func bodyAllowedForStatus(status int) bool {
|
|
|
switch {
|
|
switch {
|
|
|
case status >= 100 && status <= 199:
|
|
case status >= 100 && status <= 199:
|
|
@@ -566,7 +565,7 @@ func (c *Context) Status(code int) {
|
|
|
c.writermem.WriteHeader(code)
|
|
c.writermem.WriteHeader(code)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Header is a intelligent shortcut for c.Writer.Header().Set(key, value)
|
|
|
|
|
|
|
+// Header is a intelligent shortcut for c.Writer.Header().Set(key, value).
|
|
|
// It writes a header in the response.
|
|
// It writes a header in the response.
|
|
|
// If value == "", this method removes the header `c.Writer.Header().Del(key)`
|
|
// If value == "", this method removes the header `c.Writer.Header().Del(key)`
|
|
|
func (c *Context) Header(key, value string) {
|
|
func (c *Context) Header(key, value string) {
|
|
@@ -577,12 +576,12 @@ func (c *Context) Header(key, value string) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// GetHeader returns value from request headers
|
|
|
|
|
|
|
+// GetHeader returns value from request headers.
|
|
|
func (c *Context) GetHeader(key string) string {
|
|
func (c *Context) GetHeader(key string) string {
|
|
|
return c.requestHeader(key)
|
|
return c.requestHeader(key)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// GetRawData return stream data
|
|
|
|
|
|
|
+// GetRawData return stream data.
|
|
|
func (c *Context) GetRawData() ([]byte, error) {
|
|
func (c *Context) GetRawData() ([]byte, error) {
|
|
|
return ioutil.ReadAll(c.Request.Body)
|
|
return ioutil.ReadAll(c.Request.Body)
|
|
|
}
|
|
}
|