Project version protections through item placement
This commit is contained in:
@@ -163,26 +163,54 @@ func nestedDirectiveDiagnostics(node *document.Node, current Path) []Diagnostic
|
||||
}
|
||||
|
||||
func fragmentVersionClaim(root *document.Node, rule *VersionRule) *document.Node {
|
||||
return fragmentVersionClaimProjected(root, rule, -1, nil)
|
||||
}
|
||||
|
||||
func fragmentVersionClaimProjected(root *document.Node, rule *VersionRule, sourceIndex int, ranges []sequenceRange) *document.Node {
|
||||
if rule == nil {
|
||||
return nil
|
||||
}
|
||||
current := root
|
||||
currentPath := Path{}
|
||||
if len(rule.Path.segments) == 0 {
|
||||
return current
|
||||
}
|
||||
for _, segment := range rule.Path.segments {
|
||||
if current.Kind != document.Mapping {
|
||||
switch current.Kind {
|
||||
case document.Mapping:
|
||||
if segment.kind == pathSegmentIndex {
|
||||
return current
|
||||
}
|
||||
index := documentEntryIndex(current.Mapping, segment.key)
|
||||
if index < 0 {
|
||||
return nil
|
||||
}
|
||||
current = current.Mapping[index].Value
|
||||
currentPath = currentPath.Key(segment.key)
|
||||
case document.Sequence:
|
||||
if segment.kind != pathSegmentIndex {
|
||||
return current
|
||||
}
|
||||
index := segment.index
|
||||
for _, assigned := range ranges {
|
||||
if assigned.sourceIndex != sourceIndex || assigned.path.String() != currentPath.String() {
|
||||
continue
|
||||
}
|
||||
if index < assigned.start || index >= assigned.end {
|
||||
return nil
|
||||
}
|
||||
index -= assigned.start
|
||||
break
|
||||
}
|
||||
if index < 0 || index >= len(current.Sequence) {
|
||||
return nil
|
||||
}
|
||||
current = current.Sequence[index]
|
||||
currentPath = currentPath.Index(index)
|
||||
default:
|
||||
// A scalar or null at an ancestor replaces the protected subtree.
|
||||
return current
|
||||
}
|
||||
if segment.kind == pathSegmentIndex {
|
||||
return nil
|
||||
}
|
||||
key := segment.key
|
||||
index := documentEntryIndex(current.Mapping, key)
|
||||
if index < 0 {
|
||||
return nil
|
||||
}
|
||||
current = current.Mapping[index].Value
|
||||
}
|
||||
return current
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user