mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
Fixing with_entries context #1925
Can now update key/value w.r.t each other
This commit is contained in:
parent
8cde0c837c
commit
e81b600744
@ -142,15 +142,18 @@ func withEntriesOperator(d *dataTreeNavigator, context Context, expressionNode *
|
||||
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
|
||||
newResults := list.New()
|
||||
|
||||
for itemEl := splatted.MatchingNodes.Front(); itemEl != nil; itemEl = itemEl.Next() {
|
||||
result, err := d.GetMatchingNodes(splatted.SingleChildContext(itemEl.Value.(*CandidateNode)), expressionNode.RHS)
|
||||
if err != nil {
|
||||
return Context{}, err
|
||||
}
|
||||
newResults.PushBackList(result.MatchingNodes)
|
||||
}
|
||||
|
||||
selfExpression := &ExpressionNode{Operation: &Operation{OperationType: selfReferenceOpType}}
|
||||
collected, err := collectTogether(d, result, selfExpression)
|
||||
collected, err := collectTogether(d, splatted.ChildContext(newResults), selfExpression)
|
||||
if err != nil {
|
||||
return Context{}, err
|
||||
}
|
||||
|
@ -66,11 +66,21 @@ var entriesOperatorScenarios = []expressionScenario{
|
||||
{
|
||||
description: "Use with_entries to update keys",
|
||||
document: `{a: 1, b: 2}`,
|
||||
expression: `with_entries(.key |= "KEY_" + .)`,
|
||||
// expression: `to_entries | with(.[]; .key |= "KEY_" + .) | from_entries`,
|
||||
expression: `with_entries(.key |= "KEY_" + .)`,
|
||||
expected: []string{
|
||||
"D0, P[], (!!map)::KEY_a: 1\nKEY_b: 2\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
description: "Use with_entries to update keys comment",
|
||||
document: `{a: 1, b: 2}`,
|
||||
expression: `with_entries(.key headComment= .value)`,
|
||||
expected: []string{
|
||||
"D0, P[], (!!map)::# 1\na: 1\n# 2\nb: 2\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "Custom sort map keys",
|
||||
subdescription: "Use to_entries to convert to an array of key/value pairs, sort the array using sort/sort_by/etc, and convert it back.",
|
||||
|
Loading…
Reference in New Issue
Block a user