Skip to content

Commit 0731148

Browse files
Gustrynyalldawson
authored andcommitted
move function map to hstore into QgsHstoreUtils
1 parent b2a2a26 commit 0731148

File tree

6 files changed

+77
-11
lines changed

6 files changed

+77
-11
lines changed

python/core/auto_generated/qgshstoreutils.sip.in

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ Returns a QVariantMap object containing the key and values from a hstore-formatt
2121

2222
:param string: The hstored-formatted string
2323

24+
.. versionadded:: 3.4
25+
%End
26+
27+
QString build( const QVariantMap &map );
28+
%Docstring
29+
Build a hstore-formatted string from a QVariantMap.
30+
31+
:param map: The map to format as a string
32+
2433
.. versionadded:: 3.4
2534
%End
2635

src/core/expression/qgsexpressionfunction.cpp

+1-11
Original file line numberDiff line numberDiff line change
@@ -4316,17 +4316,7 @@ static QVariant fcnHstoreToMap( const QVariantList &values, const QgsExpressionC
43164316
static QVariant fcnMapToHstore( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
43174317
{
43184318
QVariantMap map = QgsExpressionUtils::getMapValue( values.at( 0 ), parent );
4319-
QStringList list;
4320-
4321-
for ( QVariantMap::const_iterator it = map.constBegin(); it != map.constEnd(); ++it )
4322-
{
4323-
QString key = it.key();
4324-
QString value = it.value().toString();
4325-
list << QString( "\"%1\"=>\"%2\"" ).arg( key.replace( "\\", "\\\\" ).replace( "\"", "\\\"" ),
4326-
value.replace( "\\", "\\\\" ).replace( "\"", "\\\"" ) );
4327-
}
4328-
4329-
return list.join( ',' );
4319+
return QgsHstoreUtils::build( map );
43304320
}
43314321

43324322
static QVariant fcnMap( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )

src/core/qgshstoreutils.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,16 @@ QVariantMap QgsHstoreUtils::parse( const QString &string )
8484

8585
return map;
8686
}
87+
88+
QString QgsHstoreUtils::build( const QVariantMap &map )
89+
{
90+
QStringList list;
91+
for ( QVariantMap::const_iterator it = map.constBegin(); it != map.constEnd(); ++it )
92+
{
93+
QString key = it.key();
94+
QString value = it.value().toString();
95+
list << QString( "\"%1\"=>\"%2\"" ).arg( key.replace( "\\", "\\\\" ).replace( "\"", "\\\"" ),
96+
value.replace( "\\", "\\\\" ).replace( "\"", "\\\"" ) );
97+
}
98+
return list.join( ',' );
99+
}

src/core/qgshstoreutils.h

+7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ namespace QgsHstoreUtils
4040
*/
4141
CORE_EXPORT QVariantMap parse( const QString &string );
4242

43+
/**
44+
* Build a hstore-formatted string from a QVariantMap.
45+
* \param map The map to format as a string
46+
* \since QGIS 3.4
47+
*/
48+
CORE_EXPORT QString build( const QVariantMap &map );
49+
4350
};
4451

4552
#endif //QGSHSTOREUTILS_H

tests/src/core/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ SET(TESTS
116116
testqgsgradients.cpp
117117
testqgsgraduatedsymbolrenderer.cpp
118118
testqgshistogram.cpp
119+
testqgshstoreutils.cpp
119120
testqgsimageoperation.cpp
120121
testqgsinternalgeometryengine.cpp
121122
testqgsinvertedpolygonrenderer.cpp

tests/src/core/testqgshstoreutils.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/***************************************************************************
2+
testqgshstoreutils.cpp
3+
--------------------------------------
4+
Date : October 2018
5+
Copyright : (C) 2018 Etienne Trimaille
6+
Email : etienne dot trimaille at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#include <QString>
17+
#include "qgstest.h"
18+
#include "qgshstoreutils.h"
19+
20+
class TestQgsHstoreUtils : public QObject
21+
{
22+
Q_OBJECT
23+
private slots:
24+
25+
void testHstore();
26+
};
27+
28+
29+
void TestQgsHstoreUtils::testHstore()
30+
{
31+
QVariantMap map;
32+
map[QStringLiteral( "1" )] = "one";
33+
map[QStringLiteral( "2" )] = "two";
34+
map[QStringLiteral( "3" )] = "three";
35+
36+
QCOMPARE( QgsHstoreUtils::parse( QStringLiteral( "1=>one,2=>two,3=>three" ) ), map );
37+
QCOMPARE( QgsHstoreUtils::build( map ), QStringLiteral( "\"1\"=>\"one\",\"2\"=>\"two\",\"3\"=>\"three\"" ) );
38+
39+
map.clear();
40+
map[QStringLiteral( "1" )] = "one";
41+
// if a key is missing its closing quote, the map construction process will stop and a partial map is returned
42+
QCOMPARE( QgsHstoreUtils::parse( QStringLiteral( "\"1\"=>\"one\",\"2=>\"two\"" ) ), QVariantMap( map ) );
43+
}
44+
45+
QGSTEST_MAIN( TestQgsHstoreUtils )
46+
#include "testqgshstoreutils.moc"

0 commit comments

Comments
 (0)