aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2018-04-14 14:10:33 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2018-04-14 14:10:33 +0900
commite5cb9b6380040e40de1b3766ac90296d3828375a (patch)
tree43f10d985de1ef2dae7c29b18be74ffa21e0808a
parent07d2b179bb08c4b941eca3f20b9ef31740ad95bd (diff)
downloadlibquotient-e5cb9b6380040e40de1b3766ac90296d3828375a.tar.gz
libquotient-e5cb9b6380040e40de1b3766ac90296d3828375a.zip
Mustache templates: Overload toJson() instead of operator QJsonObject()/QJsonValue()
It slightly reduces the header interface and shortcuts the actual call chain (not that it had any performance implications, just easier reasoning).
-rw-r--r--lib/jobs/{{base}}.cpp.mustache36
-rw-r--r--lib/jobs/{{base}}.h.mustache4
2 files changed, 23 insertions, 17 deletions
diff --git a/lib/jobs/{{base}}.cpp.mustache b/lib/jobs/{{base}}.cpp.mustache
index 5e94dcf0..e44e6a2a 100644
--- a/lib/jobs/{{base}}.cpp.mustache
+++ b/lib/jobs/{{base}}.cpp.mustache
@@ -8,12 +8,15 @@
{{/operations}}
using namespace QMatrixClient;
{{#models.model}}{{^trivial?}}
-{{qualifiedName}}::operator QJsonValue() const
+namespace QMatrixClient
{
- QJsonObject o;
- {{#vars}}o.insert("{{baseName}}", toJson({{nameCamelCase}}));
- {{/vars}}
- return o;
+ QJsonObject toJson(const {{qualifiedName}}& pod)
+ {
+ QJsonObject o;
+ {{#vars}}o.insert("{{baseName}}", toJson(pod.{{nameCamelCase}}));
+ {{/vars}}
+ return o;
+ }
}
{{qualifiedName}} FromJson<{{qualifiedName}}>::operator()(QJsonValue jv)
@@ -28,15 +31,16 @@ using namespace QMatrixClient;
{{/trivial?}}{{/models.model}}{{#operations}}
static const auto basePath = QStringLiteral("{{basePathWithoutHost}}");
{{# operation}}{{#models.model}}{{^trivial?}}
-{{qualifiedName}}::operator QJsonObject() const
-{
- QJsonObject o;
- {{#vars}}o.insert("{{baseName}}", toJson({{nameCamelCase}}));
- {{/vars}}
- return o;
-}
namespace QMatrixClient
{
+ QJsonObject toJson(const {{qualifiedName}}& pod)
+ {
+ QJsonObject o;
+ {{#vars}}o.insert("{{baseName}}", toJson(pod.{{nameCamelCase}}));
+ {{/vars}}
+ return o;
+ }
+
template <> struct FromJson<{{qualifiedName}}>
{
{{qualifiedName}} operator()(QJsonValue jv)
@@ -81,9 +85,11 @@ QUrl {{camelCaseOperationId}}Job::makeRequestUrl(QUrl baseUrl{{#allParams?}}, {{
{
{{#headerParams?}}{{#headerParams}} setRequestHeader("{{baseName}}", {{paramName}}.toLatin1());
{{/headerParams}}
-{{/headerParams?}}{{!
-}}{{#bodyParams?}}{{!
-}}{{#inlineBody}} setRequestData(Data({{nameCamelCase}}));{{/inlineBody}}{{!
+{{/headerParams?
+}}{{#bodyParams?
+}}{{#inlineBody}} setRequestData(Data({{!
+ }}{{#consumesNonJson?}}{{nameCamelCase}}{{/consumesNonJson?
+ }}{{^consumesNonJson?}}toJson({{nameCamelCase}}){{/consumesNonJson?}}));{{/inlineBody
}}{{^inlineBody}} QJsonObject _data;{{#bodyParams}}
{{^required?}}{{#string?}} if (!{{paramName}}.isEmpty())
{{/string?}}{{/required?}} _data.insert("{{baseName}}", toJson({{paramName}}));{{/bodyParams}}
diff --git a/lib/jobs/{{base}}.h.mustache b/lib/jobs/{{base}}.h.mustache
index 12633604..8606e251 100644
--- a/lib/jobs/{{base}}.h.mustache
+++ b/lib/jobs/{{base}}.h.mustache
@@ -17,9 +17,10 @@ namespace QMatrixClient
{
{{#vars}}{{dataType.name}} {{nameCamelCase}};
{{/vars}}
- operator QJsonObject() const;
};
+ QJsonObject toJson(const {{name}}& pod);
+
template <> struct FromJson<{{name}}>
{
{{name}} operator()(QJsonValue jv);
@@ -38,7 +39,6 @@ namespace QMatrixClient
{
{{#vars}}{{dataType.name}} {{nameCamelCase}};
{{/vars}}
- operator QJsonObject() const;
};
{{/ trivial?}}{{/model}}
// End of inner data structures