154 lines
5.1 KiB
Go
154 lines
5.1 KiB
Go
package workflow
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"maps"
|
|
"reflect"
|
|
"slices"
|
|
"sync"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/stableid"
|
|
)
|
|
|
|
// StepType is one named workflow step type: the vocabulary word a YAML
|
|
// workflow names, plus the factory that builds its stage.
|
|
type StepType struct {
|
|
Name string
|
|
Factory StepFactory
|
|
// Settings is a zero value of the step's settings type, from which the
|
|
// control plane derives the settings schema editors build forms from.
|
|
// Nil means the step takes no settings.
|
|
Settings any
|
|
}
|
|
|
|
// StepTypeProvider is the typed extension point through which a plugin
|
|
// contributes workflow step types. StepTypes is the capability-specific
|
|
// operation — a provider exposing identity metadata alone would be a metadata
|
|
// entry, not a step-type provider — and it is the only method the family calls.
|
|
type StepTypeProvider interface {
|
|
// Name identifies the provider in registration diagnostics.
|
|
Name() string
|
|
// StepTypes returns every step type this provider contributes. The
|
|
// manager validates the whole contribution before applying any of it.
|
|
StepTypes() []StepType
|
|
}
|
|
|
|
// Manager owns registration and resolution of workflow step types. Each
|
|
// process builds one from the shared provider set and injects it. Register
|
|
// every provider before the first resolution.
|
|
type Manager struct {
|
|
mu sync.Mutex
|
|
registry StepRegistry
|
|
// owners maps each step type to the provider that registered it.
|
|
owners map[string]string
|
|
settings map[string]any
|
|
providers []string
|
|
}
|
|
|
|
// NewManager builds an empty family manager. It holds no vocabulary of its own:
|
|
// the shipped stages are a provider like any other, registered by the process's
|
|
// composition root, so the vocabulary every side resolves is the one that was
|
|
// actually registered.
|
|
func NewManager() *Manager {
|
|
return &Manager{registry: StepRegistry{}, owners: map[string]string{}, settings: map[string]any{}}
|
|
}
|
|
|
|
// Register validates a provider's whole contribution and applies all of it or
|
|
// none. It refuses nil or unnamed providers, bad identifiers, missing
|
|
// factories, duplicates and types another provider already owns.
|
|
func (m *Manager) Register(provider StepTypeProvider) error {
|
|
if isNilProvider(provider) {
|
|
return errors.New("workflow step type provider is nil")
|
|
}
|
|
name := provider.Name()
|
|
if !stableid.Valid(name) {
|
|
return fmt.Errorf("workflow step type provider name %q is not a stable identifier", name)
|
|
}
|
|
|
|
contributed := provider.StepTypes()
|
|
pending := make(StepRegistry, len(contributed))
|
|
settings := make(map[string]any, len(contributed))
|
|
order := make([]string, 0, len(contributed))
|
|
for _, stepType := range contributed {
|
|
switch {
|
|
case !stableid.Valid(stepType.Name):
|
|
return fmt.Errorf("workflow step type provider %q: step type %q is not a stable identifier", name, stepType.Name)
|
|
case stepType.Factory == nil:
|
|
return fmt.Errorf("workflow step type provider %q: step type %q has no factory", name, stepType.Name)
|
|
}
|
|
if _, declared := pending[stepType.Name]; declared {
|
|
return fmt.Errorf("workflow step type provider %q declares step type %q twice", name, stepType.Name)
|
|
}
|
|
pending[stepType.Name] = stepType.Factory
|
|
settings[stepType.Name] = stepType.Settings
|
|
order = append(order, stepType.Name)
|
|
}
|
|
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
for _, stepType := range order {
|
|
if owner, claimed := m.owners[stepType]; claimed {
|
|
return fmt.Errorf("workflow step type %q is already claimed by provider %q", stepType, owner)
|
|
}
|
|
}
|
|
for _, stepType := range order {
|
|
m.registry[stepType] = pending[stepType]
|
|
m.settings[stepType] = settings[stepType]
|
|
m.owners[stepType] = name
|
|
}
|
|
m.providers = append(m.providers, name)
|
|
return nil
|
|
}
|
|
|
|
// Registry returns the assembled vocabulary for the parser and compiler entry
|
|
// points. It is a copy: the closed set is a property of the manager, not
|
|
// something a consumer can widen.
|
|
func (m *Manager) Registry() StepRegistry {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
registry := make(StepRegistry, len(m.registry))
|
|
maps.Copy(registry, m.registry)
|
|
return registry
|
|
}
|
|
|
|
// Settings returns each registered step type's settings value (see
|
|
// StepType.Settings), keyed by type name.
|
|
func (m *Manager) Settings() map[string]any {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
return maps.Clone(m.settings)
|
|
}
|
|
|
|
// StepTypes returns every registered step type name, sorted.
|
|
func (m *Manager) StepTypes() []string {
|
|
registry := m.Registry()
|
|
names := make([]string, 0, len(registry))
|
|
for name := range registry {
|
|
names = append(names, name)
|
|
}
|
|
slices.Sort(names)
|
|
return names
|
|
}
|
|
|
|
// Providers returns the registered provider names, sorted.
|
|
func (m *Manager) Providers() []string {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
return slices.Sorted(slices.Values(m.providers))
|
|
}
|
|
|
|
// isNilProvider catches a nil interface and a typed nil pointer alike: a
|
|
// provider cannot answer Name() in either case, so it is refused rather than
|
|
// dereferenced. Mirrors internal/domain/memory's engine check.
|
|
func isNilProvider(provider StepTypeProvider) bool {
|
|
value := reflect.ValueOf(provider)
|
|
switch value.Kind() {
|
|
case reflect.Invalid:
|
|
return true
|
|
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:
|
|
return value.IsNil()
|
|
}
|
|
return false
|
|
}
|