mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-14 00:07:35 -04:00
[FEATURE] Add new expression function env
This commit is contained in:
parent
f354a8560b
commit
26557c9a88
13
resources/function_help/json/env
Normal file
13
resources/function_help/json/env
Normal file
@ -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><array: 'One', 'Two', 'A very long string that is going to be trunca...></i>" ) );
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
QGSTEST_MAIN( TestQgsExpression )
|
||||
|
Loading…
x
Reference in New Issue
Block a user