Skip to content

Commit

Permalink
[FEATURE] Add new expression function env
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Mar 1, 2017
1 parent f354a85 commit 26557c9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
13 changes: 13 additions & 0 deletions resources/function_help/json/env
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "env",
"type": "function",
"description": "Gets an environment variable and returns its content as a string. If the variable is not found, `NULL` will be returned. This is handy to inject system specific configuration like drive letters or path prefixes. Definition of environment variables depends on the operating system, please check with your system administrator or the operating system documentation how this can be set..",
"arguments": [
{"arg":"name","description":"The name of the environment variable which should be retrieved."}
],
"examples": [
{ "expression":"env( 'LANG' )", "returns":"'en_US.UTF-8'"},
{ "expression":"env( 'MY_OWN_PREFIX_VAR' )", "returns":"'Z:'"},
{ "expression":"env( 'I_DO_NOT_EXIST' )", "returns":"NULL"}
]
}
6 changes: 6 additions & 0 deletions src/core/qgsexpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3723,6 +3723,11 @@ static QVariant fcnMapAVals( const QVariantList& values, const QgsExpressionCont
return getMapValue( values.at( 0 ), parent ).values();
}

static QVariant fcnEnvVar( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent )
{
QString envVarName = values.at( 0 ).toString();
return QProcessEnvironment::systemEnvironment().value( envVarName );
}

bool QgsExpression::registerFunction( QgsExpression::Function* function, bool transferOwnership )
{
Expand Down Expand Up @@ -4115,6 +4120,7 @@ const QList<QgsExpression::Function*>& QgsExpression::Functions()
// QgsFeatureRequest::setSubsetOfAttributes and causes all attributes to be fetched by the
// feature request
<< new StaticFunction( QStringLiteral( "eval" ), 1, fcnEval, QStringLiteral( "General" ), QString(), true, QSet<QString>() << QgsFeatureRequest::ALL_ATTRIBUTES )
<< new StaticFunction( QStringLiteral( "env" ), 1, fcnEnvVar, QStringLiteral( "General" ), QString() )
<< new StaticFunction( QStringLiteral( "attribute" ), 2, fcnAttribute, QStringLiteral( "Record" ), QString(), false, QSet<QString>() << QgsFeatureRequest::ALL_ATTRIBUTES )

// functions for arrays
Expand Down
27 changes: 26 additions & 1 deletion tests/src/core/testqgsexpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2670,6 +2670,32 @@ class TestQgsExpression: public QObject
QCOMPARE( result.toString(), QString( "f2" ) );
}

void test_env()
{
QgsExpressionContext context;

setenv( "TESTENV_STRING", "Hello World", 1 );
QgsExpression e( "env('TESTENV_STRING')" );

QVariant result = e.evaluate( &context );

QCOMPARE( result.toString(), QStringLiteral( "Hello World" ) );
unsetenv( "TESTENV_STRING" );

setenv( "TESTENV_INT", "5", 1 );
QgsExpression e2( "env('TESTENV_INT')" );

QVariant result2 = e2.evaluate( &context );

QCOMPARE( result2.toString(), QStringLiteral( "5" ) );
unsetenv( "TESTENV_INT" );

QgsExpression e3( "env('TESTENV_I_DO_NOT_EXIST')" );
QVariant result3 = e3.evaluate( &context );

Q_ASSERT( result3.isNull() );
}

void test_formatPreviewString()
{
QCOMPARE( QgsExpression::formatPreviewString( QVariant( "hello" ) ), QString( "'hello'" ) );
Expand All @@ -2691,7 +2717,6 @@ class TestQgsExpression: public QObject
QCOMPARE( QgsExpression::formatPreviewString( QVariant( stringList ) ),
QString( "<i>&lt;array: 'One', 'Two', 'A very long string that is going to be trunca...&gt;</i>" ) );
}

};

QGSTEST_MAIN( TestQgsExpression )
Expand Down

0 comments on commit 26557c9

Please sign in to comment.