Prepare pipelines before source execution
This commit is contained in:
@@ -8,16 +8,19 @@ import (
|
||||
)
|
||||
|
||||
type ChunkerConstructor func() (contracts.Chunker, error)
|
||||
type ChunkerBuilder func(BuildRequest) (contracts.Chunker, error)
|
||||
|
||||
type ChunkerRegistry struct {
|
||||
constructors map[string]ChunkerConstructor
|
||||
specs map[string]ModuleSpec
|
||||
builders map[string]ChunkerBuilder
|
||||
optionValidators map[string]OptionValidator
|
||||
specs map[string]ModuleSpec
|
||||
}
|
||||
|
||||
func NewChunkerRegistry() *ChunkerRegistry {
|
||||
return &ChunkerRegistry{
|
||||
constructors: make(map[string]ChunkerConstructor),
|
||||
specs: make(map[string]ModuleSpec),
|
||||
builders: make(map[string]ChunkerBuilder),
|
||||
optionValidators: make(map[string]OptionValidator),
|
||||
specs: make(map[string]ModuleSpec),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +29,15 @@ func (r *ChunkerRegistry) Register(key string, constructor ChunkerConstructor) e
|
||||
}
|
||||
|
||||
func (r *ChunkerRegistry) RegisterWithSpec(spec ModuleSpec, constructor ChunkerConstructor) error {
|
||||
if constructor == nil {
|
||||
return fmt.Errorf("chunker constructor for %q must not be nil", strings.TrimSpace(spec.Key))
|
||||
}
|
||||
return r.RegisterBuilderWithSpec(spec, allowLegacyOptions, func(BuildRequest) (contracts.Chunker, error) {
|
||||
return constructor()
|
||||
})
|
||||
}
|
||||
|
||||
func (r *ChunkerRegistry) RegisterBuilderWithSpec(spec ModuleSpec, validateOptions OptionValidator, builder ChunkerBuilder) error {
|
||||
if r == nil {
|
||||
return fmt.Errorf("chunker registry must not be nil")
|
||||
}
|
||||
@@ -34,25 +46,36 @@ func (r *ChunkerRegistry) RegisterWithSpec(spec ModuleSpec, constructor ChunkerC
|
||||
if err := validateModuleSpec("chunker", StageChunk, normalizedSpec); err != nil {
|
||||
return err
|
||||
}
|
||||
if constructor == nil {
|
||||
return fmt.Errorf("chunker constructor for %q must not be nil", normalizedSpec.Key)
|
||||
if validateOptions == nil {
|
||||
return fmt.Errorf("chunker option validator for %q must not be nil", normalizedSpec.Key)
|
||||
}
|
||||
if _, ok := r.constructors[normalizedSpec.Key]; ok {
|
||||
if builder == nil {
|
||||
return fmt.Errorf("chunker builder for %q must not be nil", normalizedSpec.Key)
|
||||
}
|
||||
if _, ok := r.builders[normalizedSpec.Key]; ok {
|
||||
return fmt.Errorf("chunker %q is already registered", normalizedSpec.Key)
|
||||
}
|
||||
|
||||
if r.constructors == nil {
|
||||
r.constructors = make(map[string]ChunkerConstructor)
|
||||
if r.builders == nil {
|
||||
r.builders = make(map[string]ChunkerBuilder)
|
||||
}
|
||||
if r.optionValidators == nil {
|
||||
r.optionValidators = make(map[string]OptionValidator)
|
||||
}
|
||||
if r.specs == nil {
|
||||
r.specs = make(map[string]ModuleSpec)
|
||||
}
|
||||
r.constructors[normalizedSpec.Key] = constructor
|
||||
r.builders[normalizedSpec.Key] = builder
|
||||
r.optionValidators[normalizedSpec.Key] = validateOptions
|
||||
r.specs[normalizedSpec.Key] = cloneModuleSpec(normalizedSpec)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ChunkerRegistry) Build(key string) (contracts.Chunker, error) {
|
||||
return r.BuildWithRequest(key, BuildRequest{})
|
||||
}
|
||||
|
||||
func (r *ChunkerRegistry) BuildWithRequest(key string, request BuildRequest) (contracts.Chunker, error) {
|
||||
if r == nil {
|
||||
return nil, fmt.Errorf("chunker registry must not be nil")
|
||||
}
|
||||
@@ -62,12 +85,12 @@ func (r *ChunkerRegistry) Build(key string) (contracts.Chunker, error) {
|
||||
return nil, fmt.Errorf("chunker key must not be empty")
|
||||
}
|
||||
|
||||
constructor, ok := r.constructors[normalizedKey]
|
||||
builder, ok := r.builders[normalizedKey]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("chunker %q is not registered", normalizedKey)
|
||||
}
|
||||
|
||||
chunker, err := constructor()
|
||||
chunker, err := builder(cloneBuildRequest(request))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("build chunker %q: %w", normalizedKey, err)
|
||||
}
|
||||
@@ -81,6 +104,18 @@ func (r *ChunkerRegistry) Build(key string) (contracts.Chunker, error) {
|
||||
return chunker, nil
|
||||
}
|
||||
|
||||
func (r *ChunkerRegistry) ValidateOptions(key string, options map[string]any) error {
|
||||
if r == nil {
|
||||
return fmt.Errorf("chunker registry must not be nil")
|
||||
}
|
||||
normalizedKey := strings.TrimSpace(key)
|
||||
validator, ok := r.optionValidators[normalizedKey]
|
||||
if !ok {
|
||||
return fmt.Errorf("chunker %q is not registered", normalizedKey)
|
||||
}
|
||||
return validateRegisteredOptions(validator, options)
|
||||
}
|
||||
|
||||
func (r *ChunkerRegistry) Spec(key string) (ModuleSpec, bool) {
|
||||
if r == nil {
|
||||
return ModuleSpec{}, false
|
||||
@@ -98,5 +133,5 @@ func (r *ChunkerRegistry) RegisteredKeys() []string {
|
||||
return nil
|
||||
}
|
||||
|
||||
return sortedRegistryKeys(r.constructors)
|
||||
return sortedRegistryKeys(r.builders)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user