yq/pkg/yqlib/operator_pick.go

85 lines
2.3 KiB
Go
Raw Normal View History

2022-03-09 03:38:02 +00:00
package yqlib
import (
"container/list"
"fmt"
)
func pickMap(original *CandidateNode, indices *CandidateNode) *CandidateNode {
2022-03-09 03:38:02 +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 {
clonedKey := original.Content[indexInMap].Copy()
clonedValue := original.Content[indexInMap+1].Copy()
2022-03-09 03:38:02 +00:00
filteredContent = append(filteredContent, clonedKey, clonedValue)
}
}
newNode := original.CopyWithoutContent()
newNode.AddChildren(filteredContent)
2022-03-09 03:38:02 +00:00
return newNode
}
func pickSequence(original *CandidateNode, indices *CandidateNode) (*CandidateNode, error) {
2022-03-09 03:38:02 +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) {
filteredContent = append(filteredContent, original.Content[indexInArray].Copy())
2022-03-09 03:38:02 +00:00
}
}
newNode := original.CopyWithoutContent()
newNode.AddChildren(filteredContent)
2022-03-09 03:38:02 +00:00
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
}
indicesToPick := &CandidateNode{}
2022-03-09 03:38:02 +00:00
if contextIndicesToPick.MatchingNodes.Len() > 0 {
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() {
node := el.Value.(*CandidateNode)
2022-03-09 03:38:02 +00:00
var replacement *CandidateNode
if node.Kind == MappingNode {
2022-03-09 03:38:02 +00:00
replacement = pickMap(node, indicesToPick)
} 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 indices from type %v (%v)", node.Tag, node.GetNicePath())
2022-03-09 03:38:02 +00:00
}
replacement.LeadingContent = node.LeadingContent
results.PushBack(replacement)
2022-03-09 03:38:02 +00:00
}
return context.ChildContext(results), nil
}