diff options
author | Alexey Rusakov <Kitsune-Ral@users.sf.net> | 2022-05-31 18:58:27 +0200 |
---|---|---|
committer | Alexey Rusakov <Kitsune-Ral@users.sf.net> | 2022-05-31 18:58:27 +0200 |
commit | 21ae4eca4c06e500ec04a52ad42772bf8e8e9b6f (patch) | |
tree | 271fe41d8d9d0a5eecfd07d16973c30b4b1c9e8c /lib/converters.h | |
parent | 42811660094c88a4a1bfa8bd8ace5f4b148c246a (diff) | |
download | libquotient-21ae4eca4c06e500ec04a52ad42772bf8e8e9b6f.tar.gz libquotient-21ae4eca4c06e500ec04a52ad42772bf8e8e9b6f.zip |
Tweak QOlmAccount and data structures around
This is mainly to plug the definition of a string-to-variant map
for one-time keys (see
https://spec.matrix.org/v1.2/client-server-api/#key-algorithms) into
the CS API generated code (see the "shortcut OneTimeKeys" commit for
gtad.yaml); but along with it came considerable streamlining of code
in qolmaccount.cpp. Using std::variant to store that map also warranted
converters.h to gain support for that type (even wider than toJson()
that is already in dev - a non-trivial merge from dev is in order).
Diffstat (limited to 'lib/converters.h')
-rw-r--r-- | lib/converters.h | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/converters.h b/lib/converters.h index 5e3becb8..64a5cfb6 100644 --- a/lib/converters.h +++ b/lib/converters.h @@ -224,6 +224,26 @@ struct QUOTIENT_API JsonConverter<QVariant> { static QVariant load(const QJsonValue& jv); }; +template <typename... Ts> +inline QJsonValue toJson(const std::variant<Ts...>& v) +{ + // std::visit requires all overloads to return the same type - and + // QJsonValue is a perfect candidate for that same type (assuming that + // variants never occur on the top level in Matrix API) + return std::visit( + [](const auto& value) { return QJsonValue { toJson(value) }; }, v); +} + +template <typename T> +struct QUOTIENT_API JsonConverter<std::variant<QString, T>> { + static std::variant<QString, T> load(const QJsonValue& jv) + { + if (jv.isString()) + return fromJson<QString>(jv); + return fromJson<T>(jv); + } +}; + template <typename T> struct JsonConverter<Omittable<T>> { static QJsonValue dump(const Omittable<T>& from) |