Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] array_foreach expression function - add counter variable #54962

Merged
merged 3 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion resources/function_help/json/array_foreach
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
"description": "an array"
}, {
"arg": "expression",
"description": "an expression to evaluate on each item. The variable `@element` will be replaced by the current value."
"description": "an expression to evaluate on each item. The variable `@element` will be replaced by the current value and the variable `@counter` by the current index (starting with 0)."
}],
"examples": [{
"expression": "array_foreach(array('a','b','c'),upper(@element))",
"returns": "[ 'A', 'B', 'C' ]"
}, {
"expression": "array_foreach(array(1,2,3),@element + 10)",
"returns": "[ 11, 12, 13 ]"
}, {
"expression": "array_foreach(array(1,2,3),@element + @counter)",
"returns": "[ 1, 3, 5 ]"
}],
"tags": ["evaluated", "array", "iterate", "item"]
}
5 changes: 4 additions & 1 deletion src/core/expression/qgsexpressionfunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9353,9 +9353,11 @@ QVariant QgsArrayForeachExpressionFunction::run( QgsExpressionNode::NodeList *ar
QgsExpressionContextScope *subScope = new QgsExpressionContextScope();
subContext->appendScope( subScope );

for ( QVariantList::const_iterator it = array.constBegin(); it != array.constEnd(); ++it )
int i = 0;
for ( QVariantList::const_iterator it = array.constBegin(); it != array.constEnd(); ++it, ++i )
{
subScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "element" ), *it, true ) );
subScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "counter" ), i, true ) );
result << args->at( 1 )->eval( parent, subContext );
}

Expand Down Expand Up @@ -9393,6 +9395,7 @@ bool QgsArrayForeachExpressionFunction::prepare( const QgsExpressionNodeFunction

QgsExpressionContextScope *subScope = new QgsExpressionContextScope();
subScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "element" ), QVariant(), true ) );
subScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "counter" ), QVariant(), true ) );
subContext.appendScope( subScope );

args->at( 1 )->prepare( parent, &subContext );
Expand Down
1 change: 1 addition & 0 deletions tests/src/core/testqgsexpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4340,6 +4340,7 @@ class TestQgsExpression: public QObject
QVariantList foreachExpected;
foreachExpected << 10 << 20 << 40;
QCOMPARE( QgsExpression( "array_foreach(array(1, 2, 4), @element * 10)" ).evaluate( &context ), QVariant( foreachExpected ) );
QCOMPARE( QgsExpression( "array_foreach(array(10, 19, 38), @element + @counter)" ).evaluate( &context ), QVariant( foreachExpected ) );

QVariantList filterExpected = QVariantList() << 1 << 2;
QCOMPARE( QgsExpression( "array_filter(array(1, 2, 4), @element < 3)" ).evaluate( &context ), QVariant( filterExpected ) );
Expand Down