Skip to content
Permalink
Browse files
[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 590086169b600d84d55bb59d1690250bbf8ef82c
Showing with 8 additions and 1 deletion.
  1. +2 −1 src/core/expression/qgsexpressionnodeimpl.cpp
  2. +6 −0 tests/src/core/testqgsexpression.cpp
@@ -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();
}
}
@@ -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]" ) );
@@ -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
}


0 comments on commit 5900861

Please sign in to comment.