Add framework composition runner
This commit is contained in:
83
internal/framework/extractorregistry/registry.go
Normal file
83
internal/framework/extractorregistry/registry.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package extractorregistry
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
)
|
||||
|
||||
type Constructor func() (contracts.Extractor, error)
|
||||
|
||||
type Registry struct {
|
||||
constructors map[string]Constructor
|
||||
}
|
||||
|
||||
func New() *Registry {
|
||||
return &Registry{
|
||||
constructors: make(map[string]Constructor),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Registry) Register(key string, constructor Constructor) error {
|
||||
if r == nil {
|
||||
return fmt.Errorf("extractor registry must not be nil")
|
||||
}
|
||||
|
||||
normalizedKey := strings.TrimSpace(key)
|
||||
if normalizedKey == "" {
|
||||
return fmt.Errorf("extractor key must not be empty")
|
||||
}
|
||||
if constructor == nil {
|
||||
return fmt.Errorf("extractor constructor for %q must not be nil", normalizedKey)
|
||||
}
|
||||
if _, ok := r.constructors[normalizedKey]; ok {
|
||||
return fmt.Errorf("extractor %q is already registered", normalizedKey)
|
||||
}
|
||||
|
||||
r.constructors[normalizedKey] = constructor
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Registry) Build(key string) (contracts.Extractor, 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")
|
||||
}
|
||||
|
||||
constructor, ok := r.constructors[normalizedKey]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("extractor %q is not registered", normalizedKey)
|
||||
}
|
||||
|
||||
extractor, err := constructor()
|
||||
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 *Registry) 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
|
||||
}
|
||||
232
internal/framework/extractorregistry/registry_test.go
Normal file
232
internal/framework/extractorregistry/registry_test.go
Normal file
@@ -0,0 +1,232 @@
|
||||
package extractorregistry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
)
|
||||
|
||||
func TestRegisterAndBuild(t *testing.T) {
|
||||
registry := New()
|
||||
|
||||
if err := registry.Register("generic-extractor", fakeConstructor("generic-extractor")); err != nil {
|
||||
t.Fatalf("Register() error = %v, want nil", err)
|
||||
}
|
||||
|
||||
extractor, err := registry.Build("generic-extractor")
|
||||
if err != nil {
|
||||
t.Fatalf("Build() error = %v, want nil", err)
|
||||
}
|
||||
if extractor.Key() != "generic-extractor" {
|
||||
t.Fatalf("extractor.Key() = %q, want generic-extractor", extractor.Key())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterAndBuildTrimKeys(t *testing.T) {
|
||||
registry := New()
|
||||
|
||||
if err := registry.Register(" generic-extractor ", fakeConstructor("generic-extractor")); err != nil {
|
||||
t.Fatalf("Register() error = %v, want nil", err)
|
||||
}
|
||||
|
||||
extractor, err := registry.Build("\tgeneric-extractor\n")
|
||||
if err != nil {
|
||||
t.Fatalf("Build() error = %v, want nil", err)
|
||||
}
|
||||
if extractor.Key() != "generic-extractor" {
|
||||
t.Fatalf("extractor.Key() = %q, want generic-extractor", extractor.Key())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterRejectsEmptyKey(t *testing.T) {
|
||||
registry := New()
|
||||
|
||||
err := registry.Register(" \t", fakeConstructor("generic-extractor"))
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("Register() error = nil, want error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "key must not be empty") {
|
||||
t.Fatalf("Register() error = %q, want empty key error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterRejectsDuplicateKey(t *testing.T) {
|
||||
registry := New()
|
||||
if err := registry.Register("generic-extractor", fakeConstructor("generic-extractor")); err != nil {
|
||||
t.Fatalf("Register() error = %v, want nil", err)
|
||||
}
|
||||
|
||||
err := registry.Register(" generic-extractor ", fakeConstructor("generic-extractor"))
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("Register() error = nil, want error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "already registered") {
|
||||
t.Fatalf("Register() error = %q, want duplicate key error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterRejectsNilConstructor(t *testing.T) {
|
||||
registry := New()
|
||||
|
||||
err := registry.Register("generic-extractor", nil)
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("Register() error = nil, want error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "constructor") {
|
||||
t.Fatalf("Register() error = %q, want constructor error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildRejectsUnknownKey(t *testing.T) {
|
||||
registry := New()
|
||||
|
||||
_, err := registry.Build("missing-extractor")
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("Build() error = nil, want error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "not registered") {
|
||||
t.Fatalf("Build() error = %q, want unknown key error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildWrapsConstructorError(t *testing.T) {
|
||||
registry := New()
|
||||
constructorErr := errors.New("constructor failed")
|
||||
if err := registry.Register("generic-extractor", func() (contracts.Extractor, error) {
|
||||
return nil, constructorErr
|
||||
}); err != nil {
|
||||
t.Fatalf("Register() error = %v, want nil", err)
|
||||
}
|
||||
|
||||
_, err := registry.Build("generic-extractor")
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("Build() error = nil, want error")
|
||||
}
|
||||
if !errors.Is(err, constructorErr) {
|
||||
t.Fatalf("Build() error = %v, want wrapped constructor error", err)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "generic-extractor") {
|
||||
t.Fatalf("Build() error = %q, want key context", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildRejectsNilExtractor(t *testing.T) {
|
||||
registry := New()
|
||||
if err := registry.Register("generic-extractor", func() (contracts.Extractor, error) {
|
||||
return nil, nil
|
||||
}); err != nil {
|
||||
t.Fatalf("Register() error = %v, want nil", err)
|
||||
}
|
||||
|
||||
_, err := registry.Build("generic-extractor")
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("Build() error = nil, want error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "returned nil") {
|
||||
t.Fatalf("Build() error = %q, want nil extractor error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildRejectsExtractorKeyMismatch(t *testing.T) {
|
||||
registry := New()
|
||||
if err := registry.Register("generic-extractor", fakeConstructor("other-extractor")); err != nil {
|
||||
t.Fatalf("Register() error = %v, want nil", err)
|
||||
}
|
||||
|
||||
_, err := registry.Build("generic-extractor")
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("Build() error = nil, want error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "returned key") {
|
||||
t.Fatalf("Build() error = %q, want key mismatch error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisteredKeysReturnsSortedCopy(t *testing.T) {
|
||||
registry := New()
|
||||
for _, key := range []string{"zeta", "alpha", "middle"} {
|
||||
if err := registry.Register(key, fakeConstructor(key)); err != nil {
|
||||
t.Fatalf("Register(%q) error = %v, want nil", key, err)
|
||||
}
|
||||
}
|
||||
|
||||
keys := registry.RegisteredKeys()
|
||||
|
||||
want := []string{"alpha", "middle", "zeta"}
|
||||
if !reflect.DeepEqual(keys, want) {
|
||||
t.Fatalf("RegisteredKeys() = %#v, want %#v", keys, want)
|
||||
}
|
||||
|
||||
keys[0] = "changed"
|
||||
if got := registry.RegisteredKeys(); !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("RegisteredKeys() after caller mutation = %#v, want %#v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNilRegistryBehavior(t *testing.T) {
|
||||
var registry *Registry
|
||||
|
||||
if err := registry.Register("generic-extractor", fakeConstructor("generic-extractor")); err == nil {
|
||||
t.Fatal("Register() error = nil, want error")
|
||||
}
|
||||
if _, err := registry.Build("generic-extractor"); err == nil {
|
||||
t.Fatal("Build() error = nil, want error")
|
||||
}
|
||||
if keys := registry.RegisteredKeys(); keys != nil {
|
||||
t.Fatalf("RegisteredKeys() = %#v, want nil", keys)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildRejectsEmptyKey(t *testing.T) {
|
||||
registry := New()
|
||||
|
||||
_, err := registry.Build(" \n")
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("Build() error = nil, want error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "key must not be empty") {
|
||||
t.Fatalf("Build() error = %q, want empty key error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
type fakeExtractor struct {
|
||||
key string
|
||||
}
|
||||
|
||||
func fakeConstructor(key string) Constructor {
|
||||
return func() (contracts.Extractor, error) {
|
||||
return fakeExtractor{key: key}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (extractor fakeExtractor) Key() string {
|
||||
return extractor.key
|
||||
}
|
||||
|
||||
func (extractor fakeExtractor) ArtifactType() string {
|
||||
return "generic-artifact"
|
||||
}
|
||||
|
||||
func (extractor fakeExtractor) SchemaVersion() string {
|
||||
return "v1"
|
||||
}
|
||||
|
||||
func (extractor fakeExtractor) Validators() []contracts.Validator {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (extractor fakeExtractor) Extract(ctx context.Context, req contracts.ExtractionRequest) (contracts.ExtractionResult, error) {
|
||||
return contracts.ExtractionResult{}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user