190 lines
7.6 KiB
Go
190 lines
7.6 KiB
Go
package engine
|
|
|
|
import (
|
|
"gitea.maximumdirect.net/eric/ConfigKit/internal/document"
|
|
)
|
|
|
|
// assembledInput is the source/document handoff across schema and version checks.
|
|
type assembledInput struct {
|
|
source Source
|
|
parsed parsedSource
|
|
document *document.Node
|
|
composition *composedDocument
|
|
acquisition []acquisitionScope
|
|
inheritance *inheritanceScope
|
|
sources []SourceInfo
|
|
contexts map[string]ResourceContext
|
|
versionState *sourceVersionState
|
|
accounting *importAccounting
|
|
resolvedFilesystem *resolvedFilesystemContext
|
|
}
|
|
|
|
// acquiredContribution keeps a source-local payload beside its placed document.
|
|
// The latter is used for composition; the former preserves the acquisition
|
|
// boundary for later scoped assembly and evidence.
|
|
type acquiredContribution struct {
|
|
source SourceInfo
|
|
identity importNodeKey
|
|
role importContributionRole
|
|
scopeOwner importNodeKey
|
|
payload *document.Node
|
|
document *document.Node
|
|
destination Path
|
|
relativeDestination Path
|
|
processing *sourceVersionProcessingResult
|
|
sequenceDeclarations []sequenceDeclarationSpec
|
|
nested *composedDocument
|
|
pendingItem *acquisitionScope
|
|
placementCharged bool
|
|
}
|
|
|
|
// acquisitionScope is the ordered strict mapping scope produced by one input
|
|
// and its ordinary imports.
|
|
type acquisitionScope struct {
|
|
owner importNodeKey
|
|
destination Path
|
|
contributions []acquiredContribution
|
|
collectionPolicies collectionPolicyScope
|
|
}
|
|
|
|
func cloneAcquisitionScope(scope acquisitionScope) acquisitionScope {
|
|
cloned := acquisitionScope{
|
|
owner: scope.owner, destination: scope.destination.clone(),
|
|
collectionPolicies: cloneCollectionPolicyScope(scope.collectionPolicies),
|
|
}
|
|
if scope.contributions != nil {
|
|
cloned.contributions = make([]acquiredContribution, len(scope.contributions))
|
|
for index, contribution := range scope.contributions {
|
|
cloned.contributions[index] = acquiredContribution{
|
|
source: cloneSourceInfo(contribution.source), identity: contribution.identity,
|
|
role: contribution.role, scopeOwner: contribution.scopeOwner, payload: contribution.payload,
|
|
document: contribution.document, destination: contribution.destination.clone(),
|
|
relativeDestination: contribution.relativeDestination.clone(), processing: contribution.processing,
|
|
sequenceDeclarations: cloneSequenceDeclarations(contribution.sequenceDeclarations),
|
|
nested: cloneComposedDocument(contribution.nested), pendingItem: cloneAcquisitionScopePointer(contribution.pendingItem),
|
|
placementCharged: contribution.placementCharged,
|
|
}
|
|
}
|
|
}
|
|
return cloned
|
|
}
|
|
|
|
func cloneAcquisitionScopePointer(scope *acquisitionScope) *acquisitionScope {
|
|
if scope == nil {
|
|
return nil
|
|
}
|
|
cloned := cloneAcquisitionScope(*scope)
|
|
return &cloned
|
|
}
|
|
|
|
func cloneAcquisitionScopes(scopes []acquisitionScope) []acquisitionScope {
|
|
if scopes == nil {
|
|
return nil
|
|
}
|
|
cloned := make([]acquisitionScope, len(scopes))
|
|
for index, scope := range scopes {
|
|
cloned[index] = cloneAcquisitionScope(scope)
|
|
}
|
|
return cloned
|
|
}
|
|
|
|
func assembleBaseInput[T any](source Source, options Options[T]) (assembledInput, []Diagnostic) {
|
|
return assembleBaseInputTracked(source, options, Path{}, &importAccounting{}, nil, nil)
|
|
}
|
|
|
|
func assembleBaseInputTracked[T any](source Source, options Options[T], destination Path, accounting *importAccounting, visited map[resourceIdentity]importRoute, hostToken *namespaceToken) (assembledInput, []Diagnostic) {
|
|
return assembleBaseInputTrackedWithVersion(source, options, destination, accounting, visited, hostToken, nil)
|
|
}
|
|
|
|
func assembleBaseInputTrackedWithVersion[T any](source Source, options Options[T], destination Path, accounting *importAccounting, visited map[resourceIdentity]importRoute, hostToken *namespaceToken, versionState *sourceVersionState) (assembledInput, []Diagnostic) {
|
|
return assembleBaseInputTrackedForRole(source, options, destination, accounting, visited, hostToken, versionState, sourceVersionMain)
|
|
}
|
|
|
|
func registerAcquiredSource(source Source, context *FilesystemContext, info *SourceInfo, visited map[resourceIdentity]importRoute, hostToken *namespaceToken) *Diagnostic {
|
|
if visited == nil || info == nil || info.Path == "" {
|
|
return nil
|
|
}
|
|
identity := resourceIdentity{namespace: hostToken, path: info.Path}
|
|
if source.kind == sourceFilesystem {
|
|
identity.namespace = source.namespace.token
|
|
} else if context != nil && context.FS != nil {
|
|
identity.namespace = context.FS.namespace.token
|
|
}
|
|
if previous, repeated := visited[identity]; repeated {
|
|
value := importRepeatedDiagnostic(Location{SourceID: info.ID}, previous, importRoute{})
|
|
return &value
|
|
}
|
|
visited[identity] = importRoute{canonical: info.Path}
|
|
return nil
|
|
}
|
|
|
|
func applyInputLayers[T any](source Source, options Options[T], input assembledInput) (assembledInput, []Diagnostic) {
|
|
if !activeOverlay(options.Overlay) {
|
|
return input, nil
|
|
}
|
|
composition, diagnostics := compositionForInputWithPolicy(input, options.sequencePolicy)
|
|
if len(diagnostics) != 0 {
|
|
return input, diagnostics
|
|
}
|
|
assembly := newAssemblyAccumulator(composition)
|
|
if activeOverlay(options.Overlay) {
|
|
input.sources, diagnostics = loadSelectedOverlayIntoWithVersion(
|
|
source, options.Filesystem, input.parsed, assembly, input.sources,
|
|
options.Overlay, options.Definition.Version, defaultImportLimits(), input.versionState, options.sequencePolicy,
|
|
)
|
|
if len(diagnostics) != 0 {
|
|
return input, diagnostics
|
|
}
|
|
contexts, diagnostic := contextsForAcquiredSources(source, options.Filesystem, input.parsed.source, input.sources[len(input.sources)-1:])
|
|
if diagnostic != nil {
|
|
return input, []Diagnostic{*diagnostic}
|
|
}
|
|
mergeResourceContexts(&input.contexts, contexts)
|
|
}
|
|
input.composition = assembly.current
|
|
input.document = assembly.current.root
|
|
return input, nil
|
|
}
|
|
|
|
// compositionForInput initializes the structural handoff used by inheritance
|
|
// expansion. Raw source bytes and mapping keys retain their budgets.
|
|
func compositionForInput(input assembledInput) (*composedDocument, []Diagnostic) {
|
|
return compositionForInputWithPolicy(input, sequencePolicy{})
|
|
}
|
|
|
|
func compositionForInputWithPolicy(input assembledInput, policy sequencePolicy) (*composedDocument, []Diagnostic) {
|
|
return compositionForInputWithPolicies(input, collectionPolicyScope{}, policy)
|
|
}
|
|
|
|
func compositionForInputWithPolicies(input assembledInput, lower collectionPolicyScope, policy sequencePolicy) (*composedDocument, []Diagnostic) {
|
|
if input.composition != nil {
|
|
if input.composition.root == nil && len(input.composition.acquisition) != 0 {
|
|
return composeAcquiredInputWithPolicies(input, lower, policy)
|
|
}
|
|
return input.composition, nil
|
|
}
|
|
if len(input.acquisition) != 0 {
|
|
acquired := &composedDocument{acquisition: cloneAcquisitionScopes(input.acquisition)}
|
|
if input.accounting != nil {
|
|
acquired.inputBytes = input.accounting.bytes
|
|
acquired.inputNodes = input.accounting.nodes
|
|
} else {
|
|
acquired.inputBytes = input.parsed.bytes
|
|
acquired.inputNodes = countInputNodes(input.parsed.document)
|
|
}
|
|
input.composition = acquired
|
|
return composeAcquiredInputWithPolicies(input, lower, policy)
|
|
}
|
|
if input.document == nil {
|
|
return &composedDocument{root: &document.Node{Kind: document.Mapping}, origins: make(map[string]Origin)}, nil
|
|
}
|
|
composition, diagnostics := composeDocumentsWithCollectionPolicy(policy, lower, nil, input.document)
|
|
if len(diagnostics) != 0 {
|
|
return nil, diagnostics
|
|
}
|
|
composition.inputBytes = input.parsed.bytes
|
|
composition.inputNodes = countInputNodes(input.document)
|
|
composition.acquisition = cloneAcquisitionScopes(input.acquisition)
|
|
return composition, nil
|
|
}
|