Skip to content
Permalink
Browse files

[FEATURE] Add an eval expression

Funded by
 * Regional Council of Picardy
 * ADUGA
 * Ville de Nyon
 * Wetu GIT cc
  • Loading branch information
m-kuhn committed Dec 7, 2015
1 parent 6035d98 commit f961ecef465fd0dede546670356dc373b7a36ec5
Showing with 51 additions and 0 deletions.
  1. +10 −0 resources/function_help/json/eval
  2. +11 −0 src/core/qgsexpression.cpp
  3. +30 −0 tests/src/core/testqgsexpression.cpp
@@ -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...]"}
]
}
@@ -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 );
@@ -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" )
@@ -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" );

0 comments on commit f961ece

Please sign in to comment.
You can’t perform that action at this time.