here is what I need to do and can't find the resource I need in order to do it:
I need to write a generic function that will setup a handler for a MOCK server. This handler will receive a JSON object and have to unMarshall it and compare it to a reference structure and set its status accordingly depending on the correspondence between both object. Here is the trick: we do not know inside the function what is the type of the reference.
<===== Where am I at this point ====> I wrote this function, which doesn't work.
func createHandlerToTestDelete(route string, ref interface{}, received interface{}){
Servmux.HandleFunc(route,
func(w http.ResponseWriter, r *http.Request) {
// recreate a structure from body content
body, _ := ioutil.ReadAll(r.Body)
json.Unmarshal(body, &received)
// comparison between ref and received
if reflect.DeepEqual(received, ref) {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
},
)
}
here is how I am using it:
ref := MyStruct{...NotEmpty...}
received := MyStruct{}
createHandlerToTestDelete("aRoute", ref, received)
The result is that the server when doing Unmarshal will not care of the original type of the received variable.
Does someone has an idea?