Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Jan 21, 2025
1 parent 8bf425b commit 194d2bd
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions pkg/yqlib/operator_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,55 @@ func sortByOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
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)
}
var sortableArray sortableNodeArray

if candidate.Kind == MappingNode {

sortableArray = make(sortableNodeArray, len(candidate.Content)/2)
log.Warningf("Sorting map: %v", NodeToString(candidate))
for i := 1; i < len(candidate.Content); i = i + 2 {

sortableArray := make(sortableNodeArray, len(candidate.Content))
originalNode := candidate.Content[i]
compareContext, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(originalNode), expressionNode.RHS)
if err != nil {
return Context{}, err
}

for i, originalNode := range candidate.Content {
sortableArray[i/2] = sortableNode{Node: originalNode, CompareContext: compareContext, dateTimeLayout: context.GetDateTimeLayout()}

compareContext, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(originalNode), expressionNode.RHS)
if err != nil {
return Context{}, err
}

sortableArray[i] = sortableNode{Node: originalNode, CompareContext: compareContext, dateTimeLayout: context.GetDateTimeLayout()}
} else if candidate.Kind == SequenceNode {
sortableArray = make(sortableNodeArray, len(candidate.Content))

for i, originalNode := range candidate.Content {

compareContext, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(originalNode), expressionNode.RHS)
if err != nil {
return Context{}, err
}

sortableArray[i] = sortableNode{Node: originalNode, CompareContext: compareContext, dateTimeLayout: context.GetDateTimeLayout()}

}
} else {
return context, fmt.Errorf("node at path [%v] is not an array or map (it's a %v)", candidate.GetNicePath(), candidate.Tag)
}

sort.Stable(sortableArray)

sortedList := candidate.CreateReplacementWithComments(SequenceNode, "!!seq", candidate.Style)

for _, sortedNode := range sortableArray {
sortedList.AddChild(sortedNode.Node)
sortedList := candidate.CopyWithoutContent()
if candidate.Kind == MappingNode {
for _, sortedNode := range sortableArray {
sortedList.AddKeyValueChild(sortedNode.Node.Key, sortedNode.Node)
}
} else if candidate.Kind == SequenceNode {
for _, sortedNode := range sortableArray {
sortedList.AddChild(sortedNode.Node)
}
}

// convert array of value nodes back to map
results.PushBack(sortedList)
}
return context.ChildContext(results), nil
Expand Down

0 comments on commit 194d2bd

Please sign in to comment.