package pipeline import ( "fmt" "sort" "strings" "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" ) type InputAdapterConstructor func() (contracts.InputAdapter, error) type InputAdapterRegistry struct { constructors map[string]InputAdapterConstructor } func NewInputAdapterRegistry() *InputAdapterRegistry { return &InputAdapterRegistry{ constructors: make(map[string]InputAdapterConstructor), } } func (r *InputAdapterRegistry) Register(key string, constructor InputAdapterConstructor) error { if r == nil { return fmt.Errorf("input adapter registry must not be nil") } normalizedKey := strings.TrimSpace(key) if normalizedKey == "" { return fmt.Errorf("input adapter key must not be empty") } if constructor == nil { return fmt.Errorf("input adapter constructor for %q must not be nil", normalizedKey) } if _, ok := r.constructors[normalizedKey]; ok { return fmt.Errorf("input adapter %q is already registered", normalizedKey) } r.constructors[normalizedKey] = constructor return nil } func (r *InputAdapterRegistry) Build(key string) (contracts.InputAdapter, error) { if r == nil { return nil, fmt.Errorf("input adapter registry must not be nil") } normalizedKey := strings.TrimSpace(key) if normalizedKey == "" { return nil, fmt.Errorf("input adapter key must not be empty") } constructor, ok := r.constructors[normalizedKey] if !ok { return nil, fmt.Errorf("input adapter %q is not registered", normalizedKey) } adapter, err := constructor() if err != nil { return nil, fmt.Errorf("build input adapter %q: %w", normalizedKey, err) } if adapter == nil { return nil, fmt.Errorf("input adapter %q constructor returned nil", normalizedKey) } if adapter.Key() != normalizedKey { return nil, fmt.Errorf("input adapter %q returned key %q", normalizedKey, adapter.Key()) } return adapter, nil } func (r *InputAdapterRegistry) RegisteredKeys() []string { if r == nil { return nil } keys := make([]string, 0, len(r.constructors)) for key := range r.constructors { keys = append(keys, key) } sort.Strings(keys) return keys }