mirror of
https://github.com/kataras/iris.git
synced 2025-12-27 23:07:03 +00:00
_future
1. Fix index, including both start and end. So Literal[start:end+1] will be a valid part. 2. Replace any with string, add file param type 3. Start of making the evaluator, starting with regexp for param types (these expression can be changed or/and overriden by user later on) Former-commit-id: ab95265f953dadbf84170b543e1ff8840f9c4a14
This commit is contained in:
27
_future/ipel/evaluator/evaluator.go
Normal file
27
_future/ipel/evaluator/evaluator.go
Normal file
@@ -0,0 +1,27 @@
|
||||
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
|
||||
}
|
||||
56
_future/ipel/evaluator/param.go
Normal file
56
_future/ipel/evaluator/param.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package evaluator
|
||||
|
||||
import (
|
||||
"gopkg.in/kataras/iris.v6/_future/ipel/ast"
|
||||
)
|
||||
|
||||
// exported to be able to change how param types are evaluating
|
||||
var ParamTypeEvaluator = make(map[ast.ParamType]ParamEvaluator, 0)
|
||||
|
||||
func init() {
|
||||
|
||||
// string type
|
||||
// anything.
|
||||
stringRegex, err := NewParamEvaluatorFromRegexp(".*")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
ParamTypeEvaluator[ast.ParamTypeString] = stringRegex
|
||||
|
||||
// int type
|
||||
// only numbers (0-9)
|
||||
numRegex, err := NewParamEvaluatorFromRegexp("[0-9]+$")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
ParamTypeEvaluator[ast.ParamTypeInt] = numRegex
|
||||
|
||||
// alphabetical/letter type
|
||||
// letters only (upper or lowercase)
|
||||
alphabeticRegex, err := NewParamEvaluatorFromRegexp("[a-zA-Z]+$")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
ParamTypeEvaluator[ast.ParamTypeAlphabetical] = alphabeticRegex
|
||||
|
||||
// file type
|
||||
// letters (upper or lowercase)
|
||||
// numbers (0-9)
|
||||
// underscore (_)
|
||||
// dash (-)
|
||||
// point (.)
|
||||
// no spaces! or other character
|
||||
fileRegex, err := NewParamEvaluatorFromRegexp("[a-zA-Z0-9_.-]*$")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
ParamTypeEvaluator[ast.ParamTypeFile] = fileRegex
|
||||
|
||||
// path type
|
||||
// file with slashes(anywhere)
|
||||
pathRegex, err := NewParamEvaluatorFromRegexp("[a-zA-Z0-9_.-/]*$")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
ParamTypeEvaluator[ast.ParamTypePath] = pathRegex
|
||||
}
|
||||
Reference in New Issue
Block a user