Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Typeof of Go struct methods

I have this in a .go file:

type RouteMaker = func(route string, v PeopleInjection) http.HandlerFunc;

type Handler struct {}

func (h Handler) makeGetMany(route string, v PeopleInjection) http.HandlerFunc {

    type RespBody struct {...}
    type ReqBody struct {...}

    return tc.ExtractType(
        tc.TypeList{ReqBody{},RespBody{}},
        func(w http.ResponseWriter, r *http.Request) {
         // ...
    })
}

func (h Handler) makeGetOne(route string, v PeopleInjection) http.HandlerFunc {

    type RespBody struct {}
    type ReqBody struct {}

    return tc.ExtractType(
        tc.TypeList{ReqBody{},RespBody{}},
        func(w http.ResponseWriter, r *http.Request) {
          // ... 
    })
}

I used the type RouteMaker to represent the "methods" on the Handler struct (aside: is there a more apt name than "methods"?). My question is - is there a better way to reference a type of these methods other than to create a new type RouteMaker? I am looking to reference the types of the methods without having to create a new one.

I am currently using RouteMaker like so:

type RouteMaker = func(d Docs, v PeopleInjection) http.HandlerFunc
type RouteAdder = func(r string, methods []string, f RouteMaker)

func (h Handler) makeRouteAdder(router *mux.Router, v PeopleInjection) RouteAdder {
    return func(r string, methods []string, f RouteMaker) {
        router.HandleFunc(r, f(r, v)).Methods(methods...)
    }
}

func (h Handler) Mount(router *mux.Router, v PeopleInjection) Handler {
    add := h.makeRouteAdder(router, v)
    add("/api/v1/people", []string{"GET"}, h.makeGetMany)
    add("/api/v1/people/{id}", []string{"GET"}, h.makeGetOne)
    add("/api/v1/people/{id}", []string{"POST"}, h.makeCreate)
    add("/api/v1/people/{id}", []string{"DELETE"}, h.makeDelete)
    add("/api/v1/people/{id}", []string{"PUT"}, h.makeUpdateByID)
    return h
}

Comments