Skip to content

Commit

Permalink
move function map to hstore into QgsHstoreUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry authored and nyalldawson committed Oct 23, 2018
1 parent b2a2a26 commit 0731148
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 11 deletions.
9 changes: 9 additions & 0 deletions python/core/auto_generated/qgshstoreutils.sip.in
Expand Up @@ -21,6 +21,15 @@ Returns a QVariantMap object containing the key and values from a hstore-formatt

:param string: The hstored-formatted string

.. versionadded:: 3.4
%End

QString build( const QVariantMap &map );
%Docstring
Build a hstore-formatted string from a QVariantMap.

:param map: The map to format as a string

.. versionadded:: 3.4
%End

Expand Down
12 changes: 1 addition & 11 deletions src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -4316,17 +4316,7 @@ static QVariant fcnHstoreToMap( const QVariantList &values, const QgsExpressionC
static QVariant fcnMapToHstore( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
QVariantMap map = QgsExpressionUtils::getMapValue( values.at( 0 ), parent );
QStringList list;

for ( QVariantMap::const_iterator it = map.constBegin(); it != map.constEnd(); ++it )
{
QString key = it.key();
QString value = it.value().toString();
list << QString( "\"%1\"=>\"%2\"" ).arg( key.replace( "\\", "\\\\" ).replace( "\"", "\\\"" ),
value.replace( "\\", "\\\\" ).replace( "\"", "\\\"" ) );
}

return list.join( ',' );
return QgsHstoreUtils::build( map );
}

static QVariant fcnMap( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
Expand Down
13 changes: 13 additions & 0 deletions src/core/qgshstoreutils.cpp
Expand Up @@ -84,3 +84,16 @@ QVariantMap QgsHstoreUtils::parse( const QString &string )

return map;
}

QString QgsHstoreUtils::build( const QVariantMap &map )
{
QStringList list;
for ( QVariantMap::const_iterator it = map.constBegin(); it != map.constEnd(); ++it )
{
QString key = it.key();
QString value = it.value().toString();
list << QString( "\"%1\"=>\"%2\"" ).arg( key.replace( "\\", "\\\\" ).replace( "\"", "\\\"" ),
value.replace( "\\", "\\\\" ).replace( "\"", "\\\"" ) );
}
return list.join( ',' );
}
7 changes: 7 additions & 0 deletions src/core/qgshstoreutils.h
Expand Up @@ -40,6 +40,13 @@ namespace QgsHstoreUtils
*/
CORE_EXPORT QVariantMap parse( const QString &string );

/**
* Build a hstore-formatted string from a QVariantMap.
* \param map The map to format as a string
* \since QGIS 3.4
*/
CORE_EXPORT QString build( const QVariantMap &map );

};

#endif //QGSHSTOREUTILS_H
1 change: 1 addition & 0 deletions tests/src/core/CMakeLists.txt
Expand Up @@ -116,6 +116,7 @@ SET(TESTS
testqgsgradients.cpp
testqgsgraduatedsymbolrenderer.cpp
testqgshistogram.cpp
testqgshstoreutils.cpp
testqgsimageoperation.cpp
testqgsinternalgeometryengine.cpp
testqgsinvertedpolygonrenderer.cpp
Expand Down
46 changes: 46 additions & 0 deletions tests/src/core/testqgshstoreutils.cpp
@@ -0,0 +1,46 @@
/***************************************************************************
testqgshstoreutils.cpp
--------------------------------------
Date : October 2018
Copyright : (C) 2018 Etienne Trimaille
Email : etienne dot trimaille at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include <QString>
#include "qgstest.h"
#include "qgshstoreutils.h"

class TestQgsHstoreUtils : public QObject
{
Q_OBJECT
private slots:

void testHstore();
};


void TestQgsHstoreUtils::testHstore()
{
QVariantMap map;
map[QStringLiteral( "1" )] = "one";
map[QStringLiteral( "2" )] = "two";
map[QStringLiteral( "3" )] = "three";

QCOMPARE( QgsHstoreUtils::parse( QStringLiteral( "1=>one,2=>two,3=>three" ) ), map );
QCOMPARE( QgsHstoreUtils::build( map ), QStringLiteral( "\"1\"=>\"one\",\"2\"=>\"two\",\"3\"=>\"three\"" ) );

map.clear();
map[QStringLiteral( "1" )] = "one";
// if a key is missing its closing quote, the map construction process will stop and a partial map is returned
QCOMPARE( QgsHstoreUtils::parse( QStringLiteral( "\"1\"=>\"one\",\"2=>\"two\"" ) ), QVariantMap( map ) );
}

QGSTEST_MAIN( TestQgsHstoreUtils )
#include "testqgshstoreutils.moc"

0 comments on commit 0731148

Please sign in to comment.