Go here. Trying to get the chi renderer to return a list of Order
struct instances and getting a compiler error that I don't understand:
package myapp
import (
"net/http"
"github.com/go-chi/render"
)
type Order struct {
OrderId string
Status string
}
func (*Order) Bind(r *http.Request) error {
return nil
}
func GetAllOrderByCustomerId(dbClient DbClient, customerId string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// fetch all customer orders from the DB
orders,err := dbClient.FetchAllOrdersByCustomerId(customerId)
if err != nil {
log.Error("unable to fetch orders for customer", err)
render.Render(w, r, NewInternalServerError(err))
return
}
render.Bind(r, &orders)
return
}
}
When I go to compile this code I get:
fizz/buzz/myapp/order_fetcher.go:136:20: cannot use &orders (type *[]Order) as type render.Binder in argument to render.Bind:
*[]Order does not implement render.Binder (missing Bind method)
So even though I've defined a Bind
for Order
, it doesn't seem to automatically apply that Bind
to a collection/list of Orders
.
Can anyone see what I'm missing? Some endpoints will only be returning a single Order
, whereas others (like this one) need to be able to return a collection/list of Orders
.