2021-01-14 04:45:07 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2022-09-30 00:27:35 +00:00
|
|
|
func isKeyOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
|
|
|
log.Debugf("-- isKeyOperator")
|
|
|
|
|
|
|
|
var results = list.New()
|
|
|
|
|
|
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
|
|
|
candidate := el.Value.(*CandidateNode)
|
|
|
|
|
|
|
|
results.PushBack(createBooleanCandidate(candidate, candidate.IsMapKey))
|
|
|
|
}
|
|
|
|
|
|
|
|
return context.ChildContext(results), nil
|
|
|
|
}
|
|
|
|
|
2021-11-23 22:57:35 +00:00
|
|
|
func getKeyOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
|
|
|
log.Debugf("-- getKeyOperator")
|
|
|
|
|
|
|
|
var results = list.New()
|
|
|
|
|
|
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
|
|
|
candidate := el.Value.(*CandidateNode)
|
|
|
|
|
2021-11-23 23:16:48 +00:00
|
|
|
if candidate.Key != nil {
|
2023-05-02 04:07:59 +00:00
|
|
|
results.PushBack(candidate.Key)
|
2021-11-23 23:16:48 +00:00
|
|
|
}
|
2021-11-23 22:57:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return context.ChildContext(results), nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func keysOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
2021-01-14 04:45:07 +00:00
|
|
|
log.Debugf("-- keysOperator")
|
|
|
|
|
|
|
|
var results = list.New()
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
2023-06-07 17:45:42 +00:00
|
|
|
candidate := el.Value.(*CandidateNode)
|
2023-04-03 12:25:37 +00:00
|
|
|
|
|
|
|
var targetNode *CandidateNode
|
|
|
|
if candidate.Kind == MappingNode {
|
|
|
|
targetNode = getMapKeys(candidate)
|
|
|
|
} else if candidate.Kind == SequenceNode {
|
2023-10-05 05:01:24 +00:00
|
|
|
targetNode = getIndices(candidate)
|
2021-01-14 04:45:07 +00:00
|
|
|
} else {
|
2023-04-03 12:25:37 +00:00
|
|
|
return Context{}, fmt.Errorf("Cannot get keys of %v, keys only works for maps and arrays", candidate.Tag)
|
2021-01-14 04:45:07 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 12:25:37 +00:00
|
|
|
// result := candidate.CreateReplacement(targetNode)
|
|
|
|
results.PushBack(targetNode)
|
2021-01-14 04:45:07 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
return context.ChildContext(results), nil
|
2021-01-14 04:45:07 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 12:25:37 +00:00
|
|
|
func getMapKeys(node *CandidateNode) *CandidateNode {
|
|
|
|
contents := make([]*CandidateNode, 0)
|
2021-01-14 04:45:07 +00:00
|
|
|
for index := 0; index < len(node.Content); index = index + 2 {
|
|
|
|
contents = append(contents, node.Content[index])
|
|
|
|
}
|
2023-04-03 12:25:37 +00:00
|
|
|
return &CandidateNode{Kind: SequenceNode, Tag: "!!seq", Content: contents}
|
2021-01-14 04:45:07 +00:00
|
|
|
}
|
|
|
|
|
2023-10-05 05:01:24 +00:00
|
|
|
func getIndices(node *CandidateNode) *CandidateNode {
|
2023-04-03 12:25:37 +00:00
|
|
|
var contents = make([]*CandidateNode, len(node.Content))
|
2021-01-14 04:45:07 +00:00
|
|
|
|
|
|
|
for index := range node.Content {
|
2023-04-03 12:25:37 +00:00
|
|
|
contents[index] = &CandidateNode{
|
|
|
|
Kind: ScalarNode,
|
2021-01-14 04:45:07 +00:00
|
|
|
Tag: "!!int",
|
|
|
|
Value: fmt.Sprintf("%v", index),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 12:25:37 +00:00
|
|
|
return &CandidateNode{Kind: SequenceNode, Tag: "!!seq", Content: contents}
|
2021-01-14 04:45:07 +00:00
|
|
|
}
|