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 "schemaentryelement.h" 0008 #include "util.h" 0009 0010 #include <QJsonArray> 0011 #include <QJsonObject> 0012 #include <QSharedData> 0013 0014 using namespace KUserFeedback::Console; 0015 0016 namespace KUserFeedback { 0017 namespace Console { 0018 class SchemaEntryElementData : public QSharedData 0019 { 0020 public: 0021 QString name; 0022 SchemaEntryElement::Type type = SchemaEntryElement::String; 0023 }; 0024 0025 static const struct { 0026 SchemaEntryElement::Type type; 0027 const char *name; 0028 } element_type_table[] { 0029 { SchemaEntryElement::Integer, "int" }, 0030 { SchemaEntryElement::Number, "number" }, 0031 { SchemaEntryElement::String, "string" }, 0032 { SchemaEntryElement::Boolean, "bool" } 0033 }; 0034 0035 } 0036 } 0037 0038 SchemaEntryElement::SchemaEntryElement() : 0039 d(new SchemaEntryElementData) 0040 { 0041 } 0042 0043 SchemaEntryElement::SchemaEntryElement(const SchemaEntryElement&) = default; 0044 SchemaEntryElement::~SchemaEntryElement() = default; 0045 SchemaEntryElement& SchemaEntryElement::operator=(const SchemaEntryElement&) = default; 0046 0047 bool SchemaEntryElement::operator==(const SchemaEntryElement& other) const 0048 { 0049 return d->name == other.d->name 0050 && d->type == other.d->type; 0051 } 0052 0053 QString SchemaEntryElement::name() const 0054 { 0055 return d->name; 0056 } 0057 0058 void SchemaEntryElement::setName(const QString& name) 0059 { 0060 d->name = name; 0061 } 0062 0063 SchemaEntryElement::Type SchemaEntryElement::type() const 0064 { 0065 return d->type; 0066 } 0067 0068 void SchemaEntryElement::setType(SchemaEntryElement::Type type) 0069 { 0070 d->type = type; 0071 } 0072 0073 QJsonObject SchemaEntryElement::toJsonObject() const 0074 { 0075 QJsonObject obj; 0076 obj.insert(QStringLiteral("name"), d->name); 0077 obj.insert(QStringLiteral("type"), QLatin1String(element_type_table[d->type].name)); 0078 return obj; 0079 } 0080 0081 QVector<SchemaEntryElement> SchemaEntryElement::fromJson(const QJsonArray& array) 0082 { 0083 QVector<SchemaEntryElement> res; 0084 res.reserve(array.size()); 0085 0086 foreach (const auto &v, array) { 0087 const auto obj = v.toObject(); 0088 SchemaEntryElement e; 0089 e.setName(obj.value(QLatin1String("name")).toString()); 0090 e.setType(Util::stringToEnum<SchemaEntryElement::Type>(obj.value(QLatin1String("type")).toString(), element_type_table)); 0091 res.push_back(e); 0092 } 0093 0094 return res; 0095 } 0096 0097 #include "moc_schemaentryelement.cpp"