First init

This commit is contained in:
SuperK
2026-05-27 10:22:35 +08:00
commit beb00ad078
2 changed files with 39 additions and 0 deletions

36
pipeline.go Normal file
View File

@@ -0,0 +1,36 @@
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)
}