Carry collection selections through configuration

This commit is contained in:
2026-11-29 17:53:36 +00:00
parent a0c5ca8761
commit 10221b0624
10 changed files with 172 additions and 6 deletions

View File

@@ -25,10 +25,24 @@ type ReferenceBinding struct {
LaneID string `json:"lane_id,omitempty"`
SlotName string `json:"slot_name"`
Source string `json:"source,omitempty"`
Collections []string `json:"collections,omitempty"`
Artifact *ArtifactReference `json:"artifact,omitempty"`
BindingSource string `json:"binding_source,omitempty"`
}
// MarshalJSON preserves the distinction between default and empty selections.
func (binding ReferenceBinding) MarshalJSON() ([]byte, error) {
type bindingFields ReferenceBinding
var collections *[]string
if binding.Collections != nil {
collections = &binding.Collections
}
return json.Marshal(struct {
bindingFields
Collections *[]string `json:"collections,omitempty"`
}{bindingFields: bindingFields(binding), Collections: collections})
}
type ReferenceUnbind struct {
Stage ModuleStage `json:"stage,omitempty"`
LaneID string `json:"lane_id,omitempty"`

View File

@@ -75,6 +75,9 @@ func cloneReferenceSources(references map[string]ReferenceSource) map[string]Ref
}
func cloneReferenceSource(source ReferenceSource) ReferenceSource {
if source.Collections != nil {
source.Collections = append([]string{}, source.Collections...)
}
if source.Artifact != nil {
artifact := *source.Artifact
source.Artifact = &artifact

View File

@@ -37,8 +37,9 @@ type ArtifactReference struct {
// ReferenceSource is the discriminated source form used by configured
// reference maps. Exactly one of Path and Artifact is set after resolution.
type ReferenceSource struct {
Path string `json:"path,omitempty"`
Artifact *ArtifactReference `json:"artifact,omitempty"`
Path string `json:"path,omitempty"`
Artifact *ArtifactReference `json:"artifact,omitempty"`
Collections []string `json:"collections,omitempty"`
}
func ExternalReference(path string) ReferenceSource {
@@ -64,10 +65,19 @@ func (source ReferenceSource) MarshalJSON() ([]byte, error) {
return nil, fmt.Errorf("reference source must not contain both an external path and an artifact selector")
}
if source.Artifact != nil {
if source.Collections != nil {
return nil, fmt.Errorf("collection selection requires an external path")
}
return json.Marshal(struct {
Artifact *ArtifactReference `json:"artifact"`
}{Artifact: source.Artifact})
}
if source.Collections != nil {
return json.Marshal(struct {
Path string `json:"path"`
Collections []string `json:"collections"`
}{Path: source.Path, Collections: source.Collections})
}
return json.Marshal(source.Path)
}

View File

@@ -9,11 +9,17 @@ import (
)
func referenceTarget(stage ModuleStage, laneID string, module string, bindings []ReferenceBinding) ResolvedReferenceTarget {
bindings = append([]ReferenceBinding(nil), bindings...)
for i := range bindings {
if bindings[i].Collections != nil {
bindings[i].Collections = append([]string{}, bindings[i].Collections...)
}
}
return ResolvedReferenceTarget{
Stage: stage,
LaneID: strings.TrimSpace(laneID),
Module: strings.TrimSpace(module),
Bindings: append([]ReferenceBinding(nil), bindings...),
Bindings: bindings,
}
}
@@ -187,12 +193,20 @@ func normalizeTargetReferenceSource(target referenceResolutionTarget, slotName s
if slotName == "" {
return "", ReferenceSource{}, fmt.Errorf("%s reference slot name must not be empty", referenceTargetErrorContext(target))
}
for _, name := range source.Collections {
if name == "" {
return "", ReferenceSource{}, fmt.Errorf("%s reference slot %q collection name must not be empty", referenceTargetErrorContext(target), slotName)
}
}
if source.Artifact == nil {
source.Path = strings.TrimSpace(source.Path)
if source.Path == "" {
return "", ReferenceSource{}, fmt.Errorf("%s reference slot %q source must not be empty", referenceTargetErrorContext(target), slotName)
}
} else {
if source.Collections != nil {
return "", ReferenceSource{}, fmt.Errorf("%s reference slot %q collection selection requires an external path", referenceTargetErrorContext(target), slotName)
}
if strings.TrimSpace(source.Path) != "" {
return "", ReferenceSource{}, fmt.Errorf("%s reference slot %q must contain exactly one source form", referenceTargetErrorContext(target), slotName)
}
@@ -222,6 +236,9 @@ func addReferenceBinding(target referenceResolutionTarget, slots map[string]cont
binding.Artifact = source.Artifact
} else {
binding.Source = source.Path
if source.Collections != nil {
binding.Collections = append([]string{}, source.Collections...)
}
}
bindings[slotName] = binding
return nil

View File

@@ -1,8 +1,11 @@
package pipeline
import (
"encoding/json"
"strings"
"testing"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
)
func TestReferenceSourceMapsConflictPreservesTrimmedSourceKinds(t *testing.T) {
@@ -35,6 +38,53 @@ func TestReferenceSourceMapsConflictPreservesTrimmedSourceKinds(t *testing.T) {
}
}
func TestReferenceCollectionResolutionAndCopy(t *testing.T) {
pipelineSource := ReferenceSource{Path: "base.yml", Collections: []string{"base"}}
localSource := ReferenceSource{Path: "local.yml", Collections: []string{}}
target := referenceResolutionTarget{
PipelineID: "demo", Stage: StageExtract, Module: "extractor",
Slots: []contracts.ReferenceSlot{{Name: "glossary"}},
PipelineReferences: map[string]ReferenceSource{"glossary": pipelineSource},
LocalReferences: map[string]ReferenceSource{"glossary": localSource},
}
bindings, err := resolveReferenceTargetBindings(target)
if err != nil || len(bindings) != 1 || bindings[0].Source != "local.yml" || bindings[0].Collections == nil || len(bindings[0].Collections) != 0 {
t.Fatalf("local binding = %#v, %v", bindings, err)
}
target.LocalReferences["glossary"] = ReferenceSource{Path: "other.yml"}
bindings, err = resolveReferenceTargetBindings(target)
if err != nil || bindings[0].Collections != nil {
t.Fatalf("replacement inherited collections: %#v, %v", bindings, err)
}
target.LocalReferences = nil
bindings, err = resolveReferenceTargetBindings(target)
if err != nil {
t.Fatal(err)
}
bindings[0].Collections[0] = "changed"
if pipelineSource.Collections[0] != "base" {
t.Fatal("binding selection aliases input")
}
profile := ClonePipelineProfile(PipelineProfile{References: map[string]ReferenceSource{"glossary": pipelineSource, "empty": localSource}})
profile.References["glossary"].Collections[0] = "changed"
if pipelineSource.Collections[0] != "base" || profile.References["empty"].Collections == nil {
t.Fatal("profile clone lost selection isolation or explicit emptiness")
}
withEmpty, err := json.Marshal(localSource)
if err != nil || !strings.Contains(string(withEmpty), `"collections":[]`) {
t.Fatalf("encoded reference = %s, %v", withEmpty, err)
}
for _, test := range []struct {
selection []string
wantEmpty bool
}{{nil, false}, {[]string{}, true}} {
encoded, err := json.Marshal(ReferenceBinding{SlotName: "glossary", Collections: test.selection})
if err != nil || strings.Contains(string(encoded), `"collections":[]`) != test.wantEmpty {
t.Fatalf("encoded binding = %s, %v", encoded, err)
}
}
}
func TestNormalizeTargetReferenceSourceOwnsAndValidatesSelector(t *testing.T) {
target := referenceResolutionTarget{PipelineID: "demo", StepID: "consumer", LaneID: "lane", Stage: StageExtract, Module: "extractor"}
input := ReferenceSource{Artifact: &ArtifactReference{Step: " producer ", Lane: " output "}}