forked from qt-creator/qt-creator
SDKtool: Handle lists of values in find and findKey operations
Change-Id: Ifb5e322f5ddc62f5f5cc3d464faf592b05195603 Reviewed-by: Tobias Hunger <tobias.hunger@digia.com>
This commit is contained in:
@@ -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
|
|||||||
|| !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;
|
||||||
|
if (in.type() == QVariant::Map) {
|
||||||
|
QVariantMap map = in.toMap();
|
||||||
for (QVariantMap::const_iterator i = map.begin(); i != map.end(); ++i) {
|
for (QVariantMap::const_iterator i = map.begin(); i != map.end(); ++i) {
|
||||||
|
QString pfx = prefix;
|
||||||
|
if (!pfx.isEmpty())
|
||||||
|
pfx.append(QLatin1Char('/'));
|
||||||
if (i.key() == key) {
|
if (i.key() == key) {
|
||||||
result << key;
|
result << pfx + key;
|
||||||
continue;
|
} 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);
|
} else if (in.type() == QVariant::List) {
|
||||||
foreach (const QString &subKey, subKeyList)
|
QVariantList list = in.toList();
|
||||||
result << i.key() + QLatin1Char('/') + subKey;
|
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;
|
return result;
|
||||||
|
|||||||
@@ -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 @@
|
|||||||
|
|
||||||
#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
|
|||||||
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
|
|||||||
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) {
|
||||||
|
QVariantMap map = in.toMap();
|
||||||
for (QVariantMap::const_iterator i = map.begin(); i != map.end(); ++i) {
|
for (QVariantMap::const_iterator i = map.begin(); i != map.end(); ++i) {
|
||||||
if (i.value() == value)
|
QString pfx = prefix;
|
||||||
result << i.key();
|
if (!pfx.isEmpty())
|
||||||
if (i.value().type() == QVariant::Map) {
|
pfx.append(QLatin1Char('/'));
|
||||||
const QStringList subKeys = findValues(i.value().toMap(), value);
|
pfx.append(i.key());
|
||||||
foreach (const QString &subKey, subKeys)
|
result.append(findValues(i.value(), value, pfx));
|
||||||
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(findValues(list.at(pos), value, pfx));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user