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"
|
2021-05-13 23:43:52 +00:00
|
|
|
)
|
|
|
|
|
2024-01-11 02:17:34 +00:00
|
|
|
func unique(d *dataTreeNavigator, context Context, _ *ExpressionNode) (Context, error) {
|
2021-05-13 23:43:52 +00:00
|
|
|
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) {
|
|
|
|
|
2024-02-15 22:41:33 +00:00
|
|
|
log.Debugf("uniqueBy Operator")
|
2021-05-13 23:43:52 +00:00
|
|
|
var results = list.New()
|
|
|
|
|
|
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
|
|
|
candidate := el.Value.(*CandidateNode)
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
if candidate.Kind != SequenceNode {
|
2024-06-15 07:04:48 +00:00
|
|
|
return Context{}, fmt.Errorf("only arrays are supported for unique")
|
2021-05-13 23:43:52 +00:00
|
|
|
}
|
2021-05-14 05:01:44 +00:00
|
|
|
|
2021-05-13 23:43:52 +00:00
|
|
|
var newMatches = orderedmap.NewOrderedMap()
|
2023-10-18 01:11:53 +00:00
|
|
|
for _, child := range candidate.Content {
|
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
|
|
|
|
}
|
|
|
|
|
2024-06-15 07:04:48 +00:00
|
|
|
keyValue, err := getUniqueKeyValue(rhs)
|
|
|
|
if err != nil {
|
|
|
|
return Context{}, err
|
2021-05-16 04:00:30 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 23:43:52 +00:00
|
|
|
_, exists := newMatches.Get(keyValue)
|
|
|
|
|
|
|
|
if !exists {
|
2023-10-18 01:11:53 +00:00
|
|
|
newMatches.Set(keyValue, child)
|
2021-05-13 23:43:52 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-18 01:11:53 +00:00
|
|
|
resultNode := candidate.CreateReplacementWithComments(SequenceNode, "!!seq", candidate.Style)
|
2021-05-13 23:43:52 +00:00
|
|
|
for el := newMatches.Front(); el != nil; el = el.Next() {
|
2023-10-18 01:11:53 +00:00
|
|
|
resultNode.AddChild(el.Value.(*CandidateNode))
|
2021-05-13 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
results.PushBack(resultNode)
|
2021-05-13 23:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return context.ChildContext(results), nil
|
|
|
|
|
2021-05-14 05:01:44 +00:00
|
|
|
}
|
2024-06-15 07:04:48 +00:00
|
|
|
|
|
|
|
func getUniqueKeyValue(rhs Context) (string, error) {
|
|
|
|
keyValue := "null"
|
2024-06-15 07:06:17 +00:00
|
|
|
var err error
|
2024-06-15 07:04:48 +00:00
|
|
|
|
|
|
|
if rhs.MatchingNodes.Len() > 0 {
|
|
|
|
first := rhs.MatchingNodes.Front()
|
|
|
|
keyCandidate := first.Value.(*CandidateNode)
|
|
|
|
keyValue = keyCandidate.Value
|
|
|
|
if keyCandidate.Kind != ScalarNode {
|
|
|
|
keyValue, err = encodeToString(keyCandidate, encoderPreferences{YamlFormat, 0})
|
|
|
|
}
|
|
|
|
}
|
2024-06-15 07:06:17 +00:00
|
|
|
return keyValue, err
|
2024-06-15 07:04:48 +00:00
|
|
|
}
|