Exemplo n.º 1
0
func (router *Router) dispatch(req *Request) (name string, handler requestHandler, params denco.Params, found bool) {
	path := util.NormPath(req.URL.Path)
	data, params, found := router.forward.Lookup(path)
	if !found {
		return "", nil, nil, false
	}
	route := data.(*Route)
	handler, found = route.dispatch(req.Method)
	return route.Name, handler, params, found
}
Exemplo n.º 2
0
func (r *Route) reverse(v ...interface{}) (string, error) {
	switch vlen, nlen := len(v), len(r.paramNames); {
	case vlen < nlen:
		return "", fmt.Errorf("kocha: too few arguments: %v (controller is %T)", r.Name, r.Controller)
	case vlen > nlen:
		return "", fmt.Errorf("kocha: too many arguments: %v (controller is %T)", r.Name, r.Controller)
	case vlen+nlen == 0:
		return r.Path, nil
	}
	var oldnew []string
	for i := 0; i < len(v); i++ {
		oldnew = append(oldnew, r.paramNames[i], fmt.Sprint(v[i]))
	}
	replacer := strings.NewReplacer(oldnew...)
	path := replacer.Replace(r.Path)
	return util.NormPath(path), nil
}