File indexing completed on 2024-12-15 03:45:00
0001 /* 0002 SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: MIT 0005 */ 0006 0007 #include "schemaentry.h" 0008 #include "schemaentryelement.h" 0009 #include "util.h" 0010 0011 #include <QDebug> 0012 #include <QJsonArray> 0013 #include <QJsonObject> 0014 #include <QSharedData> 0015 0016 using namespace KUserFeedback::Console; 0017 0018 namespace KUserFeedback { 0019 namespace Console { 0020 0021 class SchemaEntryData : public QSharedData 0022 { 0023 public: 0024 QString name; 0025 int internalId = -1; 0026 SchemaEntry::DataType dataType = SchemaEntry::Scalar; 0027 QVector<SchemaEntryElement> elements; 0028 }; 0029 0030 static const struct { 0031 SchemaEntry::DataType type; 0032 const char *name; 0033 } data_types_table[] { 0034 { SchemaEntry::Scalar, "scalar" }, 0035 { SchemaEntry::List, "list" }, 0036 { SchemaEntry::Map, "map" } 0037 }; 0038 0039 } 0040 } 0041 0042 SchemaEntry::SchemaEntry() : d(new SchemaEntryData) {} 0043 SchemaEntry::SchemaEntry(const SchemaEntry&) = default; 0044 SchemaEntry::~SchemaEntry() = default; 0045 SchemaEntry& SchemaEntry::operator=(const SchemaEntry&) = default; 0046 0047 bool SchemaEntry::operator==(const SchemaEntry &other) const 0048 { 0049 return d->name == other.d->name 0050 && d->internalId == other.d->internalId 0051 && d->dataType == other.d->dataType 0052 && d->elements == other.d->elements; 0053 } 0054 0055 int SchemaEntry::internalId() const 0056 { 0057 return d->internalId; 0058 } 0059 0060 void SchemaEntry::setInternalId(int internalId) 0061 { 0062 d->internalId = internalId; 0063 } 0064 0065 QString SchemaEntry::name() const 0066 { 0067 return d->name; 0068 } 0069 0070 void SchemaEntry::setName(const QString& name) 0071 { 0072 d->name = name; 0073 } 0074 0075 SchemaEntry::DataType SchemaEntry::dataType() const 0076 { 0077 return d->dataType; 0078 } 0079 0080 void SchemaEntry::setDataType(SchemaEntry::DataType type) 0081 { 0082 d->dataType = type; 0083 } 0084 0085 QVector<SchemaEntryElement> SchemaEntry::elements() const 0086 { 0087 return d->elements; 0088 } 0089 0090 void SchemaEntry::setElements(const QVector<SchemaEntryElement> &elements) 0091 { 0092 d->elements = elements; 0093 } 0094 0095 SchemaEntryElement SchemaEntry::element(const QString& name) const 0096 { 0097 const auto it = std::find_if(d->elements.cbegin(), d->elements.cend(), [name](const auto &entry) { 0098 return entry.name() == name; 0099 }); 0100 if (it == d->elements.cend()) 0101 return {}; 0102 return *it; 0103 } 0104 0105 QJsonObject SchemaEntry::toJsonObject() const 0106 { 0107 QJsonObject obj; 0108 if (d->internalId >= 0) 0109 obj.insert(QStringLiteral("id"), d->internalId); 0110 obj.insert(QStringLiteral("name"), d->name); 0111 obj.insert(QStringLiteral("type"), QLatin1String(data_types_table[d->dataType].name)); 0112 0113 QJsonArray array; 0114 for (const auto &element : qAsConst(d->elements)) 0115 array.push_back(element.toJsonObject()); 0116 obj.insert(QStringLiteral("elements"), array); 0117 return obj; 0118 } 0119 0120 QVector<SchemaEntry> SchemaEntry::fromJson(const QJsonArray &array) 0121 { 0122 QVector<SchemaEntry> res; 0123 res.reserve(array.size()); 0124 0125 foreach (const auto &v, array) { 0126 const auto obj = v.toObject(); 0127 SchemaEntry entry; 0128 entry.setName(obj.value(QStringLiteral("name")).toString()); 0129 entry.setDataType(Util::stringToEnum<DataType>(obj.value(QLatin1String("type")).toString(), data_types_table)); 0130 entry.setElements(SchemaEntryElement::fromJson(obj.value(QLatin1String("elements")).toArray())); 0131 0132 res.push_back(entry); 0133 } 0134 0135 return res; 0136 } 0137 0138 #include "moc_schemaentry.cpp"