-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
39 lines (33 loc) · 877 Bytes
/
process.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
package gocess
import "context"
// Process is a collection of steps.
type Process[T any] interface {
Execute(ctx context.Context, input T) (T, error)
}
// GoProcess is a bunch of steps that are executed in sequence.
type GoProcess[T any] struct {
Steps []Step[T]
}
// NewGoProcess creates a new GoProcess.
func NewGoProcess[T any](steps ...Step[T]) *GoProcess[T] {
return &GoProcess[T]{
Steps: steps,
}
}
// Execute will execute the steps in the process.
func (p *GoProcess[T]) Execute(ctx context.Context, input T) (T, error) {
var err error
for _, step := range p.Steps {
if ctx.Err() != nil {
return input, ctx.Err()
}
if input, err = step.Execute(ctx, input); err != nil {
return input, err
}
}
return input, nil
}
// AddStep adds a step to a defined GoProcess
func (p *GoProcess[T]) AddStep(step Step[T]) {
p.Steps = append(p.Steps, step)
}