From 22949df0fd0bd43ac62dc86845e247cb9cc4f896 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Sat, 10 May 2025 07:14:47 +1000 Subject: [PATCH] Fixing running map against empty array bug #2359 --- pkg/yqlib/operator_map.go | 4 +++ pkg/yqlib/operator_map_test.go | 60 ++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/pkg/yqlib/operator_map.go b/pkg/yqlib/operator_map.go index 216d78ae..bbbd8afe 100644 --- a/pkg/yqlib/operator_map.go +++ b/pkg/yqlib/operator_map.go @@ -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) diff --git a/pkg/yqlib/operator_map_test.go b/pkg/yqlib/operator_map_test.go index 916bfa52..721e3096 100644 --- a/pkg/yqlib/operator_map_test.go +++ b/pkg/yqlib/operator_map_test.go @@ -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}`,