Skip to content

Commit f961ece

Browse files
committed
[FEATURE] Add an eval expression
Funded by * Regional Council of Picardy * ADUGA * Ville de Nyon * Wetu GIT cc
1 parent 6035d98 commit f961ece

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

resources/function_help/json/eval

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "eval",
3+
"type": "function",
4+
"description": "Evaluates an expression which is passed in a string. Useful to expand dynamic parameters passed as context variables or fields.",
5+
"arguments": [ {"arg":"expression","description":"an expression string"}],
6+
"examples": [
7+
{ "expression":"eval('\\'nice\\'')", "returns":"'nice'"},
8+
{ "expression":"eval(@expression_var)", "returns":"[whatever the result of evaluating @expression_var might be...]"}
9+
]
10+
}

src/core/qgsexpression.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,16 @@ static QVariant fcnGetVariable( const QVariantList& values, const QgsExpressionC
409409
return context->variable( name );
410410
}
411411

412+
static QVariant fcnEval( const QVariantList& values, const QgsExpressionContext* context, QgsExpression* parent )
413+
{
414+
if ( !context )
415+
return QVariant();
416+
417+
QString expString = getStringValue( values.at( 0 ), parent );
418+
QgsExpression expression( expString );
419+
return expression.evaluate( context );
420+
}
421+
412422
static QVariant fcnSqrt( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent )
413423
{
414424
double x = getDoubleValue( values.at( 0 ), parent );
@@ -2522,6 +2532,7 @@ const QList<QgsExpression::Function*>& QgsExpression::Functions()
25222532
//return all attributes string for referencedColumns - this is caught by
25232533
// QgsFeatureRequest::setSubsetOfAttributes and causes all attributes to be fetched by the
25242534
// feature request
2535+
<< new StaticFunction( "eval", 1, fcnEval, "General", QString(), true, QStringList( QgsFeatureRequest::AllAttributes ) )
25252536
<< new StaticFunction( "attribute", 2, fcnAttribute, "Record", QString(), false, QStringList( QgsFeatureRequest::AllAttributes ) )
25262537

25272538
<< new StaticFunction( "_specialcol_", 1, fcnSpecialColumn, "Special" )

tests/src/core/testqgsexpression.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,36 @@ class TestQgsExpression: public QObject
15301530
Q_NOWARN_DEPRECATED_POP
15311531
}
15321532

1533+
void eval_eval()
1534+
{
1535+
QgsFeature f( 100 );
1536+
QgsFields fields;
1537+
fields.append( QgsField( "col1" ) );
1538+
fields.append( QgsField( "second_column", QVariant::Int ) );
1539+
f.setFields( fields, true );
1540+
f.setAttribute( QString( "col1" ), QString( "test value" ) );
1541+
f.setAttribute( QString( "second_column" ), 5 );
1542+
1543+
QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, QgsFields() );
1544+
1545+
QgsExpression exp1( "eval()" );
1546+
QVariant v1 = exp1.evaluate( &context );
1547+
1548+
Q_ASSERT( !v1.isValid() );
1549+
1550+
QgsExpression exp2( "eval('4')" );
1551+
QVariant v2 = exp2.evaluate( &context );
1552+
QCOMPARE( v2, QVariant( 4 ) );
1553+
1554+
QgsExpression exp3( "eval('\"second_column\" * 2')" );
1555+
QVariant v3 = exp3.evaluate( &context );
1556+
QCOMPARE( v3, QVariant( 10 ) );
1557+
1558+
QgsExpression exp4( "eval('\"col1\"')" );
1559+
QVariant v4 = exp4.evaluate( &context );
1560+
QCOMPARE( v4, QVariant( "test value" ) );
1561+
}
1562+
15331563
void expression_from_expression_data()
15341564
{
15351565
QTest::addColumn<QString>( "string" );

0 commit comments

Comments
 (0)