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
testMap.insert(QLatin1String("testint"), 23);
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;
result = findKey(testMap, QLatin1String("missing"));
if (!result.isEmpty())
......@@ -112,22 +125,36 @@ bool FindKeyOperation::test() const
|| !result.contains(QLatin1String("testbool")))
return false;
result = findKey(testMap, QLatin1String("findMe"));
if (result.count() != 1
|| !result.contains(QLatin1String("aList[2][1]/findMe")))
return false;
return true;
}
#endif
QStringList FindKeyOperation::findKey(const QVariantMap &map, const QString &key)
QStringList FindKeyOperation::findKey(const QVariant &in, const QString &key, const QString &prefix)
{
QStringList result;
for (QVariantMap::const_iterator i = map.begin(); i != map.end(); ++i) {
if (i.key() == key) {
result << key;
continue;
if (in.type() == QVariant::Map) {
QVariantMap map = in.toMap();
for (QVariantMap::const_iterator i = map.begin(); i != map.end(); ++i) {
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) {
QStringList subKeyList = findKey(i.value().toMap(), key);
foreach (const QString &subKey, subKeyList)
result << i.key() + QLatin1Char('/') + subKey;
} 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(findKey(list.at(pos), key, pfx));
}
}
return result;
......
......@@ -47,7 +47,8 @@ public:
bool test() const;
#endif
static QStringList findKey(const QVariantMap &map, const QString &key);
static QStringList findKey(const QVariant &in, const QString &key,
const QString &prefix = QString());
private:
QString m_file;
......
......@@ -31,6 +31,10 @@
#include <iostream>
// <debug>
#include <QDebug>
// </debug>
QString FindValueOperation::name() const
{
return QLatin1String("find");
......@@ -95,12 +99,26 @@ bool FindValueOperation::test() const
cur.insert(QLatin1String("testint2"), 53);
subKeys.insert(QLatin1String("subsubkeys"), cur);
subKeys.insert(QLatin1String("testbool"), true);
subKeys.insert(QLatin1String("testbool2"), false);
subKeys.insert(QLatin1String("otherint"), 53);
testMap.insert(QLatin1String("subkeys"), subKeys);
subKeys.clear();
testMap.insert(QLatin1String("subkeys2"), subKeys);
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;
result = findValues(testMap, QVariant(23));
if (result.count() != 1
......@@ -117,25 +135,36 @@ bool FindValueOperation::test() const
if (!result.isEmpty())
return false;
result = findValues(testMap, QVariant(QString::fromLatin1("FindInList")));
if (result.count() != 1
|| !result.contains(QLatin1String("aList[2][1]/findMe")))
return false;
return true;
}
#endif
QStringList FindValueOperation::findValues(const QVariantMap &map, const QVariant &value)
QStringList FindValueOperation::findValues(const QVariant &in, const QVariant &value,
const QString &prefix)
{
QStringList result;
if (!value.isValid())
return result;
for (QVariantMap::const_iterator i = map.begin(); i != map.end(); ++i) {
if (i.value() == value)
result << i.key();
if (i.value().type() == QVariant::Map) {
const QStringList subKeys = findValues(i.value().toMap(), value);
foreach (const QString &subKey, subKeys)
result << i.key() + QLatin1Char('/') + subKey;
if (in.type() == value.type() && in == value) {
result << prefix;
} else if (in.type() == QVariant::Map) {
QVariantMap map = in.toMap();
for (QVariantMap::const_iterator i = map.begin(); i != map.end(); ++i) {
QString pfx = prefix;
if (!pfx.isEmpty())
pfx.append(QLatin1Char('/'));
pfx.append(i.key());
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;
}
......@@ -47,7 +47,8 @@ public:
bool test() const;
#endif
static QStringList findValues(const QVariantMap &map, const QVariant &value);
static QStringList findValues(const QVariant &in, const QVariant &value,
const QString &prefix = QString());
private:
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