yq/pkg/yqlib/operator_sort.go

178 lines
4.8 KiB
Go
Raw Normal View History

2021-11-28 02:25:22 +00:00
package yqlib
import (
"container/list"
"fmt"
"sort"
"strconv"
"strings"
2022-11-04 01:21:12 +00:00
"time"
2021-11-28 02:25:22 +00:00
)
2021-12-04 02:54:12 +00:00
func sortOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
selfExpression := &ExpressionNode{Operation: &Operation{OperationType: selfReferenceOpType}}
expressionNode.RHS = selfExpression
2021-12-04 02:54:12 +00:00
return sortByOperator(d, context, expressionNode)
}
2021-11-28 02:25:22 +00:00
// context represents the current matching nodes in the expression pipeline
// expressionNode is your current expression (sort_by)
2021-11-28 02:25:22 +00:00
func sortByOperator(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)
if candidate.Kind != SequenceNode {
return context, fmt.Errorf("node at path [%v] is not an array (it's a %v)", candidate.GetNicePath(), candidate.Tag)
2021-11-28 02:25:22 +00:00
}
sortableArray := make(sortableNodeArray, len(candidate.Content))
2021-11-28 02:25:22 +00:00
for i, originalNode := range candidate.Content {
2021-11-28 02:25:22 +00:00
compareContext, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(originalNode), expressionNode.RHS)
2021-11-28 02:25:22 +00:00
if err != nil {
return Context{}, err
}
sortableArray[i] = sortableNode{Node: originalNode, CompareContext: compareContext, dateTimeLayout: context.GetDateTimeLayout()}
2021-12-04 02:54:12 +00:00
2021-11-28 02:25:22 +00:00
}
2021-12-04 02:54:12 +00:00
sort.Stable(sortableArray)
2021-11-28 02:25:22 +00:00
sortedList := candidate.CreateReplacementWithComments(SequenceNode, "!!seq", candidate.Style)
2021-11-28 02:25:22 +00:00
for _, sortedNode := range sortableArray {
sortedList.AddChild(sortedNode.Node)
2021-11-28 02:25:22 +00:00
}
results.PushBack(sortedList)
2021-11-28 02:25:22 +00:00
}
return context.ChildContext(results), nil
}
type sortableNode struct {
Node *CandidateNode
CompareContext Context
2022-11-04 01:21:12 +00:00
dateTimeLayout string
2021-11-28 02:25:22 +00:00
}
type sortableNodeArray []sortableNode
func (a sortableNodeArray) Len() int { return len(a) }
func (a sortableNodeArray) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a sortableNodeArray) Less(i, j int) bool {
lhsContext := a[i].CompareContext
rhsContext := a[j].CompareContext
rhsEl := rhsContext.MatchingNodes.Front()
for lhsEl := lhsContext.MatchingNodes.Front(); lhsEl != nil && rhsEl != nil; lhsEl = lhsEl.Next() {
lhs := lhsEl.Value.(*CandidateNode)
rhs := rhsEl.Value.(*CandidateNode)
result := a.compare(lhs, rhs, a[i].dateTimeLayout)
if result < 0 {
return true
} else if result > 0 {
return false
}
2021-11-28 02:25:22 +00:00
rhsEl = rhsEl.Next()
}
return false
}
func (a sortableNodeArray) compare(lhs *CandidateNode, rhs *CandidateNode, dateTimeLayout string) int {
2022-11-04 01:21:12 +00:00
lhsTag := lhs.Tag
rhsTag := rhs.Tag
if !strings.HasPrefix(lhsTag, "!!") {
// custom tag - we have to have a guess
lhsTag = lhs.guessTagFromCustomType()
2022-11-04 01:21:12 +00:00
}
if !strings.HasPrefix(rhsTag, "!!") {
// custom tag - we have to have a guess
rhsTag = rhs.guessTagFromCustomType()
2022-11-04 01:21:12 +00:00
}
isDateTime := lhsTag == "!!timestamp" && rhsTag == "!!timestamp"
layout := dateTimeLayout
2022-11-04 01:21:12 +00:00
// if the lhs is a string, it might be a timestamp in a custom format.
if lhsTag == "!!str" && layout != time.RFC3339 {
_, errLhs := parseDateTime(layout, lhs.Value)
_, errRhs := parseDateTime(layout, rhs.Value)
isDateTime = errLhs == nil && errRhs == nil
}
if lhsTag == "!!null" && rhsTag != "!!null" {
return -1
2022-11-04 01:21:12 +00:00
} else if lhsTag != "!!null" && rhsTag == "!!null" {
return 1
2022-11-04 01:21:12 +00:00
} else if lhsTag == "!!bool" && rhsTag != "!!bool" {
return -1
2022-11-04 01:21:12 +00:00
} else if lhsTag != "!!bool" && rhsTag == "!!bool" {
return 1
2022-11-04 01:21:12 +00:00
} else if lhsTag == "!!bool" && rhsTag == "!!bool" {
lhsTruthy := isTruthyNode(lhs)
2021-12-04 02:54:12 +00:00
rhsTruthy := isTruthyNode(rhs)
if lhsTruthy == rhsTruthy {
return 0
} else if lhsTruthy {
return 1
}
return -1
2022-11-04 01:21:12 +00:00
} else if isDateTime {
lhsTime, err := parseDateTime(layout, lhs.Value)
if err != nil {
log.Warningf("Could not parse time %v with layout %v for sort, sorting by string instead: %w", lhs.Value, layout, err)
return strings.Compare(lhs.Value, rhs.Value)
2022-11-04 01:21:12 +00:00
}
rhsTime, err := parseDateTime(layout, rhs.Value)
if err != nil {
log.Warningf("Could not parse time %v with layout %v for sort, sorting by string instead: %w", rhs.Value, layout, err)
return strings.Compare(lhs.Value, rhs.Value)
2022-11-04 01:21:12 +00:00
}
if lhsTime.Equal(rhsTime) {
return 0
} else if lhsTime.Before(rhsTime) {
return -1
}
return 1
2022-11-04 01:21:12 +00:00
} else if lhsTag == "!!int" && rhsTag == "!!int" {
2022-05-06 03:46:14 +00:00
_, lhsNum, err := parseInt64(lhs.Value)
2021-11-28 02:25:22 +00:00
if err != nil {
panic(err)
}
2022-05-06 03:46:14 +00:00
_, rhsNum, err := parseInt64(rhs.Value)
2021-11-28 02:25:22 +00:00
if err != nil {
panic(err)
}
return int(lhsNum - rhsNum)
2022-11-04 01:21:12 +00:00
} else if (lhsTag == "!!int" || lhsTag == "!!float") && (rhsTag == "!!int" || rhsTag == "!!float") {
2021-11-28 02:25:22 +00:00
lhsNum, err := strconv.ParseFloat(lhs.Value, 64)
if err != nil {
panic(err)
}
rhsNum, err := strconv.ParseFloat(rhs.Value, 64)
if err != nil {
panic(err)
}
if lhsNum == rhsNum {
return 0
} else if lhsNum < rhsNum {
return -1
}
return 1
2021-11-28 02:25:22 +00:00
}
return strings.Compare(lhs.Value, rhs.Value)
2021-11-28 02:25:22 +00:00
}