feedkit: split the former maximumdirect.net/weatherd project in two.
feedkit now contains a reusable core, while weatherfeeder is a concrete implementation that includes weather-specific functions.
This commit is contained in:
43
pipeline/pipeline.go
Normal file
43
pipeline/pipeline.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/feedkit/event"
|
||||
)
|
||||
|
||||
// Processor can mutate/drop events (dedupe, rate-limit, normalization tweaks).
|
||||
type Processor interface {
|
||||
Process(ctx context.Context, in event.Event) (out *event.Event, err error)
|
||||
}
|
||||
|
||||
type Pipeline struct {
|
||||
Processors []Processor
|
||||
}
|
||||
|
||||
func (p *Pipeline) Process(ctx context.Context, e event.Event) (*event.Event, error) {
|
||||
if err := e.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("pipeline: invalid input event: %w", err)
|
||||
}
|
||||
|
||||
cur := &e
|
||||
|
||||
for _, proc := range p.Processors {
|
||||
out, err := proc.Process(ctx, *cur)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if out == nil {
|
||||
// Dropped by policy.
|
||||
return nil, nil
|
||||
}
|
||||
cur = out
|
||||
}
|
||||
|
||||
if err := cur.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("pipeline: invalid output event: %w", err)
|
||||
}
|
||||
|
||||
return cur, nil
|
||||
}
|
||||
Reference in New Issue
Block a user