Add framework composition runner
This commit is contained in:
83
internal/framework/inputregistry/registry.go
Normal file
83
internal/framework/inputregistry/registry.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package inputregistry
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
)
|
||||
|
||||
type Constructor func() (contracts.InputAdapter, 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("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 *Registry) 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 *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
|
||||
}
|
||||
229
internal/framework/inputregistry/registry_test.go
Normal file
229
internal/framework/inputregistry/registry_test.go
Normal file
@@ -0,0 +1,229 @@
|
||||
package inputregistry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
)
|
||||
|
||||
func TestRegisterAndBuild(t *testing.T) {
|
||||
registry := New()
|
||||
|
||||
if err := registry.Register("generic-input", fakeConstructor("generic-input")); err != nil {
|
||||
t.Fatalf("Register() error = %v, want nil", err)
|
||||
}
|
||||
|
||||
adapter, err := registry.Build("generic-input")
|
||||
if err != nil {
|
||||
t.Fatalf("Build() error = %v, want nil", err)
|
||||
}
|
||||
if adapter.Key() != "generic-input" {
|
||||
t.Fatalf("adapter.Key() = %q, want generic-input", adapter.Key())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterAndBuildTrimKeys(t *testing.T) {
|
||||
registry := New()
|
||||
|
||||
if err := registry.Register(" generic-input ", fakeConstructor("generic-input")); err != nil {
|
||||
t.Fatalf("Register() error = %v, want nil", err)
|
||||
}
|
||||
|
||||
adapter, err := registry.Build("\tgeneric-input\n")
|
||||
if err != nil {
|
||||
t.Fatalf("Build() error = %v, want nil", err)
|
||||
}
|
||||
if adapter.Key() != "generic-input" {
|
||||
t.Fatalf("adapter.Key() = %q, want generic-input", adapter.Key())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterRejectsEmptyKey(t *testing.T) {
|
||||
registry := New()
|
||||
|
||||
err := registry.Register(" \t", fakeConstructor("generic-input"))
|
||||
|
||||
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-input", fakeConstructor("generic-input")); err != nil {
|
||||
t.Fatalf("Register() error = %v, want nil", err)
|
||||
}
|
||||
|
||||
err := registry.Register(" generic-input ", fakeConstructor("generic-input"))
|
||||
|
||||
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-input", 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-input")
|
||||
|
||||
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-input", func() (contracts.InputAdapter, error) {
|
||||
return nil, constructorErr
|
||||
}); err != nil {
|
||||
t.Fatalf("Register() error = %v, want nil", err)
|
||||
}
|
||||
|
||||
_, err := registry.Build("generic-input")
|
||||
|
||||
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-input") {
|
||||
t.Fatalf("Build() error = %q, want key context", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildRejectsNilAdapter(t *testing.T) {
|
||||
registry := New()
|
||||
if err := registry.Register("generic-input", func() (contracts.InputAdapter, error) {
|
||||
return nil, nil
|
||||
}); err != nil {
|
||||
t.Fatalf("Register() error = %v, want nil", err)
|
||||
}
|
||||
|
||||
_, err := registry.Build("generic-input")
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("Build() error = nil, want error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "returned nil") {
|
||||
t.Fatalf("Build() error = %q, want nil adapter error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildRejectsAdapterKeyMismatch(t *testing.T) {
|
||||
registry := New()
|
||||
if err := registry.Register("generic-input", fakeConstructor("other-input")); err != nil {
|
||||
t.Fatalf("Register() error = %v, want nil", err)
|
||||
}
|
||||
|
||||
_, err := registry.Build("generic-input")
|
||||
|
||||
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-input", fakeConstructor("generic-input")); err == nil {
|
||||
t.Fatal("Register() error = nil, want error")
|
||||
}
|
||||
if _, err := registry.Build("generic-input"); 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 fakeAdapter struct {
|
||||
key string
|
||||
}
|
||||
|
||||
func fakeConstructor(key string) Constructor {
|
||||
return func() (contracts.InputAdapter, error) {
|
||||
return fakeAdapter{key: key}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (adapter fakeAdapter) Key() string {
|
||||
return adapter.key
|
||||
}
|
||||
|
||||
func (adapter fakeAdapter) Parse(ctx context.Context, req contracts.ParseRequest) (*source.SourceDocument, error) {
|
||||
return &source.SourceDocument{
|
||||
ID: req.SourceID,
|
||||
Kind: "document",
|
||||
Format: "text/plain",
|
||||
Digest: "sha256:abc123",
|
||||
Units: []source.SourceUnit{
|
||||
{ID: "u1", Kind: "unit", Text: "Source unit."},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user