Skip to content

Commit

Permalink
Can set custom binder #18
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Apr 15, 2015
1 parent 90ff3c1 commit ba35efe
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
14 changes: 4 additions & 10 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package echo
import (
"encoding/json"
"net/http"
"strings"
)

type (
Expand Down Expand Up @@ -34,18 +33,13 @@ func (c *Context) Param(name string) (value string) {
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 {
ct := c.Request.Header.Get(HeaderContentType)
if strings.HasPrefix(ct, MIMEJSON) {
return json.NewDecoder(c.Request.Body).Decode(v)
} else if strings.HasPrefix(ct, MIMEForm) {
return nil
}
return ErrUnsupportedMediaType
return c.echo.binder(c.Request, v)
}

// 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.
func (c *Context) Render(name string, data interface{}) error {
if c.echo.renderer == nil {
Expand Down
2 changes: 1 addition & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestContext(t *testing.T) {
Request: r,
params: make(Params, 5),
store: make(store),
echo: &Echo{},
echo: New(),
}

//**********//
Expand Down
31 changes: 27 additions & 4 deletions echo.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package echo

import (
"encoding/json"
"errors"
"io"
"log"
"net/http"
"strings"
"sync"
)

Expand All @@ -15,15 +17,22 @@ type (
middleware []MiddlewareFunc
maxParam byte
notFoundHandler HandlerFunc
binder BindFunc
renderer Renderer
pool sync.Pool
}
Middleware interface{}
MiddlewareFunc func(HandlerFunc) HandlerFunc
Handler interface{}
HandlerFunc func(*Context)
Renderer interface {
Render(io.Writer, string, interface{}) error
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 {
Render(w io.Writer, name string, data interface{}) error
}
)

Expand Down Expand Up @@ -75,6 +84,15 @@ func New() (e *Echo) {
notFoundHandler: func(c *Context) {
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.pool.New = func() interface{} {
Expand Down Expand Up @@ -115,8 +133,13 @@ func (e *Echo) NotFoundHandler(h Handler) {
e.notFoundHandler = wrapH(h)
}

// Renderer registers an HTML template renderer, it is used by
// echo.Context.Render API.
// Binder registers a custom binder. It's invoked by Context.Bind 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) {
e.renderer = r
}
Expand Down

0 comments on commit ba35efe

Please sign in to comment.