aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libqmenumodel/src/converter.cpp60
-rw-r--r--tests/client/convertertest.cpp26
2 files changed, 35 insertions, 51 deletions
diff --git a/libqmenumodel/src/converter.cpp b/libqmenumodel/src/converter.cpp
index 3131d4f..aef34d0 100644
--- a/libqmenumodel/src/converter.cpp
+++ b/libqmenumodel/src/converter.cpp
@@ -303,69 +303,53 @@ GVariant* Converter::toGVariantWithSchema(const QVariant &value, const char* sch
}
} else if (g_variant_type_is_array(schema_type)) {
if (value.canConvert(QVariant::List)) {
+ const GVariantType* entryType = g_variant_type_element(schema_type);
+ const gchar* entryTypeString = g_variant_type_peek_string(entryType);
- const GVariantType* entry_type;
- GVariant* data;
- entry_type = g_variant_type_element(schema_type);
- const gchar* entryTypeString = g_variant_type_peek_string(entry_type);
-
- const QVariantList &lst = value.toList();
- GVariant **vars = g_new(GVariant*, lst.size());
-
+ GVariantBuilder *b = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
bool ok = true;
- for (int i=0, iMax=lst.size(); i < iMax; i++) {
- data = Converter::toGVariantWithSchema(lst.at(i), entryTypeString);
+
+ for (const QVariant &v : value.toList()) {
+ GVariant *data = toGVariantWithSchema(v, entryTypeString);
if (data) {
- vars[i] = data;
- }
- else {
+ g_variant_builder_add_value(b, data);
+ } else {
ok = false;
qWarning() << "Failed to convert list to array with schema:" << schema;
break;
}
}
if (ok) {
- result = g_variant_new_array(entry_type, vars, lst.size());
+ result = g_variant_builder_end(b);
}
- g_free(vars);
+ g_variant_builder_unref(b);
}
} else if (g_variant_type_is_tuple(schema_type)) {
if (value.canConvert(QVariant::List)) {
- GVariant* data;
-
- QVariantList lst = value.toList();
- GVariant **vars = g_new(GVariant*, lst.size());
-
- const GVariantType* entry_type = g_variant_type_first(schema_type);
+ const GVariantType* entryType = g_variant_type_first(schema_type);
+ GVariantBuilder *b = g_variant_builder_new(G_VARIANT_TYPE_TUPLE);
bool ok = true;
- for (int i=0,iMax=lst.size(); i < iMax; i++) {
-
- gchar* entryTypeString = g_variant_type_dup_string(entry_type);
- data = Converter::toGVariantWithSchema(lst.at(i), entryTypeString);
+ for (const QVariant &v : value.toList()) {
+ gchar* entryTypeString = g_variant_type_dup_string(entryType);
+ GVariant *data = toGVariantWithSchema(v, entryTypeString);
+ g_free(entryTypeString);
if (data) {
- vars[i] = data;
- }
- else {
+ g_variant_builder_add_value(b, data);
+ entryType = g_variant_type_next(entryType);
+ } else {
ok = false;
- qWarning() << "Failed to convert list to tuple with schema:" << schema;
- g_free(entryTypeString);
- break;
- }
- g_free(entryTypeString);
-
- entry_type = g_variant_type_next(entry_type);
- if (!entry_type) {
+ qWarning() << "Failed to convert list to array with schema:" << schema;
break;
}
}
if (ok) {
- result = g_variant_new_tuple(vars, lst.size());
+ result = g_variant_builder_end(b);
}
- g_free(vars);
+ g_variant_builder_unref(b);
}
}
diff --git a/tests/client/convertertest.cpp b/tests/client/convertertest.cpp
index 32d6091..5a9367f 100644
--- a/tests/client/convertertest.cpp
+++ b/tests/client/convertertest.cpp
@@ -295,38 +295,38 @@ private Q_SLOTS:
void testSchemaConvert()
{
// convert to integer
- compareWithSchema(QVariant::fromValue<int>(1), "i");
- compareWithSchema(QVariant::fromValue<double>(1.1), "i");
+ QVERIFY(compareWithSchema(QVariant::fromValue<int>(1), "i"));
+ QVERIFY(compareWithSchema(QVariant::fromValue<double>(1.1), "i"));
// convert to integer
- compareWithSchema(QVariant::fromValue<bool>(true), "b");
- compareWithSchema(QVariant::fromValue<int>(1), "b");
+ QVERIFY(compareWithSchema(QVariant::fromValue<bool>(true), "b"));
+ QVERIFY(compareWithSchema(QVariant::fromValue<int>(1), "b"));
// convert to double
- compareWithSchema(QVariant::fromValue<double>(1.0), "d");
- compareWithSchema(QVariant::fromValue<int>(1), "d");
+ QVERIFY(compareWithSchema(QVariant::fromValue<double>(1.0), "d"));
+ QVERIFY(compareWithSchema(QVariant::fromValue<int>(1), "d"));
// convert to string
- compareWithSchema(QVariant::fromValue<int>(1), "s");
- compareWithSchema(QVariant::fromValue<double>(1.1), "s");
+ QVERIFY(compareWithSchema(QVariant::fromValue<int>(1), "s"));
+ QVERIFY(compareWithSchema(QVariant::fromValue<double>(1.1), "s"));
// convert to tuple
- compareWithSchema(QVariantList() << QVariant::fromValue<bool>(true) << QVariant::fromValue<int>(1) << QVariant::fromValue<int>(1) << QVariant::fromValue<QString>("test1"), "(bdis)");
+ QVERIFY(compareWithSchema(QVariantList() << QVariant::fromValue<bool>(true) << QVariant::fromValue<int>(1) << QVariant::fromValue<int>(1) << QVariant::fromValue<QString>("test1"), "(bdis)"));
// convert to array
- compareWithSchema(QVariantList() << QVariant::fromValue<int>(1) << QVariant::fromValue<int>(1), "ad");
- compareWithSchema(QVariantList() << QVariant::fromValue<QString>("test1") << QVariant::fromValue<QString>("test2"), "as");
+ QVERIFY(compareWithSchema(QVariantList() << QVariant::fromValue<int>(1) << QVariant::fromValue<int>(1), "ad"));
+ QVERIFY(compareWithSchema(QVariantList() << QVariant::fromValue<QString>("test1") << QVariant::fromValue<QString>("test2"), "as"));
// convert to array of tuple
QVariantList si1(QVariantList() << QVariant::fromValue<QString>("test1") << QVariant::fromValue<int>(1));
QVariantList si2(QVariantList() << QVariant::fromValue<QString>("test1") << QVariant::fromValue<int>(1));
- compareWithSchema(QVariantList() << QVariant::fromValue(si1) << QVariant::fromValue(si2), "a(sd)");
+ QVERIFY(compareWithSchema(QVariantList() << QVariant::fromValue(si1) << QVariant::fromValue(si2), "a(sd)"));
// convert to vardict
QVariantMap map;
map["test1"] = QVariant::fromValue<int>(1);
map["test2"] = QVariant::fromValue<double>(1);
- compareWithSchema(map, "a{sv}");
+ QVERIFY(compareWithSchema(map, "a{sv}"));
}
};