Example #1
0
//Child checks the route routemaker if the specific childroute exits
func (r *Route) Child(path string) *Route {
	if strings.EqualFold(path, "/") || path == "" {
		return r
	}

	vs := splitPatternAndRemovePrefix(path)

	if len(vs) <= 0 {
		return nil
	}

	fs := vs[0]

	if len(vs) > 1 {
		vs = vs[1:]
	} else {
		vs = vs[0:0]
	}

	id, _, _ := reggy.YankSpecial(fs)

	r.lock.RLock()
	d, ok := r.childRoutes[id]
	r.lock.RUnlock()

	if !ok {
		return nil
	}

	return d.Child(strings.Join(vs, "/"))
}
Example #2
0
//New adds a new route to the current routes routemaker as a subroute
//the path string can only be a single route not a multiple
//So '/io/sucker/{f:[/w]}' will be broken down and each piece will be created
//according to its tree
//Note: '/' returns the route itself
func (r *Route) New(path string) {
	if strings.EqualFold(path, "/") || path == "" {
		return
	}

	vs := splitPatternAndRemovePrefix(path)

	if len(vs) <= 0 {
		return
	}

	fs := vs[0]

	if len(vs) > 1 {
		vs = vs[1:]
	} else {
		vs = vs[0:0]
	}

	id, _, _ := reggy.YankSpecial(fs)

	r.lock.RLock()
	d, ok := r.childRoutes[id]
	r.lock.RUnlock()

	if !ok {
		rs := FromRoute(r, fs)

		r.lock.Lock()
		r.childRoutes[rs.Path] = rs
		r.lock.Unlock()

		rs.New(strings.Join(vs, "/"))
		return
	}

	d.New(strings.Join(vs, "/"))
}