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:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user