Esempio n. 1
0
func (ri *routeInfo) reverse(v ...interface{}) string {
	route := ri.route
	switch vlen, nlen := len(v), len(ri.paramNames); {
	case vlen < nlen:
		panic(fmt.Errorf("too few arguments: %v (controller is %v)", route.Name, reflect.TypeOf(route.Controller).Name()))
	case vlen > nlen:
		panic(fmt.Errorf("too many arguments: %v (controller is %v)", route.Name, reflect.TypeOf(route.Controller).Name()))
	case vlen+nlen == 0:
		return route.Path
	}
	var arg map[string]string
	for _, arg = range route.MethodTypes {
		break
	}
	for i := 0; i < len(v); i++ {
		t := arg[ri.paramNames[i]]
		validateParser := typeValidateParsers[t]
		if !validateParser.Validate(v[i]) {
			panic(fmt.Errorf("parameter type mismatch: %v (controller is %v)", route.Name, reflect.TypeOf(route.Controller).Name()))
		}
	}
	var oldnew []string
	for i := 0; i < len(v); i++ {
		oldnew = append(oldnew, ri.rawParamNames[i], fmt.Sprint(v[i]))
	}
	replacer := strings.NewReplacer(oldnew...)
	path := replacer.Replace(route.Path)
	return util.NormPath(path)
}
Esempio n. 2
0
func (router *router) dispatch(req *http.Request) (controller *reflect.Value, method *reflect.Value, args []reflect.Value) {
	methodName := strings.Title(strings.ToLower(req.Method))
	path := util.NormPath(req.URL.Path)
	data, params := router.forward.Lookup(path)
	if data == nil {
		return nil, nil, nil
	}
	route := data.(*Route)
	return route.dispatch(methodName, params)
}
Esempio n. 3
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
}
Esempio n. 4
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
}