2021-05-09 03:59:23 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func entrySeqFor(key *CandidateNode, value *CandidateNode) *CandidateNode {
|
|
|
|
var keyKey = &CandidateNode{Kind: ScalarNode, Tag: "!!str", Value: "key"}
|
|
|
|
var valueKey = &CandidateNode{Kind: ScalarNode, Tag: "!!str", Value: "value"}
|
2021-05-09 03:59:23 +00:00
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
return &CandidateNode{
|
|
|
|
Kind: MappingNode,
|
2021-05-09 05:12:50 +00:00
|
|
|
Tag: "!!map",
|
2023-10-18 01:11:53 +00:00
|
|
|
Content: []*CandidateNode{keyKey, key, valueKey, value},
|
2021-05-09 03:59:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toEntriesFromMap(candidateNode *CandidateNode) *CandidateNode {
|
2023-10-18 01:11:53 +00:00
|
|
|
var sequence = candidateNode.CreateReplacementWithComments(SequenceNode, "!!seq", 0)
|
2021-05-09 03:59:23 +00:00
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
var contents = candidateNode.Content
|
2021-05-09 03:59:23 +00:00
|
|
|
for index := 0; index < len(contents); index = index + 2 {
|
|
|
|
key := contents[index]
|
|
|
|
value := contents[index+1]
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
sequence.AddChild(entrySeqFor(key, value))
|
2021-05-09 03:59:23 +00:00
|
|
|
}
|
2023-10-18 01:11:53 +00:00
|
|
|
return sequence
|
2021-05-09 03:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func toEntriesfromSeq(candidateNode *CandidateNode) *CandidateNode {
|
2023-10-18 01:11:53 +00:00
|
|
|
var sequence = candidateNode.CreateReplacementWithComments(SequenceNode, "!!seq", 0)
|
2021-05-09 03:59:23 +00:00
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
var contents = candidateNode.Content
|
2021-05-09 03:59:23 +00:00
|
|
|
for index := 0; index < len(contents); index = index + 1 {
|
2023-10-18 01:11:53 +00:00
|
|
|
key := &CandidateNode{Kind: ScalarNode, Tag: "!!int", Value: fmt.Sprintf("%v", index)}
|
2021-05-09 03:59:23 +00:00
|
|
|
value := contents[index]
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
sequence.AddChild(entrySeqFor(key, value))
|
2021-05-09 03:59:23 +00:00
|
|
|
}
|
2023-10-18 01:11:53 +00:00
|
|
|
return sequence
|
2021-05-09 03:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
switch candidate.Kind {
|
|
|
|
case MappingNode:
|
2021-05-09 03:59:23 +00:00
|
|
|
results.PushBack(toEntriesFromMap(candidate))
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
case SequenceNode:
|
2021-05-09 03:59:23 +00:00
|
|
|
results.PushBack(toEntriesfromSeq(candidate))
|
|
|
|
default:
|
2023-10-18 01:11:53 +00:00
|
|
|
if candidate.Tag != "!!null" {
|
|
|
|
return Context{}, fmt.Errorf("%v has no keys", candidate.Tag)
|
2021-05-10 00:42:43 +00:00
|
|
|
}
|
2021-05-09 03:59:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return context.ChildContext(results), nil
|
2021-05-09 04:18:25 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func parseEntry(candidateNode *CandidateNode, position int) (*CandidateNode, *CandidateNode, error) {
|
2021-05-09 04:18:25 +00:00
|
|
|
prefs := traversePreferences{DontAutoCreate: true}
|
|
|
|
|
2022-06-23 09:22:11 +00:00
|
|
|
keyResults, err := traverseMap(Context{}, candidateNode, createStringScalarNode("key"), prefs, false)
|
2021-05-09 04:18:25 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
} else if keyResults.Len() != 1 {
|
2022-06-23 09:22:11 +00:00
|
|
|
return nil, nil, fmt.Errorf("expected to find one 'key' entry but found %v in position %v", keyResults.Len(), position)
|
2021-05-09 04:18:25 +00:00
|
|
|
}
|
|
|
|
|
2022-06-23 09:22:11 +00:00
|
|
|
valueResults, err := traverseMap(Context{}, candidateNode, createStringScalarNode("value"), prefs, false)
|
2021-05-09 04:18:25 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
} else if valueResults.Len() != 1 {
|
2022-06-23 09:22:11 +00:00
|
|
|
return nil, nil, fmt.Errorf("expected to find one 'value' entry but found %v in position %v", valueResults.Len(), position)
|
2021-05-09 04:18:25 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
return keyResults.Front().Value.(*CandidateNode), valueResults.Front().Value.(*CandidateNode), nil
|
2021-05-09 04:18:25 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-12-20 22:30:08 +00:00
|
|
|
func fromEntries(candidateNode *CandidateNode) (*CandidateNode, error) {
|
2023-10-18 01:11:53 +00:00
|
|
|
var node = candidateNode.CopyWithoutContent()
|
2021-05-09 04:18:25 +00:00
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
var contents = candidateNode.Content
|
2021-05-09 04:18:25 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
node.AddKeyValueChild(key, value)
|
2021-05-09 04:18:25 +00:00
|
|
|
}
|
2023-10-18 01:11:53 +00:00
|
|
|
node.Kind = MappingNode
|
|
|
|
node.Tag = "!!map"
|
|
|
|
return node, nil
|
2021-05-09 04:18:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
switch candidate.Kind {
|
|
|
|
case 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() {
|
2022-05-31 06:28:53 +00:00
|
|
|
candidate := el.Value.(*CandidateNode)
|
|
|
|
|
2021-11-30 02:19:30 +00:00
|
|
|
//run expression against entries
|
|
|
|
// splat toEntries and pipe it into Rhs
|
2022-05-31 06:28:53 +00:00
|
|
|
splatted, err := splat(context.SingleChildContext(candidate), 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
|
|
|
|
}
|
2023-10-18 01:11:53 +00:00
|
|
|
log.Debug("candidate %v", NodeToString(candidate))
|
|
|
|
log.Debug("candidate leading content: %v", candidate.LeadingContent)
|
2022-05-31 06:28:53 +00:00
|
|
|
collected.LeadingContent = candidate.LeadingContent
|
2023-10-18 01:11:53 +00:00
|
|
|
log.Debug("candidate FootComment: [%v]", candidate.FootComment)
|
|
|
|
|
|
|
|
collected.HeadComment = candidate.HeadComment
|
|
|
|
collected.FootComment = candidate.FootComment
|
2022-05-31 06:28:53 +00:00
|
|
|
|
|
|
|
log.Debugf("**** collected %v", collected.LeadingContent)
|
2021-11-30 02:19:30 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|