traefik/vendor/github.com/vulcand/route/parse.go

48 lines
958 B
Go
Raw Normal View History

2017-02-07 21:33:23 +00:00
package route
import (
"fmt"
"github.com/vulcand/predicate"
)
// IsValid checks whether expression is valid
func IsValid(expr string) bool {
_, err := parse(expr, &match{})
return err == nil
}
func parse(expression string, result *match) (matcher, error) {
p, err := predicate.NewParser(predicate.Def{
Functions: map[string]interface{}{
"Host": hostTrieMatcher,
"HostRegexp": hostRegexpMatcher,
"Path": pathTrieMatcher,
"PathRegexp": pathRegexpMatcher,
"Method": methodTrieMatcher,
"MethodRegexp": methodRegexpMatcher,
"Header": headerTrieMatcher,
"HeaderRegexp": headerRegexpMatcher,
},
Operators: predicate.Operators{
AND: newAndMatcher,
},
})
if err != nil {
return nil, err
}
out, err := p.Parse(expression)
if err != nil {
return nil, err
}
m, ok := out.(matcher)
if !ok {
return nil, fmt.Errorf("unknown result type: %T", out)
}
m.setMatch(result)
return m, nil
}