Skip to content
Permalink
Browse files

move function map to hstore into QgsHstoreUtils

  • Loading branch information
Gustry authored and nyalldawson committed Oct 23, 2018
1 parent b2a2a26 commit 0731148b6b49075483c718d123309b1e22deef45
@@ -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

@@ -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 * )
@@ -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( ',' );
}
@@ -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
@@ -116,6 +116,7 @@ SET(TESTS
testqgsgradients.cpp
testqgsgraduatedsymbolrenderer.cpp
testqgshistogram.cpp
testqgshstoreutils.cpp
testqgsimageoperation.cpp
testqgsinternalgeometryengine.cpp
testqgsinvertedpolygonrenderer.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.
You can’t perform that action at this time.