1
0
mirror of https://github.com/astaxie/beego.git synced 2025-07-03 17:00:19 +00:00

beego: Refactoring Filter & add comments

This commit is contained in:
astaxie
2014-05-20 18:47:41 +08:00
parent 8b374d7f90
commit 17104c25a2
3 changed files with 58 additions and 106 deletions

View File

@ -448,7 +448,7 @@ func (p *ControllerRegistor) AddAutoPrefix(prefix string, c ControllerInterface)
// [Deprecated] use InsertFilter.
// Add FilterFunc with pattern for action.
func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc) error {
mr, err := buildFilter(pattern, filter)
mr, err := p.buildFilter(pattern, filter)
if err != nil {
return err
}
@ -471,7 +471,7 @@ func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc
// Add a FilterFunc with pattern rule and action constant.
func (p *ControllerRegistor) InsertFilter(pattern string, pos int, filter FilterFunc) error {
mr, err := buildFilter(pattern, filter)
mr, err := p.buildFilter(pattern, filter)
if err != nil {
return err
}
@ -480,6 +480,25 @@ func (p *ControllerRegistor) InsertFilter(pattern string, pos int, filter Filter
return nil
}
func (p *ControllerRegistor) buildFilter(pattern string, filter FilterFunc) (*FilterRouter, error) {
mr := new(FilterRouter)
mr.params = make(map[int]string)
mr.filterFunc = filter
j, params, parts := p.splitRoute(pattern)
if j != 0 {
pattern = strings.Join(parts, "/")
regex, regexErr := regexp.Compile(pattern)
if regexErr != nil {
return nil, regexErr
}
mr.regex = regex
mr.hasregex = true
}
mr.params = params
mr.pattern = pattern
return mr, nil
}
// UrlFor does another controller handler in this request function.
// it can access any controller method.
func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string {