Hand checked processing selections to execution
This commit is contained in:
136
internal/modules/registry.go
Normal file
136
internal/modules/registry.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package modules
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/core/modulecatalog"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/contracts"
|
||||
glossarymodule "gitea.maximumdirect.net/eric/audita/v3/internal/modules/glossary"
|
||||
grammarmodule "gitea.maximumdirect.net/eric/audita/v3/internal/modules/grammar"
|
||||
homophonesmodule "gitea.maximumdirect.net/eric/audita/v3/internal/modules/homophones"
|
||||
spokenwordmodule "gitea.maximumdirect.net/eric/audita/v3/internal/modules/spoken_word"
|
||||
)
|
||||
|
||||
const (
|
||||
ModuleKeyGlossary = modulecatalog.KeyGlossary
|
||||
ModuleKeyHomophones = modulecatalog.KeyHomophones
|
||||
ModuleKeySpokenWord = modulecatalog.KeySpokenWord
|
||||
ModuleKeyGrammar = modulecatalog.KeyGrammar
|
||||
)
|
||||
|
||||
const (
|
||||
ReasonUnsupportedModule = "unsupported_module"
|
||||
)
|
||||
|
||||
// IsKnownModuleKey reports whether a module key is recognized by the production
|
||||
// registry scaffold.
|
||||
func IsKnownModuleKey(key string) bool {
|
||||
return modulecatalog.IsSupported(key)
|
||||
}
|
||||
|
||||
// ConstructRequest identifies the module to construct.
|
||||
type ConstructRequest struct {
|
||||
RunSpec contracts.ModuleRunSpec
|
||||
}
|
||||
|
||||
// Constructor builds one module instance from a run spec and explicit deps.
|
||||
type Constructor func(ctx context.Context, req ConstructRequest) (contracts.TranscriptModule, error)
|
||||
|
||||
// Factory resolves configured module specs into module instances.
|
||||
type Factory struct {
|
||||
constructors map[string]Constructor
|
||||
}
|
||||
|
||||
// NewFactory creates a production module registry.
|
||||
func NewFactory() *Factory {
|
||||
factory := &Factory{
|
||||
constructors: make(map[string]Constructor, len(modulecatalog.SupportedKeys())),
|
||||
}
|
||||
_ = factory.RegisterConstructor(ModuleKeyGlossary, constructGlossaryModule)
|
||||
_ = factory.RegisterConstructor(ModuleKeyHomophones, constructHomophonesModule)
|
||||
_ = factory.RegisterConstructor(ModuleKeySpokenWord, constructSpokenWordModule)
|
||||
_ = factory.RegisterConstructor(ModuleKeyGrammar, constructGrammarModule)
|
||||
return factory
|
||||
}
|
||||
|
||||
// RegisterConstructor registers a constructor for a known module key.
|
||||
func (f *Factory) RegisterConstructor(moduleKey string, constructor Constructor) error {
|
||||
if f == nil {
|
||||
return fmt.Errorf("module factory is nil")
|
||||
}
|
||||
key := strings.TrimSpace(moduleKey)
|
||||
if !IsKnownModuleKey(key) {
|
||||
return &UnsupportedModuleError{ModuleKey: key}
|
||||
}
|
||||
if constructor == nil {
|
||||
return fmt.Errorf("constructor for module %q must not be nil", key)
|
||||
}
|
||||
f.constructors[key] = constructor
|
||||
return nil
|
||||
}
|
||||
|
||||
func constructGrammarModule(ctx context.Context, req ConstructRequest) (contracts.TranscriptModule, error) {
|
||||
_ = ctx
|
||||
_ = req
|
||||
return grammarmodule.New()
|
||||
}
|
||||
|
||||
func constructGlossaryModule(ctx context.Context, req ConstructRequest) (contracts.TranscriptModule, error) {
|
||||
_ = ctx
|
||||
_ = req
|
||||
return glossarymodule.New()
|
||||
}
|
||||
|
||||
func constructHomophonesModule(ctx context.Context, req ConstructRequest) (contracts.TranscriptModule, error) {
|
||||
_ = ctx
|
||||
_ = req
|
||||
return homophonesmodule.New()
|
||||
}
|
||||
|
||||
func constructSpokenWordModule(ctx context.Context, req ConstructRequest) (contracts.TranscriptModule, error) {
|
||||
_ = ctx
|
||||
_ = req
|
||||
return spokenwordmodule.New()
|
||||
}
|
||||
|
||||
// ModuleForSpec resolves one configured run spec into a module instance.
|
||||
func (f *Factory) ModuleForSpec(spec contracts.ModuleRunSpec) (contracts.TranscriptModule, error) {
|
||||
if f == nil {
|
||||
return nil, fmt.Errorf("module factory is nil")
|
||||
}
|
||||
|
||||
key := strings.TrimSpace(spec.ModuleKey)
|
||||
if !IsKnownModuleKey(key) {
|
||||
return nil, &UnsupportedModuleError{ModuleKey: key}
|
||||
}
|
||||
|
||||
constructor, ok := f.constructors[key]
|
||||
if !ok || constructor == nil {
|
||||
return nil, fmt.Errorf("internal module registry error: constructor for module %q is not configured", key)
|
||||
}
|
||||
|
||||
module, err := constructor(context.Background(), ConstructRequest{RunSpec: spec})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("construct module %q: %w", spec.InstanceName, err)
|
||||
}
|
||||
if module == nil {
|
||||
return nil, fmt.Errorf("constructor for module %q returned nil module", key)
|
||||
}
|
||||
return module, nil
|
||||
}
|
||||
|
||||
// UnsupportedModuleError indicates a configured module key is unknown.
|
||||
type UnsupportedModuleError struct {
|
||||
ModuleKey string
|
||||
}
|
||||
|
||||
func (e *UnsupportedModuleError) Error() string {
|
||||
return fmt.Sprintf("unsupported module key %q", strings.TrimSpace(e.ModuleKey))
|
||||
}
|
||||
|
||||
// ReasonCode returns a stable reason code suitable for reporting.
|
||||
func (e *UnsupportedModuleError) ReasonCode() string {
|
||||
return ReasonUnsupportedModule
|
||||
}
|
||||
164
internal/modules/registry_test.go
Normal file
164
internal/modules/registry_test.go
Normal file
@@ -0,0 +1,164 @@
|
||||
package modules
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
frameworkvalidators "gitea.maximumdirect.net/eric/audita/v3/internal/framework/validators"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/core/config"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/core/modulecatalog"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/proposals"
|
||||
)
|
||||
|
||||
type noopModule struct {
|
||||
key string
|
||||
}
|
||||
|
||||
func (m noopModule) Key() string { return m.key }
|
||||
func (m noopModule) ReplacementPolicy() proposals.ReplacementPolicy {
|
||||
return proposals.ReplacementPolicyRequireUnique
|
||||
}
|
||||
func (m noopModule) Validators() []frameworkvalidators.Validator { return nil }
|
||||
func (m noopModule) Propose(ctx context.Context, req contracts.ProposalRequest) (contracts.ProposalResult, error) {
|
||||
_ = ctx
|
||||
_ = req
|
||||
return contracts.ProposalResult{}, nil
|
||||
}
|
||||
|
||||
func TestKnownModuleKeyRecognition(t *testing.T) {
|
||||
for _, key := range modulecatalog.SupportedKeys() {
|
||||
if !IsKnownModuleKey(key) {
|
||||
t.Fatalf("expected key %q to be recognized", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnknownModuleKeyNotRecognized(t *testing.T) {
|
||||
if IsKnownModuleKey("made_up") {
|
||||
t.Fatal("expected unknown key to be unrecognized")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepeatedRunSpecNamingRemainsDeterministic(t *testing.T) {
|
||||
specs := contracts.ResolveModuleRunSpecs(&config.Config{Pipeline: config.Pipeline{Stages: []config.Stage{{Module: "glossary"}, {Module: "glossary"}, {Module: "grammar"}}}})
|
||||
if specs[0].InstanceName != "glossary_1" || specs[1].InstanceName != "glossary_2" || specs[2].InstanceName != "grammar_3" {
|
||||
t.Fatalf("unexpected instance names: %+v", specs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnsupportedUnknownModuleKeyFailsCleanly(t *testing.T) {
|
||||
factory := NewFactory()
|
||||
_, err := factory.ModuleForSpec(contracts.ModuleRunSpec{ModuleKey: "unknown", InstanceName: "unknown"})
|
||||
if err == nil {
|
||||
t.Fatal("expected unsupported-module error")
|
||||
}
|
||||
|
||||
var unsupported *UnsupportedModuleError
|
||||
if !errors.As(err, &unsupported) {
|
||||
t.Fatalf("expected UnsupportedModuleError, got %T (%v)", err, err)
|
||||
}
|
||||
if unsupported.ReasonCode() != ReasonUnsupportedModule {
|
||||
t.Fatalf("unexpected reason code: %q", unsupported.ReasonCode())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecognizedButMissingConstructorFailsWithInternalRegistryError(t *testing.T) {
|
||||
factory := NewFactory()
|
||||
// Force a missing constructor state for a known key.
|
||||
factory.constructors[ModuleKeySpokenWord] = nil
|
||||
|
||||
_, err := factory.ModuleForSpec(contracts.ModuleRunSpec{ModuleKey: ModuleKeySpokenWord, InstanceName: ModuleKeySpokenWord})
|
||||
if err == nil {
|
||||
t.Fatal("expected constructor-missing error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "internal module registry error") {
|
||||
t.Fatalf("expected internal registry error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGrammarIsRegisteredAndConstructibleByDefault(t *testing.T) {
|
||||
factory := NewFactory()
|
||||
module, err := factory.ModuleForSpec(contracts.ModuleRunSpec{ModuleKey: ModuleKeyGrammar, InstanceName: ModuleKeyGrammar})
|
||||
if err != nil {
|
||||
t.Fatalf("ModuleForSpec error: %v", err)
|
||||
}
|
||||
if module.Key() != ModuleKeyGrammar {
|
||||
t.Fatalf("expected grammar module key, got %q", module.Key())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGlossaryIsRegisteredAndConstructibleByDefault(t *testing.T) {
|
||||
factory := NewFactory()
|
||||
module, err := factory.ModuleForSpec(contracts.ModuleRunSpec{ModuleKey: ModuleKeyGlossary, InstanceName: ModuleKeyGlossary})
|
||||
if err != nil {
|
||||
t.Fatalf("ModuleForSpec error: %v", err)
|
||||
}
|
||||
if module.Key() != ModuleKeyGlossary {
|
||||
t.Fatalf("expected glossary module key, got %q", module.Key())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHomophonesIsRegisteredAndConstructibleByDefault(t *testing.T) {
|
||||
factory := NewFactory()
|
||||
module, err := factory.ModuleForSpec(contracts.ModuleRunSpec{ModuleKey: ModuleKeyHomophones, InstanceName: ModuleKeyHomophones})
|
||||
if err != nil {
|
||||
t.Fatalf("ModuleForSpec error: %v", err)
|
||||
}
|
||||
if module.Key() != ModuleKeyHomophones {
|
||||
t.Fatalf("expected homophones module key, got %q", module.Key())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSpokenWordIsRegisteredAndConstructibleByDefault(t *testing.T) {
|
||||
factory := NewFactory()
|
||||
module, err := factory.ModuleForSpec(contracts.ModuleRunSpec{ModuleKey: ModuleKeySpokenWord, InstanceName: ModuleKeySpokenWord})
|
||||
if err != nil {
|
||||
t.Fatalf("ModuleForSpec error: %v", err)
|
||||
}
|
||||
if module.Key() != ModuleKeySpokenWord {
|
||||
t.Fatalf("expected spoken_word module key, got %q", module.Key())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterConstructorAndConstruct(t *testing.T) {
|
||||
factory := NewFactory()
|
||||
if err := factory.RegisterConstructor(ModuleKeyGlossary, func(ctx context.Context, req ConstructRequest) (contracts.TranscriptModule, error) {
|
||||
_ = ctx
|
||||
if req.RunSpec.InstanceName != "glossary_1" {
|
||||
t.Fatalf("expected run spec instance name, got %q", req.RunSpec.InstanceName)
|
||||
}
|
||||
return noopModule{key: req.RunSpec.ModuleKey}, nil
|
||||
}); err != nil {
|
||||
t.Fatalf("RegisterConstructor error: %v", err)
|
||||
}
|
||||
|
||||
module, err := factory.ModuleForSpec(contracts.ModuleRunSpec{
|
||||
ModuleKey: ModuleKeyGlossary,
|
||||
InstanceName: "glossary_1",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("ModuleForSpec error: %v", err)
|
||||
}
|
||||
if module.Key() != ModuleKeyGlossary {
|
||||
t.Fatalf("unexpected module key %q", module.Key())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterConstructorRejectsUnknownModuleKey(t *testing.T) {
|
||||
factory := NewFactory()
|
||||
err := factory.RegisterConstructor("unknown", func(ctx context.Context, req ConstructRequest) (contracts.TranscriptModule, error) {
|
||||
_ = ctx
|
||||
_ = req
|
||||
return noopModule{key: "unknown"}, nil
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected register failure for unknown key")
|
||||
}
|
||||
var unsupported *UnsupportedModuleError
|
||||
if !errors.As(err, &unsupported) {
|
||||
t.Fatalf("expected UnsupportedModuleError, got %T (%v)", err, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user