2020-11-03 23:48:43 +00:00
|
|
|
package yqlib
|
2020-10-21 01:54:58 +00:00
|
|
|
|
2020-10-21 02:54:51 +00:00
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
[Mike: cat, Bob: dog]
|
|
|
|
[Thing: rabbit, peter: sam]
|
|
|
|
|
|
|
|
==> cross multiply
|
|
|
|
|
|
|
|
{Mike: cat, Thing: rabbit}
|
|
|
|
{Mike: cat, peter: sam}
|
|
|
|
...
|
|
|
|
*/
|
2020-10-21 01:54:58 +00:00
|
|
|
|
2024-01-11 02:17:34 +00:00
|
|
|
func collectObjectOperator(d *dataTreeNavigator, originalContext Context, _ *ExpressionNode) (Context, error) {
|
2024-02-15 22:41:33 +00:00
|
|
|
log.Debugf("collectObjectOperation")
|
2020-10-21 02:54:51 +00:00
|
|
|
|
2021-09-12 06:55:55 +00:00
|
|
|
context := originalContext.WritableClone()
|
2021-05-21 22:22:45 +00:00
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
if context.MatchingNodes.Len() == 0 {
|
2023-10-18 01:11:53 +00:00
|
|
|
candidate := &CandidateNode{Kind: MappingNode, Tag: "!!map", Value: "{}"}
|
2024-02-15 22:41:33 +00:00
|
|
|
log.Debugf("collectObjectOperation - starting with empty map")
|
2021-02-02 07:17:59 +00:00
|
|
|
return context.SingleChildContext(candidate), nil
|
2020-11-13 02:19:54 +00:00
|
|
|
}
|
2021-02-02 07:17:59 +00:00
|
|
|
first := context.MatchingNodes.Front().Value.(*CandidateNode)
|
2023-10-18 01:11:53 +00:00
|
|
|
var rotated = make([]*list.List, len(first.Content))
|
2020-11-13 02:19:54 +00:00
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
for i := 0; i < len(first.Content); i++ {
|
2020-11-13 02:19:54 +00:00
|
|
|
rotated[i] = list.New()
|
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
2020-11-13 02:19:54 +00:00
|
|
|
candidateNode := el.Value.(*CandidateNode)
|
2023-10-18 01:11:53 +00:00
|
|
|
|
|
|
|
for i := 0; i < len(first.Content); i++ {
|
|
|
|
log.Debugf("rotate[%v] = %v", i, NodeToString(candidateNode.Content[i]))
|
|
|
|
log.Debugf("children:\n%v", NodeContentToString(candidateNode.Content[i], 0))
|
|
|
|
rotated[i].PushBack(candidateNode.Content[i])
|
2020-11-13 02:19:54 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-15 22:41:33 +00:00
|
|
|
log.Debugf("collectObjectOperation, length of rotated is %v", len(rotated))
|
2020-11-13 02:19:54 +00:00
|
|
|
|
|
|
|
newObject := list.New()
|
2023-10-18 01:11:53 +00:00
|
|
|
for i := 0; i < len(first.Content); i++ {
|
2021-02-02 07:17:59 +00:00
|
|
|
additions, err := collect(d, context.ChildContext(list.New()), rotated[i])
|
2020-11-13 02:19:54 +00:00
|
|
|
if err != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, err
|
2020-11-13 02:19:54 +00:00
|
|
|
}
|
2023-10-18 01:11:53 +00:00
|
|
|
// we should reset the parents and keys of these top level nodes,
|
|
|
|
// as they are new
|
|
|
|
for el := additions.MatchingNodes.Front(); el != nil; el = el.Next() {
|
|
|
|
addition := el.Value.(*CandidateNode)
|
|
|
|
additionCopy := addition.Copy()
|
|
|
|
|
|
|
|
additionCopy.SetParent(nil)
|
|
|
|
additionCopy.Key = nil
|
|
|
|
|
2024-02-15 22:41:33 +00:00
|
|
|
log.Debugf("collectObjectOperation, adding result %v", NodeToString(additionCopy))
|
2023-10-18 01:11:53 +00:00
|
|
|
|
|
|
|
newObject.PushBack(additionCopy)
|
|
|
|
}
|
2020-11-13 02:19:54 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
return context.ChildContext(newObject), nil
|
2020-11-13 02:19:54 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func collect(d *dataTreeNavigator, context Context, remainingMatches *list.List) (Context, error) {
|
2020-10-21 02:54:51 +00:00
|
|
|
if remainingMatches.Len() == 0 {
|
2021-02-02 07:17:59 +00:00
|
|
|
return context, nil
|
2020-10-21 02:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
candidate := remainingMatches.Remove(remainingMatches.Front()).(*CandidateNode)
|
2024-02-15 22:41:33 +00:00
|
|
|
log.Debugf("collectObjectOperation - collect %v", NodeToString(candidate))
|
2020-12-28 00:24:42 +00:00
|
|
|
|
2021-12-20 22:30:08 +00:00
|
|
|
splatted, err := splat(context.SingleChildContext(candidate),
|
2021-01-13 05:54:28 +00:00
|
|
|
traversePreferences{DontFollowAlias: true, IncludeMapKeys: false})
|
2020-10-28 02:00:26 +00:00
|
|
|
|
2020-10-21 02:54:51 +00:00
|
|
|
if err != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, err
|
2020-10-21 02:54:51 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
if context.MatchingNodes.Len() == 0 {
|
2024-02-15 22:41:33 +00:00
|
|
|
log.Debugf("collectObjectOperation - collect context is empty, next")
|
2020-10-21 02:54:51 +00:00
|
|
|
return collect(d, splatted, remainingMatches)
|
|
|
|
}
|
|
|
|
|
|
|
|
newAgg := list.New()
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
2020-10-21 02:54:51 +00:00
|
|
|
aggCandidate := el.Value.(*CandidateNode)
|
2021-02-02 07:17:59 +00:00
|
|
|
for splatEl := splatted.MatchingNodes.Front(); splatEl != nil; splatEl = splatEl.Next() {
|
2020-10-21 02:54:51 +00:00
|
|
|
splatCandidate := splatEl.Value.(*CandidateNode)
|
2024-02-15 22:41:33 +00:00
|
|
|
log.Debugf("collectObjectOperation; splatCandidate: %v", NodeToString(splatCandidate))
|
2023-10-18 01:11:53 +00:00
|
|
|
newCandidate := aggCandidate.Copy()
|
2024-02-15 22:41:33 +00:00
|
|
|
log.Debugf("collectObjectOperation; aggCandidate: %v", NodeToString(aggCandidate))
|
2020-10-21 02:54:51 +00:00
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
newCandidate, err = multiply(multiplyPreferences{AppendArrays: false})(d, context, newCandidate, splatCandidate)
|
2023-10-18 01:11:53 +00:00
|
|
|
|
2020-10-21 02:54:51 +00:00
|
|
|
if err != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, err
|
2020-10-21 02:54:51 +00:00
|
|
|
}
|
|
|
|
newAgg.PushBack(newCandidate)
|
|
|
|
}
|
|
|
|
}
|
2021-02-02 07:17:59 +00:00
|
|
|
return collect(d, context.ChildContext(newAgg), remainingMatches)
|
2020-10-21 02:54:51 +00:00
|
|
|
|
2020-10-21 01:54:58 +00:00
|
|
|
}
|