Go chi render 难以处理绑定列表
Go chi renderer having difficult processing bound lists
去这里。试图让 chi 渲染器 return 一个 Order
结构实例的列表并得到一个我不理解的编译器错误:
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
}
}
当我去编译这段代码时,我得到:
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)
所以即使我为 Order
定义了 Bind
,它似乎并没有自动将 Bind
应用到 [=16 的 collection/list =].
任何人都可以看到我错过了什么吗?一些端点只会 returning 一个 Order
,而其他端点(像这个)需要能够 return collection/list of Orders
.
与 chi
存储库中的示例一样,您必须创建一个辅助方法来 呈现某些内容的列表 ,在您的情况下,订单列表。
首先,您必须实现 render.Renderer
方法,然后创建一个辅助方法来构建 render.Renderer
.
的列表
我已经根据示例改编了您的代码 here:
type Order struct {
OrderId string
Status string
}
// Render implement render.Renderer
func (*Order) Render(w http.ResponseWriter, r *http.Request) error {
// do something
return nil
}
// newOrderList is a helper method to make list of render.Renderer
func newOrderList(orders []*Order) []render.Renderer {
list := []render.Renderer{}
for _, order := range orders {
list = append(list, order)
}
return list
}
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 list of orders
render.RenderList(w, r, newOrderList(orders))
return
}
}
去这里。试图让 chi 渲染器 return 一个 Order
结构实例的列表并得到一个我不理解的编译器错误:
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
}
}
当我去编译这段代码时,我得到:
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)
所以即使我为 Order
定义了 Bind
,它似乎并没有自动将 Bind
应用到 [=16 的 collection/list =].
任何人都可以看到我错过了什么吗?一些端点只会 returning 一个 Order
,而其他端点(像这个)需要能够 return collection/list of Orders
.
与 chi
存储库中的示例一样,您必须创建一个辅助方法来 呈现某些内容的列表 ,在您的情况下,订单列表。
首先,您必须实现 render.Renderer
方法,然后创建一个辅助方法来构建 render.Renderer
.
我已经根据示例改编了您的代码 here:
type Order struct {
OrderId string
Status string
}
// Render implement render.Renderer
func (*Order) Render(w http.ResponseWriter, r *http.Request) error {
// do something
return nil
}
// newOrderList is a helper method to make list of render.Renderer
func newOrderList(orders []*Order) []render.Renderer {
list := []render.Renderer{}
for _, order := range orders {
list = append(list, order)
}
return list
}
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 list of orders
render.RenderList(w, r, newOrderList(orders))
return
}
}