File indexing completed on 2024-12-15 03:44:59

0001 /*
0002     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #include "aggregation.h"
0008 #include "util.h"
0009 
0010 #include <QJsonArray>
0011 #include <QJsonObject>
0012 
0013 using namespace KUserFeedback::Console;
0014 
0015 static const struct {
0016     Aggregation::Type type;
0017     const char *name;
0018 } aggregation_types_table[] {
0019     { Aggregation::None, "none" },
0020     { Aggregation::Category, "category" },
0021     { Aggregation::RatioSet, "ratio_set" },
0022     { Aggregation::Numeric, "numeric" }
0023 };
0024 
0025 Aggregation::Aggregation() = default;
0026 Aggregation::~Aggregation() = default;
0027 
0028 bool Aggregation::isValid() const
0029 {
0030     return m_type != None && m_elements.size() > 0;
0031 }
0032 
0033 Aggregation::Type Aggregation::type() const
0034 {
0035     return m_type;
0036 }
0037 
0038 void Aggregation::setType(Aggregation::Type t)
0039 {
0040     m_type = t;
0041 }
0042 
0043 QString Aggregation::name() const
0044 {
0045     return m_name;
0046 }
0047 
0048 void Aggregation::setName(const QString& name)
0049 {
0050     m_name = name;
0051 }
0052 
0053 QVector<AggregationElement> Aggregation::elements() const
0054 {
0055     return m_elements;
0056 }
0057 
0058 void Aggregation::setElements(const QVector<AggregationElement>& elements)
0059 {
0060     m_elements = elements;
0061 }
0062 
0063 QJsonObject Aggregation::toJsonObject() const
0064 {
0065     QJsonObject obj;
0066     obj.insert(QStringLiteral("type"), QLatin1String(aggregation_types_table[m_type].name));
0067     obj.insert(QStringLiteral("name"), m_name);
0068     QJsonArray elems;
0069     for (const auto &e : m_elements)
0070         elems.push_back(e.toJsonObject());
0071     obj.insert(QStringLiteral("elements"), elems);
0072     return obj;
0073 }
0074 
0075 QVector<Aggregation> Aggregation::fromJson(const Product &product, const QJsonArray& a)
0076 {
0077     QVector<Aggregation> aggrs;
0078     aggrs.reserve(a.size());
0079     for (const auto &v : a) {
0080         if (!v.isObject())
0081             continue;
0082         const auto obj = v.toObject();
0083 
0084         Aggregation aggr;
0085         aggr.setType(Util::stringToEnum<Aggregation::Type>(obj.value(QLatin1String("type")).toString(), aggregation_types_table));
0086         aggr.setName(obj.value(QLatin1String("name")).toString());
0087         aggr.setElements(AggregationElement::fromJson(product, obj.value(QLatin1String("elements")).toArray()));
0088         aggrs.push_back(aggr);
0089     }
0090     return aggrs;
0091 }
0092 
0093 #include "moc_aggregation.cpp"