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. --- gtad/template.cpp.mustache | 182 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 gtad/template.cpp.mustache (limited to 'gtad/template.cpp.mustache') diff --git a/gtad/template.cpp.mustache b/gtad/template.cpp.mustache new file mode 100644 index 00000000..b3bd4de9 --- /dev/null +++ b/gtad/template.cpp.mustache @@ -0,0 +1,182 @@ +{{>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