1
0
mirror of https://github.com/labstack/echo.git synced 2025-07-03 00:56:59 +02:00

Can set custom binder #18

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana
2015-04-15 10:10:05 -07:00
parent 90ff3c14b2
commit ba35efed61
3 changed files with 32 additions and 15 deletions

View File

@ -3,7 +3,6 @@ package echo
import ( import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"strings"
) )
type ( type (
@ -34,18 +33,13 @@ func (c *Context) Param(name string) (value string) {
return return
} }
// Bind decodes the body into provided type based on Content-Type header. // Bind binds the request body into specified type v. Default binder does it
// based on Content-Type header.
func (c *Context) Bind(v interface{}) error { func (c *Context) Bind(v interface{}) error {
ct := c.Request.Header.Get(HeaderContentType) return c.echo.binder(c.Request, v)
if strings.HasPrefix(ct, MIMEJSON) {
return json.NewDecoder(c.Request.Body).Decode(v)
} else if strings.HasPrefix(ct, MIMEForm) {
return nil
}
return ErrUnsupportedMediaType
} }
// Render calls the registered HTML template renderer and sends a text/html // Render invokes the registered HTML template renderer and sends a text/html
// response. // response.
func (c *Context) Render(name string, data interface{}) error { func (c *Context) Render(name string, data interface{}) error {
if c.echo.renderer == nil { if c.echo.renderer == nil {

View File

@ -28,7 +28,7 @@ func TestContext(t *testing.T) {
Request: r, Request: r,
params: make(Params, 5), params: make(Params, 5),
store: make(store), store: make(store),
echo: &Echo{}, echo: New(),
} }
//**********// //**********//

29
echo.go
View File

@ -1,10 +1,12 @@
package echo package echo
import ( import (
"encoding/json"
"errors" "errors"
"io" "io"
"log" "log"
"net/http" "net/http"
"strings"
"sync" "sync"
) )
@ -15,6 +17,7 @@ type (
middleware []MiddlewareFunc middleware []MiddlewareFunc
maxParam byte maxParam byte
notFoundHandler HandlerFunc notFoundHandler HandlerFunc
binder BindFunc
renderer Renderer renderer Renderer
pool sync.Pool pool sync.Pool
} }
@ -22,8 +25,14 @@ type (
MiddlewareFunc func(HandlerFunc) HandlerFunc MiddlewareFunc func(HandlerFunc) HandlerFunc
Handler interface{} Handler interface{}
HandlerFunc func(*Context) HandlerFunc func(*Context)
BindFunc func(r *http.Request, v interface{}) error
// Renderer is the interface that wraps the Render method.
//
// Render renders the HTML template with given name and specified data.
// It writes the output to w.
Renderer interface { Renderer interface {
Render(io.Writer, string, interface{}) error Render(w io.Writer, name string, data interface{}) error
} }
) )
@ -75,6 +84,15 @@ func New() (e *Echo) {
notFoundHandler: func(c *Context) { notFoundHandler: func(c *Context) {
http.Error(c.Response, http.StatusText(http.StatusNotFound), http.StatusNotFound) http.Error(c.Response, http.StatusText(http.StatusNotFound), http.StatusNotFound)
}, },
binder: func(r *http.Request, v interface{}) error {
ct := r.Header.Get(HeaderContentType)
if strings.HasPrefix(ct, MIMEJSON) {
return json.NewDecoder(r.Body).Decode(v)
} else if strings.HasPrefix(ct, MIMEForm) {
return nil
}
return ErrUnsupportedMediaType
},
} }
e.Router = NewRouter(e) e.Router = NewRouter(e)
e.pool.New = func() interface{} { e.pool.New = func() interface{} {
@ -115,8 +133,13 @@ func (e *Echo) NotFoundHandler(h Handler) {
e.notFoundHandler = wrapH(h) e.notFoundHandler = wrapH(h)
} }
// Renderer registers an HTML template renderer, it is used by // Binder registers a custom binder. It's invoked by Context.Bind API.
// echo.Context.Render API. func (e *Echo) Binder(b BindFunc) {
e.binder = b
}
// Renderer registers an HTML template renderer. It's invoked by Context.Render
// API.
func (e *Echo) Renderer(r Renderer) { func (e *Echo) Renderer(r Renderer) {
e.renderer = r e.renderer = r
} }