From 6767cb8eccea7b74531f59f165a28afa0bec61f4 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 8 Jun 2018 23:24:51 +0900 Subject: csapi: Fix boolean query parameters incorrectly passed --- lib/converters.h | 63 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 12 deletions(-) (limited to 'lib/converters.h') diff --git a/lib/converters.h b/lib/converters.h index ff06e232..711d0a7c 100644 --- a/lib/converters.h +++ b/lib/converters.h @@ -21,9 +21,11 @@ #include #include // Includes #include +#include #include #include +#include #if 0 // Waiting for C++17 #include @@ -332,25 +334,39 @@ namespace QMatrixClient namespace _impl { // This one is for types that don't have isEmpty() - template + template struct AddNode { - static void impl(QJsonObject& o, QString key, JsonT&& value) + static void impl(InserterT inserter, QString key, JsonT&& value) { - o.insert(std::move(key), std::forward(value)); + inserter(std::move(key), std::forward(value)); } }; // This one is for types that have isEmpty() - template - struct AddNode().isEmpty())> + template + struct AddNode().isEmpty())> { - static void impl(QJsonObject& o, QString key, JsonT&& value) + static void impl(InserterT inserter, QString key, JsonT&& value) { if (!value.isEmpty()) - o.insert(std::move(key), std::forward(value)); + inserter(std::move(key), std::forward(value)); } }; + + template + inline void maybeAdd(InserterT inserter, QString key, ValT&& value) + { + auto&& json = toJson(std::forward(value)); + // QJsonValue doesn't have isEmpty and consumes all other types + // (QString, QJsonObject etc.). + AddNode> + ::impl(inserter, std::move(key), std::move(json)); + + } + } // namespace _impl static constexpr bool IfNotEmpty = false; @@ -358,10 +374,33 @@ namespace QMatrixClient template inline void addToJson(QJsonObject& o, QString key, ValT&& value) { - auto&& json = toJson(std::forward(value)); - // QJsonValue doesn't have isEmpty and consumes all other types - // (QString, QJsonObject etc.). - _impl::AddNode> - ::impl(o, std::move(key), std::move(json)); + using namespace std::placeholders; + _impl::maybeAdd(bind(&QJsonObject::insert, o, _1, _2), + key, value); + } + + template + inline void addToQuery(QUrlQuery& query, QString key, ValT&& value) + { + using namespace std::placeholders; + _impl::maybeAdd( + [&query] (QString k, auto&& jsonValue) { + query.addQueryItem(k, jsonValue.toString()); + }, key, value); + } + + template + inline void addToQuery(QUrlQuery& query, QString key, QString value) + { + if (Force || !value.isEmpty()) + query.addQueryItem(key, value); } + + template + inline void addToQuery(QUrlQuery& query, QString key, bool value) + { + query.addQueryItem(key, + value ? QStringLiteral("true") : QStringLiteral("false")); + } + } // namespace QMatrixClient -- cgit v1.2.3