Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
adds binding.Bindable interface (#1613)
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates authored Mar 3, 2019
1 parent c51fc3b commit 8b4163c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
10 changes: 10 additions & 0 deletions binding/bindable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package binding

import "net/http"

// Bindable when implemented, on a type
// will override any Binders that have been
// configured when using buffalo#Context.Bind
type Bindable interface {
Bind(*http.Request) error
}
28 changes: 28 additions & 0 deletions binding/bindable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package binding

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"
)

type orbison struct {
bound bool
}

func (o *orbison) Bind(req *http.Request) error {
o.bound = true
return nil
}

func Test_Bindable(t *testing.T) {
r := require.New(t)

req := httptest.NewRequest("GET", "/", nil)
o := &orbison{}
r.False(o.bound)
r.NoError(Exec(req, o))
r.True(o.bound)
}
4 changes: 4 additions & 0 deletions binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func Register(contentType string, fn Binder) {
// is "application/xml" it will use "xml.NewDecoder". The default
// binder is "https://github.com/monoculum/formam".
func Exec(req *http.Request, value interface{}) error {
if ba, ok := value.(Bindable); ok {
return ba.Bind(req)
}

ct := httpx.ContentType(req)
if ct == "" {
return errors.New("blank content type")
Expand Down

0 comments on commit 8b4163c

Please sign in to comment.