Skip to content

Commit 0a25f7d

Browse files
authored
Merge pull request #2 from WoSai/hotfix/trigger_ci
补充注释
2 parents c650f20 + 916285c commit 0a25f7d

15 files changed

Lines changed: 106 additions & 10 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ on:
88
branchs:
99
- release/*
1010
- hotfix/*
11+
- master
1112
tags:
1213
- v*
1314

application/rule.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ import (
1515
)
1616

1717
var (
18+
// MockApplication 全局的mockApplication对象
1819
MockApplication *mockApplication
1920

20-
ErrRuleNotFound = errors.New("rule not founded")
21+
// ErrRuleNotFound 定义的无匹配规则时的错误
22+
ErrRuleNotFound = errors.New("rule not found")
2123
)
2224

2325
type (
26+
// AsyncJob 异步的摆渡任务接口定义
2427
AsyncJob interface {
2528
Period() time.Duration
2629
Do() error
@@ -36,6 +39,7 @@ type (
3639
}
3740
)
3841

42+
// BuildMockApplication mockApplication的工厂函数
3943
func BuildMockApplication(rr domain.RuleRepository, er domain.ExecutorRepository, job AsyncJob) *mockApplication {
4044
MockApplication = &mockApplication{rule: rr, executor: er, job: job}
4145
go func() {
@@ -140,6 +144,7 @@ func convertRegulationVO(reg *domain.Regulation) *types.RegulationDTO {
140144
return r
141145
}
142146

147+
// CreateRule 创建规则的user case
143148
func (srv *mockApplication) CreateRule(ctx context.Context, rule *types.RuleDTO) (string, error) {
144149
ru := convertRuleDTO(rule)
145150
rid, _ := ru.SupplyID()
@@ -156,6 +161,7 @@ func (srv *mockApplication) CreateRule(ctx context.Context, rule *types.RuleDTO)
156161
return rid, nil
157162
}
158163

164+
// GetRule 获取规则的user case
159165
func (srv *mockApplication) GetRule(ctx context.Context, rid string) (*types.RuleDTO, error) {
160166
re, err := srv.rule.GetRuleByID(ctx, rid)
161167
if err != nil {
@@ -170,6 +176,7 @@ func (srv *mockApplication) GetRule(ctx context.Context, rid string) (*types.Rul
170176
return rule, nil
171177
}
172178

179+
// DeleteRule 删除规则的user case
173180
func (srv *mockApplication) DeleteRule(ctx context.Context, rid string) error {
174181
if err := srv.rule.DeleteRule(ctx, rid); err != nil {
175182
misc.Logger.Error("failed to delete rule entity", zap.String("rule_id", rid), zap.Error(err))
@@ -178,6 +185,7 @@ func (srv *mockApplication) DeleteRule(ctx context.Context, rid string) error {
178185
return nil
179186
}
180187

188+
// PutRule 全量更新规则的user case
181189
func (srv *mockApplication) PutRule(ctx context.Context, rule *types.RuleDTO) error {
182190
or, err := srv.rule.GetRuleByID(ctx, rule.ID)
183191
if err != nil {
@@ -198,6 +206,7 @@ func (srv *mockApplication) PutRule(ctx context.Context, rule *types.RuleDTO) er
198206
return nil
199207
}
200208

209+
// PatchRule 部分更新规则的user case
201210
func (srv *mockApplication) PatchRule(ctx context.Context, rule *types.RuleDTO) error {
202211
or, err := srv.rule.GetRuleByID(ctx, rule.ID)
203212
if err != nil {
@@ -218,6 +227,7 @@ func (srv *mockApplication) PatchRule(ctx context.Context, rule *types.RuleDTO)
218227
return nil
219228
}
220229

230+
// Export 导出的user case
221231
func (srv *mockApplication) Export(ctx context.Context) ([]*types.RuleDTO, error) {
222232
res, err := srv.rule.Export(ctx)
223233
if err != nil {
@@ -236,6 +246,7 @@ func (srv *mockApplication) Export(ctx context.Context) ([]*types.RuleDTO, error
236246
return rules, nil
237247
}
238248

249+
// Import 导入规则的user case
239250
func (srv *mockApplication) Import(ctx context.Context, rules ...*types.RuleDTO) error {
240251
res := make([]*domain.Rule, len(rules))
241252
for index, rule := range rules {
@@ -255,6 +266,7 @@ func (srv *mockApplication) Import(ctx context.Context, rules ...*types.RuleDTO)
255266
return nil
256267
}
257268

269+
// MockAPI Mock接口的user case
258270
func (srv *mockApplication) MockAPI(ctx *fasthttp.RequestCtx) error {
259271
index := atomic.AddUint64(&srv.counter, 1)
260272
misc.Logger.Info("received request", zap.Uint64("index", index), zap.ByteString("path", ctx.Request.URI().Path()), zap.ByteString("method", ctx.Request.Header.Method()))

client/client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,31 @@ import (
88
)
99

1010
type (
11+
// DeepMockClient Client对象
1112
DeepMockClient struct {
1213
url string
1314
client *requests.Session
1415
}
1516

17+
// DeepMockError client error结构体
1618
DeepMockError struct {
1719
code int
1820
err string
1921
}
2022

23+
// Response client通用的返回响应
2124
Response struct {
2225
Code int `json:"code"`
2326
ErrorMessage string `json:"err_msg,omitempty"`
2427
}
2528

29+
// RuleResponse 单规则接口返回报文
2630
RuleResponse struct {
2731
Response
2832
Data *types.RuleDO `json:"data,omitempty"`
2933
}
3034

35+
// RulesResponse 多规则接口返回报文
3136
RulesResponse struct {
3237
Response
3338
Data []*types.RuleDO `json:"data,omitempty"`
@@ -41,17 +46,20 @@ const (
4146
returnCodeOK = 200
4247
)
4348

49+
// NewDeepMockError client error工厂函数
4450
func NewDeepMockError(res Response) *DeepMockError {
4551
return &DeepMockError{
4652
code: res.Code,
4753
err: res.ErrorMessage,
4854
}
4955
}
5056

57+
// Error error的实现
5158
func (e *DeepMockError) Error() string {
5259
return fmt.Sprintf("[%d]: %s", e.code, e.err)
5360
}
5461

62+
// NewDeepMockClient client的工厂函数
5563
func NewDeepMockClient(url string) *DeepMockClient {
5664
session := requests.NewSession(requests.Option{Name: "DeepMock Go Client"})
5765
return &DeepMockClient{
@@ -60,6 +68,7 @@ func NewDeepMockClient(url string) *DeepMockClient {
6068
}
6169
}
6270

71+
// CreateMockRule 创建规则接口
6372
func (c *DeepMockClient) CreateMockRule(rule *types.RuleDO) (*types.RuleDO, error) {
6473
res := new(RuleResponse)
6574
_, _, err := c.client.Post(c.url+entrypointRule, requests.Params{Json: rule}, requests.UnmarshalJSONResponse(res))
@@ -72,6 +81,7 @@ func (c *DeepMockClient) CreateMockRule(rule *types.RuleDO) (*types.RuleDO, erro
7281
return res.Data, nil
7382
}
7483

84+
// DeleteMockRule 删除规则接口
7585
func (c *DeepMockClient) DeleteMockRule(rid string) error {
7686
res := new(RuleResponse)
7787
_, _, err := c.client.Delete(c.url+entrypointRule, requests.Params{Json: requests.Any{"id": rid}}, requests.UnmarshalJSONResponse(res))
@@ -84,6 +94,7 @@ func (c *DeepMockClient) DeleteMockRule(rid string) error {
8494
return nil
8595
}
8696

97+
// GetMockRule 获取规则接口
8798
func (c *DeepMockClient) GetMockRule(rid string) (*types.RuleDO, error) {
8899
res := new(RuleResponse)
89100
_, _, err := c.client.Get(c.url+entrypointRule+"/"+rid, requests.Params{}, requests.UnmarshalJSONResponse(res))
@@ -96,6 +107,7 @@ func (c *DeepMockClient) GetMockRule(rid string) (*types.RuleDO, error) {
96107
return res.Data, nil
97108
}
98109

110+
// PutMockRule 全量更新规则接口
99111
func (c *DeepMockClient) PutMockRule(rule *types.RuleDO) (*types.RuleDO, error) {
100112
res := new(RuleResponse)
101113
_, _, err := c.client.Put(c.url+entrypointRule, requests.Params{Json: rule}, requests.UnmarshalJSONResponse(res))
@@ -108,6 +120,7 @@ func (c *DeepMockClient) PutMockRule(rule *types.RuleDO) (*types.RuleDO, error)
108120
return res.Data, nil
109121
}
110122

123+
// PatchMockRule 部分更新规则接口
111124
func (c *DeepMockClient) PatchMockRule(rule *types.RuleDO) (*types.RuleDO, error) {
112125
res := new(RuleResponse)
113126
_, _, err := c.client.Patch(c.url+entrypointRule, requests.Params{Json: rule}, requests.UnmarshalJSONResponse(res))
@@ -120,6 +133,7 @@ func (c *DeepMockClient) PatchMockRule(rule *types.RuleDO) (*types.RuleDO, error
120133
return res.Data, nil
121134
}
122135

136+
// ExportRules 导出所有规则
123137
func (c *DeepMockClient) ExportRules() ([]*types.RuleDO, error) {
124138
res := new(RulesResponse)
125139
_, _, err := c.client.Get(c.url+entrypointRules, requests.Params{}, requests.UnmarshalJSONResponse(res))
@@ -132,6 +146,7 @@ func (c *DeepMockClient) ExportRules() ([]*types.RuleDO, error) {
132146
return res.Data, nil
133147
}
134148

149+
// ImportRules 导入规则
135150
func (c *DeepMockClient) ImportRules(rules ...*types.RuleDO) error {
136151
res := new(RulesResponse)
137152
_, _, err := c.client.Post(c.url+entrypointRules, requests.Params{Json: rules}, requests.UnmarshalJSONResponse(res))

domain/executor.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const (
2424
// FilterModeRegular 正则表达式模式
2525
FilterModeRegular FilterMode = "regular"
2626

27+
// ModeField 筛选模式的字段名称
2728
ModeField = "mode"
2829
)
2930

@@ -32,8 +33,10 @@ var (
3233
)
3334

3435
type (
36+
// FilterMode 筛选模式定义
3537
FilterMode = string
3638

39+
// Executor 规则执行器
3740
Executor struct {
3841
ID string
3942
Method []byte
@@ -44,20 +47,24 @@ type (
4447
Version int
4548
}
4649

50+
// WeightPicker 权重随机值选择器
4751
WeightPicker map[string]*WeightDice
4852

53+
// WeightDice 权重随机值对象
4954
WeightDice struct {
5055
total int
5156
distribution []string
5257
factor map[string]uint
5358
}
5459

60+
// RegulationExecutor 报文规则执行器
5561
RegulationExecutor struct {
5662
IsDefault bool
5763
Filter *FilterExecutor
5864
Template *TemplateExecutor
5965
}
6066

67+
// TemplateExecutor 响应报文模板执行器
6168
TemplateExecutor struct {
6269
IsGolangTemplate bool
6370
IsBinData bool
@@ -66,6 +73,7 @@ type (
6673
body []byte
6774
}
6875

76+
// RenderContext 动态渲染的上下文
6977
RenderContext struct {
7078
Variable map[string]interface{}
7179
Weight map[string]string
@@ -75,31 +83,36 @@ type (
7583
Json map[string]interface{}
7684
}
7785

86+
// FilterExecutor 筛选执行器
7887
FilterExecutor struct {
7988
Query *QueryFilterExecutor
8089
Header *HeaderFilterExecutor
8190
Body *BodyFilterExecutor
8291
}
8392

93+
// BodyFilterExecutor Body报文筛选执行器
8494
BodyFilterExecutor struct {
8595
mode FilterMode
8696
regular *regexp.Regexp
8797
keyword []byte
8898
}
8999

100+
// HeaderFilterExecutor 请求头筛选执行器
90101
HeaderFilterExecutor struct {
91102
params map[string][]byte
92103
mode FilterMode
93104
regulars map[string]*regexp.Regexp
94105
}
95106

107+
// QueryFilterExecutor Query参数筛选执行器
96108
QueryFilterExecutor struct {
97109
params map[string][]byte
98110
mode FilterMode
99111
regulars map[string]*regexp.Regexp
100112
}
101113
)
102114

115+
// DiceAll 返回所有权重因子的值
103116
func (wp WeightPicker) DiceAll() map[string]string {
104117
ret := make(map[string]string)
105118
for k, v := range wp {
@@ -108,6 +121,7 @@ func (wp WeightPicker) DiceAll() map[string]string {
108121
return ret
109122
}
110123

124+
// Dice 更具权重值随机返回某个值
111125
func (wd *WeightDice) Dice() string {
112126
return wd.distribution[rand.Intn(wd.total)]
113127
}
@@ -139,6 +153,7 @@ func (hfe *HeaderFilterExecutor) filterByRegular(header *fasthttp.RequestHeader)
139153
return true
140154
}
141155

156+
// Filter 筛选函数
142157
func (hfe *HeaderFilterExecutor) Filter(header *fasthttp.RequestHeader) bool {
143158
if hfe == nil {
144159
return true
@@ -189,6 +204,7 @@ func (qfe *QueryFilterExecutor) filterByRegular(args *fasthttp.Args) bool {
189204
return true
190205
}
191206

207+
// Filter 筛选函数
192208
func (qfe *QueryFilterExecutor) Filter(args *fasthttp.Args) bool {
193209
if qfe == nil {
194210
return true
@@ -212,6 +228,7 @@ func (qfe *QueryFilterExecutor) Filter(args *fasthttp.Args) bool {
212228
}
213229
}
214230

231+
// Filter 筛选函数
215232
func (bfe *BodyFilterExecutor) Filter(body []byte) bool {
216233
if bfe == nil {
217234
return true
@@ -232,6 +249,7 @@ func (bfe *BodyFilterExecutor) Filter(body []byte) bool {
232249
}
233250
}
234251

252+
// Filter 筛选函数
235253
func (fe *FilterExecutor) Filter(request *fasthttp.Request) bool {
236254
if fe == nil {
237255
return true
@@ -249,6 +267,7 @@ func (fe *FilterExecutor) Filter(request *fasthttp.Request) bool {
249267
return true
250268
}
251269

270+
// Render 渲染函数
252271
func (te *TemplateExecutor) Render(ctx *fasthttp.RequestCtx, v map[string]interface{}, weight map[string]string) error {
253272
te.header.CopyTo(&ctx.Response.Header)
254273
if !te.IsGolangTemplate {
@@ -271,22 +290,20 @@ func (te *TemplateExecutor) Render(ctx *fasthttp.RequestCtx, v map[string]interf
271290
return te.template.Execute(ctx.Response.BodyWriter(), rc)
272291
}
273292

293+
// Render 渲染函数
274294
func (re *RegulationExecutor) Render(ctx *fasthttp.RequestCtx, v map[string]interface{}, w map[string]string) error {
275295
return re.Template.Render(ctx, v, w)
276296
}
277297

278-
// todo:
279-
func NewExecutor() (*Executor, error) {
280-
return nil, nil
281-
}
282-
298+
// Match 请求匹配函数
283299
func (exe *Executor) Match(path, method []byte) bool {
284300
if bytes.Compare(method, exe.Method) != 0 {
285301
return false
286302
}
287303
return exe.Path.Match(path)
288304
}
289305

306+
// FindRegulationExecutor 查找符合的报文规则执行器
290307
func (exe *Executor) FindRegulationExecutor(request *fasthttp.Request) *RegulationExecutor {
291308
var reg *RegulationExecutor
292309

domain/repository.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package domain
33
import "context"
44

55
type (
6+
// RuleRepository 规则存储库接口定义
67
RuleRepository interface {
78
CreateRule(context.Context, *Rule) error
89
UpdateRule(context.Context, *Rule) error
@@ -12,9 +13,9 @@ type (
1213
Import(context.Context, ...*Rule) error
1314
}
1415

16+
// ExecutorRepository 执行器接口定义
1517
ExecutorRepository interface {
1618
FindExecutor(context.Context, []byte, []byte) (*Executor, bool)
1719
ImportAll(context.Context, ...*Executor)
18-
Purge(context.Context)
1920
}
2021
)

0 commit comments

Comments
 (0)