Skip to content

Commit

Permalink
[processing] When copying algorithm parameter values as json,
Browse files Browse the repository at this point in the history
handle data defined field/expression based properties

Refs #50482
  • Loading branch information
nyalldawson committed Jan 16, 2023
1 parent 7b29e79 commit f43ba11
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -2499,7 +2499,6 @@ QVariant QgsProcessingParameterDefinition::valueAsJsonObjectPrivate( const QVari
break;
}


if ( value.userType() == QMetaType::type( "QgsProperty" ) )
{
const QgsProperty prop = value.value< QgsProperty >();
Expand All @@ -2509,12 +2508,10 @@ QVariant QgsProcessingParameterDefinition::valueAsJsonObjectPrivate( const QVari
return QVariant();
case QgsProperty::StaticProperty:
return valueAsJsonObject( prop.staticValue(), context );

// these are not supported for serialization
case QgsProperty::FieldBasedProperty:
return QVariantMap( {{QStringLiteral( "type" ), QStringLiteral( "data_defined" )}, {QStringLiteral( "field" ), prop.field() }} );
case QgsProperty::ExpressionBasedProperty:
QgsDebugMsg( QStringLiteral( "could not convert expression/field based property to JSON object" ) );
return QVariant();
return QVariantMap( {{QStringLiteral( "type" ), QStringLiteral( "data_defined" )}, {QStringLiteral( "expression" ), prop.expressionString() }} );
}
}

Expand Down
30 changes: 30 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -6107,6 +6107,10 @@ void TestQgsProcessing::parameterString()
QCOMPARE( def->valueAsJsonObject( "uri='complex' username=\"complex\"", context ), QVariant( QStringLiteral( "uri='complex' username=\"complex\"" ) ) );
QCOMPARE( def->valueAsJsonObject( QStringLiteral( "c:\\test\\new data\\test.dat" ), context ), QVariant( QStringLiteral( "c:\\test\\new data\\test.dat" ) ) );

QCOMPARE( def->valueAsJsonObject( QVariant::fromValue( QgsProperty::fromValue( QStringLiteral( "test" ) ) ), context ), QVariant( QStringLiteral( "test" ) ) );
QCOMPARE( def->valueAsJsonObject( QVariant::fromValue( QgsProperty::fromField( QStringLiteral( "a field" ) ) ), context ), QVariantMap( {{QStringLiteral( "type" ), QStringLiteral( "data_defined" )}, {QStringLiteral( "field" ), QStringLiteral( "a field" ) }} ) );
QCOMPARE( def->valueAsJsonObject( QVariant::fromValue( QgsProperty::fromExpression( QStringLiteral( "\"a field\" * 2" ) ) ), context ), QVariantMap( {{QStringLiteral( "type" ), QStringLiteral( "data_defined" )}, {QStringLiteral( "expression" ), QStringLiteral( "\"a field\" * 2" ) }} ) );

bool ok = false;
QCOMPARE( def->valueAsString( QVariant(), context, ok ), QString() );
QVERIFY( ok );
Expand Down Expand Up @@ -11703,6 +11707,32 @@ void TestQgsProcessing::preprocessParameters()
QCOMPARE( outputs.value( QStringLiteral( "data defined field" ) ).value< QgsProperty >().field(), QStringLiteral( "DEPTH_FIELD" ) );
QCOMPARE( outputs.value( QStringLiteral( "data defined expression" ) ).value< QgsProperty >().propertyType(), QgsProperty::ExpressionBasedProperty );
QCOMPARE( outputs.value( QStringLiteral( "data defined expression" ) ).value< QgsProperty >().expressionString(), QStringLiteral( "A_FIELD * 200" ) );

// test round trip of data defined parameters
const QgsProcessingAlgorithm *bufferAlg = QgsApplication::processingRegistry()->algorithmById( "native:buffer" );
QVERIFY( bufferAlg );

inputs.clear();
inputs.insert( QStringLiteral( "DISTANCE" ), QgsProperty::fromField( QStringLiteral( "DEPTH_FIELD" ) ) );

QgsProcessingContext context;
QVariantMap exportedParams = bufferAlg->asMap( inputs, context );
outputs = QgsProcessingUtils::preprocessQgisProcessParameters( inputs, ok, error );
QVERIFY( ok );
QVERIFY( error.isEmpty() );

QCOMPARE( outputs.value( QStringLiteral( "DISTANCE" ) ).value< QgsProperty >().propertyType(), QgsProperty::FieldBasedProperty );
QCOMPARE( outputs.value( QStringLiteral( "DISTANCE" ) ).value< QgsProperty >().field(), QStringLiteral( "DEPTH_FIELD" ) );

inputs.insert( QStringLiteral( "DISTANCE" ), QgsProperty::fromExpression( QStringLiteral( "A_FIELD * 200" ) ) );

exportedParams = bufferAlg->asMap( inputs, context );
outputs = QgsProcessingUtils::preprocessQgisProcessParameters( inputs, ok, error );
QVERIFY( ok );
QVERIFY( error.isEmpty() );

QCOMPARE( outputs.value( QStringLiteral( "DISTANCE" ) ).value< QgsProperty >().propertyType(), QgsProperty::ExpressionBasedProperty );
QCOMPARE( outputs.value( QStringLiteral( "DISTANCE" ) ).value< QgsProperty >().expressionString(), QStringLiteral( "A_FIELD * 200" ) );
}

QGSTEST_MAIN( TestQgsProcessing )
Expand Down

0 comments on commit f43ba11

Please sign in to comment.