-
Notifications
You must be signed in to change notification settings - Fork 34
/
request_cond.go
86 lines (77 loc) · 2.38 KB
/
request_cond.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package teaconfigs
import (
"github.com/iwind/TeaGo/types"
"github.com/iwind/TeaGo/utils/string"
"regexp"
"strings"
)
// 重写条件定义
type RequestCond struct {
Id string `yaml:"id" json:"id"` // ID
// 要测试的字符串
// 其中可以使用跟请求相关的参数,比如:
// ${arg.name}, ${requestPath}
Param string `yaml:"param" json:"param"`
// 运算符
Operator RequestCondOperator `yaml:"operator" json:"operator"`
// 对比
Value string `yaml:"value" json:"value"`
regValue *regexp.Regexp
floatValue float64
}
// 取得新对象
func NewRequestCond() *RequestCond {
return &RequestCond{
Id: stringutil.Rand(16),
}
}
// 校验配置
func (this *RequestCond) Validate() error {
if this.Operator == RequestCondOperatorRegexp || this.Operator == RequestCondOperatorNotRegexp {
reg, err := regexp.Compile(this.Value)
if err != nil {
return err
}
this.regValue = reg
} else if this.Operator == RequestCondOperatorGt || this.Operator == RequestCondOperatorGte || this.Operator == RequestCondOperatorLt || this.Operator == RequestCondOperatorLte {
this.floatValue = types.Float64(this.Value)
}
return nil
}
// 将此条件应用于请求,检查是否匹配
func (this *RequestCond) Match(formatter func(source string) string) bool {
paramValue := formatter(this.Param)
switch this.Operator {
case RequestCondOperatorRegexp:
if this.regValue == nil {
return false
}
return this.regValue.MatchString(paramValue)
case RequestCondOperatorNotRegexp:
if this.regValue == nil {
return false
}
return !this.regValue.MatchString(paramValue)
case RequestCondOperatorGt:
return types.Float64(paramValue) > this.floatValue
case RequestCondOperatorGte:
return types.Float64(paramValue) >= this.floatValue
case RequestCondOperatorLt:
return types.Float64(paramValue) < this.floatValue
case RequestCondOperatorLte:
return types.Float64(paramValue) <= this.floatValue
case RequestCondOperatorEq:
return paramValue == this.Value
case RequestCondOperatorNot:
return paramValue != this.Value
case RequestCondOperatorPrefix:
return strings.HasPrefix(paramValue, this.Value)
case RequestCondOperatorSuffix:
return strings.HasSuffix(paramValue, this.Value)
case RequestCondOperatorContains:
return strings.Contains(paramValue, this.Value)
case RequestCondOperatorNotContains:
return !strings.Contains(paramValue, this.Value)
}
return false
}