-
Notifications
You must be signed in to change notification settings - Fork 2
/
complie.go
39 lines (36 loc) · 914 Bytes
/
complie.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
package matchers
import (
"fmt"
"regexp"
)
func (matcher *Matcher) CompileMatchers() error {
var ok bool
computedType, err := toMatcherTypes(matcher.GetType().String())
if err != nil {
return fmt.Errorf("没有匹配规则: %s", matcher.Type)
}
matcher.matcherType = computedType
if err := matcher.Validate(); err != nil {
return err
}
if matcher.Part == "" {
matcher.Part = "body"
}
for _, regex := range matcher.Regex {
compiled, err := regexp.Compile(regex)
if err != nil {
return fmt.Errorf("could not compile regex: %s", regex)
}
matcher.regexCompiled = append(matcher.regexCompiled, compiled)
}
// Set up the condition type, if any.
if matcher.Condition != "" {
matcher.condition, ok = ConditionTypes[matcher.Condition]
if !ok {
return fmt.Errorf("unknown condition specified: %s", matcher.Condition)
}
} else {
matcher.condition = ORCondition
}
return nil
}