Skip to content
Permalink
Browse files

[FEATURE] Add new expression function `env`

  • Loading branch information
m-kuhn committed Mar 1, 2017
1 parent f354a85 commit 26557c9a88e8a74e308a414f8cfa9d445b8df71a
Showing with 45 additions and 1 deletion.
  1. +13 −0 resources/function_help/json/env
  2. +6 −0 src/core/qgsexpression.cpp
  3. +26 −1 tests/src/core/testqgsexpression.cpp
@@ -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"}
]
}
@@ -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 )
{
@@ -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
@@ -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'" ) );
@@ -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 )

0 comments on commit 26557c9

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