I'm trying to create a generator of handlers that works for several kinds of entities.
The following code works if the items slice is of a specific type (eg: var items []Person
), but I'm having problems generalizing it: specifically "ERROR: Can't get items: datastore: invalid entity type
".
Any idea of how to declare items so that it have a type valid for GetAll?
func ScaffoldList(entity interface{}, collection string, templ *template.Template) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
c := appengine.NewContext(r)
T := reflect.TypeOf(entity)
q := datastore.NewQuery(T.String())
items := reflect.MakeSlice(reflect.SliceOf(T), 0, 10)
_, err := q.GetAll(c, &items)
if err != nil{
c.Errorf("Can't get items: %v", err)
http.Error(w, "Can't get items", http.StatusInternalServerError)
return
}
context.Set(r, collection, items)
ion.RenderTemplate(templ).ServeHTTP(w, r)
})
}