yq/pkg/yqlib/operator_entries.go

178 lines
5.3 KiB
Go
Raw Normal View History

2021-05-09 03:59:23 +00:00
package yqlib
import (
"container/list"
"fmt"
)
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"}
2023-11-30 03:04:54 +00:00
candidate := &CandidateNode{Kind: MappingNode, Tag: "!!map"}
candidate.AddKeyValueChild(keyKey, key)
candidate.AddKeyValueChild(valueKey, value)
return candidate
2021-05-09 03:59:23 +00:00
}
func toEntriesFromMap(candidateNode *CandidateNode) *CandidateNode {
var sequence = candidateNode.CreateReplacementWithComments(SequenceNode, "!!seq", 0)
2021-05-09 03:59:23 +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]
sequence.AddChild(entrySeqFor(key, value))
2021-05-09 03:59:23 +00:00
}
return sequence
2021-05-09 03:59:23 +00:00
}
func toEntriesfromSeq(candidateNode *CandidateNode) *CandidateNode {
var sequence = candidateNode.CreateReplacementWithComments(SequenceNode, "!!seq", 0)
2021-05-09 03:59:23 +00:00
var contents = candidateNode.Content
2021-05-09 03:59:23 +00:00
for index := 0; index < len(contents); index = index + 1 {
key := &CandidateNode{Kind: ScalarNode, Tag: "!!int", Value: fmt.Sprintf("%v", index)}
2021-05-09 03:59:23 +00:00
value := contents[index]
sequence.AddChild(entrySeqFor(key, value))
2021-05-09 03:59:23 +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)
switch candidate.Kind {
case MappingNode:
2021-05-09 03:59:23 +00:00
results.PushBack(toEntriesFromMap(candidate))
case SequenceNode:
2021-05-09 03:59:23 +00:00
results.PushBack(toEntriesfromSeq(candidate))
default:
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
}
func parseEntry(candidateNode *CandidateNode, position int) (*CandidateNode, *CandidateNode, error) {
2021-05-09 04:18:25 +00:00
prefs := traversePreferences{DontAutoCreate: true}
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 {
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
}
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 {
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
}
return keyResults.Front().Value.(*CandidateNode), valueResults.Front().Value.(*CandidateNode), nil
2021-05-09 04:18:25 +00:00
}
func fromEntries(candidateNode *CandidateNode) (*CandidateNode, error) {
var node = candidateNode.CopyWithoutContent()
2021-05-09 04:18:25 +00:00
var contents = candidateNode.Content
2021-05-09 04:18:25 +00:00
for index := 0; index < len(contents); index = index + 1 {
key, value, err := parseEntry(contents[index], index)
2021-05-09 04:18:25 +00:00
if err != nil {
return nil, err
}
node.AddKeyValueChild(key, value)
2021-05-09 04:18:25 +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)
switch candidate.Kind {
case SequenceNode:
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
}
var results = list.New()
2021-05-09 05:12:50 +00:00
for el := toEntries.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
//run expression against entries
// splat toEntries and pipe it into Rhs
splatted, err := splat(context.SingleChildContext(candidate), traversePreferences{})
if err != nil {
return Context{}, err
}
result, err := d.GetMatchingNodes(splatted, expressionNode.RHS)
log.Debug("expressionNode.Rhs %v", expressionNode.RHS.Operation.OperationType)
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
}
log.Debug("candidate %v", NodeToString(candidate))
log.Debug("candidate leading content: %v", candidate.LeadingContent)
collected.LeadingContent = candidate.LeadingContent
log.Debug("candidate FootComment: [%v]", candidate.FootComment)
collected.HeadComment = candidate.HeadComment
collected.FootComment = candidate.FootComment
log.Debugf("**** collected %v", collected.LeadingContent)
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
return context.ChildContext(results), nil
2021-05-09 05:12:50 +00:00
}