242 lines
8.7 KiB
Go
242 lines
8.7 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
)
|
|
|
|
type LegacyRawExtractorConstructor func() (contracts.LegacyRawExtractor, error)
|
|
type LegacyRawExtractorBuilder func(BuildRequest) (contracts.LegacyRawExtractor, error)
|
|
|
|
type ExtractorRegistry struct {
|
|
legacyBuilders map[string]LegacyRawExtractorBuilder
|
|
legacyValidators map[string]OptionValidator
|
|
typedEntries map[string]typedExtractorEntry
|
|
specs map[string]ModuleSpec
|
|
}
|
|
|
|
type typedExtractorEntry struct {
|
|
spec ModuleSpec
|
|
valueType reflect.Type
|
|
validateOptions OptionValidator
|
|
builder func(BuildRequest) (any, error)
|
|
extract typedExtractOperation
|
|
rawBuilder LegacyRawExtractorBuilder
|
|
}
|
|
|
|
func NewExtractorRegistry() *ExtractorRegistry {
|
|
return &ExtractorRegistry{
|
|
legacyBuilders: make(map[string]LegacyRawExtractorBuilder),
|
|
legacyValidators: make(map[string]OptionValidator),
|
|
typedEntries: make(map[string]typedExtractorEntry),
|
|
specs: make(map[string]ModuleSpec),
|
|
}
|
|
}
|
|
|
|
func (r *ExtractorRegistry) RegisterLegacyRaw(key string, constructor LegacyRawExtractorConstructor) error {
|
|
return r.RegisterLegacyRawWithSpec(defaultModuleSpec(key, StageExtract), constructor)
|
|
}
|
|
|
|
func (r *ExtractorRegistry) RegisterLegacyRawWithSpec(spec ModuleSpec, constructor LegacyRawExtractorConstructor) error {
|
|
if constructor == nil {
|
|
return fmt.Errorf("extractor constructor for %q must not be nil", strings.TrimSpace(spec.Key))
|
|
}
|
|
return r.RegisterLegacyRawBuilderWithSpec(spec, allowLegacyOptions, func(BuildRequest) (contracts.LegacyRawExtractor, error) {
|
|
return constructor()
|
|
})
|
|
}
|
|
|
|
func (r *ExtractorRegistry) RegisterLegacyRawBuilderWithSpec(spec ModuleSpec, validateOptions OptionValidator, builder LegacyRawExtractorBuilder) error {
|
|
if r == nil {
|
|
return fmt.Errorf("extractor registry must not be nil")
|
|
}
|
|
normalizedSpec := normalizeModuleSpec(spec)
|
|
if err := validateModuleSpec("extractor", StageExtract, normalizedSpec); err != nil {
|
|
return err
|
|
}
|
|
if normalizedSpec.ArtifactKind != "" {
|
|
return fmt.Errorf("legacy raw extractor %q must not declare an artifact kind", normalizedSpec.Key)
|
|
}
|
|
if validateOptions == nil {
|
|
return fmt.Errorf("extractor option validator for %q must not be nil", normalizedSpec.Key)
|
|
}
|
|
if builder == nil {
|
|
return fmt.Errorf("extractor builder for %q must not be nil", normalizedSpec.Key)
|
|
}
|
|
if _, ok := r.specs[normalizedSpec.Key]; ok {
|
|
return fmt.Errorf("extractor %q is already registered", normalizedSpec.Key)
|
|
}
|
|
if r.legacyBuilders == nil {
|
|
r.legacyBuilders = make(map[string]LegacyRawExtractorBuilder)
|
|
}
|
|
if r.legacyValidators == nil {
|
|
r.legacyValidators = make(map[string]OptionValidator)
|
|
}
|
|
if r.specs == nil {
|
|
r.specs = make(map[string]ModuleSpec)
|
|
}
|
|
r.legacyBuilders[normalizedSpec.Key] = builder
|
|
r.legacyValidators[normalizedSpec.Key] = validateOptions
|
|
r.specs[normalizedSpec.Key] = cloneModuleSpec(normalizedSpec)
|
|
return nil
|
|
}
|
|
|
|
func RegisterExtractor[T any](registry *ExtractorRegistry, spec ModuleSpec, constructor func() (contracts.Extractor[T], error)) error {
|
|
if constructor == nil {
|
|
return fmt.Errorf("extractor constructor for %q must not be nil", strings.TrimSpace(spec.Key))
|
|
}
|
|
return RegisterExtractorBuilder(registry, spec, allowLegacyOptions, func(BuildRequest) (contracts.Extractor[T], error) {
|
|
return constructor()
|
|
})
|
|
}
|
|
|
|
func RegisterExtractorBuilder[T any](registry *ExtractorRegistry, spec ModuleSpec, validateOptions OptionValidator, builder func(BuildRequest) (contracts.Extractor[T], error)) error {
|
|
return registerExtractorBuilder(registry, spec, validateOptions, builder, nil)
|
|
}
|
|
|
|
// RegisterExtractorBuilderWithRawAdapter registers a typed extractor while a
|
|
// raw downstream remains in use. Resolution selects the adapter until the
|
|
// registration is replaced with the typed-only builder.
|
|
func RegisterExtractorBuilderWithRawAdapter[T any](registry *ExtractorRegistry, spec ModuleSpec, validateOptions OptionValidator, builder func(BuildRequest) (contracts.Extractor[T], error), rawBuilder LegacyRawExtractorBuilder) error {
|
|
if rawBuilder == nil {
|
|
return fmt.Errorf("extractor raw adapter builder for %q must not be nil", strings.TrimSpace(spec.Key))
|
|
}
|
|
return registerExtractorBuilder(registry, spec, validateOptions, builder, rawBuilder)
|
|
}
|
|
|
|
func registerExtractorBuilder[T any](registry *ExtractorRegistry, spec ModuleSpec, validateOptions OptionValidator, builder func(BuildRequest) (contracts.Extractor[T], error), rawBuilder LegacyRawExtractorBuilder) error {
|
|
if registry == nil {
|
|
return fmt.Errorf("extractor registry must not be nil")
|
|
}
|
|
normalizedSpec := normalizeModuleSpec(spec)
|
|
if err := validateModuleSpec("extractor", StageExtract, normalizedSpec); err != nil {
|
|
return err
|
|
}
|
|
if normalizedSpec.ArtifactKind == "" {
|
|
return fmt.Errorf("typed extractor %q artifact kind must not be empty", normalizedSpec.Key)
|
|
}
|
|
if validateOptions == nil {
|
|
return fmt.Errorf("extractor option validator for %q must not be nil", normalizedSpec.Key)
|
|
}
|
|
if builder == nil {
|
|
return fmt.Errorf("extractor builder for %q must not be nil", normalizedSpec.Key)
|
|
}
|
|
if _, ok := registry.specs[normalizedSpec.Key]; ok {
|
|
return fmt.Errorf("extractor %q is already registered", normalizedSpec.Key)
|
|
}
|
|
entry := typedExtractorEntry{
|
|
spec: cloneModuleSpec(normalizedSpec),
|
|
valueType: reflect.TypeFor[T](),
|
|
validateOptions: validateOptions,
|
|
builder: func(request BuildRequest) (any, error) {
|
|
return builder(cloneBuildRequest(request))
|
|
},
|
|
extract: func(ctx context.Context, implementation any, request contracts.TypedExtractionRequest) (erasedTypedResult, error) {
|
|
extractor, ok := implementation.(contracts.Extractor[T])
|
|
if !ok {
|
|
return erasedTypedResult{}, fmt.Errorf("extractor %q has incompatible implementation %T", normalizedSpec.Key, implementation)
|
|
}
|
|
result, err := extractor.Extract(ctx, request)
|
|
if err != nil {
|
|
return erasedTypedResult{}, err
|
|
}
|
|
return erasedTypedResult{Value: result.Value, Warnings: result.Warnings}, nil
|
|
},
|
|
rawBuilder: rawBuilder,
|
|
}
|
|
if registry.typedEntries == nil {
|
|
registry.typedEntries = make(map[string]typedExtractorEntry)
|
|
}
|
|
if registry.specs == nil {
|
|
registry.specs = make(map[string]ModuleSpec)
|
|
}
|
|
registry.typedEntries[normalizedSpec.Key] = entry
|
|
registry.specs[normalizedSpec.Key] = cloneModuleSpec(normalizedSpec)
|
|
return nil
|
|
}
|
|
|
|
func (r *ExtractorRegistry) BuildLegacyRaw(key string) (contracts.LegacyRawExtractor, error) {
|
|
return r.BuildLegacyRawWithRequest(key, BuildRequest{})
|
|
}
|
|
|
|
func (r *ExtractorRegistry) BuildLegacyRawWithRequest(key string, request BuildRequest) (contracts.LegacyRawExtractor, error) {
|
|
if r == nil {
|
|
return nil, fmt.Errorf("extractor registry must not be nil")
|
|
}
|
|
normalizedKey := strings.TrimSpace(key)
|
|
if normalizedKey == "" {
|
|
return nil, fmt.Errorf("extractor key must not be empty")
|
|
}
|
|
builder, ok := r.legacyBuilders[normalizedKey]
|
|
if !ok {
|
|
if entry, typedOK := r.typedEntries[normalizedKey]; typedOK && entry.rawBuilder != nil {
|
|
builder = entry.rawBuilder
|
|
ok = true
|
|
}
|
|
}
|
|
if !ok {
|
|
return nil, fmt.Errorf("legacy raw extractor %q is not registered", normalizedKey)
|
|
}
|
|
extractor, err := builder(cloneBuildRequest(request))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("build extractor %q: %w", normalizedKey, err)
|
|
}
|
|
if extractor == nil {
|
|
return nil, fmt.Errorf("extractor %q constructor returned nil", normalizedKey)
|
|
}
|
|
if extractor.Key() != normalizedKey {
|
|
return nil, fmt.Errorf("extractor %q returned key %q", normalizedKey, extractor.Key())
|
|
}
|
|
return extractor, nil
|
|
}
|
|
|
|
func (r *ExtractorRegistry) validateOptions(key string, options map[string]any) error {
|
|
if r == nil {
|
|
return fmt.Errorf("extractor registry must not be nil")
|
|
}
|
|
normalizedKey := strings.TrimSpace(key)
|
|
if entry, ok := r.typedEntries[normalizedKey]; ok {
|
|
return validateRegisteredOptions(entry.validateOptions, options)
|
|
}
|
|
validator, ok := r.legacyValidators[normalizedKey]
|
|
if !ok {
|
|
return fmt.Errorf("extractor %q is not registered", normalizedKey)
|
|
}
|
|
return validateRegisteredOptions(validator, options)
|
|
}
|
|
|
|
func (r *ExtractorRegistry) Spec(key string) (ModuleSpec, bool) {
|
|
if r == nil {
|
|
return ModuleSpec{}, false
|
|
}
|
|
spec, ok := r.specs[strings.TrimSpace(key)]
|
|
if !ok {
|
|
return ModuleSpec{}, false
|
|
}
|
|
return cloneModuleSpec(spec), true
|
|
}
|
|
|
|
func (r *ExtractorRegistry) typedEntry(key string) (typedExtractorEntry, bool) {
|
|
if r == nil {
|
|
return typedExtractorEntry{}, false
|
|
}
|
|
entry, ok := r.typedEntries[strings.TrimSpace(key)]
|
|
return entry, ok
|
|
}
|
|
|
|
func (r *ExtractorRegistry) usesRawAdapter(key string) bool {
|
|
entry, ok := r.typedEntry(key)
|
|
return ok && entry.rawBuilder != nil
|
|
}
|
|
|
|
func (r *ExtractorRegistry) RegisteredKeys() []string {
|
|
if r == nil {
|
|
return nil
|
|
}
|
|
return sortedRegistryKeys(r.specs)
|
|
}
|