-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpipeline_builder.go
53 lines (47 loc) · 1.17 KB
/
pipeline_builder.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
package pipeline
import "golang.org/x/sync/errgroup"
type Builder[K any] struct {
steps []func(next StepDelegate[K]) StepDelegate[K]
}
func (t Builder[K]) Build() Pipeline[K] {
var step StepDelegate[K] = func(_ K) error {
return nil
}
for i := len(t.steps) - 1; i >= 0; i-- {
step = t.steps[i](step)
}
return Pipeline[K]{
stepDelegate: step,
}
}
func (t Builder[K]) UsePipelineStep(step Step[K]) Builder[K] {
t.steps = append(t.steps, func(next StepDelegate[K]) StepDelegate[K] {
return func(context K) error {
return step.Execute(context, next)
}
})
return t
}
func (t Builder[K]) UseConcurrentPipelineSteps(steps ...ConcurrentStep[K]) Builder[K] {
t.steps = append(t.steps, func(next StepDelegate[K]) StepDelegate[K] {
return func(context K) error {
var eg errgroup.Group
for _, step := range steps {
step := step
eg.Go(func() error {
return step.ConcurrentExecute(context)
})
}
err := eg.Wait()
if err != nil {
return err
}
return next(context)
}
})
return t
}
func (t Builder[K]) UseConditionalStepBuilder(builder *ConditionalStepBuilder[K]) Builder[K] {
t.steps = append(t.steps, builder.Build())
return t
}