Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relax types for Bind and Render funcs #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,31 @@ type Binder interface {
Bind(r *http.Request) error
}

// Bind decodes a request body and executes the Binder method of the
// payload structure.
func Bind(r *http.Request, v Binder) error {
// Bind decodes a request body into a payload structure.
// If v implements the Binder interface, its Binder method is called.
func Bind(r *http.Request, v interface{}) error {
if v == nil {
return nil
}
if err := Decode(r, v); err != nil {
return err
}
return binder(r, v)
if b, ok := v.(Binder); ok {
return binder(r, b)
}
return nil
}

// Render renders a single payload and respond to the client request.
func Render(w http.ResponseWriter, r *http.Request, v Renderer) error {
if err := renderer(w, r, v); err != nil {
return err
// If v implements the Renderer interface, its Render method is called.
func Render(w http.ResponseWriter, r *http.Request, v interface{}) error {
if v == nil {
return nil
}
if rd, ok := v.(Renderer); ok {
if err := renderer(w, r, rd); err != nil {
return err
}
}
Respond(w, r, v)
return nil
Expand Down