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

@@ -69,8 +69,9 @@ type ValidatorDocument struct {
// validation establishes which locations permit generated references and
// requires exactly one populated branch.
type ReferenceDocument struct {
Path *string `yaml:"path,omitempty"`
Artifact *ArtifactReferenceDocument `yaml:"artifact,omitempty"`
Path *string `yaml:"path,omitempty"`
Artifact *ArtifactReferenceDocument `yaml:"artifact,omitempty"`
Collections []string `yaml:"collections,omitempty"`
}
type ArtifactReferenceDocument struct {

View File

@@ -243,7 +243,11 @@ func documentReferencesToRuntime(documents map[string]ReferenceDocument) map[str
continue
}
if document.Path != nil {
references[name] = pipeline.ExternalReference(*document.Path)
reference := pipeline.ExternalReference(*document.Path)
if document.Collections != nil {
reference.Collections = append([]string{}, document.Collections...)
}
references[name] = reference
}
}
return references

View File

@@ -403,6 +403,14 @@ func validateDocumentReferences(references map[string]ReferenceDocument, path co
sourcePath := path.Key(name)
hasPath := source.Path != nil
hasArtifact := source.Artifact != nil
for index, collection := range source.Collections {
if collection == "" {
validation.Error(sourcePath.Key("collections").Index(index), "must not be empty")
}
}
if hasArtifact && source.Collections != nil {
validation.Error(sourcePath.Key("collections"), "is only supported with an external path")
}
if hasPath == hasArtifact {
validation.Error(sourcePath, "must select exactly one reference source")
continue

View File

@@ -0,0 +1,57 @@
package config
import (
"bytes"
"strings"
"testing"
"go.maximumdirect.net/configkit"
)
func TestReferenceCollectionsBindingAndInspection(t *testing.T) {
loaded, err := loadConfigSource(configkit.Bytes("settings.yml", []byte(`version: 6
pipelines:
main:
input: {module: input}
references:
omitted: {path: a.yml}
"null": {path: b.yml, collections: null}
empty: {path: c.yml, collections: []}
named: {path: d.yml, collections: [spells, " Mixed "]}
`)), LoadOptions{})
if err != nil {
if native, ok := err.(*configkit.LoadError); ok {
t.Fatalf("%v: %#v", err, native.Diagnostics())
}
t.Fatal(err)
}
refs := loaded.Config.Pipelines["main"].References
if refs["omitted"].Collections != nil || refs["null"].Collections != nil || refs["empty"].Collections == nil || len(refs["empty"].Collections) != 0 || refs["named"].Collections[1] != " Mixed " {
t.Fatalf("unexpected collection selections: %#v", refs)
}
copy := loaded.RuntimeConfig()
copy.Pipelines["main"].References["named"].Collections[0] = "changed"
if refs["named"].Collections[0] != "spells" {
t.Fatal("detached runtime selection mutated source")
}
var effective bytes.Buffer
if err := loaded.WriteEffective(&effective); err != nil || !strings.Contains(effective.String(), "collections: []") {
t.Fatalf("effective config = %s, %v", effective.String(), err)
}
}
func TestReferenceCollectionsRejectInvalidInput(t *testing.T) {
for _, value := range []string{
"{path: a.yml, collections: [\"\"]}",
"{path: a.yml, collections: nope}",
"{path: a.yml, collections: [3]}",
"{artifact: {step: earlier, lane: output}, collections: []}",
} {
t.Run(value, func(t *testing.T) {
content := "version: 6\npipelines:\n main:\n input: {module: input}\n artifacts:\n output:\n extract:\n module: extract\n references:\n ref: " + value + "\n"
if _, err := loadConfigSource(configkit.Bytes("settings.yml", []byte(content)), LoadOptions{}); err == nil {
t.Fatal("accepted invalid reference selection")
}
})
}
}

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 "}}