-
Notifications
You must be signed in to change notification settings - Fork 1
/
step.go
274 lines (240 loc) · 5.31 KB
/
step.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package step
import "github.com/webx-top/echo"
// NewGroup 创建新的步骤集
func NewGroup() *Steps {
return &Steps{Detail: map[string]*Step{}}
}
// New 创建新的步骤
func New(stepName string, titleAndDescription ...string) *Step {
var (
title string
description string
)
switch len(titleAndDescription) {
case 2:
description = titleAndDescription[1]
case 1:
title = titleAndDescription[0]
}
return &Step{Name: stepName, Title: title, Description: description, Config: echo.H{}}
}
// Processor 步骤处理逻辑
type Processor func(echo.Context) error
// Step 单个步骤信息
type Step struct {
Name string // 英文标识名
Title string // 步骤标题
Description string // 步骤说明
Active bool // 是否已完成步骤
Index int
Config echo.H
processors []Processor
}
func (s *Step) SetTitle(title string) *Step {
s.Title = title
return s
}
func (s *Step) SetDescription(description string) *Step {
s.Description = description
return s
}
func (s *Step) SetActive(active bool) *Step {
s.Active = active
return s
}
func (s *Step) SetActiveByCurrentIndex(currentIndex int) *Step {
s.Active = currentIndex >= s.Index
return s
}
func (s *Step) SetConfig(config echo.H) *Step {
s.Config = config
return s
}
func (s *Step) SetConfigKV(key string, value interface{}) *Step {
s.Config.Set(key, value)
return s
}
// Next 下一步
func (s *Step) Next(ss *Steps) *Step {
return ss.Next(s.Index)
}
// Prev 上一步
func (s *Step) Prev(ss *Steps) *Step {
return ss.Prev(s.Index)
}
// Process 执行处理逻辑
func (s *Step) Process(ctx echo.Context) error {
for _, processor := range s.processors {
if err := processor(ctx); err != nil {
return err
}
}
return nil
}
// AddProcessor 添加处理逻辑
func (s *Step) AddProcessor(processors ...Processor) *Step {
s.processors = append(s.processors, processors...)
return s
}
// Steps 步骤集合
type Steps struct {
Idents []string
Detail map[string]*Step
}
// Add 添加步骤
func (s *Steps) Add(stepName string, processors ...Processor) *Steps {
if step, found := s.Detail[stepName]; !found {
s.Detail[stepName] = &Step{
Name: stepName,
Index: len(s.Idents),
Config: echo.H{},
processors: processors,
}
s.Idents = append(s.Idents, stepName)
} else {
step.processors = append(step.processors, processors...)
}
return s
}
// AddStep 添加步骤
func (s *Steps) AddStep(step *Step) *Steps {
_, found := s.Detail[step.Name]
if found {
panic(`The step already exists: ` + step.Name)
}
step.Index = len(s.Idents)
s.Detail[step.Name] = step
s.Idents = append(s.Idents, step.Name)
return s
}
// Set 设置步骤
func (s *Steps) Set(stepName string, processors ...Processor) *Steps {
if step, found := s.Detail[stepName]; !found {
s.Detail[stepName] = &Step{
Name: stepName,
Index: len(s.Idents),
Config: echo.H{},
processors: processors,
}
s.Idents = append(s.Idents, stepName)
} else {
step.processors = processors
}
return s
}
// SetStep 设置步骤
func (s *Steps) SetStep(step *Step) *Steps {
if old, found := s.Detail[step.Name]; !found {
step.Index = len(s.Idents)
s.Detail[step.Name] = step
s.Idents = append(s.Idents, step.Name)
} else {
step.Index = old.Index
s.Detail[step.Name] = step
}
return s
}
// Get 根据名称获取
func (s *Steps) Get(stepName string) *Step {
if step, found := s.Detail[stepName]; found {
return step
}
return nil
}
// GetAny 根据名称获取
func (s *Steps) GetAny(stepNames ...string) *Step {
for _, stepName := range stepNames {
if step, found := s.Detail[stepName]; found {
return step
}
}
return nil
}
// First 第一步
func (s *Steps) First() *Step {
if len(s.Idents) == 0 {
return nil
}
return s.Get(s.Idents[0])
}
// Next 下一步
func (s *Steps) Next(index int) *Step {
next := index + 1
if len(s.Idents) <= next {
return nil
}
return s.Get(s.Idents[next])
}
// NextByName 下一步
func (s *Steps) NextByName(stepName string) *Step {
step := s.Get(stepName)
if step == nil {
return nil
}
return s.Next(step.Index)
}
// Prev 上一步
func (s *Steps) Prev(index int) *Step {
prev := index - 1
if prev < 1 || len(s.Idents) <= prev {
return nil
}
return s.Get(s.Idents[prev])
}
// PrevByName 上一步
func (s *Steps) PrevByName(stepName string) *Step {
step := s.Get(stepName)
if step == nil {
return nil
}
return s.Prev(step.Index)
}
// Last 最后一步
func (s *Steps) Last() *Step {
if len(s.Idents) == 0 {
return nil
}
return s.Get(s.Idents[len(s.Idents)-1])
}
// Size 步骤总数
func (s *Steps) Size() int {
return len(s.Idents)
}
// Slice 按顺序返回所有步骤
func (s *Steps) Slice() []*Step {
r := make([]*Step, s.Size())
for index, name := range s.Idents {
step := s.Get(name)
if step == nil {
r[index] = nil
} else {
stepCopy := *step
r[index] = &stepCopy
}
}
return r
}
func (s *Steps) GetTitle(stepName string) string {
step := s.Get(stepName)
if step == nil {
return ``
}
return step.Title
}
func (s *Steps) GetDescription(stepName string) string {
step := s.Get(stepName)
if step == nil {
return ``
}
return step.Description
}
func (s *Steps) GetIndex(stepName string) int {
step := s.Get(stepName)
if step == nil {
return -1
}
return step.Index
}
func (s *Steps) Max() int {
return s.Size() - 1
}