Kgapi

externalid.cpp
1/*
2 * SPDX-FileCopyrightText: 2021 Daniel Vrátil <dvratil@kde.org>
3 * SPDX-FileCopyrightText: 2022 Claudio Cambra <claudio.cambra@kde.org>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only
6 * SPDX-License-Identifier: LGPL-3.0-only
7 * SPDX-License-Identifier: LicenseRef-KDE-Accepted-LGPL
8 */
9
10#include "externalid.h"
11
12#include "fieldmetadata.h"
13#include "peopleservice.h"
14
15#include <QJsonArray>
16#include <QJsonObject>
17#include <QJsonValue>
18#include <QSharedData>
19
20#include <algorithm>
21
22namespace KGAPI2::People
23{
24class ExternalId::Private : public QSharedData
25{
26public:
27 explicit Private() = default;
28 Private(const Private &) = default;
29 Private(Private &&) noexcept = delete;
30 Private &operator=(const Private &) = delete;
31 Private &operator=(Private &&) noexcept = delete;
32 ~Private() = default;
33
34 bool operator==(const Private &other) const
35 {
36 return type == other.type && formattedType == other.formattedType && value == other.value && metadata == other.metadata;
37 }
38
39 bool operator!=(const Private &other) const
40 {
41 return !(*this == other);
42 }
43
44 QString type{};
45 QString formattedType{};
46 QString value{};
47 FieldMetadata metadata{};
48};
49
51 : d(new Private)
52{
53}
54
55ExternalId::ExternalId(const ExternalId &) = default;
56ExternalId::ExternalId(ExternalId &&) noexcept = default;
57ExternalId &ExternalId::operator=(const ExternalId &) = default;
58ExternalId &ExternalId::operator=(ExternalId &&) noexcept = default;
59ExternalId::~ExternalId() = default;
60
61bool ExternalId::operator==(const ExternalId &other) const
62{
63 return *d == *other.d;
64}
65
66bool ExternalId::operator!=(const ExternalId &other) const
67{
68 return !(*this == other);
69}
70
72{
73 return d->type;
74}
75
76void ExternalId::setType(const QString &value)
77{
78 d->type = value;
79}
81{
82 return d->formattedType;
83}
85{
86 return d->value;
87}
88
89void ExternalId::setValue(const QString &value)
90{
91 d->value = value;
92}
94{
95 return d->metadata;
96}
97
99{
100 d->metadata = value;
101}
102
103ExternalId ExternalId::fromJSON(const QJsonObject &obj)
104{
105 ExternalId externalId;
106
107 if(!obj.isEmpty()) {
108 const auto metadata = obj.value(QStringLiteral("metadata")).toObject();
109 externalId.d->metadata = FieldMetadata::fromJSON(metadata);
110 externalId.d->value = obj.value(QStringLiteral("value")).toString();
111 externalId.d->type = obj.value(QStringLiteral("type")).toString();
112 externalId.d->formattedType = obj.value(QStringLiteral("formattedType")).toString();
113 }
114
115 return externalId;
116}
117
118QList<ExternalId> ExternalId::fromJSONArray(const QJsonArray &data)
119{
120 QList<ExternalId> externalIds;
121
122 for(const auto &externalId : data) {
123 if(externalId.isObject()) {
124 const auto objectifiedExternalId = externalId.toObject();
125 externalIds.append(fromJSON(objectifiedExternalId));
126 }
127 }
128
129 return externalIds;
130}
131
132QJsonValue ExternalId::toJSON() const
133{
134 QJsonObject obj;
135
136 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", d->type);
137 // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "formattedType", d->formattedType);
138 PeopleUtils::addValueToJsonObjectIfValid(obj, "value", d->value);
139 // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata", d->metadata.toJSON());
140 return obj;
141}
142
143} // namespace KGAPI2::People
An identifier from an external entity related to the person.
Definition externalid.h:34
void setMetadata(const FieldMetadata &value)
Sets value of the metadata property.
FieldMetadata metadata() const
Metadata about the external ID.
QString formattedType() const
Output only.
ExternalId()
Constructs a new ExternalId.
QString value() const
The value of the external ID.
void setValue(const QString &value)
Sets value of the value property.
void setType(const QString &value)
Sets value of the type property.
QString type() const
The type of the external ID.
Metadata about a field.
bool isEmpty() const const
QJsonValue value(QLatin1StringView key) const const
QJsonObject toObject() const const
QString toString() const const
void append(QList< T > &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:00:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.