-
Notifications
You must be signed in to change notification settings - Fork 11
/
parse.go
51 lines (40 loc) · 963 Bytes
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
}