Skip to content

Commit aeb9463

Browse files
authoredNov 17, 2020
Merge pull request #39921 from roya0045/negative_array_Get
[Feature][Expression]Negative array get
2 parents d303a6b + a36de09 commit aeb9463

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed
 

‎resources/function_help/json/array_get

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
"name": "array_get",
33
"type": "function",
44
"groups": ["Arrays"],
5-
"description": "Returns the Nth value (0 for the first one) of an array.",
5+
"description": "Returns the Nth value (0 for the first one) or the last -Nth value (-1 for the last one) of an array.",
66
"arguments": [ {"arg":"array","description":"an array"},
77
{"arg":"index","description":"the index to get (0 based)"}],
8-
"examples": [ { "expression":"array_get(array('a','b','c'),1)", "returns":"'b'"}]
8+
"examples": [
9+
{ "expression":"array_get(array('a','b','c'),1)", "returns":"'b'"},
10+
{ "expression":"array_get(array('a','b','c'),-1)", "returns":"'c'"}
11+
]
912
}

‎src/core/expression/qgsexpressionfunction.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5324,8 +5324,10 @@ static QVariant fcnArrayGet( const QVariantList &values, const QgsExpressionCont
53245324
{
53255325
const QVariantList list = QgsExpressionUtils::getListValue( values.at( 0 ), parent );
53265326
const int pos = QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent );
5327-
if ( pos < 0 || pos >= list.length() ) return QVariant();
5328-
return list.at( pos );
5327+
if ( pos < list.length() && pos >= 0 ) return list.at( pos );
5328+
else if ( pos < 0 && ( list.length() + pos ) >= 0 )
5329+
return list.at( list.length() + pos );
5330+
return QVariant();
53295331
}
53305332

53315333
static QVariant fcnArrayFirst( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )

‎tests/src/core/testqgsexpression.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3333,7 +3333,8 @@ class TestQgsExpression: public QObject
33333333

33343334
QCOMPARE( QgsExpression( "array_get(\"strings\", 1)" ).evaluate( &context ), QVariant( "two" ) );
33353335
QCOMPARE( QgsExpression( "array_get(\"strings\", 2)" ).evaluate( &context ), QVariant() );
3336-
QCOMPARE( QgsExpression( "array_get(\"strings\", -1)" ).evaluate( &context ), QVariant() );
3336+
QCOMPARE( QgsExpression( "array_get(\"strings\", -1)" ).evaluate( &context ), QVariant( "two" ) );
3337+
QCOMPARE( QgsExpression( "array_get(\"strings\", -4)" ).evaluate( &context ), QVariant() );
33373338

33383339
QStringList appendExpected = array;
33393340
appendExpected << QStringLiteral( "three" );
@@ -3409,7 +3410,9 @@ class TestQgsExpression: public QObject
34093410

34103411
QCOMPARE( QgsExpression( "array_get(\"ints\", 1)" ).evaluate( &context ), QVariant( -2 ) );
34113412
QCOMPARE( QgsExpression( "array_get(\"ints\", 2)" ).evaluate( &context ), QVariant() );
3412-
QCOMPARE( QgsExpression( "array_get(\"ints\", -1)" ).evaluate( &context ), QVariant() );
3413+
QCOMPARE( QgsExpression( "array_get(\"ints\", -1)" ).evaluate( &context ), QVariant( -2 ) );
3414+
QCOMPARE( QgsExpression( "array_get(\"ints\", -2)" ).evaluate( &context ), QVariant( 1 ) );
3415+
QCOMPARE( QgsExpression( "array_get(\"ints\", -3)" ).evaluate( &context ), QVariant() );
34133416

34143417
QVariantList appendExpected = array;
34153418
appendExpected << 3;

0 commit comments

Comments
 (0)
Please sign in to comment.