Prepare pipelines before source execution
This commit is contained in:
@@ -9,24 +9,28 @@ import (
|
||||
)
|
||||
|
||||
type LegacyRawNormalizerConstructor func() (contracts.LegacyRawNormalizer, error)
|
||||
type LegacyRawNormalizerBuilder func(BuildRequest) (contracts.LegacyRawNormalizer, error)
|
||||
|
||||
type NormalizerRegistry struct {
|
||||
legacyConstructors map[string]LegacyRawNormalizerConstructor
|
||||
legacySpecs map[string]ModuleSpec
|
||||
typedEntries map[artifactVariantKey]typedNormalizerEntry
|
||||
legacyBuilders map[string]LegacyRawNormalizerBuilder
|
||||
legacyValidators map[string]OptionValidator
|
||||
legacySpecs map[string]ModuleSpec
|
||||
typedEntries map[artifactVariantKey]typedNormalizerEntry
|
||||
}
|
||||
|
||||
type typedNormalizerEntry struct {
|
||||
spec ModuleSpec
|
||||
valueType reflect.Type
|
||||
constructor func() (any, error)
|
||||
spec ModuleSpec
|
||||
valueType reflect.Type
|
||||
validateOptions OptionValidator
|
||||
builder func(BuildRequest) (any, error)
|
||||
}
|
||||
|
||||
func NewNormalizerRegistry() *NormalizerRegistry {
|
||||
return &NormalizerRegistry{
|
||||
legacyConstructors: make(map[string]LegacyRawNormalizerConstructor),
|
||||
legacySpecs: make(map[string]ModuleSpec),
|
||||
typedEntries: make(map[artifactVariantKey]typedNormalizerEntry),
|
||||
legacyBuilders: make(map[string]LegacyRawNormalizerBuilder),
|
||||
legacyValidators: make(map[string]OptionValidator),
|
||||
legacySpecs: make(map[string]ModuleSpec),
|
||||
typedEntries: make(map[artifactVariantKey]typedNormalizerEntry),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +39,15 @@ func (r *NormalizerRegistry) RegisterLegacyRaw(key string, constructor LegacyRaw
|
||||
}
|
||||
|
||||
func (r *NormalizerRegistry) RegisterLegacyRawWithSpec(spec ModuleSpec, constructor LegacyRawNormalizerConstructor) error {
|
||||
if constructor == nil {
|
||||
return fmt.Errorf("normalizer constructor for %q must not be nil", strings.TrimSpace(spec.Key))
|
||||
}
|
||||
return r.RegisterLegacyRawBuilderWithSpec(spec, allowLegacyOptions, func(BuildRequest) (contracts.LegacyRawNormalizer, error) {
|
||||
return constructor()
|
||||
})
|
||||
}
|
||||
|
||||
func (r *NormalizerRegistry) RegisterLegacyRawBuilderWithSpec(spec ModuleSpec, validateOptions OptionValidator, builder LegacyRawNormalizerBuilder) error {
|
||||
if r == nil {
|
||||
return fmt.Errorf("normalizer registry must not be nil")
|
||||
}
|
||||
@@ -45,24 +58,40 @@ func (r *NormalizerRegistry) RegisterLegacyRawWithSpec(spec ModuleSpec, construc
|
||||
if normalizedSpec.ArtifactKind != "" {
|
||||
return fmt.Errorf("legacy raw normalizer %q must not declare an artifact kind", normalizedSpec.Key)
|
||||
}
|
||||
if constructor == nil {
|
||||
return fmt.Errorf("normalizer constructor for %q must not be nil", normalizedSpec.Key)
|
||||
if validateOptions == nil {
|
||||
return fmt.Errorf("normalizer option validator for %q must not be nil", normalizedSpec.Key)
|
||||
}
|
||||
if _, ok := r.legacyConstructors[normalizedSpec.Key]; ok {
|
||||
if builder == nil {
|
||||
return fmt.Errorf("normalizer builder for %q must not be nil", normalizedSpec.Key)
|
||||
}
|
||||
if _, ok := r.legacyBuilders[normalizedSpec.Key]; ok {
|
||||
return fmt.Errorf("legacy raw normalizer %q is already registered", normalizedSpec.Key)
|
||||
}
|
||||
if r.legacyConstructors == nil {
|
||||
r.legacyConstructors = make(map[string]LegacyRawNormalizerConstructor)
|
||||
if r.legacyBuilders == nil {
|
||||
r.legacyBuilders = make(map[string]LegacyRawNormalizerBuilder)
|
||||
}
|
||||
if r.legacyValidators == nil {
|
||||
r.legacyValidators = make(map[string]OptionValidator)
|
||||
}
|
||||
if r.legacySpecs == nil {
|
||||
r.legacySpecs = make(map[string]ModuleSpec)
|
||||
}
|
||||
r.legacyConstructors[normalizedSpec.Key] = constructor
|
||||
r.legacyBuilders[normalizedSpec.Key] = builder
|
||||
r.legacyValidators[normalizedSpec.Key] = validateOptions
|
||||
r.legacySpecs[normalizedSpec.Key] = cloneModuleSpec(normalizedSpec)
|
||||
return nil
|
||||
}
|
||||
|
||||
func RegisterNormalizer[T any](registry *NormalizerRegistry, spec ModuleSpec, constructor func() (contracts.Normalizer[T], error)) error {
|
||||
if constructor == nil {
|
||||
return fmt.Errorf("normalizer constructor for %q must not be nil", strings.TrimSpace(spec.Key))
|
||||
}
|
||||
return RegisterNormalizerBuilder(registry, spec, allowLegacyOptions, func(BuildRequest) (contracts.Normalizer[T], error) {
|
||||
return constructor()
|
||||
})
|
||||
}
|
||||
|
||||
func RegisterNormalizerBuilder[T any](registry *NormalizerRegistry, spec ModuleSpec, validateOptions OptionValidator, builder func(BuildRequest) (contracts.Normalizer[T], error)) error {
|
||||
if registry == nil {
|
||||
return fmt.Errorf("normalizer registry must not be nil")
|
||||
}
|
||||
@@ -73,8 +102,11 @@ func RegisterNormalizer[T any](registry *NormalizerRegistry, spec ModuleSpec, co
|
||||
if normalizedSpec.ArtifactKind == "" {
|
||||
return fmt.Errorf("typed normalizer %q artifact kind must not be empty", normalizedSpec.Key)
|
||||
}
|
||||
if constructor == nil {
|
||||
return fmt.Errorf("normalizer constructor for %q must not be nil", normalizedSpec.Key)
|
||||
if validateOptions == nil {
|
||||
return fmt.Errorf("normalizer option validator for %q must not be nil", normalizedSpec.Key)
|
||||
}
|
||||
if builder == nil {
|
||||
return fmt.Errorf("normalizer builder for %q must not be nil", normalizedSpec.Key)
|
||||
}
|
||||
key := artifactVariantKey{module: normalizedSpec.Key, kind: normalizedSpec.ArtifactKind}
|
||||
if _, ok := registry.typedEntries[key]; ok {
|
||||
@@ -84,16 +116,21 @@ func RegisterNormalizer[T any](registry *NormalizerRegistry, spec ModuleSpec, co
|
||||
registry.typedEntries = make(map[artifactVariantKey]typedNormalizerEntry)
|
||||
}
|
||||
registry.typedEntries[key] = typedNormalizerEntry{
|
||||
spec: cloneModuleSpec(normalizedSpec),
|
||||
valueType: reflect.TypeFor[T](),
|
||||
constructor: func() (any, error) {
|
||||
return constructor()
|
||||
spec: cloneModuleSpec(normalizedSpec),
|
||||
valueType: reflect.TypeFor[T](),
|
||||
validateOptions: validateOptions,
|
||||
builder: func(request BuildRequest) (any, error) {
|
||||
return builder(cloneBuildRequest(request))
|
||||
},
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *NormalizerRegistry) BuildLegacyRaw(key string) (contracts.LegacyRawNormalizer, error) {
|
||||
return r.BuildLegacyRawWithRequest(key, BuildRequest{})
|
||||
}
|
||||
|
||||
func (r *NormalizerRegistry) BuildLegacyRawWithRequest(key string, request BuildRequest) (contracts.LegacyRawNormalizer, error) {
|
||||
if r == nil {
|
||||
return nil, fmt.Errorf("normalizer registry must not be nil")
|
||||
}
|
||||
@@ -101,11 +138,11 @@ func (r *NormalizerRegistry) BuildLegacyRaw(key string) (contracts.LegacyRawNorm
|
||||
if normalizedKey == "" {
|
||||
return nil, fmt.Errorf("normalizer key must not be empty")
|
||||
}
|
||||
constructor, ok := r.legacyConstructors[normalizedKey]
|
||||
builder, ok := r.legacyBuilders[normalizedKey]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("legacy raw normalizer %q is not registered", normalizedKey)
|
||||
}
|
||||
normalizer, err := constructor()
|
||||
normalizer, err := builder(cloneBuildRequest(request))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("build normalizer %q: %w", normalizedKey, err)
|
||||
}
|
||||
@@ -118,6 +155,25 @@ func (r *NormalizerRegistry) BuildLegacyRaw(key string) (contracts.LegacyRawNorm
|
||||
return normalizer, nil
|
||||
}
|
||||
|
||||
func (r *NormalizerRegistry) validateOptions(key string, kind contracts.ArtifactKind, options map[string]any) error {
|
||||
if r == nil {
|
||||
return fmt.Errorf("normalizer registry must not be nil")
|
||||
}
|
||||
normalizedKey := strings.TrimSpace(key)
|
||||
if kind != "" {
|
||||
entry, ok := r.typedEntry(normalizedKey, kind)
|
||||
if !ok {
|
||||
return fmt.Errorf("normalizer %q variant for artifact kind %q is not registered", normalizedKey, kind)
|
||||
}
|
||||
return validateRegisteredOptions(entry.validateOptions, options)
|
||||
}
|
||||
validator, ok := r.legacyValidators[normalizedKey]
|
||||
if !ok {
|
||||
return fmt.Errorf("legacy raw normalizer %q is not registered", normalizedKey)
|
||||
}
|
||||
return validateRegisteredOptions(validator, options)
|
||||
}
|
||||
|
||||
func (r *NormalizerRegistry) Spec(key string) (ModuleSpec, bool) {
|
||||
if r == nil {
|
||||
return ModuleSpec{}, false
|
||||
|
||||
Reference in New Issue
Block a user