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, matchMap *list.List, pathNode *PathTreeNode) (*list.List, error) {
|
|
|
|
log.Debugf("-- collectObjectOperation")
|
2020-10-21 02:54:51 +00:00
|
|
|
|
2020-11-13 02:19:54 +00:00
|
|
|
if matchMap.Len() == 0 {
|
2020-11-22 02:16:54 +00:00
|
|
|
node := &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map", Value: "{}"}
|
|
|
|
candidate := &CandidateNode{Node: node}
|
|
|
|
return nodeToMap(candidate), nil
|
2020-11-13 02:19:54 +00:00
|
|
|
}
|
|
|
|
first := matchMap.Front().Value.(*CandidateNode)
|
|
|
|
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 := matchMap.Front(); el != nil; el = el.Next() {
|
|
|
|
candidateNode := el.Value.(*CandidateNode)
|
|
|
|
for i := 0; i < len(first.Node.Content); i++ {
|
|
|
|
rotated[i].PushBack(createChildCandidate(candidateNode, i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newObject := list.New()
|
|
|
|
for i := 0; i < len(first.Node.Content); i++ {
|
|
|
|
additions, err := collect(d, list.New(), rotated[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
newObject.PushBackList(additions)
|
|
|
|
}
|
|
|
|
|
|
|
|
return newObject, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func createChildCandidate(candidate *CandidateNode, index int) *CandidateNode {
|
|
|
|
return &CandidateNode{
|
|
|
|
Document: candidate.Document,
|
|
|
|
Path: append(candidate.Path, index),
|
|
|
|
Filename: candidate.Filename,
|
|
|
|
Node: candidate.Node.Content[index],
|
|
|
|
}
|
2020-10-21 02:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func collect(d *dataTreeNavigator, aggregate *list.List, remainingMatches *list.List) (*list.List, error) {
|
|
|
|
if remainingMatches.Len() == 0 {
|
|
|
|
return aggregate, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
candidate := remainingMatches.Remove(remainingMatches.Front()).(*CandidateNode)
|
|
|
|
splatted, err := Splat(d, nodeToMap(candidate))
|
2020-10-28 02:00:26 +00:00
|
|
|
|
|
|
|
for splatEl := splatted.Front(); splatEl != nil; splatEl = splatEl.Next() {
|
|
|
|
splatEl.Value.(*CandidateNode).Path = nil
|
|
|
|
}
|
|
|
|
|
2020-10-21 02:54:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if aggregate.Len() == 0 {
|
|
|
|
return collect(d, splatted, remainingMatches)
|
|
|
|
}
|
|
|
|
|
|
|
|
newAgg := list.New()
|
|
|
|
|
|
|
|
for el := aggregate.Front(); el != nil; el = el.Next() {
|
|
|
|
aggCandidate := el.Value.(*CandidateNode)
|
|
|
|
for splatEl := splatted.Front(); splatEl != nil; splatEl = splatEl.Next() {
|
|
|
|
splatCandidate := splatEl.Value.(*CandidateNode)
|
2020-11-13 03:07:11 +00:00
|
|
|
newCandidate, err := aggCandidate.Copy()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-21 02:54:51 +00:00
|
|
|
newCandidate.Path = nil
|
|
|
|
|
2020-11-27 23:41:09 +00:00
|
|
|
newCandidate, err = multiply(&MultiplyPreferences{AppendArrays: false})(d, newCandidate, splatCandidate)
|
2020-10-21 02:54:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
newAgg.PushBack(newCandidate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return collect(d, newAgg, remainingMatches)
|
|
|
|
|
2020-10-21 01:54:58 +00:00
|
|
|
}
|