Fixing running map against empty array bug #2359

This commit is contained in:
Mike Farah 2025-05-10 07:14:47 +10:00
parent 734e2cd254
commit 22949df0fd
2 changed files with 64 additions and 0 deletions

View File

@ -41,6 +41,10 @@ func mapOperator(d *dataTreeNavigator, context Context, expressionNode *Expressi
if err != nil {
return Context{}, err
}
if splatted.MatchingNodes.Len() == 0 {
results.PushBack(candidate.Copy())
continue
}
result, err := d.GetMatchingNodes(splatted, expressionNode.RHS)
log.Debug("expressionNode.Rhs %v", expressionNode.RHS.Operation.OperationType)

View File

@ -15,6 +15,46 @@ var mapOperatorScenarios = []expressionScenario{
"D0, P[], (!!seq)::[6, 7, 8]\n",
},
},
{
description: "mapping against an empty array should do nothing",
skipDoc: true,
document: `[]`,
document2: `["cat"]`,
expression: `map(3)`,
expected: []string{
"D0, P[], (!!seq)::[]\n",
"D0, P[], (!!seq)::[3]\n",
},
},
{
description: "mapping against an empty array should do nothing",
skipDoc: true,
document: `[[], [5]]`,
expression: `.[] |= map(3)`,
expected: []string{
"D0, P[], (!!seq)::[[], [3]]\n",
},
},
{
description: "mapping against an empty array should do nothing #2",
skipDoc: true,
document: `[]`,
document2: `[5]`,
expression: `map(3 + .)`,
expected: []string{
"D0, P[], (!!seq)::[]\n",
"D0, P[], (!!seq)::[8]\n",
},
},
{
description: "mapping against an empty array should do nothing",
skipDoc: true,
document: `[[], [5]]`,
expression: `.[] |= map(3 + .)`,
expected: []string{
"D0, P[], (!!seq)::[[], [8]]\n",
},
},
{
skipDoc: true,
expression: `[] | map(. + 42)`,
@ -39,6 +79,26 @@ var mapOperatorScenarios = []expressionScenario{
"D0, P[], (!!seq)::[2, 3, 4]\n",
},
},
{
skipDoc: true,
document: `{}`,
document2: `{b: 12}`,
expression: `map_values(3)`,
expected: []string{
"D0, P[], (!!map)::{}\n",
"D0, P[], (!!map)::{b: 3}\n",
},
},
{
skipDoc: true,
document: `{}`,
document2: `{b: 12}`,
expression: `map_values(3 + .)`,
expected: []string{
"D0, P[], (!!map)::{}\n",
"D0, P[], (!!map)::{b: 15}\n",
},
},
{
skipDoc: true,
document: `{a: 1, b: 2, c: 3}`,