yq/pkg/yqlib/operator_reverse.go

34 lines
943 B
Go
Raw Normal View History

2022-02-22 03:15:31 +00:00
package yqlib
import (
"container/list"
"fmt"
)
func reverseOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
results := list.New()
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
2023-04-09 01:14:51 +00:00
candidateNode := candidate.unwrapDocument()
2022-02-22 03:15:31 +00:00
2023-04-09 01:14:51 +00:00
if candidateNode.Kind != SequenceNode {
2022-02-22 03:15:31 +00:00
return context, fmt.Errorf("node at path [%v] is not an array (it's a %v)", candidate.GetNicePath(), candidate.GetNiceTag())
}
2023-06-06 19:10:28 +00:00
reverseList := candidate.CreateReplacementWithComments(SequenceNode, "!!seq", candidateNode.Style)
2023-05-09 03:51:21 +00:00
reverseContent := make([]*CandidateNode, len(candidateNode.Content))
2022-02-22 03:15:31 +00:00
for i, originalNode := range candidateNode.Content {
2023-05-09 03:51:21 +00:00
reverseContent[len(candidateNode.Content)-i-1] = originalNode
2022-02-22 03:15:31 +00:00
}
2023-05-09 03:51:21 +00:00
reverseList.AddChildren(reverseContent)
2023-04-09 01:14:51 +00:00
results.PushBack(reverseList)
2022-02-22 03:15:31 +00:00
}
return context.ChildContext(results), nil
}