allow multiple rules
This commit is contained in:
parent
c5ac563e74
commit
84076db78e
1 changed files with 44 additions and 30 deletions
74
rules.go
74
rules.go
|
@ -109,39 +109,53 @@ func (r *Rules) Parse(expression string) (*mux.Route, error) {
|
||||||
f := func(c rune) bool {
|
f := func(c rune) bool {
|
||||||
return c == ':'
|
return c == ':'
|
||||||
}
|
}
|
||||||
// get function
|
|
||||||
parsedFunctions := strings.FieldsFunc(expression, f)
|
// Allow multiple rules separated by ;
|
||||||
if len(parsedFunctions) == 0 {
|
splitRule := func(c rune) bool {
|
||||||
return nil, errors.New("Error parsing rule: " + expression)
|
return c == ';'
|
||||||
}
|
|
||||||
parsedFunction, ok := functions[parsedFunctions[0]]
|
|
||||||
if !ok {
|
|
||||||
return nil, errors.New("Error parsing rule: " + expression + ". Unknown function: " + parsedFunctions[0])
|
|
||||||
}
|
|
||||||
parsedFunctions = append(parsedFunctions[:0], parsedFunctions[1:]...)
|
|
||||||
fargs := func(c rune) bool {
|
|
||||||
return c == ',' || c == ';'
|
|
||||||
}
|
|
||||||
// get function
|
|
||||||
parsedArgs := strings.FieldsFunc(strings.Join(parsedFunctions, ":"), fargs)
|
|
||||||
if len(parsedArgs) == 0 {
|
|
||||||
return nil, errors.New("Error parsing args from rule: " + expression)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inputs := make([]reflect.Value, len(parsedArgs))
|
parsedRules := strings.FieldsFunc(expression, splitRule)
|
||||||
for i := range parsedArgs {
|
|
||||||
inputs[i] = reflect.ValueOf(parsedArgs[i])
|
var resultRoute *mux.Route
|
||||||
}
|
|
||||||
method := reflect.ValueOf(parsedFunction)
|
for _, rule := range parsedRules {
|
||||||
if method.IsValid() {
|
// get function
|
||||||
resultRoute := method.Call(inputs)[0].Interface().(*mux.Route)
|
parsedFunctions := strings.FieldsFunc(rule, f)
|
||||||
if r.err != nil {
|
if len(parsedFunctions) == 0 {
|
||||||
return nil, r.err
|
return nil, errors.New("Error parsing rule: " + rule)
|
||||||
}
|
}
|
||||||
if resultRoute.GetError() != nil {
|
parsedFunction, ok := functions[parsedFunctions[0]]
|
||||||
return nil, resultRoute.GetError()
|
if !ok {
|
||||||
|
return nil, errors.New("Error parsing rule: " + rule + ". Unknown function: " + parsedFunctions[0])
|
||||||
|
}
|
||||||
|
parsedFunctions = append(parsedFunctions[:0], parsedFunctions[1:]...)
|
||||||
|
fargs := func(c rune) bool {
|
||||||
|
return c == ','
|
||||||
|
}
|
||||||
|
// get function
|
||||||
|
parsedArgs := strings.FieldsFunc(strings.Join(parsedFunctions, ":"), fargs)
|
||||||
|
if len(parsedArgs) == 0 {
|
||||||
|
return nil, errors.New("Error parsing args from rule: " + rule)
|
||||||
|
}
|
||||||
|
|
||||||
|
inputs := make([]reflect.Value, len(parsedArgs))
|
||||||
|
for i := range parsedArgs {
|
||||||
|
inputs[i] = reflect.ValueOf(parsedArgs[i])
|
||||||
|
}
|
||||||
|
method := reflect.ValueOf(parsedFunction)
|
||||||
|
if method.IsValid() {
|
||||||
|
resultRoute = method.Call(inputs)[0].Interface().(*mux.Route)
|
||||||
|
if r.err != nil {
|
||||||
|
return nil, r.err
|
||||||
|
}
|
||||||
|
if resultRoute.GetError() != nil {
|
||||||
|
return nil, resultRoute.GetError()
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return nil, errors.New("Method not found: " + parsedFunctions[0])
|
||||||
}
|
}
|
||||||
return resultRoute, nil
|
|
||||||
}
|
}
|
||||||
return nil, errors.New("Method not found: " + parsedFunctions[0])
|
return resultRoute, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue