-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathworkflow.go
More file actions
82 lines (72 loc) · 1.69 KB
/
Copy pathworkflow.go
File metadata and controls
82 lines (72 loc) · 1.69 KB
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
package xmux
import (
"context"
"net/http"
)
// ====================== 增强版流程基类:支持 顺序 + 分支 ======================
type BaseFlow struct {
W http.ResponseWriter
R *http.Request
Ctx context.Context
Ins *FlowData
err error
}
func (b *BaseFlow) Init(w http.ResponseWriter, r *http.Request) {
b.W = w
b.R = r
b.Ctx = r.Context()
b.Ins = GetInstance(r)
b.err = nil
}
// ---------------------- 核心能力 ----------------------
// 1. 顺序执行(原来的)
func (b *BaseFlow) Then(fns ...func() error) *BaseFlow {
if b.err != nil {
return b
}
for _, fn := range fns {
if err := fn(); err != nil {
b.err = err
return b
}
}
return b
}
// 2. 条件分支:满足条件才执行(解决你的判断/分支需求)
func (b *BaseFlow) If(cond bool, fns ...func() error) *BaseFlow {
if b.err != nil || !cond {
return b
}
return b.Then(fns...)
}
// 3. 二选一分支:if / else
func (b *BaseFlow) IfElse(cond bool, do func() error, elseDo func() error) *BaseFlow {
if b.err != nil {
return b
}
if cond {
return b.Then(do)
}
return b.Then(elseDo)
}
// 4. 任意条件跳过(满足则跳过本段)
func (b *BaseFlow) SkipIf(cond bool, fns ...func() error) *BaseFlow {
if b.err != nil || cond {
return b
}
return b.Then(fns...)
}
// ---------------------- 错误 ----------------------
func (b *BaseFlow) Err() error { return b.err }
// ====================== 接口与适配器(不变) ======================
type Flow interface {
Init(w http.ResponseWriter, r *http.Request)
Run()
}
func Adapt(newFlow func() Flow) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
f := newFlow()
f.Init(w, r)
f.Run()
}
}