Centralize pure pipeline profile and binding copies
This commit is contained in:
139
internal/framework/pipeline/profile_copy.go
Normal file
139
internal/framework/pipeline/profile_copy.go
Normal file
@@ -0,0 +1,139 @@
|
||||
package pipeline
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
// ClonePipelineProfile detaches the supported mutable profile values without
|
||||
// interpreting defaults, normalizing identifiers, or changing nil/empty values.
|
||||
func ClonePipelineProfile(profile PipelineProfile) PipelineProfile {
|
||||
profile.Mode = cloneControl(profile.Mode)
|
||||
profile.MaxAdditionalTurns = cloneControl(profile.MaxAdditionalTurns)
|
||||
profile.Cache = cloneControl(profile.Cache)
|
||||
profile.Input = CloneModuleBinding(profile.Input)
|
||||
profile.Chunk = CloneModuleBinding(profile.Chunk)
|
||||
profile.Output = CloneModuleBinding(profile.Output)
|
||||
profile.References = cloneReferenceSources(profile.References)
|
||||
profile.Artifacts = cloneArtifactProfiles(profile.Artifacts)
|
||||
if profile.Steps != nil {
|
||||
steps := make([]PipelineStepProfile, len(profile.Steps))
|
||||
for i, step := range profile.Steps {
|
||||
step.References = cloneReferenceSources(step.References)
|
||||
step.Artifacts = cloneArtifactProfiles(step.Artifacts)
|
||||
steps[i] = step
|
||||
}
|
||||
profile.Steps = steps
|
||||
}
|
||||
return profile
|
||||
}
|
||||
|
||||
func cloneArtifactProfiles(artifacts map[string]ArtifactLaneProfile) map[string]ArtifactLaneProfile {
|
||||
if artifacts == nil {
|
||||
return nil
|
||||
}
|
||||
cloned := make(map[string]ArtifactLaneProfile, len(artifacts))
|
||||
for key, lane := range artifacts {
|
||||
lane.Extract = CloneModuleBinding(lane.Extract)
|
||||
lane.Merge = CloneModuleBinding(lane.Merge)
|
||||
lane.Normalize = CloneModuleBinding(lane.Normalize)
|
||||
lane.Validators = cloneModuleBindings(lane.Validators)
|
||||
lane.References = cloneReferenceSources(lane.References)
|
||||
cloned[key] = lane
|
||||
}
|
||||
return cloned
|
||||
}
|
||||
|
||||
// CloneModuleBinding copies supported mutable binding values without normalization.
|
||||
func CloneModuleBinding(binding ModuleBinding) ModuleBinding {
|
||||
binding.Mode = cloneControl(binding.Mode)
|
||||
binding.MaxAdditionalTurns = cloneControl(binding.MaxAdditionalTurns)
|
||||
binding.Cache = cloneControl(binding.Cache)
|
||||
binding.Options = CloneModuleOptions(binding.Options)
|
||||
binding.References = cloneReferenceSources(binding.References)
|
||||
binding.Validators = cloneValidatorOverride(binding.Validators)
|
||||
return binding
|
||||
}
|
||||
|
||||
func cloneModuleBindings(bindings []ModuleBinding) []ModuleBinding {
|
||||
if bindings == nil {
|
||||
return nil
|
||||
}
|
||||
out := make([]ModuleBinding, len(bindings))
|
||||
for i, binding := range bindings {
|
||||
out[i] = CloneModuleBinding(binding)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneReferenceSources(references map[string]ReferenceSource) map[string]ReferenceSource {
|
||||
if references == nil {
|
||||
return nil
|
||||
}
|
||||
out := make(map[string]ReferenceSource, len(references))
|
||||
for key, source := range references {
|
||||
out[key] = cloneReferenceSource(source)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneReferenceSource(source ReferenceSource) ReferenceSource {
|
||||
if source.Artifact != nil {
|
||||
artifact := *source.Artifact
|
||||
source.Artifact = &artifact
|
||||
}
|
||||
return source
|
||||
}
|
||||
|
||||
func cloneValidatorOverride(override ValidatorOverride) ValidatorOverride {
|
||||
override.Validators = cloneModuleBindings(override.Validators)
|
||||
return override
|
||||
}
|
||||
|
||||
// CloneModuleOptions detaches the supported option containers without interpretation.
|
||||
func CloneModuleOptions(options map[string]any) map[string]any {
|
||||
if options == nil {
|
||||
return nil
|
||||
}
|
||||
out := make(map[string]any, len(options))
|
||||
for key, value := range options {
|
||||
out[key] = cloneOptionValue(value)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneOptionValue(value any) any {
|
||||
switch typed := value.(type) {
|
||||
case map[string]any:
|
||||
return CloneModuleOptions(typed)
|
||||
case []any:
|
||||
if typed == nil {
|
||||
return []any(nil)
|
||||
}
|
||||
out := make([]any, len(typed))
|
||||
for i, item := range typed {
|
||||
out[i] = cloneOptionValue(item)
|
||||
}
|
||||
return out
|
||||
case []string:
|
||||
if typed == nil {
|
||||
return []string(nil)
|
||||
}
|
||||
out := make([]string, len(typed))
|
||||
copy(out, typed)
|
||||
return out
|
||||
case []byte:
|
||||
if typed == nil {
|
||||
return []byte(nil)
|
||||
}
|
||||
out := make([]byte, len(typed))
|
||||
copy(out, typed)
|
||||
return out
|
||||
case json.RawMessage:
|
||||
if typed == nil {
|
||||
return json.RawMessage(nil)
|
||||
}
|
||||
out := make(json.RawMessage, len(typed))
|
||||
copy(out, typed)
|
||||
return out
|
||||
default:
|
||||
return value
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user