Fixing with_entries context #1925

Can now update key/value w.r.t each other
This commit is contained in:
Mike Farah 2024-02-11 10:25:38 +11:00
parent 8cde0c837c
commit e81b600744
2 changed files with 20 additions and 7 deletions

View File

@ -142,15 +142,18 @@ func withEntriesOperator(d *dataTreeNavigator, context Context, expressionNode *
return Context{}, err return Context{}, err
} }
result, err := d.GetMatchingNodes(splatted, expressionNode.RHS) newResults := list.New()
log.Debug("expressionNode.Rhs %v", expressionNode.RHS.Operation.OperationType)
log.Debug("result %v", result) for itemEl := splatted.MatchingNodes.Front(); itemEl != nil; itemEl = itemEl.Next() {
if err != nil { result, err := d.GetMatchingNodes(splatted.SingleChildContext(itemEl.Value.(*CandidateNode)), expressionNode.RHS)
return Context{}, err if err != nil {
return Context{}, err
}
newResults.PushBackList(result.MatchingNodes)
} }
selfExpression := &ExpressionNode{Operation: &Operation{OperationType: selfReferenceOpType}} selfExpression := &ExpressionNode{Operation: &Operation{OperationType: selfReferenceOpType}}
collected, err := collectTogether(d, result, selfExpression) collected, err := collectTogether(d, splatted.ChildContext(newResults), selfExpression)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }

View File

@ -66,11 +66,21 @@ var entriesOperatorScenarios = []expressionScenario{
{ {
description: "Use with_entries to update keys", description: "Use with_entries to update keys",
document: `{a: 1, b: 2}`, 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{ expected: []string{
"D0, P[], (!!map)::KEY_a: 1\nKEY_b: 2\n", "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", 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.", 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.",