From 3392e66fd015e191b01f6e3fc6839edc3948e31f Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sat, 8 Dec 2018 15:36:04 +0900 Subject: Refactor toJson/fillJson Both now use through a common JsonConverter<> template class with its base definition tuned for structs/QJsonObjects and specialisations for non-object types. This new implementation doesn't work with virtual fillJson functions yet (so EventContent classes still use toJson as a member function) and does not cope quite well with non-constructible objects (you have to specialise JsonConverter<> rather than, more intuitively, JsonObjectConverter<>), but overall is more streamlined compared to the previous implementation. It also fixes one important issue that pushed for a rewrite: the previous implementation was not working with structure hierarchies at all so (in particular) the Filter part of CS API was totally disfunctional. --- lib/csapi/{{base}}.cpp.mustache | 69 +++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 33 deletions(-) (limited to 'lib/csapi/{{base}}.cpp.mustache') diff --git a/lib/csapi/{{base}}.cpp.mustache b/lib/csapi/{{base}}.cpp.mustache index 64fd8bf3..ff888d76 100644 --- a/lib/csapi/{{base}}.cpp.mustache +++ b/lib/csapi/{{base}}.cpp.mustache @@ -8,49 +8,52 @@ {{/operations}} using namespace QMatrixClient; {{#models.model}}{{#in?}} -QJsonObject QMatrixClient::toJson(const {{qualifiedName}}& pod) +void JsonObjectConverter<{{qualifiedName}}>::dumpTo( + QJsonObject& jo, const {{qualifiedName}}& pod) { - QJsonObject jo{{#propertyMap}} = toJson(pod.{{nameCamelCase}}){{/propertyMap}};{{#vars}} - addParam<{{^required?}}IfNotEmpty{{/required?}}>(jo, QStringLiteral("{{baseName}}"), pod.{{nameCamelCase}});{{/vars}} - return jo; -} +{{#propertyMap}} fillJson(jo, pod.{{nameCamelCase}}); +{{/propertyMap}}{{#parents}} fillJson<{{name}}>(jo, pod); +{{/parents}}{{#vars}} addParam<{{^required?}}IfNotEmpty{{/required?}}>(jo, QStringLiteral("{{baseName}}"), pod.{{nameCamelCase}}); +{{/vars}}}{{!<- dumpTo() ends here}} {{/in?}}{{#out?}} -{{qualifiedName}} FromJsonObject<{{qualifiedName}}>::operator()({{^propertyMap}}const QJsonObject&{{/propertyMap}}{{#propertyMap}}QJsonObject{{/propertyMap}} jo) const +void JsonObjectConverter<{{qualifiedName}}>::fillFrom( + {{^propertyMap}}const QJsonObject&{{/propertyMap + }}{{#propertyMap}}QJsonObject{{/propertyMap}} jo, {{qualifiedName}}& result) { - {{qualifiedName}} result; -{{#vars}} result.{{nameCamelCase}} = - fromJson<{{dataType.qualifiedName}}>(jo.{{#propertyMap}}take{{/propertyMap}}{{^propertyMap}}value{{/propertyMap}}("{{baseName}}"_ls)); +{{#parents}} fillFromJson<{{qualifiedName}}>(jo, result); +{{/parents}}{{#vars}} fromJson(jo.{{#propertyMap}}take{{/propertyMap + }}{{^propertyMap}}value{{/propertyMap}}("{{baseName}}"_ls), result.{{nameCamelCase}}); {{/vars}}{{#propertyMap}} - result.{{nameCamelCase}} = fromJson<{{dataType.qualifiedName}}>(jo);{{/propertyMap}} - return result; -} + fromJson(jo, result.{{nameCamelCase}}); +{{/propertyMap}}} {{/out?}}{{/models.model}}{{#operations}} static const auto basePath = QStringLiteral("{{basePathWithoutHost}}"); {{# operation}}{{#models}} namespace QMatrixClient { // Converters -{{#model}}{{#in?}} - QJsonObject toJson(const {{qualifiedName}}& pod) - { - QJsonObject jo{{#propertyMap}} = toJson(pod.{{nameCamelCase}}){{/propertyMap}};{{#vars}} - addParam<{{^required?}}IfNotEmpty{{/required?}}>(jo, QStringLiteral("{{baseName}}"), pod.{{nameCamelCase}});{{/vars}} - return jo; - } -{{/in?}}{{#out?}} - template <> struct FromJsonObject<{{qualifiedName}}> +{{#model}} + template <> struct JsonObjectConverter<{{qualifiedName}}> { - {{qualifiedName}} operator()({{^propertyMap}}const QJsonObject&{{/propertyMap}}{{#propertyMap}}QJsonObject{{/propertyMap}} jo) const +{{#in?}} static void dumpTo(QJsonObject& jo, const {{qualifiedName}}& pod) { - {{qualifiedName}} result; -{{#vars}} result.{{nameCamelCase}} = - fromJson<{{dataType.qualifiedName}}>(jo.{{#propertyMap}}take{{/propertyMap}}{{^propertyMap}}value{{/propertyMap}}("{{baseName}}"_ls)); -{{/vars}}{{#propertyMap}} - result.{{nameCamelCase}} = fromJson<{{dataType.qualifiedName}}>(jo);{{/propertyMap}} - return result; - } - }; -{{/out?}}{{/model}}} // namespace QMatrixClient +{{#propertyMap}} fillJson(jo, pod.{{nameCamelCase}}); + {{/propertyMap}}{{#parents}}fillJson<{{name}}>(jo, pod); + {{/parents}}{{#vars +}} addParam<{{^required?}}IfNotEmpty{{/required?}}>(jo, QStringLiteral("{{baseName}}"), pod.{{nameCamelCase}}); +{{/vars}} } +{{/in?}}{{#out? +}} static void fillFrom({{^propertyMap}}const QJsonObject&{{/propertyMap + }}{{#propertyMap}}QJsonObject{{/propertyMap}} jo, {{qualifiedName}}& result) + { +{{#parents}} fillFromJson<{{qualifiedName}}{{!of the parent!}}>(jo, result); + {{/parents}}{{#vars +}} fromJson(jo.{{#propertyMap}}take{{/propertyMap + }}{{^propertyMap}}value{{/propertyMap}}("{{baseName}}"_ls), result.{{nameCamelCase}}); +{{/vars}}{{#propertyMap}} fromJson(jo, result.{{nameCamelCase}}); +{{/propertyMap}} } +{{/out?}} }; +{{/model}}} // namespace QMatrixClient {{/ models}}{{#responses}}{{#normalResponse?}}{{#allProperties?}} class {{camelCaseOperationId}}Job::Private { @@ -109,12 +112,12 @@ BaseJob::Status {{camelCaseOperationId}}Job::parseReply(QNetworkReply* reply) }{{/ producesNonJson?}}{{^producesNonJson?}} BaseJob::Status {{camelCaseOperationId}}Job::parseJson(const QJsonDocument& data) { -{{#inlineResponse}} d->{{paramName}} = fromJson<{{dataType.name}}>(data); +{{#inlineResponse}} fromJson(data, d->{{paramName}}); {{/inlineResponse}}{{^inlineResponse}} auto json = data.object(); {{#properties}}{{#required?}} if (!json.contains("{{baseName}}"_ls)) return { JsonParseError, "The key '{{baseName}}' not found in the response" }; -{{/required?}} d->{{paramName}} = fromJson<{{dataType.name}}>(json.value("{{baseName}}"_ls)); +{{/required?}} fromJson(json.value("{{baseName}}"_ls), d->{{paramName}}); {{/properties}}{{/inlineResponse}} return Success; }{{/ producesNonJson?}} {{/allProperties?}}{{/normalResponse?}}{{/responses}}{{/operation}}{{/operations}} -- cgit v1.2.3 From 10c4be59438ac614a01800fb44e351e7039fe6e6 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 7 Jun 2019 10:37:41 +0900 Subject: *.cpp.mustache: switch from now-deprecated JsonParseError to IncorrectResponse --- lib/csapi/{{base}}.cpp.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/csapi/{{base}}.cpp.mustache') diff --git a/lib/csapi/{{base}}.cpp.mustache b/lib/csapi/{{base}}.cpp.mustache index ff888d76..010f9116 100644 --- a/lib/csapi/{{base}}.cpp.mustache +++ b/lib/csapi/{{base}}.cpp.mustache @@ -115,7 +115,7 @@ BaseJob::Status {{camelCaseOperationId}}Job::parseJson(const QJsonDocument& data {{#inlineResponse}} fromJson(data, d->{{paramName}}); {{/inlineResponse}}{{^inlineResponse}} auto json = data.object(); {{#properties}}{{#required?}} if (!json.contains("{{baseName}}"_ls)) - return { JsonParseError, + return { IncorrectResponse, "The key '{{baseName}}' not found in the response" }; {{/required?}} fromJson(json.value("{{baseName}}"_ls), d->{{paramName}}); {{/properties}}{{/inlineResponse}} return Success; -- cgit v1.2.3 From e083d327e6f6581210f8d077d8bbe1151e81e82c Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 23 Jun 2019 19:50:09 +0900 Subject: csapi/: Make Mustache files more readable Now that clang-format takes care of formatting, the template readability can be prioritised. --- lib/csapi/{{base}}.cpp.mustache | 173 +++++++++++++++++++++++----------------- 1 file changed, 102 insertions(+), 71 deletions(-) (limited to 'lib/csapi/{{base}}.cpp.mustache') diff --git a/lib/csapi/{{base}}.cpp.mustache b/lib/csapi/{{base}}.cpp.mustache index ff888d76..f9a63412 100644 --- a/lib/csapi/{{base}}.cpp.mustache +++ b/lib/csapi/{{base}}.cpp.mustache @@ -1,80 +1,98 @@ {{>preamble}} #include "{{filenameBase}}.h" {{^models}} -#include "converters.h" -{{/models}}{{#operations}} -{{#producesNonJson?}}#include -{{/producesNonJson?}}#include +#include "converters.h"{{/models}} +{{#operations}} +{{#producesNonJson?}} +#include +{{/producesNonJson?}} +#include {{/operations}} using namespace QMatrixClient; -{{#models.model}}{{#in?}} -void JsonObjectConverter<{{qualifiedName}}>::dumpTo( - QJsonObject& jo, const {{qualifiedName}}& pod) +{{#models.model}} + {{#in?}} +void JsonObjectConverter<{{qualifiedName}}>::dumpTo(QJsonObject& jo, const {{qualifiedName}}& pod) { -{{#propertyMap}} fillJson(jo, pod.{{nameCamelCase}}); -{{/propertyMap}}{{#parents}} fillJson<{{name}}>(jo, pod); -{{/parents}}{{#vars}} addParam<{{^required?}}IfNotEmpty{{/required?}}>(jo, QStringLiteral("{{baseName}}"), pod.{{nameCamelCase}}); -{{/vars}}}{{!<- dumpTo() ends here}} -{{/in?}}{{#out?}} -void JsonObjectConverter<{{qualifiedName}}>::fillFrom( - {{^propertyMap}}const QJsonObject&{{/propertyMap - }}{{#propertyMap}}QJsonObject{{/propertyMap}} jo, {{qualifiedName}}& result) +{{#propertyMap +}} fillJson(jo, pod.{{nameCamelCase}}); +{{/propertyMap}}{{#parents +}} fillJson<{{name}}>(jo, pod); +{{/parents}}{{#vars +}} addParam<{{^required?}}IfNotEmpty{{/required?}}>(jo, QStringLiteral("{{baseName}}"), pod.{{nameCamelCase}}); +{{/vars}} +} + {{/in?}}{{#out?}} +void JsonObjectConverter<{{qualifiedName}}>::fillFrom({{>maybeCrefJsonObject}} jo, {{qualifiedName}}& result) { -{{#parents}} fillFromJson<{{qualifiedName}}>(jo, result); -{{/parents}}{{#vars}} fromJson(jo.{{#propertyMap}}take{{/propertyMap - }}{{^propertyMap}}value{{/propertyMap}}("{{baseName}}"_ls), result.{{nameCamelCase}}); -{{/vars}}{{#propertyMap}} - fromJson(jo, result.{{nameCamelCase}}); -{{/propertyMap}}} -{{/out?}}{{/models.model}}{{#operations}} +{{#parents +}} fillFromJson<{{qualifiedName}}>(jo, result); +{{/parents}}{{#vars +}} fromJson(jo.{{>takeOrValue}}("{{baseName}}"_ls), result.{{nameCamelCase}}); +{{/vars}}{{#propertyMap +}} fromJson(jo, result.{{nameCamelCase}}); +{{/propertyMap}} +} + {{/out?}} +{{/models.model}} +{{#operations}} static const auto basePath = QStringLiteral("{{basePathWithoutHost}}"); -{{# operation}}{{#models}} + {{#operation}}{{#models}} +// Converters namespace QMatrixClient { - // Converters -{{#model}} - template <> struct JsonObjectConverter<{{qualifiedName}}> + {{#model}} +template <> struct JsonObjectConverter<{{qualifiedName}}> +{ + {{#in? +}} static void dumpTo(QJsonObject& jo, const {{qualifiedName}}& pod) + { +{{#propertyMap +}} fillJson(jo, pod.{{nameCamelCase}}); +{{/propertyMap}}{{#parents +}} fillJson<{{name}}>(jo, pod); +{{/parents}}{{#vars +}} addParam<{{^required?}}IfNotEmpty{{/required?}}>(jo, QStringLiteral("{{baseName}}"), pod.{{nameCamelCase}}); +{{/vars}} + } + {{/in?}}{{#out? +}} static void fillFrom({{>maybeCrefJsonObject}} jo, {{qualifiedName}}& result) { -{{#in?}} static void dumpTo(QJsonObject& jo, const {{qualifiedName}}& pod) - { -{{#propertyMap}} fillJson(jo, pod.{{nameCamelCase}}); - {{/propertyMap}}{{#parents}}fillJson<{{name}}>(jo, pod); - {{/parents}}{{#vars -}} addParam<{{^required?}}IfNotEmpty{{/required?}}>(jo, QStringLiteral("{{baseName}}"), pod.{{nameCamelCase}}); -{{/vars}} } -{{/in?}}{{#out? -}} static void fillFrom({{^propertyMap}}const QJsonObject&{{/propertyMap - }}{{#propertyMap}}QJsonObject{{/propertyMap}} jo, {{qualifiedName}}& result) - { -{{#parents}} fillFromJson<{{qualifiedName}}{{!of the parent!}}>(jo, result); - {{/parents}}{{#vars -}} fromJson(jo.{{#propertyMap}}take{{/propertyMap - }}{{^propertyMap}}value{{/propertyMap}}("{{baseName}}"_ls), result.{{nameCamelCase}}); -{{/vars}}{{#propertyMap}} fromJson(jo, result.{{nameCamelCase}}); -{{/propertyMap}} } -{{/out?}} }; -{{/model}}} // namespace QMatrixClient -{{/ models}}{{#responses}}{{#normalResponse?}}{{#allProperties?}} +{{#parents +}} fillFromJson<{{qualifiedName}}{{!of the parent!}}>(jo, result); +{{/parents}}{{#vars +}} fromJson(jo.{{>takeOrValue}}("{{baseName}}"_ls), result.{{nameCamelCase}}); +{{/vars}}{{#propertyMap +}} fromJson(jo, result.{{nameCamelCase}}); +{{/propertyMap}} + } + {{/out?}} +}; + {{/model}} +} // namespace QMatrixClient + {{/models}} + {{#responses}}{{#normalResponse?}}{{#allProperties?}} class {{camelCaseOperationId}}Job::Private { public:{{#allProperties}} {{>maybeOmittableType}} {{paramName}};{{/allProperties}} }; -{{/ allProperties?}}{{/normalResponse?}}{{/responses}}{{#queryParams?}} + {{/allProperties?}}{{/normalResponse?}}{{/responses}} + {{#queryParams?}} BaseJob::Query queryTo{{camelCaseOperationId}}({{#queryParams}}{{>joinedParamDef}}{{/queryParams}}) { BaseJob::Query _q;{{#queryParams}} addParam<{{^required?}}IfNotEmpty{{/required?}}>(_q, QStringLiteral("{{baseName}}"), {{paramName}});{{/queryParams}} return _q; } -{{/queryParams?}}{{^bodyParams}} + {{/queryParams?}} + {{^bodyParams}} QUrl {{camelCaseOperationId}}Job::makeRequestUrl(QUrl baseUrl{{#allParams?}}, {{#allParams}}{{>joinedParamDef}}{{/allParams}}{{/allParams?}}) { return BaseJob::makeRequestUrl(std::move(baseUrl), basePath{{#pathParts}} % {{_}}{{/pathParts}}{{#queryParams?}}, queryTo{{camelCaseOperationId}}({{>passQueryParams}}){{/queryParams?}}); } -{{/ bodyParams}} + {{/bodyParams}} static const auto {{camelCaseOperationId}}JobName = QStringLiteral("{{camelCaseOperationId}}Job"); {{camelCaseOperationId}}Job::{{camelCaseOperationId}}Job({{#allParams}}{{>joinedParamDef}}{{/allParams}}) @@ -84,40 +102,53 @@ static const auto {{camelCaseOperationId}}JobName = QStringLiteral("{{camelCaseO {}{{/queryParams?}}, false{{/skipAuth}}){{#responses}}{{#normalResponse?}}{{#allProperties?}} , d(new Private){{/allProperties?}}{{/normalResponse?}}{{/responses}} { -{{#headerParams?}}{{#headerParams}} setRequestHeader("{{baseName}}", {{paramName}}.toLatin1()); -{{/headerParams}} -{{/headerParams? -}}{{#bodyParams? -}}{{#inlineBody}} setRequestData(Data({{! - }}{{#consumesNonJson?}}{{nameCamelCase}}{{/consumesNonJson? - }}{{^consumesNonJson?}}toJson({{nameCamelCase}}){{/consumesNonJson?}}));{{/inlineBody -}}{{^inlineBody}} QJsonObject _data;{{#bodyParams}} +{{#headerParams?}}{{#headerParams +}} setRequestHeader("{{baseName}}", {{paramName}}.toLatin1());{{/headerParams}} +{{/headerParams?}}{{#bodyParams? +}}{{#inlineBody +}} setRequestData(Data({{#consumesNonJson?}}{{nameCamelCase}}{{/consumesNonJson? + }}{{^consumesNonJson?}}toJson({{nameCamelCase}}){{/consumesNonJson?}})); +{{/inlineBody}}{{^inlineBody +}} QJsonObject _data;{{#bodyParams}} addParam<{{^required?}}IfNotEmpty{{/required?}}>(_data, QStringLiteral("{{baseName}}"), {{paramName}});{{/bodyParams}} - setRequestData(_data);{{/inlineBody}} -{{/bodyParams?}}{{#producesNonJson?}} setExpectedContentTypes({ {{#produces}}"{{_}}"{{>cjoin}}{{/produces}} }); -{{/producesNonJson?}}}{{!<- mind the actual brace}} -{{# responses}}{{#normalResponse?}}{{#allProperties?}} + setRequestData(_data); +{{/inlineBody}}{{/bodyParams? +}}{{#producesNonJson? +}} setExpectedContentTypes({ {{#produces}}"{{_}}"{{>cjoin}}{{/produces}} }); +{{/producesNonJson?}} +} + {{#responses}}{{#normalResponse?}}{{#allProperties?}} {{camelCaseOperationId}}Job::~{{camelCaseOperationId}}Job() = default; -{{# allProperties}} + {{#allProperties}} {{>qualifiedMaybeCrefType}} {{camelCaseOperationId}}Job::{{paramName}}(){{^moveOnly}} const{{/moveOnly}} { return {{#moveOnly}}std::move({{/moveOnly}}d->{{paramName}}{{#moveOnly}}){{/moveOnly}}; } -{{/ allProperties}}{{#producesNonJson?}} + {{/allProperties}} + {{#producesNonJson?}} BaseJob::Status {{camelCaseOperationId}}Job::parseReply(QNetworkReply* reply) { {{#headers}}d->{{paramName}} = reply->rawHeader("{{baseName}}");{{! We don't check for required headers yet }} {{/headers}}{{#properties}}d->{{paramName}} = reply;{{/properties}} return Success; -}{{/ producesNonJson?}}{{^producesNonJson?}} +} + {{/producesNonJson?}}{{^producesNonJson?}} BaseJob::Status {{camelCaseOperationId}}Job::parseJson(const QJsonDocument& data) { -{{#inlineResponse}} fromJson(data, d->{{paramName}}); -{{/inlineResponse}}{{^inlineResponse}} auto json = data.object(); -{{#properties}}{{#required?}} if (!json.contains("{{baseName}}"_ls)) +{{#inlineResponse +}} fromJson(data, d->{{paramName}}); +{{/inlineResponse}}{{^inlineResponse +}} auto json = data.object(); +{{# properties}}{{#required? +}} if (!json.contains("{{baseName}}"_ls)) return { JsonParseError, "The key '{{baseName}}' not found in the response" }; -{{/required?}} fromJson(json.value("{{baseName}}"_ls), d->{{paramName}}); -{{/properties}}{{/inlineResponse}} return Success; -}{{/ producesNonJson?}} -{{/allProperties?}}{{/normalResponse?}}{{/responses}}{{/operation}}{{/operations}} +{{/required? +}} fromJson(json.value("{{baseName}}"_ls), d->{{paramName}}); +{{/ properties}} +{{/inlineResponse +}} return Success; +} + {{/producesNonJson?}} + {{/allProperties?}}{{/normalResponse?}}{{/responses}} +{{/operation}}{{/operations}} -- cgit v1.2.3 From 27ca32a1e5a56e09b9cc1d94224d2831004dcf3d Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 7 Jul 2019 19:32:34 +0900 Subject: Namespace: QMatrixClient -> Quotient (with back comp alias) --- lib/csapi/{{base}}.cpp.mustache | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/csapi/{{base}}.cpp.mustache') diff --git a/lib/csapi/{{base}}.cpp.mustache b/lib/csapi/{{base}}.cpp.mustache index 8ebac6ef..6cbd1b65 100644 --- a/lib/csapi/{{base}}.cpp.mustache +++ b/lib/csapi/{{base}}.cpp.mustache @@ -8,7 +8,7 @@ {{/producesNonJson?}} #include {{/operations}} -using namespace QMatrixClient; +using namespace Quotient; {{#models.model}} {{#in?}} void JsonObjectConverter<{{qualifiedName}}>::dumpTo(QJsonObject& jo, const {{qualifiedName}}& pod) @@ -38,7 +38,7 @@ void JsonObjectConverter<{{qualifiedName}}>::fillFrom({{>maybeCrefJsonObject}} j static const auto basePath = QStringLiteral("{{basePathWithoutHost}}"); {{#operation}}{{#models}} // Converters -namespace QMatrixClient +namespace Quotient { {{#model}} template <> struct JsonObjectConverter<{{qualifiedName}}> -- cgit v1.2.3 From 44ea26d3c32dd2f4c4aa53b85ef7ec75fb354cb3 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 17 Apr 2020 07:39:10 +0200 Subject: gtad.yaml, *.mustache: upgrade to GTAD 0.7 beta That means much more readable templates thanks to GTAD 0.7 in turn using the upgrade Mustache engine. --- lib/csapi/{{base}}.cpp.mustache | 200 +++++++++++++++++++++++----------------- 1 file changed, 114 insertions(+), 86 deletions(-) (limited to 'lib/csapi/{{base}}.cpp.mustache') diff --git a/lib/csapi/{{base}}.cpp.mustache b/lib/csapi/{{base}}.cpp.mustache index 6cbd1b65..b3bd4de9 100644 --- a/lib/csapi/{{base}}.cpp.mustache +++ b/lib/csapi/{{base}}.cpp.mustache @@ -3,74 +3,84 @@ {{^models}} #include "converters.h"{{/models}} {{#operations}} -{{#producesNonJson?}} + {{#producesNonJson?}} #include -{{/producesNonJson?}} + {{/producesNonJson?}} #include {{/operations}} + using namespace Quotient; + {{#models.model}} {{#in?}} -void JsonObjectConverter<{{qualifiedName}}>::dumpTo(QJsonObject& jo, const {{qualifiedName}}& pod) -{ -{{#propertyMap -}} fillJson(jo, pod.{{nameCamelCase}}); -{{/propertyMap}}{{#parents -}} fillJson<{{name}}>(jo, pod); -{{/parents}}{{#vars -}} addParam<{{^required?}}IfNotEmpty{{/required?}}>(jo, QStringLiteral("{{baseName}}"), pod.{{nameCamelCase}}); -{{/vars}} +void JsonObjectConverter<{{qualifiedName}}>::dumpTo( + QJsonObject& jo, const {{qualifiedName}}& pod) +{ {{#propertyMap}} + fillJson(jo, pod.{{nameCamelCase}}); + {{/propertyMap}}{{#parents}} + fillJson<{{name}}>(jo, pod); + {{/parents}}{{#vars}} + addParam<{{^required?}}IfNotEmpty{{/required?}}>(jo, + QStringLiteral("{{baseName}}"), pod.{{nameCamelCase}}); + {{/vars}} } - {{/in?}}{{#out?}} -void JsonObjectConverter<{{qualifiedName}}>::fillFrom({{>maybeCrefJsonObject}} jo, {{qualifiedName}}& result) -{ -{{#parents -}} fillFromJson<{{qualifiedName}}>(jo, result); -{{/parents}}{{#vars -}} fromJson(jo.{{>takeOrValue}}("{{baseName}}"_ls), result.{{nameCamelCase}}); -{{/vars}}{{#propertyMap -}} fromJson(jo, result.{{nameCamelCase}}); -{{/propertyMap}} + {{/in?}} + + {{#out?}} +void JsonObjectConverter<{{qualifiedName}}>::fillFrom( + {{>maybeCrefJsonObject}} jo, {{qualifiedName}}& result) +{ {{#parents}} + fillFromJson<{{qualifiedName}}>(jo, result); + {{/parents}}{{#vars}} + fromJson(jo.{{>takeOrValue}}("{{baseName}}"_ls), result.{{nameCamelCase}}); + {{/vars}}{{#propertyMap}} + fromJson(jo, result.{{nameCamelCase}}); + {{/propertyMap}} } {{/out?}} + {{/models.model}} {{#operations}} + static const auto basePath = QStringLiteral("{{basePathWithoutHost}}"); {{#operation}}{{#models}} + // Converters -namespace Quotient -{ +namespace Quotient { {{#model}} -template <> struct JsonObjectConverter<{{qualifiedName}}> -{ - {{#in? -}} static void dumpTo(QJsonObject& jo, const {{qualifiedName}}& pod) - { -{{#propertyMap -}} fillJson(jo, pod.{{nameCamelCase}}); -{{/propertyMap}}{{#parents -}} fillJson<{{name}}>(jo, pod); -{{/parents}}{{#vars -}} addParam<{{^required?}}IfNotEmpty{{/required?}}>(jo, QStringLiteral("{{baseName}}"), pod.{{nameCamelCase}}); -{{/vars}} + +template <> struct JsonObjectConverter<{{qualifiedName}}> { + {{#in?}} + static void dumpTo(QJsonObject& jo, const {{qualifiedName}}& pod) + { {{#propertyMap}} + fillJson(jo, pod.{{nameCamelCase}}); + {{/propertyMap}}{{#parents}} + fillJson<{{name}}>(jo, pod); + {{/parents}}{{#vars}} + addParam<{{^required?}}IfNotEmpty{{/required?}}>(jo, + QStringLiteral("{{baseName}}"), pod.{{nameCamelCase}}); + {{/vars}} } - {{/in?}}{{#out? -}} static void fillFrom({{>maybeCrefJsonObject}} jo, {{qualifiedName}}& result) - { -{{#parents -}} fillFromJson<{{qualifiedName}}{{!of the parent!}}>(jo, result); -{{/parents}}{{#vars -}} fromJson(jo.{{>takeOrValue}}("{{baseName}}"_ls), result.{{nameCamelCase}}); -{{/vars}}{{#propertyMap -}} fromJson(jo, result.{{nameCamelCase}}); -{{/propertyMap}} + {{/in?}} + {{#out?}} + static void fillFrom({{>maybeCrefJsonObject}} jo, {{qualifiedName}}& result) + { {{#parents}} + fillFromJson<{{qualifiedName}}{{!of the parent!}}>(jo, result); + {{/parents}}{{#vars}} + fromJson(jo.{{>takeOrValue}}("{{baseName}}"_ls), + result.{{nameCamelCase}}); + {{/vars}}{{#propertyMap}} + fromJson(jo, result.{{nameCamelCase}}); + {{/propertyMap}} } {{/out?}} }; {{/model}} -} // namespace QMatrixClient + +} // namespace Quotient {{/models}} {{#responses}}{{#normalResponse?}}{{#allProperties?}} + class {{camelCaseOperationId}}Job::Private { public:{{#allProperties}} @@ -78,76 +88,94 @@ class {{camelCaseOperationId}}Job::Private }; {{/allProperties?}}{{/normalResponse?}}{{/responses}} {{#queryParams?}} -BaseJob::Query queryTo{{camelCaseOperationId}}({{#queryParams}}{{>joinedParamDef}}{{/queryParams}}) + +BaseJob::Query queryTo{{camelCaseOperationId}}( + {{#queryParams}}{{>joinedParamDef}}{{/queryParams}}) { BaseJob::Query _q;{{#queryParams}} - addParam<{{^required?}}IfNotEmpty{{/required?}}>(_q, QStringLiteral("{{baseName}}"), {{paramName}});{{/queryParams}} + addParam<{{^required?}}IfNotEmpty{{/required?}}>(_q, + QStringLiteral("{{baseName}}"), {{paramName}});{{/queryParams}} return _q; } {{/queryParams?}} {{^bodyParams}} -QUrl {{camelCaseOperationId}}Job::makeRequestUrl(QUrl baseUrl{{#allParams?}}, {{#allParams}}{{>joinedParamDef}}{{/allParams}}{{/allParams?}}) + +QUrl {{camelCaseOperationId}}Job::makeRequestUrl(QUrl baseUrl{{#allParams?}}, + {{#allParams}}{{>joinedParamDef}}{{/allParams}}{{/allParams?}}) { return BaseJob::makeRequestUrl(std::move(baseUrl), basePath{{#pathParts}} % {{_}}{{/pathParts}}{{#queryParams?}}, queryTo{{camelCaseOperationId}}({{>passQueryParams}}){{/queryParams?}}); -} - {{/bodyParams}} -static const auto {{camelCaseOperationId}}JobName = QStringLiteral("{{camelCaseOperationId}}Job"); - -{{camelCaseOperationId}}Job::{{camelCaseOperationId}}Job({{#allParams}}{{>joinedParamDef}}{{/allParams}}) - : BaseJob(HttpVerb::{{#_cap}}{{#_tolower}}{{httpMethod}}{{/_tolower}}{{/_cap}}, {{camelCaseOperationId}}JobName, - basePath{{#pathParts}} % {{_}}{{/pathParts}}{{#queryParams?}}, - queryTo{{camelCaseOperationId}}({{>passQueryParams}}){{/queryParams?}}{{#skipAuth}}{{#queryParams?}}, - {}{{/queryParams?}}, false{{/skipAuth}}){{#responses}}{{#normalResponse?}}{{#allProperties?}} - , d(new Private){{/allProperties?}}{{/normalResponse?}}{{/responses}} -{ -{{#headerParams?}}{{#headerParams -}} setRequestHeader("{{baseName}}", {{paramName}}.toLatin1());{{/headerParams}} -{{/headerParams?}}{{#bodyParams? -}}{{#inlineBody -}} setRequestData(Data({{#consumesNonJson?}}{{nameCamelCase}}{{/consumesNonJson? - }}{{^consumesNonJson?}}toJson({{nameCamelCase}}){{/consumesNonJson?}})); -{{/inlineBody}}{{^inlineBody -}} QJsonObject _data;{{#bodyParams}} - addParam<{{^required?}}IfNotEmpty{{/required?}}>(_data, QStringLiteral("{{baseName}}"), {{paramName}});{{/bodyParams}} +} {{/bodyParams}} + +{{camelCaseOperationId}}Job::{{camelCaseOperationId}}Job( + {{#allParams}}{{>joinedParamDef}}{{/allParams}}) + : BaseJob(HttpVerb::{{#_cap}}{{#_tolower}}{{httpMethod}}{{/_tolower}}{{/_cap}}, + QStringLiteral("{{camelCaseOperationId}}Job"), {{!object name}} + basePath{{#pathParts}} % {{_}}{{/pathParts}} {{!API endpoint}} + {{#queryParams? + }} , queryTo{{camelCaseOperationId}}({{>passQueryParams}}) + {{/queryParams? + }}{{#skipAuth}}{{#queryParams?}}, {}{{/queryParams?}}, false{{/skipAuth}} ) + {{#responses}}{{#normalResponse?}}{{#allProperties? + }}, d(new Private){{/allProperties?}}{{/normalResponse?}}{{/responses}} +{ {{#headerParams}} + setRequestHeader("{{baseName}}", {{paramName}}.toLatin1()); + {{/headerParams}}{{#bodyParams?}} + {{#inlineBody}} + setRequestData(Data({{!avoid extra linebreaks}}{{ + #consumesNonJson?}}{{nameCamelCase}}{{/consumesNonJson?}}{{ + ^consumesNonJson?}}toJson({{nameCamelCase}}){{/consumesNonJson? + }})); + {{/inlineBody}}{{^inlineBody}} + QJsonObject _data; + {{#bodyParams}} + addParam<{{^required?}}IfNotEmpty{{/required?}}>(_data, + QStringLiteral("{{baseName}}"), {{paramName}}); + {{/bodyParams}} setRequestData(_data); -{{/inlineBody}}{{/bodyParams? -}}{{#producesNonJson? -}} setExpectedContentTypes({ {{#produces}}"{{_}}"{{>cjoin}}{{/produces}} }); -{{/producesNonJson?}} + {{/inlineBody}} + {{/bodyParams?}}{{#producesNonJson?}} + setExpectedContentTypes({ {{#produces}}"{{_}}"{{>cjoin}}{{/produces}} }); + {{/producesNonJson?}} } {{#responses}}{{#normalResponse?}}{{#allProperties?}} + {{camelCaseOperationId}}Job::~{{camelCaseOperationId}}Job() = default; {{#allProperties}} -{{>qualifiedMaybeCrefType}} {{camelCaseOperationId}}Job::{{paramName}}(){{^moveOnly}} const{{/moveOnly}} + +{{>qualifiedMaybeCrefType}} + {{camelCaseOperationId}}Job::{{paramName}}(){{^moveOnly}} const{{/moveOnly}} { return {{#moveOnly}}std::move({{/moveOnly}}d->{{paramName}}{{#moveOnly}}){{/moveOnly}}; } {{/allProperties}} + {{#producesNonJson?}} BaseJob::Status {{camelCaseOperationId}}Job::parseReply(QNetworkReply* reply) { - {{#headers}}d->{{paramName}} = reply->rawHeader("{{baseName}}");{{! We don't check for required headers yet }} + {{! We don't check for required headers yet }} + {{#headers}}d->{{paramName}} = reply->rawHeader("{{baseName}}"); {{/headers}}{{#properties}}d->{{paramName}} = reply;{{/properties}} return Success; } {{/producesNonJson?}}{{^producesNonJson?}} BaseJob::Status {{camelCaseOperationId}}Job::parseJson(const QJsonDocument& data) { -{{#inlineResponse -}} fromJson(data, d->{{paramName}}); -{{/inlineResponse}}{{^inlineResponse -}} auto json = data.object(); -{{# properties}}{{#required? -}} if (!json.contains("{{baseName}}"_ls)) + {{#inlineResponse}} + fromJson(data, d->{{paramName}}); + {{/inlineResponse}}{{^inlineResponse}} + auto json = data.object(); + {{#properties}}{{#required?}} + if (!json.contains("{{baseName}}"_ls)) return { IncorrectResponse, "The key '{{baseName}}' not found in the response" }; -{{/required? -}} fromJson(json.value("{{baseName}}"_ls), d->{{paramName}}); -{{/ properties}} -{{/inlineResponse -}} return Success; + {{/required?}} + fromJson(json.value("{{baseName}}"_ls), d->{{paramName}}); + {{/properties}} + {{/inlineResponse}} + + return Success; } {{/producesNonJson?}} {{/allProperties?}}{{/normalResponse?}}{{/responses}} -- cgit v1.2.3 From 51d27f3bcaacefda78dc033021b6a85152ab972e Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Mon, 1 Jun 2020 14:53:59 +0200 Subject: GTAD parts: new home and format updates for GTAD 0.7 All GTAD-related files (gtad.yaml and templates) from now live in their dedicated gtad/ directory - this helps against removing them accidentally along with the rest of the generated files. The format to list generated files in gtad.yaml has changed a bit before GTAD 0.7 beta2; gtad.yaml in this commit conforms to the new structure. --- lib/csapi/{{base}}.cpp.mustache | 182 ---------------------------------------- 1 file changed, 182 deletions(-) delete mode 100644 lib/csapi/{{base}}.cpp.mustache (limited to 'lib/csapi/{{base}}.cpp.mustache') diff --git a/lib/csapi/{{base}}.cpp.mustache b/lib/csapi/{{base}}.cpp.mustache deleted file mode 100644 index b3bd4de9..00000000 --- a/lib/csapi/{{base}}.cpp.mustache +++ /dev/null @@ -1,182 +0,0 @@ -{{>preamble}} -#include "{{filenameBase}}.h" -{{^models}} -#include "converters.h"{{/models}} -{{#operations}} - {{#producesNonJson?}} -#include - {{/producesNonJson?}} -#include -{{/operations}} - -using namespace Quotient; - -{{#models.model}} - {{#in?}} -void JsonObjectConverter<{{qualifiedName}}>::dumpTo( - QJsonObject& jo, const {{qualifiedName}}& pod) -{ {{#propertyMap}} - fillJson(jo, pod.{{nameCamelCase}}); - {{/propertyMap}}{{#parents}} - fillJson<{{name}}>(jo, pod); - {{/parents}}{{#vars}} - addParam<{{^required?}}IfNotEmpty{{/required?}}>(jo, - QStringLiteral("{{baseName}}"), pod.{{nameCamelCase}}); - {{/vars}} -} - {{/in?}} - - {{#out?}} -void JsonObjectConverter<{{qualifiedName}}>::fillFrom( - {{>maybeCrefJsonObject}} jo, {{qualifiedName}}& result) -{ {{#parents}} - fillFromJson<{{qualifiedName}}>(jo, result); - {{/parents}}{{#vars}} - fromJson(jo.{{>takeOrValue}}("{{baseName}}"_ls), result.{{nameCamelCase}}); - {{/vars}}{{#propertyMap}} - fromJson(jo, result.{{nameCamelCase}}); - {{/propertyMap}} -} - {{/out?}} - -{{/models.model}} -{{#operations}} - -static const auto basePath = QStringLiteral("{{basePathWithoutHost}}"); - {{#operation}}{{#models}} - -// Converters -namespace Quotient { - {{#model}} - -template <> struct JsonObjectConverter<{{qualifiedName}}> { - {{#in?}} - static void dumpTo(QJsonObject& jo, const {{qualifiedName}}& pod) - { {{#propertyMap}} - fillJson(jo, pod.{{nameCamelCase}}); - {{/propertyMap}}{{#parents}} - fillJson<{{name}}>(jo, pod); - {{/parents}}{{#vars}} - addParam<{{^required?}}IfNotEmpty{{/required?}}>(jo, - QStringLiteral("{{baseName}}"), pod.{{nameCamelCase}}); - {{/vars}} - } - {{/in?}} - {{#out?}} - static void fillFrom({{>maybeCrefJsonObject}} jo, {{qualifiedName}}& result) - { {{#parents}} - fillFromJson<{{qualifiedName}}{{!of the parent!}}>(jo, result); - {{/parents}}{{#vars}} - fromJson(jo.{{>takeOrValue}}("{{baseName}}"_ls), - result.{{nameCamelCase}}); - {{/vars}}{{#propertyMap}} - fromJson(jo, result.{{nameCamelCase}}); - {{/propertyMap}} - } - {{/out?}} -}; - {{/model}} - -} // namespace Quotient - {{/models}} - {{#responses}}{{#normalResponse?}}{{#allProperties?}} - -class {{camelCaseOperationId}}Job::Private -{ - public:{{#allProperties}} - {{>maybeOmittableType}} {{paramName}};{{/allProperties}} -}; - {{/allProperties?}}{{/normalResponse?}}{{/responses}} - {{#queryParams?}} - -BaseJob::Query queryTo{{camelCaseOperationId}}( - {{#queryParams}}{{>joinedParamDef}}{{/queryParams}}) -{ - BaseJob::Query _q;{{#queryParams}} - addParam<{{^required?}}IfNotEmpty{{/required?}}>(_q, - QStringLiteral("{{baseName}}"), {{paramName}});{{/queryParams}} - return _q; -} - {{/queryParams?}} - {{^bodyParams}} - -QUrl {{camelCaseOperationId}}Job::makeRequestUrl(QUrl baseUrl{{#allParams?}}, - {{#allParams}}{{>joinedParamDef}}{{/allParams}}{{/allParams?}}) -{ - return BaseJob::makeRequestUrl(std::move(baseUrl), - basePath{{#pathParts}} % {{_}}{{/pathParts}}{{#queryParams?}}, - queryTo{{camelCaseOperationId}}({{>passQueryParams}}){{/queryParams?}}); -} {{/bodyParams}} - -{{camelCaseOperationId}}Job::{{camelCaseOperationId}}Job( - {{#allParams}}{{>joinedParamDef}}{{/allParams}}) - : BaseJob(HttpVerb::{{#_cap}}{{#_tolower}}{{httpMethod}}{{/_tolower}}{{/_cap}}, - QStringLiteral("{{camelCaseOperationId}}Job"), {{!object name}} - basePath{{#pathParts}} % {{_}}{{/pathParts}} {{!API endpoint}} - {{#queryParams? - }} , queryTo{{camelCaseOperationId}}({{>passQueryParams}}) - {{/queryParams? - }}{{#skipAuth}}{{#queryParams?}}, {}{{/queryParams?}}, false{{/skipAuth}} ) - {{#responses}}{{#normalResponse?}}{{#allProperties? - }}, d(new Private){{/allProperties?}}{{/normalResponse?}}{{/responses}} -{ {{#headerParams}} - setRequestHeader("{{baseName}}", {{paramName}}.toLatin1()); - {{/headerParams}}{{#bodyParams?}} - {{#inlineBody}} - setRequestData(Data({{!avoid extra linebreaks}}{{ - #consumesNonJson?}}{{nameCamelCase}}{{/consumesNonJson?}}{{ - ^consumesNonJson?}}toJson({{nameCamelCase}}){{/consumesNonJson? - }})); - {{/inlineBody}}{{^inlineBody}} - QJsonObject _data; - {{#bodyParams}} - addParam<{{^required?}}IfNotEmpty{{/required?}}>(_data, - QStringLiteral("{{baseName}}"), {{paramName}}); - {{/bodyParams}} - setRequestData(_data); - {{/inlineBody}} - {{/bodyParams?}}{{#producesNonJson?}} - setExpectedContentTypes({ {{#produces}}"{{_}}"{{>cjoin}}{{/produces}} }); - {{/producesNonJson?}} -} - {{#responses}}{{#normalResponse?}}{{#allProperties?}} - -{{camelCaseOperationId}}Job::~{{camelCaseOperationId}}Job() = default; - {{#allProperties}} - -{{>qualifiedMaybeCrefType}} - {{camelCaseOperationId}}Job::{{paramName}}(){{^moveOnly}} const{{/moveOnly}} -{ - return {{#moveOnly}}std::move({{/moveOnly}}d->{{paramName}}{{#moveOnly}}){{/moveOnly}}; -} - {{/allProperties}} - - {{#producesNonJson?}} -BaseJob::Status {{camelCaseOperationId}}Job::parseReply(QNetworkReply* reply) -{ - {{! We don't check for required headers yet }} - {{#headers}}d->{{paramName}} = reply->rawHeader("{{baseName}}"); - {{/headers}}{{#properties}}d->{{paramName}} = reply;{{/properties}} - return Success; -} - {{/producesNonJson?}}{{^producesNonJson?}} -BaseJob::Status {{camelCaseOperationId}}Job::parseJson(const QJsonDocument& data) -{ - {{#inlineResponse}} - fromJson(data, d->{{paramName}}); - {{/inlineResponse}}{{^inlineResponse}} - auto json = data.object(); - {{#properties}}{{#required?}} - if (!json.contains("{{baseName}}"_ls)) - return { IncorrectResponse, - "The key '{{baseName}}' not found in the response" }; - {{/required?}} - fromJson(json.value("{{baseName}}"_ls), d->{{paramName}}); - {{/properties}} - {{/inlineResponse}} - - return Success; -} - {{/producesNonJson?}} - {{/allProperties?}}{{/normalResponse?}}{{/responses}} -{{/operation}}{{/operations}} -- cgit v1.2.3