diff --git a/resources/function_help/json/env b/resources/function_help/json/env new file mode 100644 index 00000000000..fa0de3a259a --- /dev/null +++ b/resources/function_help/json/env @@ -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"} + ] +} diff --git a/src/core/qgsexpression.cpp b/src/core/qgsexpression.cpp index b269716b9c9..8abd21d4f84 100644 --- a/src/core/qgsexpression.cpp +++ b/src/core/qgsexpression.cpp @@ -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::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() << QgsFeatureRequest::ALL_ATTRIBUTES ) + << new StaticFunction( QStringLiteral( "env" ), 1, fcnEnvVar, QStringLiteral( "General" ), QString() ) << new StaticFunction( QStringLiteral( "attribute" ), 2, fcnAttribute, QStringLiteral( "Record" ), QString(), false, QSet() << QgsFeatureRequest::ALL_ATTRIBUTES ) // functions for arrays diff --git a/tests/src/core/testqgsexpression.cpp b/tests/src/core/testqgsexpression.cpp index 23ef01f4b6f..76ba72acc37 100644 --- a/tests/src/core/testqgsexpression.cpp +++ b/tests/src/core/testqgsexpression.cpp @@ -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( "<array: 'One', 'Two', 'A very long string that is going to be trunca...>" ) ); } - }; QGSTEST_MAIN( TestQgsExpression )