Skip to content
Permalink
Browse files

[feature][expression] base64 to and from

  • Loading branch information
JanCaha authored and nyalldawson committed May 5, 2020
1 parent c6773a4 commit 357bc1d1d71c8cb592244ef5036a9ef36f9202d9
Showing with 29 additions and 0 deletions.
  1. +25 −0 src/core/expression/qgsexpressionfunction.cpp
  2. +4 −0 tests/src/core/testqgsexpression.cpp
@@ -5624,6 +5624,25 @@ static QVariant fcnHashSha256( const QVariantList &values, const QgsExpressionCo
return fcnHash( QgsExpressionUtils::getStringValue( values.at( 0 ), parent ), QCryptographicHash::Sha256 );
}

static QVariant fcnToBase64( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
QString value = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );

const QByteArray input = value.toLocal8Bit();

return QVariant( QString( input.toBase64() ) );
}

static QVariant fcnFromBase64( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
QString value = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );

const QByteArray base64 = value.toLocal8Bit();
const QByteArray decoded = QByteArray::fromBase64( base64 );

return QVariant( QString( decoded ) );
}

const QList<QgsExpressionFunction *> &QgsExpression::Functions()
{
// The construction of the list isn't thread-safe, and without the mutex,
@@ -5953,6 +5972,12 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
<< new QgsStaticExpressionFunction( QStringLiteral( "sha256" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ),
fcnHashSha256, QStringLiteral( "Conversions" ) )

//base64
<< new QgsStaticExpressionFunction( QStringLiteral( "to_base64" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ),
fcnToBase64, QStringLiteral( "Conversions" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "from_base64" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ),
fcnFromBase64, QStringLiteral( "Conversions" ) )

// deprecated stuff - hidden from users
<< new QgsStaticExpressionFunction( QStringLiteral( "$scale" ), QgsExpressionFunction::ParameterList(), fcnMapScale, QStringLiteral( "deprecated" ) );

@@ -1641,6 +1641,10 @@ class TestQgsExpression: public QObject
QTest::newRow( "hash('QGIS', 'keccak_384')" ) << QStringLiteral( "hash('QGIS', 'keccak_384')" ) << false << QVariant( "c57a3aed9d856fa04e5eeee9b62b6e027cca81ba574116d3cc1f0d48a1ef9e5886ff463ea8d0fac772ee473bf92f810d" );
QTest::newRow( "hash('QGIS', 'keccak_512')" ) << QStringLiteral( "hash('QGIS', 'keccak_512')" ) << false << QVariant( "6f0f751776b505e317de222508fa5d3ed7099d8f07c74fed54ccee6e7cdc6b89b4a085e309f2ee5210c942bbeb142bdfe48f84f912e0f3f41bdbf47110c2d344" );
#endif
QTest::newRow( "to_base64 NULL" ) << QStringLiteral( "to_base64(NULL)" ) << false << QVariant();
QTest::newRow( "to_base64" ) << QStringLiteral( "to_base64('QGIS')" ) << false << QVariant( "UUdJUw==" );
QTest::newRow( "from_base64 NULL" ) << QStringLiteral( "from_base64(NULL)" ) << false << QVariant();
QTest::newRow( "from_base64" ) << QStringLiteral( "from_base64('UUdJUw==')" ) << false << QVariant( "QGIS" );
}


0 comments on commit 357bc1d

Please sign in to comment.
You can’t perform that action at this time.