Exemple #1
0
func (self *Route) compileReversiblePattern() (*re.Expression, error) {
	var alternativePattern string

	matches := toriWebRoutingSimplePattern.SearchAll(self.Pattern)

	if matches.HasAny() {
		values := matches.Key("key")

		for _, key := range *values {
			spotCheckPattern := re.Compile("<" + key + ">")
			spotCheckMatches := spotCheckPattern.SearchAll(self.Pattern)

			if spotCheckMatches.CountIndices() > 1 {
				return nil, RouteWithDuplicatedKeyError
			}
		}
	}

	alternativePattern = toriWebRoutingSimplePattern.ReplaceAll(self.Pattern, "(?P<${key}>[^/]+)")
	self.RePattern = re.Compile(fmt.Sprintf("^%s$", alternativePattern))

	return self.RePattern, nil
}
Exemple #2
0
func (self *Route) compileNonReversiblePattern() *re.Expression {
	self.RePattern = re.Compile(self.Pattern)

	return self.RePattern
}
Exemple #3
0
package tori

import "fmt"
import "github.com/shiroyuki/re"

var toriWebRoutingSimplePattern = re.Compile("<(?P<key>[^>]+)>")

// There are two types of routes in Tori Framework: reversible and non-reversible.
// The reversible one can be use to with other classes to re-create the route that
// refers to the given one. For example:
//
//      // Suppose we have a router object.
//      route  := NewRoute("/user/{alias}")
//      params := map[string]string{
//          "alias": "shiroyuki"
//      }
//
//      requestPath := route.For(params) // expected: /user/shiroyuki
type Route struct {
	Pattern    string
	RePattern  *re.Expression
	Reversible bool
	Cacheable  bool
}

// Create a route.
func NewRoute(pattern string, reversible bool) *Route {
	// TODO add the assertion to check if the pattern has a prefix "/". Raise exceptions if necessary.

	return &Route{
		Pattern:    pattern,