Skip to content

Commit 26557c9

Browse files
committed
[FEATURE] Add new expression function env
1 parent f354a85 commit 26557c9

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

resources/function_help/json/env

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "env",
3+
"type": "function",
4+
"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..",
5+
"arguments": [
6+
{"arg":"name","description":"The name of the environment variable which should be retrieved."}
7+
],
8+
"examples": [
9+
{ "expression":"env( 'LANG' )", "returns":"'en_US.UTF-8'"},
10+
{ "expression":"env( 'MY_OWN_PREFIX_VAR' )", "returns":"'Z:'"},
11+
{ "expression":"env( 'I_DO_NOT_EXIST' )", "returns":"NULL"}
12+
]
13+
}

src/core/qgsexpression.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -3723,6 +3723,11 @@ static QVariant fcnMapAVals( const QVariantList& values, const QgsExpressionCont
37233723
return getMapValue( values.at( 0 ), parent ).values();
37243724
}
37253725

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

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

41204126
// functions for arrays

tests/src/core/testqgsexpression.cpp

+26-1
Original file line numberDiff line numberDiff line change
@@ -2670,6 +2670,32 @@ class TestQgsExpression: public QObject
26702670
QCOMPARE( result.toString(), QString( "f2" ) );
26712671
}
26722672

2673+
void test_env()
2674+
{
2675+
QgsExpressionContext context;
2676+
2677+
setenv( "TESTENV_STRING", "Hello World", 1 );
2678+
QgsExpression e( "env('TESTENV_STRING')" );
2679+
2680+
QVariant result = e.evaluate( &context );
2681+
2682+
QCOMPARE( result.toString(), QStringLiteral( "Hello World" ) );
2683+
unsetenv( "TESTENV_STRING" );
2684+
2685+
setenv( "TESTENV_INT", "5", 1 );
2686+
QgsExpression e2( "env('TESTENV_INT')" );
2687+
2688+
QVariant result2 = e2.evaluate( &context );
2689+
2690+
QCOMPARE( result2.toString(), QStringLiteral( "5" ) );
2691+
unsetenv( "TESTENV_INT" );
2692+
2693+
QgsExpression e3( "env('TESTENV_I_DO_NOT_EXIST')" );
2694+
QVariant result3 = e3.evaluate( &context );
2695+
2696+
Q_ASSERT( result3.isNull() );
2697+
}
2698+
26732699
void test_formatPreviewString()
26742700
{
26752701
QCOMPARE( QgsExpression::formatPreviewString( QVariant( "hello" ) ), QString( "'hello'" ) );
@@ -2691,7 +2717,6 @@ class TestQgsExpression: public QObject
26912717
QCOMPARE( QgsExpression::formatPreviewString( QVariant( stringList ) ),
26922718
QString( "<i>&lt;array: 'One', 'Two', 'A very long string that is going to be trunca...&gt;</i>" ) );
26932719
}
2694-
26952720
};
26962721

26972722
QGSTEST_MAIN( TestQgsExpression )

0 commit comments

Comments
 (0)