2021-05-13 23:43:52 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
"fmt"
|
2021-05-14 05:01:44 +00:00
|
|
|
|
|
|
|
"github.com/elliotchance/orderedmap"
|
|
|
|
yaml "gopkg.in/yaml.v3"
|
2021-05-13 23:43:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func unique(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
|
|
|
selfExpression := &ExpressionNode{Operation: &Operation{OperationType: selfReferenceOpType}}
|
2022-02-07 00:55:55 +00:00
|
|
|
uniqueByExpression := &ExpressionNode{Operation: &Operation{OperationType: uniqueByOpType}, RHS: selfExpression}
|
2021-05-13 23:43:52 +00:00
|
|
|
return uniqueBy(d, context, uniqueByExpression)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func uniqueBy(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
|
|
|
|
|
|
|
log.Debugf("-- uniqueBy Operator")
|
|
|
|
var results = list.New()
|
|
|
|
|
|
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
|
|
|
candidate := el.Value.(*CandidateNode)
|
|
|
|
candidateNode := unwrapDoc(candidate.Node)
|
|
|
|
|
|
|
|
if candidateNode.Kind != yaml.SequenceNode {
|
|
|
|
return Context{}, fmt.Errorf("Only arrays are supported for unique")
|
|
|
|
}
|
2021-05-14 05:01:44 +00:00
|
|
|
|
2021-05-13 23:43:52 +00:00
|
|
|
var newMatches = orderedmap.NewOrderedMap()
|
|
|
|
for _, node := range candidateNode.Content {
|
|
|
|
child := &CandidateNode{Node: node}
|
2022-02-07 00:55:55 +00:00
|
|
|
rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(child), expressionNode.RHS)
|
2021-05-13 23:43:52 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return Context{}, err
|
|
|
|
}
|
|
|
|
|
2021-05-16 04:00:30 +00:00
|
|
|
keyValue := "null"
|
|
|
|
|
|
|
|
if rhs.MatchingNodes.Len() > 0 {
|
|
|
|
first := rhs.MatchingNodes.Front()
|
|
|
|
keyCandidate := first.Value.(*CandidateNode)
|
|
|
|
keyValue = keyCandidate.Node.Value
|
|
|
|
}
|
|
|
|
|
2021-05-13 23:43:52 +00:00
|
|
|
_, exists := newMatches.Get(keyValue)
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
newMatches.Set(keyValue, child.Node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resultNode := &yaml.Node{Kind: yaml.SequenceNode, Tag: "!!seq"}
|
|
|
|
for el := newMatches.Front(); el != nil; el = el.Next() {
|
|
|
|
resultNode.Content = append(resultNode.Content, el.Value.(*yaml.Node))
|
|
|
|
}
|
|
|
|
|
2022-05-31 06:28:53 +00:00
|
|
|
results.PushBack(candidate.CreateReplacementWithDocWrappers(resultNode))
|
2021-05-13 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return context.ChildContext(results), nil
|
|
|
|
|
2021-05-14 05:01:44 +00:00
|
|
|
}
|