Add type-safe artifact lane resolution
This commit is contained in:
@@ -2,40 +2,78 @@ package pipeline
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
)
|
||||
|
||||
type ValidatorConstructor func() (contracts.Validator, error)
|
||||
type LegacyRawValidatorConstructor func() (contracts.LegacyRawValidator, error)
|
||||
|
||||
type ValidatorSpec struct {
|
||||
Key string `json:"key"`
|
||||
ExecutionClass contracts.ExecutionClass `json:"execution_class"`
|
||||
}
|
||||
|
||||
type SerializedValidatorSpec struct {
|
||||
ValidatorSpec
|
||||
SupportsChunks bool `json:"supports_chunks,omitempty"`
|
||||
SupportsArtifacts bool `json:"supports_artifacts,omitempty"`
|
||||
}
|
||||
|
||||
type ValidatorTarget string
|
||||
|
||||
const (
|
||||
ValidatorTargetLegacyRaw ValidatorTarget = "legacy_raw"
|
||||
ValidatorTargetChunk ValidatorTarget = "chunk"
|
||||
ValidatorTargetSerialized ValidatorTarget = "serialized"
|
||||
ValidatorTargetTyped ValidatorTarget = "typed"
|
||||
)
|
||||
|
||||
type ValidatorRegistry struct {
|
||||
constructors map[string]ValidatorConstructor
|
||||
specs map[string]ValidatorSpec
|
||||
legacyConstructors map[string]LegacyRawValidatorConstructor
|
||||
legacySpecs map[string]ValidatorSpec
|
||||
typedEntries map[artifactVariantKey]typedValidatorEntry
|
||||
chunkEntries map[string]chunkValidatorEntry
|
||||
serializedEntries map[string]serializedValidatorEntry
|
||||
}
|
||||
|
||||
type typedValidatorEntry struct {
|
||||
spec ValidatorSpec
|
||||
kind contracts.ArtifactKind
|
||||
valueType reflect.Type
|
||||
constructor func() (any, error)
|
||||
}
|
||||
|
||||
type chunkValidatorEntry struct {
|
||||
spec ValidatorSpec
|
||||
constructor func() (contracts.ChunkValidator, error)
|
||||
}
|
||||
|
||||
type serializedValidatorEntry struct {
|
||||
spec SerializedValidatorSpec
|
||||
constructor func() (contracts.SerializedValidator, error)
|
||||
}
|
||||
|
||||
func NewValidatorRegistry() *ValidatorRegistry {
|
||||
return &ValidatorRegistry{
|
||||
constructors: make(map[string]ValidatorConstructor),
|
||||
specs: make(map[string]ValidatorSpec),
|
||||
legacyConstructors: make(map[string]LegacyRawValidatorConstructor),
|
||||
legacySpecs: make(map[string]ValidatorSpec),
|
||||
typedEntries: make(map[artifactVariantKey]typedValidatorEntry),
|
||||
chunkEntries: make(map[string]chunkValidatorEntry),
|
||||
serializedEntries: make(map[string]serializedValidatorEntry),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ValidatorRegistry) Register(key string, constructor ValidatorConstructor) error {
|
||||
return r.RegisterWithSpec(ValidatorSpec{Key: key, ExecutionClass: contracts.ExecutionClassDeterministic}, constructor)
|
||||
func (r *ValidatorRegistry) RegisterLegacyRaw(key string, constructor LegacyRawValidatorConstructor) error {
|
||||
return r.RegisterLegacyRawWithSpec(ValidatorSpec{Key: key, ExecutionClass: contracts.ExecutionClassDeterministic}, constructor)
|
||||
}
|
||||
|
||||
func (r *ValidatorRegistry) RegisterWithSpec(spec ValidatorSpec, constructor ValidatorConstructor) error {
|
||||
func (r *ValidatorRegistry) RegisterLegacyRawWithSpec(spec ValidatorSpec, constructor LegacyRawValidatorConstructor) error {
|
||||
if r == nil {
|
||||
return fmt.Errorf("validator registry must not be nil")
|
||||
}
|
||||
|
||||
normalizedSpec, err := normalizeValidatorSpec(spec)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -43,36 +81,111 @@ func (r *ValidatorRegistry) RegisterWithSpec(spec ValidatorSpec, constructor Val
|
||||
if constructor == nil {
|
||||
return fmt.Errorf("validator constructor for %q must not be nil", normalizedSpec.Key)
|
||||
}
|
||||
if _, ok := r.constructors[normalizedSpec.Key]; ok {
|
||||
return fmt.Errorf("validator %q is already registered", normalizedSpec.Key)
|
||||
if _, ok := r.legacyConstructors[normalizedSpec.Key]; ok {
|
||||
return fmt.Errorf("legacy raw validator %q is already registered", normalizedSpec.Key)
|
||||
}
|
||||
|
||||
if r.constructors == nil {
|
||||
r.constructors = make(map[string]ValidatorConstructor)
|
||||
if r.legacyConstructors == nil {
|
||||
r.legacyConstructors = make(map[string]LegacyRawValidatorConstructor)
|
||||
}
|
||||
if r.specs == nil {
|
||||
r.specs = make(map[string]ValidatorSpec)
|
||||
if r.legacySpecs == nil {
|
||||
r.legacySpecs = make(map[string]ValidatorSpec)
|
||||
}
|
||||
r.constructors[normalizedSpec.Key] = constructor
|
||||
r.specs[normalizedSpec.Key] = normalizedSpec
|
||||
r.legacyConstructors[normalizedSpec.Key] = constructor
|
||||
r.legacySpecs[normalizedSpec.Key] = normalizedSpec
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ValidatorRegistry) Build(key string) (contracts.Validator, error) {
|
||||
func RegisterTypedValidator[T any](registry *ValidatorRegistry, kind contracts.ArtifactKind, spec ValidatorSpec, constructor func() (contracts.TypedValidator[T], error)) error {
|
||||
if registry == nil {
|
||||
return fmt.Errorf("validator registry must not be nil")
|
||||
}
|
||||
normalizedSpec, err := normalizeValidatorSpec(spec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
kind = normalizeArtifactKind(kind)
|
||||
if kind == "" {
|
||||
return fmt.Errorf("typed validator %q artifact kind must not be empty", normalizedSpec.Key)
|
||||
}
|
||||
if constructor == nil {
|
||||
return fmt.Errorf("validator constructor for %q must not be nil", normalizedSpec.Key)
|
||||
}
|
||||
key := artifactVariantKey{module: normalizedSpec.Key, kind: kind}
|
||||
if _, ok := registry.typedEntries[key]; ok {
|
||||
return fmt.Errorf("validator %q variant for artifact kind %q is already registered", key.module, key.kind)
|
||||
}
|
||||
if registry.typedEntries == nil {
|
||||
registry.typedEntries = make(map[artifactVariantKey]typedValidatorEntry)
|
||||
}
|
||||
registry.typedEntries[key] = typedValidatorEntry{
|
||||
spec: normalizedSpec,
|
||||
kind: kind,
|
||||
valueType: reflect.TypeFor[T](),
|
||||
constructor: func() (any, error) {
|
||||
return constructor()
|
||||
},
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func RegisterChunkValidator(registry *ValidatorRegistry, spec ValidatorSpec, constructor func() (contracts.ChunkValidator, error)) error {
|
||||
if registry == nil {
|
||||
return fmt.Errorf("validator registry must not be nil")
|
||||
}
|
||||
normalizedSpec, err := normalizeValidatorSpec(spec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if constructor == nil {
|
||||
return fmt.Errorf("validator constructor for %q must not be nil", normalizedSpec.Key)
|
||||
}
|
||||
if _, ok := registry.chunkEntries[normalizedSpec.Key]; ok {
|
||||
return fmt.Errorf("chunk validator %q is already registered", normalizedSpec.Key)
|
||||
}
|
||||
if registry.chunkEntries == nil {
|
||||
registry.chunkEntries = make(map[string]chunkValidatorEntry)
|
||||
}
|
||||
registry.chunkEntries[normalizedSpec.Key] = chunkValidatorEntry{spec: normalizedSpec, constructor: constructor}
|
||||
return nil
|
||||
}
|
||||
|
||||
func RegisterSerializedValidator(registry *ValidatorRegistry, spec SerializedValidatorSpec, constructor func() (contracts.SerializedValidator, error)) error {
|
||||
if registry == nil {
|
||||
return fmt.Errorf("validator registry must not be nil")
|
||||
}
|
||||
normalizedValidatorSpec, err := normalizeValidatorSpec(spec.ValidatorSpec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
spec.ValidatorSpec = normalizedValidatorSpec
|
||||
if !spec.SupportsChunks && !spec.SupportsArtifacts {
|
||||
return fmt.Errorf("serialized validator %q must support chunks, artifacts, or both", spec.Key)
|
||||
}
|
||||
if constructor == nil {
|
||||
return fmt.Errorf("validator constructor for %q must not be nil", spec.Key)
|
||||
}
|
||||
if _, ok := registry.serializedEntries[spec.Key]; ok {
|
||||
return fmt.Errorf("serialized validator %q is already registered", spec.Key)
|
||||
}
|
||||
if registry.serializedEntries == nil {
|
||||
registry.serializedEntries = make(map[string]serializedValidatorEntry)
|
||||
}
|
||||
registry.serializedEntries[spec.Key] = serializedValidatorEntry{spec: spec, constructor: constructor}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ValidatorRegistry) BuildLegacyRaw(key string) (contracts.LegacyRawValidator, error) {
|
||||
if r == nil {
|
||||
return nil, fmt.Errorf("validator registry must not be nil")
|
||||
}
|
||||
|
||||
normalizedKey := strings.TrimSpace(key)
|
||||
if normalizedKey == "" {
|
||||
return nil, fmt.Errorf("validator key must not be empty")
|
||||
}
|
||||
|
||||
constructor, ok := r.constructors[normalizedKey]
|
||||
constructor, ok := r.legacyConstructors[normalizedKey]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("validator %q is not registered", normalizedKey)
|
||||
return nil, fmt.Errorf("legacy raw validator %q is not registered", normalizedKey)
|
||||
}
|
||||
|
||||
validator, err := constructor()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("build validator %q: %w", normalizedKey, err)
|
||||
@@ -83,14 +196,10 @@ func (r *ValidatorRegistry) Build(key string) (contracts.Validator, error) {
|
||||
if validator.Name() != normalizedKey {
|
||||
return nil, fmt.Errorf("validator %q returned name %q", normalizedKey, validator.Name())
|
||||
}
|
||||
spec, ok := r.specs[normalizedKey]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("validator %q spec is not registered", normalizedKey)
|
||||
}
|
||||
spec := r.legacySpecs[normalizedKey]
|
||||
if validator.ExecutionClass() != spec.ExecutionClass {
|
||||
return nil, fmt.Errorf("validator %q returned execution class %q, want %q", normalizedKey, validator.ExecutionClass(), spec.ExecutionClass)
|
||||
}
|
||||
|
||||
return validator, nil
|
||||
}
|
||||
|
||||
@@ -98,28 +207,57 @@ func (r *ValidatorRegistry) Spec(key string) (ValidatorSpec, bool) {
|
||||
if r == nil {
|
||||
return ValidatorSpec{}, false
|
||||
}
|
||||
spec, ok := r.legacySpecs[strings.TrimSpace(key)]
|
||||
return spec, ok
|
||||
}
|
||||
|
||||
spec, ok := r.specs[strings.TrimSpace(key)]
|
||||
if !ok {
|
||||
return ValidatorSpec{}, false
|
||||
func (r *ValidatorRegistry) typedEntry(key string, kind contracts.ArtifactKind) (typedValidatorEntry, bool) {
|
||||
if r == nil {
|
||||
return typedValidatorEntry{}, false
|
||||
}
|
||||
return spec, true
|
||||
entry, ok := r.typedEntries[artifactVariantKey{module: strings.TrimSpace(key), kind: normalizeArtifactKind(kind)}]
|
||||
return entry, ok
|
||||
}
|
||||
|
||||
func (r *ValidatorRegistry) chunkEntry(key string) (chunkValidatorEntry, bool) {
|
||||
if r == nil {
|
||||
return chunkValidatorEntry{}, false
|
||||
}
|
||||
entry, ok := r.chunkEntries[strings.TrimSpace(key)]
|
||||
return entry, ok
|
||||
}
|
||||
|
||||
func (r *ValidatorRegistry) serializedEntry(key string) (serializedValidatorEntry, bool) {
|
||||
if r == nil {
|
||||
return serializedValidatorEntry{}, false
|
||||
}
|
||||
entry, ok := r.serializedEntries[strings.TrimSpace(key)]
|
||||
return entry, ok
|
||||
}
|
||||
|
||||
func (r *ValidatorRegistry) registeredTypedKinds(key string) []contracts.ArtifactKind {
|
||||
if r == nil {
|
||||
return nil
|
||||
}
|
||||
module := strings.TrimSpace(key)
|
||||
kinds := make([]contracts.ArtifactKind, 0)
|
||||
for variant := range r.typedEntries {
|
||||
if variant.module == module {
|
||||
kinds = append(kinds, variant.kind)
|
||||
}
|
||||
}
|
||||
sortArtifactKinds(kinds)
|
||||
return kinds
|
||||
}
|
||||
|
||||
func (r *ValidatorRegistry) RegisteredSpecs() []ValidatorSpec {
|
||||
if r == nil || len(r.specs) == 0 {
|
||||
if r == nil || len(r.legacySpecs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
keys := make([]string, 0, len(r.specs))
|
||||
for key := range r.specs {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
|
||||
keys := sortedRegistryKeys(r.legacySpecs)
|
||||
specs := make([]ValidatorSpec, 0, len(keys))
|
||||
for _, key := range keys {
|
||||
specs = append(specs, r.specs[key])
|
||||
specs = append(specs, r.legacySpecs[key])
|
||||
}
|
||||
return specs
|
||||
}
|
||||
@@ -128,15 +266,24 @@ func (r *ValidatorRegistry) RegisteredKeys() []string {
|
||||
if r == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return sortedRegistryKeys(r.constructors)
|
||||
keys := make(map[string]struct{})
|
||||
for key := range r.legacySpecs {
|
||||
keys[key] = struct{}{}
|
||||
}
|
||||
for key := range r.typedEntries {
|
||||
keys[key.module] = struct{}{}
|
||||
}
|
||||
for key := range r.chunkEntries {
|
||||
keys[key] = struct{}{}
|
||||
}
|
||||
for key := range r.serializedEntries {
|
||||
keys[key] = struct{}{}
|
||||
}
|
||||
return sortedRegistryKeys(keys)
|
||||
}
|
||||
|
||||
func normalizeValidatorSpec(spec ValidatorSpec) (ValidatorSpec, error) {
|
||||
normalized := ValidatorSpec{
|
||||
Key: strings.TrimSpace(spec.Key),
|
||||
ExecutionClass: spec.ExecutionClass,
|
||||
}
|
||||
normalized := ValidatorSpec{Key: strings.TrimSpace(spec.Key), ExecutionClass: spec.ExecutionClass}
|
||||
if normalized.Key == "" {
|
||||
return ValidatorSpec{}, fmt.Errorf("validator key must not be empty")
|
||||
}
|
||||
@@ -147,3 +294,7 @@ func normalizeValidatorSpec(spec ValidatorSpec) (ValidatorSpec, error) {
|
||||
}
|
||||
return normalized, nil
|
||||
}
|
||||
|
||||
func sortValidatorSpecs(specs []ValidatorSpec) {
|
||||
sort.Slice(specs, func(i, j int) bool { return specs[i].Key < specs[j].Key })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user