package pipeline import ( "fmt" "sort" "strings" "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" ) type ValidatorConstructor func() (contracts.Validator, error) type ValidatorSpec struct { Key string `json:"key"` ExecutionClass contracts.ExecutionClass `json:"execution_class"` } type ValidatorRegistry struct { constructors map[string]ValidatorConstructor specs map[string]ValidatorSpec } func NewValidatorRegistry() *ValidatorRegistry { return &ValidatorRegistry{ constructors: make(map[string]ValidatorConstructor), specs: make(map[string]ValidatorSpec), } } func (r *ValidatorRegistry) Register(key string, constructor ValidatorConstructor) error { return r.RegisterWithSpec(ValidatorSpec{Key: key, ExecutionClass: contracts.ExecutionClassDeterministic}, constructor) } func (r *ValidatorRegistry) RegisterWithSpec(spec ValidatorSpec, constructor ValidatorConstructor) error { if r == nil { return fmt.Errorf("validator registry must not be nil") } normalizedSpec, err := normalizeValidatorSpec(spec) if err != nil { return err } if constructor == nil { return fmt.Errorf("validator constructor for %q must not be nil", normalizedSpec.Key) } if _, ok := r.constructors[normalizedSpec.Key]; ok { return fmt.Errorf("validator %q is already registered", normalizedSpec.Key) } if r.constructors == nil { r.constructors = make(map[string]ValidatorConstructor) } if r.specs == nil { r.specs = make(map[string]ValidatorSpec) } r.constructors[normalizedSpec.Key] = constructor r.specs[normalizedSpec.Key] = normalizedSpec return nil } func (r *ValidatorRegistry) Build(key string) (contracts.Validator, error) { if r == nil { return nil, fmt.Errorf("validator registry must not be nil") } normalizedKey := strings.TrimSpace(key) if normalizedKey == "" { return nil, fmt.Errorf("validator key must not be empty") } constructor, ok := r.constructors[normalizedKey] if !ok { return nil, fmt.Errorf("validator %q is not registered", normalizedKey) } validator, err := constructor() if err != nil { return nil, fmt.Errorf("build validator %q: %w", normalizedKey, err) } if validator == nil { return nil, fmt.Errorf("validator %q constructor returned nil", normalizedKey) } if validator.Name() != normalizedKey { return nil, fmt.Errorf("validator %q returned name %q", normalizedKey, validator.Name()) } spec, ok := r.specs[normalizedKey] if !ok { return nil, fmt.Errorf("validator %q spec is not registered", normalizedKey) } if validator.ExecutionClass() != spec.ExecutionClass { return nil, fmt.Errorf("validator %q returned execution class %q, want %q", normalizedKey, validator.ExecutionClass(), spec.ExecutionClass) } return validator, nil } func (r *ValidatorRegistry) Spec(key string) (ValidatorSpec, bool) { if r == nil { return ValidatorSpec{}, false } spec, ok := r.specs[strings.TrimSpace(key)] if !ok { return ValidatorSpec{}, false } return spec, true } func (r *ValidatorRegistry) RegisteredSpecs() []ValidatorSpec { if r == nil || len(r.specs) == 0 { return nil } keys := make([]string, 0, len(r.specs)) for key := range r.specs { keys = append(keys, key) } sort.Strings(keys) specs := make([]ValidatorSpec, 0, len(keys)) for _, key := range keys { specs = append(specs, r.specs[key]) } return specs } func (r *ValidatorRegistry) RegisteredKeys() []string { if r == nil { return nil } return sortedRegistryKeys(r.constructors) } func normalizeValidatorSpec(spec ValidatorSpec) (ValidatorSpec, error) { normalized := ValidatorSpec{ Key: strings.TrimSpace(spec.Key), ExecutionClass: spec.ExecutionClass, } if normalized.Key == "" { return ValidatorSpec{}, fmt.Errorf("validator key must not be empty") } switch normalized.ExecutionClass { case contracts.ExecutionClassDeterministic, contracts.ExecutionClassLLMBacked: default: return ValidatorSpec{}, fmt.Errorf("validator %q execution class %q is not supported", normalized.Key, normalized.ExecutionClass) } return normalized, nil }