Skip to content

Commit

Permalink
[FEATURE] Add an eval expression
Browse files Browse the repository at this point in the history
Funded by
 * Regional Council of Picardy
 * ADUGA
 * Ville de Nyon
 * Wetu GIT cc
  • Loading branch information
m-kuhn committed Dec 8, 2015
1 parent 6035d98 commit f961ece
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
10 changes: 10 additions & 0 deletions resources/function_help/json/eval
@@ -0,0 +1,10 @@
{
"name": "eval",
"type": "function",
"description": "Evaluates an expression which is passed in a string. Useful to expand dynamic parameters passed as context variables or fields.",
"arguments": [ {"arg":"expression","description":"an expression string"}],
"examples": [
{ "expression":"eval('\\'nice\\'')", "returns":"'nice'"},
{ "expression":"eval(@expression_var)", "returns":"[whatever the result of evaluating @expression_var might be...]"}
]
}
11 changes: 11 additions & 0 deletions src/core/qgsexpression.cpp
Expand Up @@ -409,6 +409,16 @@ static QVariant fcnGetVariable( const QVariantList& values, const QgsExpressionC
return context->variable( name );
}

static QVariant fcnEval( const QVariantList& values, const QgsExpressionContext* context, QgsExpression* parent )
{
if ( !context )
return QVariant();

QString expString = getStringValue( values.at( 0 ), parent );
QgsExpression expression( expString );
return expression.evaluate( context );
}

static QVariant fcnSqrt( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent )
{
double x = getDoubleValue( values.at( 0 ), parent );
Expand Down Expand Up @@ -2522,6 +2532,7 @@ const QList<QgsExpression::Function*>& QgsExpression::Functions()
//return all attributes string for referencedColumns - this is caught by
// QgsFeatureRequest::setSubsetOfAttributes and causes all attributes to be fetched by the
// feature request
<< new StaticFunction( "eval", 1, fcnEval, "General", QString(), true, QStringList( QgsFeatureRequest::AllAttributes ) )
<< new StaticFunction( "attribute", 2, fcnAttribute, "Record", QString(), false, QStringList( QgsFeatureRequest::AllAttributes ) )

<< new StaticFunction( "_specialcol_", 1, fcnSpecialColumn, "Special" )
Expand Down
30 changes: 30 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -1530,6 +1530,36 @@ class TestQgsExpression: public QObject
Q_NOWARN_DEPRECATED_POP
}

void eval_eval()
{
QgsFeature f( 100 );
QgsFields fields;
fields.append( QgsField( "col1" ) );
fields.append( QgsField( "second_column", QVariant::Int ) );
f.setFields( fields, true );
f.setAttribute( QString( "col1" ), QString( "test value" ) );
f.setAttribute( QString( "second_column" ), 5 );

QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, QgsFields() );

QgsExpression exp1( "eval()" );
QVariant v1 = exp1.evaluate( &context );

Q_ASSERT( !v1.isValid() );

QgsExpression exp2( "eval('4')" );
QVariant v2 = exp2.evaluate( &context );
QCOMPARE( v2, QVariant( 4 ) );

QgsExpression exp3( "eval('\"second_column\" * 2')" );
QVariant v3 = exp3.evaluate( &context );
QCOMPARE( v3, QVariant( 10 ) );

QgsExpression exp4( "eval('\"col1\"')" );
QVariant v4 = exp4.evaluate( &context );
QCOMPARE( v4, QVariant( "test value" ) );
}

void expression_from_expression_data()
{
QTest::addColumn<QString>( "string" );
Expand Down

0 comments on commit f961ece

Please sign in to comment.