Skip to content

Commit 22949df

Browse files
committed
Fixing running map against empty array bug #2359
1 parent 734e2cd commit 22949df

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

pkg/yqlib/operator_map.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ func mapOperator(d *dataTreeNavigator, context Context, expressionNode *Expressi
4141
if err != nil {
4242
return Context{}, err
4343
}
44+
if splatted.MatchingNodes.Len() == 0 {
45+
results.PushBack(candidate.Copy())
46+
continue
47+
}
4448

4549
result, err := d.GetMatchingNodes(splatted, expressionNode.RHS)
4650
log.Debug("expressionNode.Rhs %v", expressionNode.RHS.Operation.OperationType)

pkg/yqlib/operator_map_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,46 @@ var mapOperatorScenarios = []expressionScenario{
1515
"D0, P[], (!!seq)::[6, 7, 8]\n",
1616
},
1717
},
18+
{
19+
description: "mapping against an empty array should do nothing",
20+
skipDoc: true,
21+
document: `[]`,
22+
document2: `["cat"]`,
23+
expression: `map(3)`,
24+
expected: []string{
25+
"D0, P[], (!!seq)::[]\n",
26+
"D0, P[], (!!seq)::[3]\n",
27+
},
28+
},
29+
{
30+
description: "mapping against an empty array should do nothing",
31+
skipDoc: true,
32+
document: `[[], [5]]`,
33+
expression: `.[] |= map(3)`,
34+
expected: []string{
35+
"D0, P[], (!!seq)::[[], [3]]\n",
36+
},
37+
},
38+
{
39+
description: "mapping against an empty array should do nothing #2",
40+
skipDoc: true,
41+
document: `[]`,
42+
document2: `[5]`,
43+
expression: `map(3 + .)`,
44+
expected: []string{
45+
"D0, P[], (!!seq)::[]\n",
46+
"D0, P[], (!!seq)::[8]\n",
47+
},
48+
},
49+
{
50+
description: "mapping against an empty array should do nothing",
51+
skipDoc: true,
52+
document: `[[], [5]]`,
53+
expression: `.[] |= map(3 + .)`,
54+
expected: []string{
55+
"D0, P[], (!!seq)::[[], [8]]\n",
56+
},
57+
},
1858
{
1959
skipDoc: true,
2060
expression: `[] | map(. + 42)`,
@@ -39,6 +79,26 @@ var mapOperatorScenarios = []expressionScenario{
3979
"D0, P[], (!!seq)::[2, 3, 4]\n",
4080
},
4181
},
82+
{
83+
skipDoc: true,
84+
document: `{}`,
85+
document2: `{b: 12}`,
86+
expression: `map_values(3)`,
87+
expected: []string{
88+
"D0, P[], (!!map)::{}\n",
89+
"D0, P[], (!!map)::{b: 3}\n",
90+
},
91+
},
92+
{
93+
skipDoc: true,
94+
document: `{}`,
95+
document2: `{b: 12}`,
96+
expression: `map_values(3 + .)`,
97+
expected: []string{
98+
"D0, P[], (!!map)::{}\n",
99+
"D0, P[], (!!map)::{b: 15}\n",
100+
},
101+
},
42102
{
43103
skipDoc: true,
44104
document: `{a: 1, b: 2, c: 3}`,

0 commit comments

Comments
 (0)