Skip to content
Permalink
Browse files

Allow negative array get

  • Loading branch information
roya0045 committed Nov 9, 2020
1 parent 5b73ae2 commit 954335040ee7da88a902283d6bec0aedb8b07e8d
@@ -1,8 +1,11 @@
{
"name": "array_get",
"type": "function",
"description": "Returns the Nth value (0 for the first one) of an array.",
"description": "Returns the Nth value (0 for the first one) or the last -Nth value (-1 for the last one) of an array.",
"arguments": [ {"arg":"array","description":"an array"},
{"arg":"index","description":"the index to get (0 based)"}],
"examples": [ { "expression":"array_get(array('a','b','c'),1)", "returns":"'b'"}]
"examples": [
{ "expression":"array_get(array('a','b','c'),1)", "returns":"'b'"},
{ "expression":"array_get(array('a','b','c'),-1)", "returns":"'c'"}
]
}
@@ -5229,7 +5229,8 @@ static QVariant fcnArrayGet( const QVariantList &values, const QgsExpressionCont
{
const QVariantList list = QgsExpressionUtils::getListValue( values.at( 0 ), parent );
const int pos = QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent );
if ( pos < 0 || pos >= list.length() ) return QVariant();
if ( pos >= list.length() ) return QVariant();
if ( pos < 0 && ( list.size() + pos ) >= 0 ) return list.at( list.size() + pos );
return list.at( pos );
}

@@ -3326,7 +3326,9 @@ class TestQgsExpression: public QObject

QCOMPARE( QgsExpression( "array_get(\"ints\", 1)" ).evaluate( &context ), QVariant( -2 ) );
QCOMPARE( QgsExpression( "array_get(\"ints\", 2)" ).evaluate( &context ), QVariant() );
QCOMPARE( QgsExpression( "array_get(\"ints\", -1)" ).evaluate( &context ), QVariant() );
QCOMPARE( QgsExpression( "array_get(\"ints\", -1)" ).evaluate( &context ), QVariant( -2 ) );
QCOMPARE( QgsExpression( "array_get(\"ints\", -2)" ).evaluate( &context ), QVariant( 1 ) );
QCOMPARE( QgsExpression( "array_get(\"ints\", -3)" ).evaluate( &context ), QVariant() );

QVariantList appendExpected = array;
appendExpected << 3;

0 comments on commit 9543350

Please sign in to comment.
You can’t perform that action at this time.