mardi 26 juillet 2016

Get struct from function type

I currently have the following code

func (r *Router) Get(path string, controller interface{}) {
    ...
    t := reflect.TypeOf(controller)
    ...
}

That is called doing the following

Route.Get("/", controllers.Test.IsWorking)

The second argument is basically this

type Test struct {
    *Controller
    Name string
}

func (t Test) IsWorking() {

}

type Controller struct {
    Method   string
    Request  *http.Request
    Response http.ResponseWriter
    Data     map[interface{}]interface{}
}

What I want is to get what struct the function passed on the Router.Get needs so I can create that struct using reflect and set the fields manually (pass the main Controller)

I am not sure how to achieve this... TypeOf returns

func(controllers.Test)

I cant use Eleme() since the struct is not expecting a pointer... how should I approach this. The reflect package docs arent very clear on the usage





Aucun commentaire:

Enregistrer un commentaire