Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[expression] Allow use of (and return null to) index operator against…
… null node/value
  • Loading branch information
nirvn authored and nyalldawson committed Jun 9, 2021
1 parent 752b414 commit 5900861
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/core/expression/qgsexpressionnodeimpl.cpp
Expand Up @@ -1669,7 +1669,8 @@ QVariant QgsExpressionNodeIndexOperator::evalNode( QgsExpression *parent, const
}

default:
parent->setEvalErrorString( tr( "[] can only be used with map or array values, not %1" ).arg( QMetaType::typeName( container.type() ) ) );
if ( !container.isNull() )
parent->setEvalErrorString( tr( "[] can only be used with map or array values, not %1" ).arg( QMetaType::typeName( container.type() ) ) );
return QVariant();
}
}
Expand Down
6 changes: 6 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -3902,6 +3902,9 @@ class TestQgsExpression: public QObject
e = QgsExpression( QStringLiteral( "array(1,2,3)[-4]" ) );
QVERIFY( !e.evaluate( &context ).isValid() );
QVERIFY( !e.hasEvalError() ); // no eval error - we are tolerant to this
e = QgsExpression( QStringLiteral( "@null_variable[0]" ) );
QVERIFY( !e.evaluate( &context ).isValid() );
QVERIFY( !e.hasEvalError() ); // no eval error - we are tolerant to this

// maps
e = QgsExpression( QStringLiteral( "map('a',1,'b',2,'c',3)[0]" ) );
Expand All @@ -3918,6 +3921,9 @@ class TestQgsExpression: public QObject
QCOMPARE( e.evaluate( &context ).toInt(), 3 );
e = QgsExpression( QStringLiteral( "map('a',1,'bbb',2,'c',3)['b'||'b'||'b']" ) );
QCOMPARE( e.evaluate( &context ).toInt(), 2 );
e = QgsExpression( QStringLiteral( "@null_variable['key']" ) );
QVERIFY( !e.evaluate( &context ).isValid() );
QVERIFY( !e.hasEvalError() ); // no eval error - we are tolerant to this
}


Expand Down

0 comments on commit 5900861

Please sign in to comment.