Reuse document indexes for collection policy checks

This commit is contained in:
2026-10-24 14:17:09 +00:00
parent a0e5693d07
commit ea46001f78
4 changed files with 135 additions and 8 deletions

View File

@@ -138,8 +138,12 @@ func mergeOverlayMappingsWithCollectionPolicy(
for key := range declarationsByPath {
registered[key] = true
}
var overlayLookup *documentLookup
if len(result.collectionPolicies.collections) != 0 {
overlayLookup = buildDocumentLookup(overlay)
}
for key := range result.collectionPolicies.collections {
if node, found := documentAtPath(overlay, result.collectionPolicies.collections[key].destination); found && node != nil {
if node, found := documentAtPathWithLookup(overlay, result.collectionPolicies.collections[key].destination, overlayLookup); found && node != nil {
registered[key] = true
registeredPaths[key] = result.collectionPolicies.collections[key].destination.clone()
}
@@ -210,6 +214,9 @@ func mergeOverlayMappingsWithCollectionPolicy(
return nil, canonicalDiagnostics(combinationDiagnostics)
}
// Values are now complete. Index lazily for declaration-only checks;
// this lookup is never retained across the value-changing merge above.
var retainedLookup *documentLookup
for key, grouped := range declarationsByPath {
if policyValues[key] {
continue
@@ -226,7 +233,10 @@ func mergeOverlayMappingsWithCollectionPolicy(
if found {
lowerPolicy = &lower
}
retained, _ := documentAtPath(assembly.current.root, path)
if retainedLookup == nil {
retainedLookup = buildDocumentLookup(assembly.current.root)
}
retained, _ := documentAtPathWithLookup(assembly.current.root, path, retainedLookup)
if retained != nil && retained.Kind == document.Sequence && len(current.effectiveIdentityFields) != 0 {
if _, issues := validateSequenceIdentity(retained, path, current.effectiveIdentityFields, sequenceIdentityDiagnosticCodes{invalid: CodeSequenceInvalid, conflict: CodeSequenceConflict}); len(issues) != 0 {
combinationDiagnostics = append(combinationDiagnostics, issues...)