Navigation Menu

Skip to content

Commit

Permalink
[processing] Allow case-different duplicate parameter names
Browse files Browse the repository at this point in the history
But always prefer case-exact matches for parameter names. Turns
out the grass provider requires use of parameters with the same
name but different case, so we need to be able to handle this.
  • Loading branch information
nyalldawson committed Jan 31, 2018
1 parent 28ff28a commit fc68bb2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion python/core/processing/qgsprocessingalgorithm.sip.in
Expand Up @@ -224,7 +224,7 @@ Returns an ordered list of parameter definitions utilized by the algorithm.
const QgsProcessingParameterDefinition *parameterDefinition( const QString &name ) const;
%Docstring
Returns a matching parameter by ``name``. Matching is done in a case-insensitive
manner.
manner, but exact case matches will be preferred.

.. seealso:: :py:func:`parameterDefinitions`
%End
Expand Down
13 changes: 11 additions & 2 deletions src/core/processing/qgsprocessingalgorithm.cpp
Expand Up @@ -243,7 +243,8 @@ bool QgsProcessingAlgorithm::addParameter( QgsProcessingParameterDefinition *def
return false;

// check for duplicate named parameters
if ( QgsProcessingAlgorithm::parameterDefinition( definition->name() ) )
const QgsProcessingParameterDefinition *existingDef = QgsProcessingAlgorithm::parameterDefinition( definition->name() );
if ( existingDef && existingDef->name() == definition->name() ) // parameterDefinition is case-insensitive, but we DO allow case-different duplicate names
{
QgsLogger::warning( QStringLiteral( "Duplicate parameter %1 registered for alg %2" ).arg( definition->name(), id() ) );
return false;
Expand Down Expand Up @@ -300,7 +301,15 @@ QVariantMap QgsProcessingAlgorithm::postProcessAlgorithm( QgsProcessingContext &

const QgsProcessingParameterDefinition *QgsProcessingAlgorithm::parameterDefinition( const QString &name ) const
{
Q_FOREACH ( const QgsProcessingParameterDefinition *def, mParameters )
// first pass - case sensitive match
for ( const QgsProcessingParameterDefinition *def : mParameters )
{
if ( def->name() == name )
return def;
}

// second pass - case insensitive
for ( const QgsProcessingParameterDefinition *def : mParameters )
{
if ( def->name().compare( name, Qt::CaseInsensitive ) == 0 )
return def;
Expand Down
2 changes: 1 addition & 1 deletion src/core/processing/qgsprocessingalgorithm.h
Expand Up @@ -247,7 +247,7 @@ class CORE_EXPORT QgsProcessingAlgorithm

/**
* Returns a matching parameter by \a name. Matching is done in a case-insensitive
* manner.
* manner, but exact case matches will be preferred.
* \see parameterDefinitions()
*/
const QgsProcessingParameterDefinition *parameterDefinition( const QString &name ) const;
Expand Down
9 changes: 9 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -147,6 +147,15 @@ class DummyAlgorithm : public QgsProcessingAlgorithm
QCOMPARE( rasterParam->defaultFileExtension(), QStringLiteral( "tif" ) ); // before alg is accessible
addParameter( rasterParam );
QCOMPARE( rasterParam->defaultFileExtension(), QStringLiteral( "tif" ) );

// should allow parameters with same name but different case (required for grass provider)
QgsProcessingParameterBoolean *p1C = new QgsProcessingParameterBoolean( "P1" );
QVERIFY( addParameter( p1C ) );
QCOMPARE( parameterDefinitions().count(), 8 );

// parameterDefinition should be case insensitive, but prioritise correct case matches
QCOMPARE( parameterDefinition( "p1" ), parameterDefinitions().at( 0 ) );
QCOMPARE( parameterDefinition( "P1" ), parameterDefinitions().at( 7 ) );
}

void runParameterChecks2()
Expand Down

0 comments on commit fc68bb2

Please sign in to comment.