Commit c0a5a21a authored by Tobias Hunger's avatar Tobias Hunger

SDKtool: Handle lists of values in find and findKey operations

Change-Id: Ifb5e322f5ddc62f5f5cc3d464faf592b05195603
Reviewed-by: default avatarTobias Hunger <tobias.hunger@digia.com>
parent aaedccf1
...@@ -96,6 +96,19 @@ bool FindKeyOperation::test() const ...@@ -96,6 +96,19 @@ bool FindKeyOperation::test() const
testMap.insert(QLatin1String("testint"), 23); testMap.insert(QLatin1String("testint"), 23);
testMap.insert(QLatin1String("testbool"), true); testMap.insert(QLatin1String("testbool"), true);
subKeys.clear();
QVariantList list1;
list1.append(QLatin1String("ignore this"));
list1.append(QLatin1String("ignore this2"));
QVariantList list2;
list2.append(QLatin1String("somevalue"));
subKeys.insert(QLatin1String("findMe"), QLatin1String("FindInList"));
list2.append(subKeys);
list2.append(QLatin1String("someothervalue"));
list1.append(QVariant(list2));
testMap.insert(QLatin1String("aList"), list1);
QStringList result; QStringList result;
result = findKey(testMap, QLatin1String("missing")); result = findKey(testMap, QLatin1String("missing"));
if (!result.isEmpty()) if (!result.isEmpty())
...@@ -112,22 +125,36 @@ bool FindKeyOperation::test() const ...@@ -112,22 +125,36 @@ bool FindKeyOperation::test() const
|| !result.contains(QLatin1String("testbool"))) || !result.contains(QLatin1String("testbool")))
return false; return false;
result = findKey(testMap, QLatin1String("findMe"));
if (result.count() != 1
|| !result.contains(QLatin1String("aList[2][1]/findMe")))
return false;
return true; return true;
} }
#endif #endif
QStringList FindKeyOperation::findKey(const QVariantMap &map, const QString &key) QStringList FindKeyOperation::findKey(const QVariant &in, const QString &key, const QString &prefix)
{ {
QStringList result; QStringList result;
for (QVariantMap::const_iterator i = map.begin(); i != map.end(); ++i) { if (in.type() == QVariant::Map) {
if (i.key() == key) { QVariantMap map = in.toMap();
result << key; for (QVariantMap::const_iterator i = map.begin(); i != map.end(); ++i) {
continue; QString pfx = prefix;
if (!pfx.isEmpty())
pfx.append(QLatin1Char('/'));
if (i.key() == key) {
result << pfx + key;
} else {
pfx.append(i.key());
result.append(findKey(i.value(), key, pfx));
}
} }
if (i.value().type() == QVariant::Map) { } else if (in.type() == QVariant::List) {
QStringList subKeyList = findKey(i.value().toMap(), key); QVariantList list = in.toList();
foreach (const QString &subKey, subKeyList) for (int pos = 0; pos < list.count(); ++pos) {
result << i.key() + QLatin1Char('/') + subKey; QString pfx = prefix + QLatin1Char('[') + QString::number(pos) + QLatin1String("]");
result.append(findKey(list.at(pos), key, pfx));
} }
} }
return result; return result;
......
...@@ -47,7 +47,8 @@ public: ...@@ -47,7 +47,8 @@ public:
bool test() const; bool test() const;
#endif #endif
static QStringList findKey(const QVariantMap &map, const QString &key); static QStringList findKey(const QVariant &in, const QString &key,
const QString &prefix = QString());
private: private:
QString m_file; QString m_file;
......
...@@ -31,6 +31,10 @@ ...@@ -31,6 +31,10 @@
#include <iostream> #include <iostream>
// <debug>
#include <QDebug>
// </debug>
QString FindValueOperation::name() const QString FindValueOperation::name() const
{ {
return QLatin1String("find"); return QLatin1String("find");
...@@ -95,12 +99,26 @@ bool FindValueOperation::test() const ...@@ -95,12 +99,26 @@ bool FindValueOperation::test() const
cur.insert(QLatin1String("testint2"), 53); cur.insert(QLatin1String("testint2"), 53);
subKeys.insert(QLatin1String("subsubkeys"), cur); subKeys.insert(QLatin1String("subsubkeys"), cur);
subKeys.insert(QLatin1String("testbool"), true); subKeys.insert(QLatin1String("testbool"), true);
subKeys.insert(QLatin1String("testbool2"), false);
subKeys.insert(QLatin1String("otherint"), 53); subKeys.insert(QLatin1String("otherint"), 53);
testMap.insert(QLatin1String("subkeys"), subKeys); testMap.insert(QLatin1String("subkeys"), subKeys);
subKeys.clear(); subKeys.clear();
testMap.insert(QLatin1String("subkeys2"), subKeys); testMap.insert(QLatin1String("subkeys2"), subKeys);
testMap.insert(QLatin1String("testint"), 23); testMap.insert(QLatin1String("testint"), 23);
subKeys.clear();
QVariantList list1;
list1.append(QLatin1String("ignore this"));
list1.append(QLatin1String("ignore this2"));
QVariantList list2;
list2.append(QLatin1String("somevalue"));
subKeys.insert(QLatin1String("findMe"), QLatin1String("FindInList"));
list2.append(subKeys);
list2.append(QLatin1String("someothervalue"));
list1.append(QVariant(list2));
testMap.insert(QLatin1String("aList"), list1);
QStringList result; QStringList result;
result = findValues(testMap, QVariant(23)); result = findValues(testMap, QVariant(23));
if (result.count() != 1 if (result.count() != 1
...@@ -117,25 +135,36 @@ bool FindValueOperation::test() const ...@@ -117,25 +135,36 @@ bool FindValueOperation::test() const
if (!result.isEmpty()) if (!result.isEmpty())
return false; return false;
result = findValues(testMap, QVariant(QString::fromLatin1("FindInList")));
if (result.count() != 1
|| !result.contains(QLatin1String("aList[2][1]/findMe")))
return false;
return true; return true;
} }
#endif #endif
QStringList FindValueOperation::findValues(const QVariantMap &map, const QVariant &value) QStringList FindValueOperation::findValues(const QVariant &in, const QVariant &value,
const QString &prefix)
{ {
QStringList result; QStringList result;
if (!value.isValid()) if (in.type() == value.type() && in == value) {
return result; result << prefix;
} else if (in.type() == QVariant::Map) {
for (QVariantMap::const_iterator i = map.begin(); i != map.end(); ++i) { QVariantMap map = in.toMap();
if (i.value() == value) for (QVariantMap::const_iterator i = map.begin(); i != map.end(); ++i) {
result << i.key(); QString pfx = prefix;
if (i.value().type() == QVariant::Map) { if (!pfx.isEmpty())
const QStringList subKeys = findValues(i.value().toMap(), value); pfx.append(QLatin1Char('/'));
foreach (const QString &subKey, subKeys) pfx.append(i.key());
result << i.key() + QLatin1Char('/') + subKey; result.append(findValues(i.value(), value, pfx));
}
} else if (in.type() == QVariant::List) {
QVariantList list = in.toList();
for (int pos = 0; pos < list.count(); ++pos) {
QString pfx = prefix + QLatin1Char('[') + QString::number(pos) + QLatin1String("]");
result.append(findValues(list.at(pos), value, pfx));
} }
} }
return result; return result;
} }
...@@ -47,7 +47,8 @@ public: ...@@ -47,7 +47,8 @@ public:
bool test() const; bool test() const;
#endif #endif
static QStringList findValues(const QVariantMap &map, const QVariant &value); static QStringList findValues(const QVariant &in, const QVariant &value,
const QString &prefix = QString());
private: private:
QString m_file; QString m_file;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment