yq/pkg/yqlib/operator_collect_object.go

102 lines
2.7 KiB
Go
Raw Normal View History

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"
2020-11-22 02:16:54 +00:00
yaml "gopkg.in/yaml.v3"
2020-10-21 02:54:51 +00:00
)
/*
[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
func collectObjectOperator(d *dataTreeNavigator, originalContext Context, expressionNode *ExpressionNode) (Context, error) {
2020-10-21 01:54:58 +00:00
log.Debugf("-- collectObjectOperation")
2020-10-21 02:54:51 +00:00
2021-09-12 06:55:55 +00:00
context := originalContext.WritableClone()
if context.MatchingNodes.Len() == 0 {
2020-11-22 02:16:54 +00:00
node := &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map", Value: "{}"}
candidate := &CandidateNode{Node: node}
return context.SingleChildContext(candidate), nil
2020-11-13 02:19:54 +00:00
}
first := context.MatchingNodes.Front().Value.(*CandidateNode)
2020-11-13 02:19:54 +00:00
var rotated []*list.List = make([]*list.List, len(first.Node.Content))
for i := 0; i < len(first.Node.Content); i++ {
rotated[i] = list.New()
}
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
2020-11-13 02:19:54 +00:00
candidateNode := el.Value.(*CandidateNode)
for i := 0; i < len(first.Node.Content); i++ {
rotated[i].PushBack(candidateNode.CreateChild(i, candidateNode.Node.Content[i]))
2020-11-13 02:19:54 +00:00
}
}
newObject := list.New()
for i := 0; i < len(first.Node.Content); i++ {
additions, err := collect(d, context.ChildContext(list.New()), rotated[i])
2020-11-13 02:19:54 +00:00
if err != nil {
return Context{}, err
2020-11-13 02:19:54 +00:00
}
newObject.PushBackList(additions.MatchingNodes)
2020-11-13 02:19:54 +00:00
}
return context.ChildContext(newObject), nil
2020-11-13 02:19:54 +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 {
return context, nil
2020-10-21 02:54:51 +00:00
}
candidate := remainingMatches.Remove(remainingMatches.Front()).(*CandidateNode)
2020-12-28 00:24:42 +00:00
splatted, err := splat(d, context.SingleChildContext(candidate),
2021-01-13 05:54:28 +00:00
traversePreferences{DontFollowAlias: true, IncludeMapKeys: false})
2020-10-28 02:00:26 +00:00
for splatEl := splatted.MatchingNodes.Front(); splatEl != nil; splatEl = splatEl.Next() {
2020-10-28 02:00:26 +00:00
splatEl.Value.(*CandidateNode).Path = nil
}
2020-10-21 02:54:51 +00:00
if err != nil {
return Context{}, err
2020-10-21 02:54:51 +00:00
}
if context.MatchingNodes.Len() == 0 {
2020-10-21 02:54:51 +00:00
return collect(d, splatted, remainingMatches)
}
newAgg := list.New()
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
2020-10-21 02:54:51 +00:00
aggCandidate := el.Value.(*CandidateNode)
for splatEl := splatted.MatchingNodes.Front(); splatEl != nil; splatEl = splatEl.Next() {
2020-10-21 02:54:51 +00:00
splatCandidate := splatEl.Value.(*CandidateNode)
2020-11-13 03:07:11 +00:00
newCandidate, err := aggCandidate.Copy()
if err != nil {
return Context{}, err
2020-11-13 03:07:11 +00:00
}
2020-10-21 02:54:51 +00:00
newCandidate.Path = nil
newCandidate, err = multiply(multiplyPreferences{AppendArrays: false})(d, context, newCandidate, splatCandidate)
2020-10-21 02:54:51 +00:00
if err != nil {
return Context{}, err
2020-10-21 02:54:51 +00:00
}
newAgg.PushBack(newCandidate)
}
}
return collect(d, context.ChildContext(newAgg), remainingMatches)
2020-10-21 02:54:51 +00:00
2020-10-21 01:54:58 +00:00
}