I develop some server in golang. I try to create some wrapper-function, which can helps me in future.
What I have:
1) I had some DTO structs, for example:
type Request struct {
Field1 string `json:"field1"`
Field2 string `json:"field2"`
}
type Response struct {
Field1 string `json:"field1"`
Field2 string `json:"field2"`
Field3 string `json:"field3"`
}
2) I had some functions, in controller layer, which (by conventions) receives 1 argument (pointer to struct) and returns 1 result (pointer to struct), for example:
func SomeHandler(request *Request) *Response{
...do something
return &Response{"first","second","third"}
}
What I need:
I need to write wrapper function which receives as argument:
- pointer to 'controller' function
- http.ResponseWriter
- *http.Request
This wrapper function must:
- Determine type of argument of 'controller' function
- Determine type of result of 'controller' function
- Instantiate and fill argument value from Body of *http.Request (decode from json)
- Call controller Function with instantiated on previous step argument
- Write results of previous step into http.ResponseWriter (encoded as json)
Wrapper must work correct with any types of 'controller' functions - signatures of this functions is different (different argument type, different result type)
Can anybody help me with implementing of this wrapper?
Aucun commentaire:
Enregistrer un commentaire