summaryrefslogtreecommitdiffstats
path: root/src/jsontools.cpp
blob: 2ffaa3933a0c0501b150309a990c075bd5665f8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include <stdexcept>
#include <QMap>
#include "jsontools.h"

namespace JsonTools
{

QMap<QString, QString> g_idToPathMap;

NoChildFoundException::NoChildFoundException(const QString &msg)
    : std::runtime_error(msg.toLatin1().data())
{}

RestCallException::RestCallException(const QString &msg)
    : std::runtime_error(msg.toLatin1().data())
{}

// Returns the object with the given key in the object. The
// object needs to have a "name" key, as such.
QJsonObject getObject(const QString &key, const QJsonObject object)
{
    const auto foundValue = object.value(key);
    if (foundValue.isUndefined())
        throw std::runtime_error("key not found: '" + key.toStdString() + "'");
    if (!foundValue.isObject())
        throw std::runtime_error("'" + key.toStdString() + "' is not an object!");
    return foundValue.toObject();
}

// Returns the array of the given key in the object
QJsonArray getArray(const QString &key, const QJsonObject object)
{
    const auto foundValue = object.value(key);
    if (foundValue.isUndefined())
        throw std::runtime_error("key not found: '" + key.toStdString() + "'");
    if (!foundValue.isArray())
        throw std::runtime_error("'" + key.toStdString() + "' is not an array!");
    return foundValue.toArray();
}

// Returns the value of the given key in the object. Same as
// object.value(), but throws an exception if not found.
QJsonValue getValue(const QString &key, const QJsonObject object)
{
    const auto foundValue = object.value(key);
    if (foundValue.isUndefined())
        throw std::runtime_error("key not found: '" + key.toStdString() + "'");
    return foundValue;
}

// Returns the value of the given key in the object as a string. Same as
// object.value(), but throws an exception if not found.
QString getString(const QString &key, const QJsonObject object)
{
    const auto foundValue = object.value(key);
    if (foundValue.isUndefined())
        throw std::runtime_error("key not found: '" + key.toStdString() + "'");
    if (!foundValue.isString())
        throw std::runtime_error("'" + key.toStdString() + "' is not a string!");
    return foundValue.toString();
}

QStringList getStringList(const QString &key, const QJsonObject object, bool required)
{
    const auto value = object[key];
    if (value.isUndefined()) {
        if (required)
            throw std::runtime_error("key not found: '" + key.toStdString() + "'");
        return {};
    }

    if (value.isString()) {
        return {value.toString()};
    } else if (value.isArray()) {
        QStringList strings;
        const QJsonArray array = value.toArray();
        for (const QJsonValue &element : array)
            strings.append(element.toString());
        return strings;
    } else {
        throw std::runtime_error("key is not string or array: '" + key.toStdString() + "'");
    }
}

QString resolvedPath(const QString &figmaId)
{
    if (!g_idToPathMap.contains(figmaId))  {
        // If the map doesn't contain figmaId, it means that we have never searched
        // for the object before. To get the path, you could call (before calling this
        // function): findChild({"id", figmaId}, m_document.object(), false) to add
        // the path to the map.
        return {};
    }
    return g_idToPathMap[figmaId];
}

bool resolvedHidden(const QString &figmaId)
{
    return resolvedPath(figmaId).contains("[hidden]");
}

void clearCache()
{
    g_idToPathMap.clear();
}

QJsonObject findChildWithKeyImpl(const QString &key
    , const QJsonObject &root
    , QStringList &currentPath)
{
    Q_ASSERT(!key.isEmpty());

    QJsonObject result;

    const bool visible = root.value("visible").toBool(true);
    currentPath.append(root["name"].toString() + (visible ? "" : " [hidden]"));

    const auto children = root.value("children").toArray();
    for (auto it = children.constBegin(); it != children.constEnd(); ++it) {
        const auto value = *it;
        if (!value.isObject())
            throw NoChildFoundException(QStringLiteral("expected only objects in array, but found ")
                + QString::number(value.type()) + ". Searched for: " + key);

        const QJsonObject object = value.toObject();
        if (object.contains(key)) {
            const QString figmaId = object["id"].toString("<no id>");
            if (!g_idToPathMap.contains(figmaId)) {
                // Store the path to all objects we searched for, for both
                // debugging and visibility purposes.
                const bool visible = object.value("visible").toBool(true);
                const QString currentPathString = currentPath.join(",");
                g_idToPathMap[figmaId] = currentPathString + "," + object["name"].toString()
                        + (visible ? "" : " [hidden]");
            }
            return object;
        }

        result = findChildWithKeyImpl(key, object, currentPath);
        if (!result.empty())
            return result;
    }

    currentPath.removeLast();
    if (result.isEmpty())
        throw NoChildFoundException(QStringLiteral("could not find Figma child with key: ") + key);

    return result;
}

void findChildrenImpl(const QStringList &keyValueList
    , const QJsonObject &root
    , bool firstOnly
    , QStringList &currentPath
    , QList<QJsonObject> &result)
{
    // Assert that the key-value list comes in pairs:
    Q_ASSERT(keyValueList.length() % 2 == 0);

    const bool visible = root.value("visible").toBool(true);
    currentPath.append(root["name"].toString() + (visible ? "" : " [hidden]"));

    const auto children = root.value("children").toArray();
    for (auto it = children.constBegin(); it != children.constEnd(); ++it) {
        const auto value = *it;
        if (!value.isObject())
            throw NoChildFoundException(QStringLiteral("expected only objects in array, but found ")
                + QString::number(value.type()) + ". Searched for: " + keyValueList.join(","));

        const QJsonObject object = value.toObject();

        for (int i = 0; i < keyValueList.length(); i += 2) {
            const auto key = keyValueList[i];
            const auto value = keyValueList[i + 1];
            const auto foundValue = object.value(key).toString();

            QRegularExpression re('^' + value + '$', QRegularExpression::CaseInsensitiveOption);
            if (!re.isValid())
                throw NoChildFoundException("value is not a valid regexp: " + foundValue);
            if (!re.match(foundValue).hasMatch())
                break;
            if (i == keyValueList.length() - 2) {
                // All key-value pairs were matched, so add the object to
                // the container. If firstOnly is set, we're done searching.
                const QString figmaId = object["id"].toString("<no id>");
                if (!g_idToPathMap.contains(figmaId)) {
                    // Store the path to all objects we searched for, for both
                    // debugging and visibility purposes.
                    const bool visible = object.value("visible").toBool(true);
                    const QString currentPathString = currentPath.join(",");
                    g_idToPathMap[figmaId] = currentPathString + "," + object["name"].toString()
                            + (visible ? "" : " [hidden]");
                }

                result.append(object);
                if (firstOnly)
                    return;
            }
        }

        findChildrenImpl(keyValueList, object, firstOnly, currentPath, result);
        if (firstOnly && result.size() == 1)
            return;
    }

    currentPath.removeLast();
}

/**
 * Search for a json object recursively inside root with the given key
 * value pairs.
*/
QJsonObject findChildImpl(const QStringList &keyValueList
    , const QJsonObject &root
    , bool warnOnDuplicates
    , QStringList &currentPath)
{
    QList<QJsonObject> result;
    const bool firstOnly = !warnOnDuplicates;

    findChildrenImpl(keyValueList, root, firstOnly, currentPath, result);

    if (result.isEmpty()) {
        Q_ASSERT(keyValueList.length() % 2 == 0);
        QStringList msg;
        for (int i = 0; i < keyValueList.count(); i += 2)
            msg << "'" + keyValueList[i] + ":" + keyValueList[i + 1] + "'";
        throw NoChildFoundException(QStringLiteral("could not find Figma child: ") + msg.join(","));
    }

    if (warnOnDuplicates && result.count() > 1) {
        QStringList msg;
        for (int i = 0; i < keyValueList.count(); i += 2)
            msg << "'" + keyValueList[i] + ":" + keyValueList[i + 1] + "'";
        qWarning().nospace().noquote() << "Warning, found duplicates for: " + msg.join(",");
        for (int i = 0; i < result.count(); ++i) {
            const QString figmaId = result[i]["id"].toString();
            const QString path = resolvedPath(figmaId);
            qWarning().nospace().noquote() << "Duplicate "
                << QString::number(i) << ": "
                << path << (i == 0 ? " [used]" : "");
        }
    }

    return result.first();
}

/**
 * Search for all json objects recursively inside root with the given key
 * value pairs.
*/
QList<QJsonObject> findChildren(const QStringList &keyValueList, const QJsonObject &root)
{
    QList<QJsonObject> result;
    QStringList currentPath;
    const QString pathToRoot = resolvedPath(root["id"].toString());
    if (!pathToRoot.isEmpty()) {
        currentPath = pathToRoot.split(',');
        currentPath.removeLast();
    }
    findChildrenImpl(keyValueList, root, false, currentPath, result);
    return result;
}

/**
 * Search for a json object recursively inside root with the given key
 * value pairs.
*/
QJsonObject findChild(const QStringList &keyValueList, const QJsonObject &root, bool warnOnDuplicates)
{
    QStringList currentPath;
    const QString pathToRoot = resolvedPath(root["id"].toString());
    if (!pathToRoot.isEmpty()) {
        currentPath = pathToRoot.split(',');
        currentPath.removeLast();
    }
    return findChildImpl(keyValueList, root, warnOnDuplicates, currentPath);
}

/**
 * Search for a json object recursively inside root that has the given
 * name path. There can be other objects in-between for each node in the
 * name path.
*/
QJsonObject findNamedChild(const QStringList &namePath, const QJsonObject &root, bool warnOnDuplicates)
{
    QStringList currentPath;
    QJsonObject child = root;
    const QString pathToRoot = resolvedPath(root["id"].toString());
    if (!pathToRoot.isEmpty()) {
        currentPath = pathToRoot.split(',');
        currentPath.removeLast();
    }

    for (const QString &name : namePath)
        child = findChildImpl({"name", name}, child, warnOnDuplicates, currentPath);

    return child;
}

/**
 * Search for a json object recursively inside root that has the given
 * key. Returns the first one found.
*/
QJsonObject findChildWithKey(const QString &key, const QJsonObject &root)
{
    QStringList currentPath;
    const QString pathToRoot = resolvedPath(root["id"].toString());
    if (!pathToRoot.isEmpty()) {
        currentPath = pathToRoot.split(',');
        currentPath.removeLast();
    }
    return findChildWithKeyImpl(key, root, currentPath);
}

bool modifyValue(const QString &figmaId, const QString &key, const QString& newValue, QJsonObject &root)
{
    // Note: The Qt JSON API is "read-only", which means that we cannot
    // directly change a value nested inside a object hierarchy as this will
    // cause the implicitly shared object to detatch. Instead we need to reassign
    // the changed objects recursively back to the root.
    // Note: this function will not search inside arrays, only objects.
    bool rootChanged = false;

    if (root.contains("figmaId")) {
        const QString id = root["figmaId"].toString();
        if (id == figmaId) {
            root[key] = newValue;
            return true;
        }
    }

    for (const auto &childKey : std::as_const(root).keys()) {
        QJsonValue childValue = root[childKey];
        if (!childValue.isObject())
            continue;
        QJsonObject childObject = childValue.toObject();
        const bool childChanged = modifyValue(figmaId, key, newValue, childObject);
        if (childChanged) {
            rootChanged = true;
            root[childKey] = childObject;
        }
    }

    return rootChanged;
}

} // namespace