From 4244cee8d5e0f760cccd2b45ad587670573ef03c Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Mon, 24 Sep 2018 19:20:10 +0900 Subject: Prepare for CS API 0.4.0 This commit consists of two parts: upgrading the API infrastructure and trivial but sweeping update to the generated files. 1. The API infrastructure (converters.h, *.mustache and some other non-generated files) now can deal with top-level JSON arrays and response inlining; better supports property maps; and gets some formatting fixes in generated code. 2. Generated files now use QJsonValue instead of QJsonObject as a default type to (un)marshall Matrix API data structures, to match the change in the infrastructure above This commit is still using the old Matrix API definitions, before CS API 0.4.0. Getting to CS API 0.4.0 will come next. --- lib/converters.h | 112 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 67 insertions(+), 45 deletions(-) (limited to 'lib/converters.h') diff --git a/lib/converters.h b/lib/converters.h index 7f78effe..70938ab9 100644 --- a/lib/converters.h +++ b/lib/converters.h @@ -22,6 +22,7 @@ #include #include // Includes +#include #include #include #include @@ -80,7 +81,7 @@ namespace QMatrixClient // non-explicit constructors. QJsonValue variantToJson(const QVariant& v); template - inline auto toJson(T&& var) + inline auto toJson(T&& /* const QVariant& or QVariant&& */ var) -> std::enable_if_t, QVariant>::value, QJsonValue> { @@ -136,18 +137,37 @@ namespace QMatrixClient return json; } + template + struct FromJsonObject + { + T operator()(const QJsonObject& jo) const { return T(jo); } + }; + template struct FromJson { - T operator()(const QJsonValue& jv) const { return T(jv); } + T operator()(const QJsonValue& jv) const + { + return FromJsonObject()(jv.toObject()); + } + T operator()(const QJsonDocument& jd) const + { + return FromJsonObject()(jd.object()); + } }; template - inline T fromJson(const QJsonValue& jv) + inline auto fromJson(const QJsonValue& jv) { return FromJson()(jv); } + template + inline auto fromJson(const QJsonDocument& jd) + { + return FromJson()(jd); + } + template <> struct FromJson { auto operator()(const QJsonValue& jv) const { return jv.toBool(); } @@ -194,14 +214,6 @@ namespace QMatrixClient } }; - template <> struct FromJson - { - auto operator()(const QJsonValue& jv) const - { - return jv.toObject(); - } - }; - template <> struct FromJson { auto operator()(const QJsonValue& jv) const @@ -223,31 +235,35 @@ namespace QMatrixClient QVariant operator()(const QJsonValue& jv) const; }; - template struct FromJson> + template + struct ArrayFromJson { - auto operator()(const QJsonValue& jv) const + auto operator()(const QJsonArray& ja) const { - using size_type = typename std::vector::size_type; - const auto jsonArray = jv.toArray(); - std::vector vect; vect.resize(size_type(jsonArray.size())); - std::transform(jsonArray.begin(), jsonArray.end(), - vect.begin(), FromJson()); + using size_type = typename VectorT::size_type; + VectorT vect; vect.resize(size_type(ja.size())); + std::transform(ja.begin(), ja.end(), + vect.begin(), FromJson()); return vect; } - }; - - template struct FromJson> - { auto operator()(const QJsonValue& jv) const { - const auto jsonArray = jv.toArray(); - QVector vect; vect.resize(jsonArray.size()); - std::transform(jsonArray.begin(), jsonArray.end(), - vect.begin(), FromJson()); - return vect; + return operator()(jv.toArray()); + } + auto operator()(const QJsonDocument& jd) const + { + return operator()(jd.array()); } }; + template + struct FromJson> : ArrayFromJson> + { }; + + template + struct FromJson> : ArrayFromJson> + { }; + template struct FromJson> { auto operator()(const QJsonValue& jv) const @@ -279,18 +295,36 @@ namespace QMatrixClient } }; - template struct FromJson> + template + struct HashMapFromJson { - auto operator()(const QJsonValue& jv) const + auto operator()(const QJsonObject& jo) const { - const auto json = jv.toObject(); - QHash h; h.reserve(json.size()); - for (auto it = json.begin(); it != json.end(); ++it) - h.insert(it.key(), fromJson(it.value())); + HashMapT h; h.reserve(jo.size()); + for (auto it = jo.begin(); it != jo.end(); ++it) + h[it.key()] = + fromJson(it.value()); return h; } + auto operator()(const QJsonValue& jv) const + { + return operator()(jv.toObject()); + } + auto operator()(const QJsonDocument& jd) const + { + return operator()(jd.object()); + } }; + template + struct FromJson> + : HashMapFromJson> + { }; + + template + struct FromJson> : HashMapFromJson> + { }; + #if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)) template <> struct FromJson> { @@ -298,18 +332,6 @@ namespace QMatrixClient }; #endif - template struct FromJson> - { - auto operator()(const QJsonValue& jv) const - { - const auto json = jv.toObject(); - std::unordered_map h; h.reserve(size_t(json.size())); - for (auto it = json.begin(); it != json.end(); ++it) - h.insert(std::make_pair(it.key(), fromJson(it.value()))); - return h; - } - }; - // Conditional insertion into a QJsonObject namespace _impl -- cgit v1.2.3