Skip to content
Permalink
Browse files
[expression] Allow use of (and return null to) index operator against…
… null node/value
  • Loading branch information
nirvn committed Jun 9, 2021
1 parent c4c7ec2 commit f6498d7d420e346b52d8ef652519aee39a301c30
Showing with 8 additions and 1 deletion.
  1. +2 −1 src/core/expression/qgsexpressionnodeimpl.cpp
  2. +6 −0 tests/src/core/testqgsexpression.cpp
@@ -1788,7 +1788,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();
}
}
@@ -4014,6 +4014,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]" ) );
@@ -4030,6 +4033,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 f6498d7

Please sign in to comment.