Skip to content

Commit ba35efe

Browse files
committed
Can set custom binder #18
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent 90ff3c1 commit ba35efe

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

context.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package echo
33
import (
44
"encoding/json"
55
"net/http"
6-
"strings"
76
)
87

98
type (
@@ -34,18 +33,13 @@ func (c *Context) Param(name string) (value string) {
3433
return
3534
}
3635

37-
// Bind decodes the body into provided type based on Content-Type header.
36+
// Bind binds the request body into specified type v. Default binder does it
37+
// based on Content-Type header.
3838
func (c *Context) Bind(v interface{}) error {
39-
ct := c.Request.Header.Get(HeaderContentType)
40-
if strings.HasPrefix(ct, MIMEJSON) {
41-
return json.NewDecoder(c.Request.Body).Decode(v)
42-
} else if strings.HasPrefix(ct, MIMEForm) {
43-
return nil
44-
}
45-
return ErrUnsupportedMediaType
39+
return c.echo.binder(c.Request, v)
4640
}
4741

48-
// Render calls the registered HTML template renderer and sends a text/html
42+
// Render invokes the registered HTML template renderer and sends a text/html
4943
// response.
5044
func (c *Context) Render(name string, data interface{}) error {
5145
if c.echo.renderer == nil {

context_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestContext(t *testing.T) {
2828
Request: r,
2929
params: make(Params, 5),
3030
store: make(store),
31-
echo: &Echo{},
31+
echo: New(),
3232
}
3333

3434
//**********//

echo.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package echo
22

33
import (
4+
"encoding/json"
45
"errors"
56
"io"
67
"log"
78
"net/http"
9+
"strings"
810
"sync"
911
)
1012

@@ -15,15 +17,22 @@ type (
1517
middleware []MiddlewareFunc
1618
maxParam byte
1719
notFoundHandler HandlerFunc
20+
binder BindFunc
1821
renderer Renderer
1922
pool sync.Pool
2023
}
2124
Middleware interface{}
2225
MiddlewareFunc func(HandlerFunc) HandlerFunc
2326
Handler interface{}
2427
HandlerFunc func(*Context)
25-
Renderer interface {
26-
Render(io.Writer, string, interface{}) error
28+
BindFunc func(r *http.Request, v interface{}) error
29+
30+
// Renderer is the interface that wraps the Render method.
31+
//
32+
// Render renders the HTML template with given name and specified data.
33+
// It writes the output to w.
34+
Renderer interface {
35+
Render(w io.Writer, name string, data interface{}) error
2736
}
2837
)
2938

@@ -75,6 +84,15 @@ func New() (e *Echo) {
7584
notFoundHandler: func(c *Context) {
7685
http.Error(c.Response, http.StatusText(http.StatusNotFound), http.StatusNotFound)
7786
},
87+
binder: func(r *http.Request, v interface{}) error {
88+
ct := r.Header.Get(HeaderContentType)
89+
if strings.HasPrefix(ct, MIMEJSON) {
90+
return json.NewDecoder(r.Body).Decode(v)
91+
} else if strings.HasPrefix(ct, MIMEForm) {
92+
return nil
93+
}
94+
return ErrUnsupportedMediaType
95+
},
7896
}
7997
e.Router = NewRouter(e)
8098
e.pool.New = func() interface{} {
@@ -115,8 +133,13 @@ func (e *Echo) NotFoundHandler(h Handler) {
115133
e.notFoundHandler = wrapH(h)
116134
}
117135

118-
// Renderer registers an HTML template renderer, it is used by
119-
// echo.Context.Render API.
136+
// Binder registers a custom binder. It's invoked by Context.Bind API.
137+
func (e *Echo) Binder(b BindFunc) {
138+
e.binder = b
139+
}
140+
141+
// Renderer registers an HTML template renderer. It's invoked by Context.Render
142+
// API.
120143
func (e *Echo) Renderer(r Renderer) {
121144
e.renderer = r
122145
}

0 commit comments

Comments
 (0)