Index CLI override validation

This commit is contained in:
2026-10-09 03:45:43 +00:00
parent e591e97f22
commit 5b8b7a2415
3 changed files with 137 additions and 16 deletions

View File

@@ -50,12 +50,16 @@ func prepareOverrides(base *composedDocument, sources []SourceInfo, overrides []
}
seen := make(map[string]int, len(overrides))
pathAccepted := make([]bool, len(overrides))
var acceptedPaths pathPrefixIndex
var baseLookup *documentLookup
if len(overrides) != 0 {
baseLookup = buildDocumentLookup(base.root)
}
var usedBytes int64
usedNodes := 0
for index, override := range overrides {
location := &Location{SourceID: prepared[index].source.ID}
if issue := validateOverridePath(override.Path, base.root); issue != nil {
if issue := validateOverridePath(override.Path, base.root, baseLookup); issue != nil {
issue.Primary = location
diagnostics = append(diagnostics, *issue)
continue
@@ -70,7 +74,7 @@ func prepareOverrides(base *composedDocument, sources []SourceInfo, overrides []
continue
}
seen[pointer] = index
if conflicting := overlappingOverride(overrides[:index], pathAccepted[:index], override.Path); conflicting >= 0 {
if conflicting, overlaps := acceptedPaths.earliestIntersectingID(pointer); overlaps {
diagnostics = append(diagnostics, Diagnostic{
Code: CodeOverrideOverlap, Severity: SeverityError, Path: override.Path.clone(),
Message: "configuration override paths overlap", Primary: location,
@@ -92,7 +96,7 @@ func prepareOverrides(base *composedDocument, sources []SourceInfo, overrides []
})
continue
}
pathAccepted[index] = true
acceptedPaths.insert(pointer, index)
builder := valueBuilder{sourceID: prepared[index].source.ID, active: make(map[valueVisit]bool)}
value, issue := builder.build(override.Value, override.Path, len(override.Path.segments))
@@ -150,7 +154,7 @@ func validOverrideLabel(label string) bool {
return true
}
func validateOverridePath(path Path, root *document.Node) *Diagnostic {
func validateOverridePath(path Path, root *document.Node, lookup *documentLookup) *Diagnostic {
if !path.valid() || len(path.segments) == 0 {
return &Diagnostic{Code: CodeOverrideInvalidPath, Severity: SeverityError, Path: path.clone(), Message: "configuration override path must be a valid non-root mapping path"}
}
@@ -165,7 +169,7 @@ func validateOverridePath(path Path, root *document.Node) *Diagnostic {
if current.Kind != document.Mapping {
return &Diagnostic{Code: CodeOverrideInvalidPath, Severity: SeverityError, Path: path.clone(), Message: "configuration override path traverses a non-mapping value"}
}
position := documentEntryIndex(current.Mapping, segment.key)
position := lookup.find(current, segment.key)
if index == len(path.segments)-1 {
continue
}
@@ -178,15 +182,6 @@ func validateOverridePath(path Path, root *document.Node) *Diagnostic {
return nil
}
func overlappingOverride(previous []Override, valid []bool, path Path) int {
for index, override := range previous {
if valid[index] && (pathContains(override.Path, path) || pathContains(path, override.Path)) {
return index
}
}
return -1
}
func pathContainsDirective(path Path) bool {
for _, segment := range path.segments {
if segment.key == directiveKey {