refactor!: introduce generic processors registry and remove normalize registry adapter
- add new `processors` package with canonical `Processor` interface - add `processors.Registry` with Register/Build/BuildChain factory model - switch `pipeline.Pipeline` to `[]processors.Processor` - replace `normalize.Registry` + registry adapter with direct `normalize.Processor` - remove `normalize/registry.go` - update root docs to position normalize as one optional processing stage - add tests for processors registry, normalize processor behavior, and pipeline flow BREAKING CHANGE: - `pipeline.Processor` removed; use `processors.Processor` - `normalize.Registry` and old normalize processor adapter APIs removed - downstream daemons must update processor wiring to new `processors.Registry` and `normalize.NewProcessor(...)`
This commit is contained in:
22
processors/doc.go
Normal file
22
processors/doc.go
Normal file
@@ -0,0 +1,22 @@
|
||||
// Package processors defines feedkit's generic processor abstraction and registry.
|
||||
//
|
||||
// Processors are optional pipeline stages that can transform, drop, or reject
|
||||
// events before dispatch to sinks.
|
||||
//
|
||||
// Registry provides name-based construction so daemons can assemble processor
|
||||
// chains without embedding switch statements in wiring code.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// reg := processors.NewRegistry()
|
||||
// reg.Register("normalize", func() (processors.Processor, error) {
|
||||
// return normalize.NewProcessor(myNormalizers, false), nil
|
||||
// })
|
||||
//
|
||||
// chain, err := reg.BuildChain([]string{"normalize"})
|
||||
// if err != nil {
|
||||
// // handle wiring error
|
||||
// }
|
||||
//
|
||||
// p := &pipeline.Pipeline{Processors: chain}
|
||||
package processors
|
||||
15
processors/processor.go
Normal file
15
processors/processor.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package processors
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"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)
|
||||
}
|
||||
|
||||
// Factory constructs a configured Processor instance.
|
||||
type Factory func() (Processor, error)
|
||||
71
processors/registry.go
Normal file
71
processors/registry.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package processors
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Registry struct {
|
||||
byDriver map[string]Factory
|
||||
}
|
||||
|
||||
func NewRegistry() *Registry {
|
||||
return &Registry{byDriver: map[string]Factory{}}
|
||||
}
|
||||
|
||||
// Register associates a processor driver name with a factory.
|
||||
//
|
||||
// Register panics for empty driver names, nil factories, and duplicates.
|
||||
func (r *Registry) Register(driver string, f Factory) {
|
||||
if r == nil {
|
||||
panic("processors.Registry.Register: registry cannot be nil")
|
||||
}
|
||||
driver = strings.TrimSpace(driver)
|
||||
if driver == "" {
|
||||
panic("processors.Registry.Register: driver cannot be empty")
|
||||
}
|
||||
if f == nil {
|
||||
panic(fmt.Sprintf("processors.Registry.Register: factory cannot be nil (driver=%q)", driver))
|
||||
}
|
||||
if r.byDriver == nil {
|
||||
r.byDriver = map[string]Factory{}
|
||||
}
|
||||
if _, exists := r.byDriver[driver]; exists {
|
||||
panic(fmt.Sprintf("processors.Registry.Register: driver %q already registered", driver))
|
||||
}
|
||||
r.byDriver[driver] = f
|
||||
}
|
||||
|
||||
// Build constructs a Processor by driver name.
|
||||
func (r *Registry) Build(driver string) (Processor, error) {
|
||||
if r == nil {
|
||||
return nil, fmt.Errorf("processors registry is nil")
|
||||
}
|
||||
driver = strings.TrimSpace(driver)
|
||||
f, ok := r.byDriver[driver]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unknown processor driver: %q", driver)
|
||||
}
|
||||
|
||||
p, err := f()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("build processor %q: %w", driver, err)
|
||||
}
|
||||
if p == nil {
|
||||
return nil, fmt.Errorf("build processor %q: factory returned nil processor", driver)
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// BuildChain constructs an ordered processor chain from a driver list.
|
||||
func (r *Registry) BuildChain(drivers []string) ([]Processor, error) {
|
||||
out := make([]Processor, 0, len(drivers))
|
||||
for i, driver := range drivers {
|
||||
p, err := r.Build(driver)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("build processor chain[%d] (%q): %w", i, strings.TrimSpace(driver), err)
|
||||
}
|
||||
out = append(out, p)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
100
processors/registry_test.go
Normal file
100
processors/registry_test.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package processors
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/feedkit/event"
|
||||
)
|
||||
|
||||
type testProcessor struct {
|
||||
name string
|
||||
}
|
||||
|
||||
func (p testProcessor) Process(context.Context, event.Event) (*event.Event, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func TestRegistryRegisterValidation(t *testing.T) {
|
||||
t.Run("empty driver panics", func(t *testing.T) {
|
||||
r := NewRegistry()
|
||||
assertPanics(t, func() {
|
||||
r.Register(" ", func() (Processor, error) { return testProcessor{name: "x"}, nil })
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("nil factory panics", func(t *testing.T) {
|
||||
r := NewRegistry()
|
||||
assertPanics(t, func() {
|
||||
r.Register("normalize", nil)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("duplicate driver panics", func(t *testing.T) {
|
||||
r := NewRegistry()
|
||||
r.Register("normalize", func() (Processor, error) { return testProcessor{name: "a"}, nil })
|
||||
assertPanics(t, func() {
|
||||
r.Register("normalize", func() (Processor, error) { return testProcessor{name: "b"}, nil })
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestRegistryBuildUnknownDriver(t *testing.T) {
|
||||
r := NewRegistry()
|
||||
_, err := r.Build("does_not_exist")
|
||||
if err == nil {
|
||||
t.Fatalf("expected error for unknown driver")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "unknown processor driver") {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryBuildChainPreservesOrder(t *testing.T) {
|
||||
r := NewRegistry()
|
||||
r.Register("first", func() (Processor, error) { return testProcessor{name: "first"}, nil })
|
||||
r.Register("second", func() (Processor, error) { return testProcessor{name: "second"}, nil })
|
||||
|
||||
chain, err := r.BuildChain([]string{"first", "second"})
|
||||
if err != nil {
|
||||
t.Fatalf("BuildChain error: %v", err)
|
||||
}
|
||||
if len(chain) != 2 {
|
||||
t.Fatalf("expected 2 processors, got %d", len(chain))
|
||||
}
|
||||
|
||||
p0, ok := chain[0].(testProcessor)
|
||||
if !ok || p0.name != "first" {
|
||||
t.Fatalf("unexpected chain[0]: %#v", chain[0])
|
||||
}
|
||||
p1, ok := chain[1].(testProcessor)
|
||||
if !ok || p1.name != "second" {
|
||||
t.Fatalf("unexpected chain[1]: %#v", chain[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryBuildChainIndexedFailure(t *testing.T) {
|
||||
r := NewRegistry()
|
||||
r.Register("ok", func() (Processor, error) { return testProcessor{name: "ok"}, nil })
|
||||
r.Register("broken", func() (Processor, error) { return nil, errors.New("boom") })
|
||||
|
||||
_, err := r.BuildChain([]string{"ok", "broken"})
|
||||
if err == nil {
|
||||
t.Fatalf("expected error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "chain[1]") {
|
||||
t.Fatalf("expected indexed error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func assertPanics(t *testing.T, fn func()) {
|
||||
t.Helper()
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatalf("expected panic")
|
||||
}
|
||||
}()
|
||||
fn()
|
||||
}
|
||||
Reference in New Issue
Block a user