Files
pipeline/pipeline.go
2026-05-27 10:22:35 +08:00

37 lines
694 B
Go

package pipeline
type Next[T any] func(T) error
type Pipe[T any] func(T, Next[T]) error
type Pipeline[T any] struct {
passable T
pipes []Pipe[T]
}
func Send[T any](passable T) *Pipeline[T] {
return (&Pipeline[T]{}).Send(passable)
}
func (p *Pipeline[T]) Send(passable T) *Pipeline[T] {
p.passable = passable
return p
}
func (p *Pipeline[T]) Through(pipes ...Pipe[T]) *Pipeline[T] {
p.pipes = pipes
return p
}
func (p *Pipeline[T]) Then(destination Next[T]) error {
next := destination
for i := len(p.pipes) - 1; i >= 0; i-- {
pipe := p.pipes[i]
currentNext := next
next = func(passable T) error {
return pipe(passable, currentNext)
}
}
return next(p.passable)
}