From 40947d42660339a9f3b87cd03e57facdbcaa459d Mon Sep 17 00:00:00 2001 From: Alessandro Pasotti Date: Fri, 17 Dec 2021 15:16:48 +0100 Subject: [PATCH] Invert args --- .../function_help/json/represent_attributes | 6 +-- src/core/expression/qgsexpressionfunction.cpp | 54 +++++++++---------- tests/src/core/testqgsexpression.cpp | 4 +- 3 files changed, 30 insertions(+), 34 deletions(-) diff --git a/resources/function_help/json/represent_attributes b/resources/function_help/json/represent_attributes index 470570fc0b2..d373c5dc14c 100644 --- a/resources/function_help/json/represent_attributes +++ b/resources/function_help/json/represent_attributes @@ -18,9 +18,9 @@ }, { "variant": "Layer and feature parameters", - "variant_description": "If called with a 'feature' and a 'layer' parameter, the function will return the representation of the attributes of the specified feature from the specified layer.", - "arguments": [ { "arg": "feature", "description": "The feature which should be evaluated." }, { "arg": "layer", "description": "The layer (or its ID or name)." } ], - "examples": [ { "expression": "represent_attributes(@atlas_feature, 'atlas_layer')", "returns" : "The representation of the attributes for the specified feature from the specified layer." } ] + "variant_description": "If called with a 'layer' and a 'feature' parameter, the function will return the representation of the attributes of the specified feature from the specified layer.", + "arguments": [ { "arg": "layer", "description": "The layer (or its ID or name)." }, { "arg": "feature", "description": "The feature which should be evaluated." } ], + "examples": [ { "expression": "represent_attributes('atlas_layer', @atlas_feature)", "returns" : "The representation of the attributes for the specified feature from the specified layer." } ] } ] } diff --git a/src/core/expression/qgsexpressionfunction.cpp b/src/core/expression/qgsexpressionfunction.cpp index 2f13c8102f2..3638efac096 100644 --- a/src/core/expression/qgsexpressionfunction.cpp +++ b/src/core/expression/qgsexpressionfunction.cpp @@ -1732,35 +1732,25 @@ static QVariant fcnRepresentAttributes( const QVariantList &values, const QgsExp QgsVectorLayer *layer = nullptr; QgsFeature feature; - if ( values.size() == 2 ) + if ( values.isEmpty() ) { - if ( ! values.at( 0 ).isNull() ) - { - feature = QgsExpressionUtils::getFeature( values.at( 0 ), parent ); - } - else if ( context && context->hasFeature() ) - { - feature = context->feature(); - } - else - { - parent->setEvalErrorString( QObject::tr( "Cannot use represent attributes function in this context: feature is not set" ) ); - return QVariant(); - } - - if ( ! values.at( 1 ).isNull() ) - { - layer = QgsExpressionUtils::getVectorLayer( values.at( 1 ), parent ); - } - else if ( context ) - { - layer = QgsExpressionUtils::getVectorLayer( context->variable( QStringLiteral( "layer" ) ), parent ); - } - else - { - parent->setEvalErrorString( QObject::tr( "Cannot use represent attributes function: layer is not set" ) ); - return QVariant(); - } + feature = context->feature(); + layer = QgsExpressionUtils::getVectorLayer( context->variable( QStringLiteral( "layer" ) ), parent ); + } + else if ( values.size() == 1 ) + { + layer = QgsExpressionUtils::getVectorLayer( context->variable( QStringLiteral( "layer" ) ), parent ); + feature = QgsExpressionUtils::getFeature( values.at( 0 ), parent ); + } + else if ( values.size() == 2 ) + { + layer = QgsExpressionUtils::getVectorLayer( values.at( 0 ), parent ); + feature = QgsExpressionUtils::getFeature( values.at( 1 ), parent ); + } + else + { + parent->setEvalErrorString( QObject::tr( "Function `represent_attributes` requires no more than two parameters. %1 given." ).arg( values.length() ) ); + return QVariant(); } if ( !layer ) @@ -1769,6 +1759,12 @@ static QVariant fcnRepresentAttributes( const QVariantList &values, const QgsExp return QVariant(); } + if ( !feature.isValid() ) + { + parent->setEvalErrorString( QObject::tr( "Cannot use represent attributes function: feature could not be resolved." ) ); + return QVariant(); + } + const QgsFields fields = feature.fields(); QVariantMap result; for ( int fieldIndex = 0; fieldIndex < fields.count(); ++fieldIndex ) @@ -7754,7 +7750,7 @@ const QList &QgsExpression::Functions() fcnAttributes, QStringLiteral( "Record and Attributes" ), QString(), false, QSet() << QgsFeatureRequest::ALL_ATTRIBUTES ); attributesFunc->setIsStatic( false ); functions << attributesFunc; - QgsStaticExpressionFunction *representAttributesFunc = new QgsStaticExpressionFunction( QStringLiteral( "represent_attributes" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "feature" ), true ) << QgsExpressionFunction::Parameter( QStringLiteral( "layer" ), true ), + QgsStaticExpressionFunction *representAttributesFunc = new QgsStaticExpressionFunction( QStringLiteral( "represent_attributes" ), -1, fcnRepresentAttributes, QStringLiteral( "Record and Attributes" ), QString(), false, QSet() << QgsFeatureRequest::ALL_ATTRIBUTES ); representAttributesFunc->setIsStatic( false ); functions << representAttributesFunc; diff --git a/tests/src/core/testqgsexpression.cpp b/tests/src/core/testqgsexpression.cpp index 29cbdd82797..18afcbddcf5 100644 --- a/tests/src/core/testqgsexpression.cpp +++ b/tests/src/core/testqgsexpression.cpp @@ -566,7 +566,7 @@ class TestQgsExpression: public QObject QgsProject::instance()->addMapLayer( &layer, false, false ); QgsExpressionContext context3; context3.setFeature( f2 ); - expression = QgsExpression( "represent_attributes($currentfeature, 'test_represent_attributes')" ); + expression = QgsExpression( "represent_attributes('test_represent_attributes', $currentfeature)" ); result = expression.evaluate( &context3 ).toMap(); @@ -580,7 +580,7 @@ class TestQgsExpression: public QObject // Test errors QgsProject::instance()->removeMapLayer( layer.id() ); - expression = QgsExpression( "represent_attributes($currentfeature, 'test_represent_attributes')" ); + expression = QgsExpression( "represent_attributes('test_represent_attributes', $currentfeature)" ); QgsExpressionContext context4; result = expression.evaluate( &context4 ).toMap(); QVERIFY( expression.hasEvalError() );