yq/pkg/yqlib/operator_keys.go

83 lines
2.1 KiB
Go
Raw Normal View History

2021-01-14 04:45:07 +00:00
package yqlib
import (
"container/list"
"fmt"
)
2024-01-11 02:17:34 +00:00
func isKeyOperator(_ *dataTreeNavigator, context Context, _ *ExpressionNode) (Context, error) {
2024-02-15 22:41:33 +00:00
log.Debugf("isKeyOperator")
2022-09-30 00:27:35 +00:00
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
}
2024-01-11 02:17:34 +00:00
func getKeyOperator(_ *dataTreeNavigator, context Context, _ *ExpressionNode) (Context, error) {
2024-02-15 22:41:33 +00:00
log.Debugf("getKeyOperator")
2021-11-23 22:57:35 +00:00
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 {
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
}
2024-01-11 02:17:34 +00:00
func keysOperator(_ *dataTreeNavigator, context Context, _ *ExpressionNode) (Context, error) {
2024-02-15 22:41:33 +00:00
log.Debugf("keysOperator")
2021-01-14 04:45:07 +00:00
var results = list.New()
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
2021-01-14 04:45:07 +00:00
candidate := el.Value.(*CandidateNode)
var targetNode *CandidateNode
if candidate.Kind == MappingNode {
targetNode = getMapKeys(candidate)
} else if candidate.Kind == SequenceNode {
targetNode = getIndices(candidate)
2021-01-14 04:45:07 +00:00
} else {
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
}
results.PushBack(targetNode)
2021-01-14 04:45:07 +00:00
}
return context.ChildContext(results), nil
2021-01-14 04:45:07 +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])
}
return &CandidateNode{Kind: SequenceNode, Tag: "!!seq", Content: contents}
2021-01-14 04:45:07 +00:00
}
func getIndices(node *CandidateNode) *CandidateNode {
var contents = make([]*CandidateNode, len(node.Content))
2021-01-14 04:45:07 +00:00
for index := range node.Content {
contents[index] = &CandidateNode{
Kind: ScalarNode,
2021-01-14 04:45:07 +00:00
Tag: "!!int",
Value: fmt.Sprintf("%v", index),
}
}
return &CandidateNode{Kind: SequenceNode, Tag: "!!seq", Content: contents}
2021-01-14 04:45:07 +00:00
}