0

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.

hotmeatballsoup
  • 385
  • 6
  • 58
  • 136

1 Answers1

1

As in the example in chi repository, you have to create a helper method to render the list of something, in your case, list of orders.

First, you have to implement the render.Renderer method then create a helper method to build a list of render.Renderer.

I've adapted your code from the example 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

    }
}
stevenferrer
  • 2,504
  • 1
  • 22
  • 33