mirror of
https://github.com/kataras/iris.git
synced 2025-01-27 04:26:33 +01:00
28 lines
529 B
Go
28 lines
529 B
Go
|
package evaluator
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"regexp"
|
||
|
)
|
||
|
|
||
|
// final evaluator signature for both param types and param funcs
|
||
|
type ParamEvaluator func(paramValue string) bool
|
||
|
|
||
|
func NewParamEvaluatorFromRegexp(expr string) (ParamEvaluator, error) {
|
||
|
if expr == "" {
|
||
|
return nil, fmt.Errorf("empty regex expression")
|
||
|
}
|
||
|
|
||
|
// add the last $ if missing (and not wildcard(?))
|
||
|
if i := expr[len(expr)-1]; i != '$' && i != '*' {
|
||
|
expr += "$"
|
||
|
}
|
||
|
|
||
|
r, err := regexp.Compile(expr)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return r.MatchString, nil
|
||
|
}
|