2021-05-09 03:59:23 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
"fmt"
|
2021-05-09 05:12:50 +00:00
|
|
|
|
2021-05-09 03:59:23 +00:00
|
|
|
yaml "gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func entrySeqFor(key *yaml.Node, value *yaml.Node) *yaml.Node {
|
2021-05-09 05:12:50 +00:00
|
|
|
var keyKey = &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: "key"}
|
|
|
|
var valueKey = &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: "value"}
|
2021-05-09 03:59:23 +00:00
|
|
|
|
|
|
|
return &yaml.Node{
|
2021-05-09 05:12:50 +00:00
|
|
|
Kind: yaml.MappingNode,
|
|
|
|
Tag: "!!map",
|
2021-05-09 03:59:23 +00:00
|
|
|
Content: []*yaml.Node{keyKey, key, valueKey, value},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toEntriesFromMap(candidateNode *CandidateNode) *CandidateNode {
|
2021-05-09 05:12:50 +00:00
|
|
|
var sequence = &yaml.Node{Kind: yaml.SequenceNode, Tag: "!!seq"}
|
2021-11-23 22:57:35 +00:00
|
|
|
var entriesNode = candidateNode.CreateReplacement(sequence)
|
2021-05-09 03:59:23 +00:00
|
|
|
|
|
|
|
var contents = unwrapDoc(candidateNode.Node).Content
|
|
|
|
for index := 0; index < len(contents); index = index + 2 {
|
|
|
|
key := contents[index]
|
|
|
|
value := contents[index+1]
|
|
|
|
|
|
|
|
sequence.Content = append(sequence.Content, entrySeqFor(key, value))
|
|
|
|
}
|
|
|
|
return entriesNode
|
|
|
|
}
|
|
|
|
|
|
|
|
func toEntriesfromSeq(candidateNode *CandidateNode) *CandidateNode {
|
2021-05-09 05:12:50 +00:00
|
|
|
var sequence = &yaml.Node{Kind: yaml.SequenceNode, Tag: "!!seq"}
|
2021-11-23 22:57:35 +00:00
|
|
|
var entriesNode = candidateNode.CreateReplacement(sequence)
|
2021-05-09 03:59:23 +00:00
|
|
|
|
|
|
|
var contents = unwrapDoc(candidateNode.Node).Content
|
|
|
|
for index := 0; index < len(contents); index = index + 1 {
|
|
|
|
key := &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!int", Value: fmt.Sprintf("%v", index)}
|
|
|
|
value := contents[index]
|
|
|
|
|
|
|
|
sequence.Content = append(sequence.Content, entrySeqFor(key, value))
|
|
|
|
}
|
|
|
|
return entriesNode
|
|
|
|
}
|
|
|
|
|
|
|
|
func toEntriesOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
|
|
|
var results = list.New()
|
|
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
|
|
|
candidate := el.Value.(*CandidateNode)
|
|
|
|
candidateNode := unwrapDoc(candidate.Node)
|
|
|
|
|
|
|
|
switch candidateNode.Kind {
|
|
|
|
case yaml.MappingNode:
|
|
|
|
results.PushBack(toEntriesFromMap(candidate))
|
|
|
|
|
|
|
|
case yaml.SequenceNode:
|
|
|
|
results.PushBack(toEntriesfromSeq(candidate))
|
|
|
|
default:
|
2021-05-10 00:42:43 +00:00
|
|
|
if candidateNode.Tag != "!!null" {
|
|
|
|
return Context{}, fmt.Errorf("%v has no keys", candidate.Node.Tag)
|
|
|
|
}
|
2021-05-09 03:59:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return context.ChildContext(results), nil
|
2021-05-09 04:18:25 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 22:30:08 +00:00
|
|
|
func parseEntry(entry *yaml.Node, position int) (*yaml.Node, *yaml.Node, error) {
|
2021-05-09 04:18:25 +00:00
|
|
|
prefs := traversePreferences{DontAutoCreate: true}
|
|
|
|
candidateNode := &CandidateNode{Node: entry}
|
|
|
|
|
|
|
|
keyResults, err := traverseMap(Context{}, candidateNode, "key", prefs, false)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
} else if keyResults.Len() != 1 {
|
|
|
|
return nil, nil, fmt.Errorf("Expected to find one 'key' entry but found %v in position %v", keyResults.Len(), position)
|
|
|
|
}
|
|
|
|
|
|
|
|
valueResults, err := traverseMap(Context{}, candidateNode, "value", prefs, false)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
} else if valueResults.Len() != 1 {
|
|
|
|
return nil, nil, fmt.Errorf("Expected to find one 'value' entry but found %v in position %v", valueResults.Len(), position)
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyResults.Front().Value.(*CandidateNode).Node, valueResults.Front().Value.(*CandidateNode).Node, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-12-20 22:30:08 +00:00
|
|
|
func fromEntries(candidateNode *CandidateNode) (*CandidateNode, error) {
|
2021-05-09 05:12:50 +00:00
|
|
|
var node = &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map"}
|
2021-11-23 22:57:35 +00:00
|
|
|
var mapCandidateNode = candidateNode.CreateReplacement(node)
|
2021-05-09 04:18:25 +00:00
|
|
|
|
|
|
|
var contents = unwrapDoc(candidateNode.Node).Content
|
|
|
|
|
|
|
|
for index := 0; index < len(contents); index = index + 1 {
|
2021-12-20 22:30:08 +00:00
|
|
|
key, value, err := parseEntry(contents[index], index)
|
2021-05-09 04:18:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
node.Content = append(node.Content, key, value)
|
|
|
|
}
|
|
|
|
return mapCandidateNode, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fromEntriesOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
|
|
|
var results = list.New()
|
|
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
|
|
|
candidate := el.Value.(*CandidateNode)
|
|
|
|
candidateNode := unwrapDoc(candidate.Node)
|
|
|
|
|
|
|
|
switch candidateNode.Kind {
|
|
|
|
case yaml.SequenceNode:
|
2021-12-20 22:30:08 +00:00
|
|
|
mapResult, err := fromEntries(candidate)
|
2021-05-09 04:18:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return Context{}, err
|
|
|
|
}
|
|
|
|
results.PushBack(mapResult)
|
|
|
|
default:
|
|
|
|
return Context{}, fmt.Errorf("from entries only runs against arrays")
|
|
|
|
}
|
|
|
|
}
|
2021-05-09 03:59:23 +00:00
|
|
|
|
2021-05-09 04:18:25 +00:00
|
|
|
return context.ChildContext(results), nil
|
2021-05-09 05:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func withEntriesOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
|
|
|
|
|
|
|
//to_entries on the context
|
|
|
|
toEntries, err := toEntriesOperator(d, context, expressionNode)
|
|
|
|
if err != nil {
|
2021-05-10 00:42:43 +00:00
|
|
|
return Context{}, err
|
2021-05-09 05:12:50 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 02:19:30 +00:00
|
|
|
var results = list.New()
|
2021-05-09 05:12:50 +00:00
|
|
|
|
2021-11-30 02:19:30 +00:00
|
|
|
for el := toEntries.MatchingNodes.Front(); el != nil; el = el.Next() {
|
|
|
|
//run expression against entries
|
|
|
|
// splat toEntries and pipe it into Rhs
|
2021-12-20 22:30:08 +00:00
|
|
|
splatted, err := splat(context.SingleChildContext(el.Value.(*CandidateNode)), traversePreferences{})
|
2021-11-30 02:19:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return Context{}, err
|
|
|
|
}
|
|
|
|
|
2022-02-07 00:55:55 +00:00
|
|
|
result, err := d.GetMatchingNodes(splatted, expressionNode.RHS)
|
|
|
|
log.Debug("expressionNode.Rhs %v", expressionNode.RHS.Operation.OperationType)
|
2021-11-30 02:19:30 +00:00
|
|
|
log.Debug("result %v", result)
|
|
|
|
if err != nil {
|
|
|
|
return Context{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
selfExpression := &ExpressionNode{Operation: &Operation{OperationType: selfReferenceOpType}}
|
|
|
|
collected, err := collectTogether(d, result, selfExpression)
|
|
|
|
if err != nil {
|
|
|
|
return Context{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fromEntries, err := fromEntriesOperator(d, context.SingleChildContext(collected), expressionNode)
|
|
|
|
if err != nil {
|
|
|
|
return Context{}, err
|
|
|
|
}
|
|
|
|
results.PushBackList(fromEntries.MatchingNodes)
|
2021-05-09 05:12:50 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//from_entries on the result
|
2021-11-30 02:19:30 +00:00
|
|
|
return context.ChildContext(results), nil
|
2021-05-09 05:12:50 +00:00
|
|
|
}
|