mirror of
				https://github.com/labstack/echo.git
				synced 2025-10-30 23:57:38 +02:00 
			
		
		
		
	Binder interface instead of BinderFunc
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
		| @@ -101,10 +101,10 @@ func (c *Context) Set(key string, val interface{}) { | ||||
| 	c.store[key] = val | ||||
| } | ||||
|  | ||||
| // Bind binds the request body into specified type v. Default binder does it based | ||||
| // on Content-Type header. | ||||
| // Bind binds the request body into specified type `i`. The default binder does | ||||
| // it based on Content-Type header. | ||||
| func (c *Context) Bind(i interface{}) error { | ||||
| 	return c.echo.binder(c.request, i) | ||||
| 	return c.echo.binder.Bind(c.request, i) | ||||
| } | ||||
|  | ||||
| // Render renders a template with data and sends a text/html response with status | ||||
|   | ||||
							
								
								
									
										37
									
								
								echo.go
									
									
									
									
									
								
							
							
						
						
									
										37
									
								
								echo.go
									
									
									
									
									
								
							| @@ -30,7 +30,7 @@ type ( | ||||
| 		notFoundHandler         HandlerFunc | ||||
| 		defaultHTTPErrorHandler HTTPErrorHandler | ||||
| 		httpErrorHandler        HTTPErrorHandler | ||||
| 		binder                  BindFunc | ||||
| 		binder                  Binder | ||||
| 		renderer                Renderer | ||||
| 		pool                    sync.Pool | ||||
| 		debug                   bool | ||||
| @@ -56,12 +56,15 @@ type ( | ||||
| 	// HTTPErrorHandler is a centralized HTTP error handler. | ||||
| 	HTTPErrorHandler func(error, *Context) | ||||
|  | ||||
| 	BindFunc func(*http.Request, interface{}) error | ||||
| 	// Binder is the interface that wraps the Bind method. | ||||
| 	Binder interface { | ||||
| 		Bind(*http.Request, interface{}) error | ||||
| 	} | ||||
|  | ||||
| 	binder struct { | ||||
| 	} | ||||
|  | ||||
| 	// 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 | ||||
| 	} | ||||
| @@ -190,16 +193,7 @@ func New() (e *Echo) { | ||||
| 		log.Println(err) | ||||
| 	} | ||||
| 	e.SetHTTPErrorHandler(e.defaultHTTPErrorHandler) | ||||
| 	e.SetBinder(func(r *http.Request, v interface{}) (err error) { | ||||
| 		ct := r.Header.Get(ContentType) | ||||
| 		err = UnsupportedMediaType | ||||
| 		if strings.HasPrefix(ct, ApplicationJSON) { | ||||
| 			err = json.NewDecoder(r.Body).Decode(v) | ||||
| 		} else if strings.HasPrefix(ct, ApplicationXML) { | ||||
| 			err = xml.NewDecoder(r.Body).Decode(v) | ||||
| 		} | ||||
| 		return | ||||
| 	}) | ||||
| 	e.SetBinder(&binder{}) | ||||
| 	return | ||||
| } | ||||
|  | ||||
| @@ -233,7 +227,7 @@ func (e *Echo) SetHTTPErrorHandler(h HTTPErrorHandler) { | ||||
| } | ||||
|  | ||||
| // SetBinder registers a custom binder. It's invoked by Context.Bind(). | ||||
| func (e *Echo) SetBinder(b BindFunc) { | ||||
| func (e *Echo) SetBinder(b Binder) { | ||||
| 	e.binder = b | ||||
| } | ||||
|  | ||||
| @@ -592,3 +586,14 @@ func wrapHandler(h Handler) HandlerFunc { | ||||
| 		panic("echo => unknown handler") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (*binder) Bind(r *http.Request, i interface{}) (err error) { | ||||
| 	ct := r.Header.Get(ContentType) | ||||
| 	err = UnsupportedMediaType | ||||
| 	if strings.HasPrefix(ct, ApplicationJSON) { | ||||
| 		err = json.NewDecoder(r.Body).Decode(i) | ||||
| 	} else if strings.HasPrefix(ct, ApplicationXML) { | ||||
| 		err = xml.NewDecoder(r.Body).Decode(i) | ||||
| 	} | ||||
| 	return | ||||
| } | ||||
|   | ||||
							
								
								
									
										19
									
								
								router.go
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								router.go
									
									
									
									
									
								
							| @@ -1,9 +1,6 @@ | ||||
| package echo | ||||
|  | ||||
| import ( | ||||
| 	"encoding/binary" | ||||
| 	"net/http" | ||||
| ) | ||||
| import "net/http" | ||||
|  | ||||
| type ( | ||||
| 	Router struct { | ||||
| @@ -215,14 +212,6 @@ func (n *node) findChildWithType(t ntype) *node { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (r *Router) treeIndex(method string) uint8 { | ||||
| 	if method[0] == 'P' { | ||||
| 		return method[0]%10 + method[1] - 65 | ||||
| 	} else { | ||||
| 		return method[0] % 10 | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (r *Router) findTree(method string) (n *node) { | ||||
| 	switch method[0] { | ||||
| 	case 'G': // GET | ||||
| @@ -233,7 +222,8 @@ func (r *Router) findTree(method string) (n *node) { | ||||
| 	case 'P': // POST, PUT or PATCH | ||||
| 		switch method[1] { | ||||
| 		case 'O': // POST | ||||
| 			m := binary.BigEndian.Uint32([]byte(method)) | ||||
| 			m := uint32(method[3]) | uint32(method[2])<<8 | uint32(method[1])<<16 | | ||||
| 				uint32(method[0])<<24 | ||||
| 			if m == 0x504f5354 { | ||||
| 				n = r.postTree | ||||
| 			} | ||||
| @@ -263,7 +253,8 @@ func (r *Router) findTree(method string) (n *node) { | ||||
| 			n = r.connectTree | ||||
| 		} | ||||
| 	case 'H': // HEAD | ||||
| 		m := binary.BigEndian.Uint32([]byte(method)) | ||||
| 		m := uint32(method[3]) | uint32(method[2])<<8 | uint32(method[1])<<16 | | ||||
| 			uint32(method[0])<<24 | ||||
| 		if m == 0x48454144 { | ||||
| 			n = r.headTree | ||||
| 		} | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| # Echo | ||||
| # Echo <iframe src="https://ghbtns.com/github-btn.html?user=labstack&repo=echo&type=star&count=true&size=large" frameborder="0" scrolling="0" width="160px" height="30px"></iframe> | ||||
|  | ||||
| A fast and unfancy micro web framework for Golang. | ||||
| A fast and unfancy micro web framework for Golang.  | ||||
|  | ||||
| --- | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user