yq/pkg/yqlib/operator_pick.go

87 lines
2.4 KiB
Go
Raw Normal View History

2022-03-09 03:38:02 +00:00
package yqlib
import (
"container/list"
"fmt"
)
2023-04-08 10:17:13 +00:00
func pickMap(original *CandidateNode, indices *CandidateNode) *CandidateNode {
2022-03-09 03:38:02 +00:00
2023-04-08 10:17:13 +00:00
filteredContent := make([]*CandidateNode, 0)
2022-03-09 03:38:02 +00:00
for index := 0; index < len(indices.Content); index = index + 1 {
keyToFind := indices.Content[index]
indexInMap := findKeyInMap(original, keyToFind)
if indexInMap > -1 {
2023-04-08 10:17:13 +00:00
clonedKey := original.Content[indexInMap].Copy()
clonedValue := original.Content[indexInMap+1].Copy()
2022-03-09 03:38:02 +00:00
filteredContent = append(filteredContent, clonedKey, clonedValue)
}
}
2023-04-08 10:17:13 +00:00
newNode := original.CopyWithoutContent()
2022-03-09 03:38:02 +00:00
newNode.Content = filteredContent
return newNode
}
2023-04-08 10:17:13 +00:00
func pickSequence(original *CandidateNode, indices *CandidateNode) (*CandidateNode, error) {
2022-03-09 03:38:02 +00:00
2023-04-08 10:17:13 +00:00
filteredContent := make([]*CandidateNode, 0)
2022-03-09 03:38:02 +00:00
for index := 0; index < len(indices.Content); index = index + 1 {
2022-05-22 11:19:59 +00:00
indexInArray, err := parseInt(indices.Content[index].Value)
2022-03-09 03:38:02 +00:00
if err != nil {
return nil, fmt.Errorf("cannot index array with %v", indices.Content[index].Value)
}
2022-05-06 03:46:14 +00:00
if indexInArray > -1 && indexInArray < len(original.Content) {
2023-04-08 10:17:13 +00:00
filteredContent = append(filteredContent, original.Content[indexInArray].Copy())
2022-03-09 03:38:02 +00:00
}
}
2023-04-08 10:17:13 +00:00
newNode := original.CopyWithoutContent()
2022-03-09 03:38:02 +00:00
newNode.Content = filteredContent
return newNode, nil
}
func pickOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
log.Debugf("Pick")
contextIndicesToPick, err := d.GetMatchingNodes(context, expressionNode.RHS)
if err != nil {
return Context{}, err
}
2023-04-08 10:17:13 +00:00
indicesToPick := &CandidateNode{}
2022-03-09 03:38:02 +00:00
if contextIndicesToPick.MatchingNodes.Len() > 0 {
2023-04-08 10:17:13 +00:00
indicesToPick = contextIndicesToPick.MatchingNodes.Front().Value.(*CandidateNode)
2022-03-09 03:38:02 +00:00
}
var results = list.New()
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
2023-04-08 10:17:13 +00:00
node := candidate.unwrapDocument()
2022-03-09 03:38:02 +00:00
2023-04-08 10:17:13 +00:00
var replacement *CandidateNode
if node.Kind == MappingNode {
2022-03-09 03:38:02 +00:00
replacement = pickMap(node, indicesToPick)
2023-04-08 10:17:13 +00:00
} else if node.Kind == SequenceNode {
2022-03-09 03:38:02 +00:00
replacement, err = pickSequence(node, indicesToPick)
if err != nil {
return Context{}, err
}
} else {
return Context{}, fmt.Errorf("cannot pick indicies from type %v (%v)", node.Tag, candidate.GetNicePath())
}
2023-04-08 10:17:13 +00:00
replacement.LeadingContent = candidate.LeadingContent
replacement.TrailingContent = candidate.TrailingContent
results.PushBack(replacement)
2022-03-09 03:38:02 +00:00
}
return context.ChildContext(results), nil
}