Commit cffaa355 authored by Volker Krause's avatar Volker Krause
Browse files

Adapt sample parsing to the new schema code and JSON format

parent c85415cf
......@@ -18,6 +18,7 @@
#include "sample.h"
#include <core/product.h>
#include <core/schemaentry.h>
#include <core/schemaentryelement.h>
#include "ratioset.h"
#include <QDateTime>
......@@ -65,29 +66,53 @@ QVector<Sample> Sample::fromJson(const QByteArray &json, const Product &product)
foreach (const auto &entry, product.schema()) {
if (!obj.contains(entry.name()))
continue;
switch (entry.type()) {
case SchemaEntry::InvalidType:
break;
case SchemaEntry::StringType:
s.d->data.insert(entry.name(), obj.value(entry.name()).toString());
break;
case SchemaEntry::IntegerType:
s.d->data.insert(entry.name(), obj.value(entry.name()).toInt());
switch (entry.dataType()) {
case SchemaEntry::Scalar:
{
const auto entryData = obj.value(entry.name()).toObject();
foreach (const auto &elem, entry.elements()) {
if (!entryData.contains(elem.name()))
continue;
// TODO schema-dependent type conversion
s.d->data.insert(entry.name() + QLatin1Char('.') + elem.name(), entryData.value(elem.name()).toVariant());
}
break;
case SchemaEntry::StringListType:
}
case SchemaEntry::List:
{
QStringList l;
const auto a = obj.value(entry.name()).toArray();
l.reserve(a.size());
foreach (const auto &v, a)
l.push_back(v.toString());
const auto entryArray = obj.value(entry.name()).toArray();
QVariantList l;
l.reserve(entryArray.size());
foreach (const auto &entryDataValue, entryArray) {
const auto entryData = entryDataValue.toObject();
QVariantMap m;
foreach (const auto &elem, entry.elements()) {
if (!entryData.contains(elem.name()))
continue;
// TODO schema-dependent type conversion
m.insert(elem.name(), entryData.value(elem.name()).toVariant());
}
l.push_back(m);
}
s.d->data.insert(entry.name(), l);
break;
}
case SchemaEntry::RatioSetType:
case SchemaEntry::Map:
{
const auto set = RatioSet::fromJson(obj.value(entry.name()).toObject());
s.d->data.insert(entry.name(), QVariant::fromValue(set));
const auto entryMap = obj.value(entry.name()).toObject();
QVariantMap m;
for (auto it = entryMap.begin(); it != entryMap.end(); ++it) {
const auto entryData = it.value().toObject();
QVariantMap m2;
foreach (const auto &elem, entry.elements()) {
if (!entryData.contains(elem.name()))
continue;
// TODO schema-dependent type conversion
m2.insert(elem.name(), entryData.value(elem.name()).toVariant());
}
m.insert(it.key(), m2);
}
s.d->data.insert(entry.name(), m);
break;
}
}
......
[
{
"name": "newPropertyRatio",
"type": "map",
"aggregation": "ratioset",
"elements": [
{
"name": "property",
"type": "number"
}
]
}
]
......@@ -7,5 +7,6 @@
<file>screens.json</file>
<file>usageCount.json</file>
<file>usageTime.json</file>
<file>propertyRatio.json</file>
</qresource>
</RCC>
......@@ -48,14 +48,27 @@ private slots:
samples = Sample::fromJson(R"([{
"id": 42,
"timestampe": "2016-11-27 16:09:06",
"timestamp": "2016-11-27 16:09:06",
"platform": { "os": "linux", "version": "suse" },
"applicationVersion": { "value": "1.9.84" },
"screens": [ { "width": 1920, "height": 1200 }, { "width": 1920, "height": 1200 } ]
"screens": [ { "width": 1920, "height": 1200 }, { "width": 640, "height": 480 } ],
"newPropertyRatio": { "value1": { "property": 0.55 }, "value2": { "property": 0.45 } }
}])", p);
QCOMPARE(samples.size(), 1);
auto s = samples.at(0);
QCOMPARE(s.timestamp(), QDateTime(QDate(2016, 11, 27), QTime(16, 9, 6)));
QCOMPARE(s.value(QLatin1String("platform.os")).toString(), QLatin1String("linux"));
QCOMPARE(s.value(QLatin1String("platform.version")).toString(), QLatin1String("suse"));
QCOMPARE(s.value(QLatin1String("applicationVersion.value")).toString(), QLatin1String("1.9.84"));
auto screens = s.value(QLatin1String("screens")).toList();
QCOMPARE(screens.size(), 2);
QCOMPARE(screens.at(0).toMap().value(QLatin1String("width")).toInt(), 1920);
QCOMPARE(screens.at(1).toMap().value(QLatin1String("height")).toInt(), 480);
auto ratio = s.value(QLatin1String("newPropertyRatio")).toMap();
QCOMPARE(ratio.size(), 2);
QCOMPARE(ratio.value(QLatin1String("value1")).toMap().value(QLatin1String("property")).toDouble(), 0.55);
QCOMPARE(ratio.value(QLatin1String("value2")).toMap().value(QLatin1String("property")).toDouble(), 0.45);
}
};
......
Supports Markdown
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