aboutsummaryrefslogtreecommitdiff
path: root/jobs
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2017-08-20 18:56:58 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2017-09-01 19:50:39 +0900
commitc28211698b8f9b82cafc0f9d9457e40c6c17632a (patch)
tree85e8d0b17476cc70b09156d5e034f4322c7b057f /jobs
parent45c138903c20d32a8a69b5637a72898bc690f1f1 (diff)
downloadlibquotient-c28211698b8f9b82cafc0f9d9457e40c6c17632a.tar.gz
libquotient-c28211698b8f9b82cafc0f9d9457e40c6c17632a.zip
Added files to (eventually) generate *Job classes automatically
KitsuneRal/api-generator project is a place where all the heavy lifting will (eventually) be implemented. This commit marks a point when the generated files at least compile (whether they work is not tested yet). Return values are so far entirely ignored.
Diffstat (limited to 'jobs')
-rw-r--r--jobs/apigen.yaml54
-rw-r--r--jobs/converters.h88
-rw-r--r--jobs/preamble.mustache18
-rw-r--r--jobs/{{base}}.cpp.mustache75
-rw-r--r--jobs/{{base}}.h.mustache65
5 files changed, 300 insertions, 0 deletions
diff --git a/jobs/apigen.yaml b/jobs/apigen.yaml
new file mode 100644
index 00000000..7ae1de81
--- /dev/null
+++ b/jobs/apigen.yaml
@@ -0,0 +1,54 @@
+env:
+ preamble: preamble.mustache
+ copyrightName: Kitsune Ral
+ copyrightEmail: <kitsune-ral@users.sf.net>
+ imports: { set: }
+ returnFile?: { bool: false }
+
+templates:
+- "{{base}}.h.mustache"
+- "{{base}}.cpp.mustache"
+
+outFilesList: apifiles.txt
+
+# Structure:
+# swaggerType: <targetTypeSpec>
+# OR
+# swaggerType:
+# - swaggerFormat: <targetTypeSpec>
+# - /swaggerFormatRegEx/: <targetTypeSpec>
+# - //: <targetTypeSpec> # default, if the format doesn't mach anything above
+# WHERE
+# targetTypeSpec = targetType OR
+# { type: targetType, imports: <filename OR [ filenames... ]>, <other attributes...> }
+types:
+ integer:
+ - int64: qint64
+ - int32: qint32
+ - //: int
+ number:
+ - float: float
+ - double: double
+ boolean: bool
+ string:
+ - /byte|binary/: { type: QByteArray, imports: <QtCore/QByteArray> }
+ - date:
+ type: QDate
+ avoidCopy?: true
+ imports: <QtCore/QDate>
+ - dateTime:
+ type: QDateTime
+ avoidCopy?: true
+ imports: <QtCore/QDateTime>
+ - //: { type: QString, imports: <QtCore/QString> }
+ file:
+ type: QByteArray
+ imports: <QtCore/QByteArray>
+ returnFile?: true
+ name: data
+ "[]": { type: "QVector<{{type}}>", imports: <QtCore/QVector> }
+ "{}":
+ type: "QHash<QString, {{type}}>"
+ imports: [ <QtCore/QHash>, <QtCore/QString> ]
+
+operations: \ No newline at end of file
diff --git a/jobs/converters.h b/jobs/converters.h
new file mode 100644
index 00000000..b52cfaa2
--- /dev/null
+++ b/jobs/converters.h
@@ -0,0 +1,88 @@
+/******************************************************************************
+* Copyright (C) 2017 Kitsune Ral <kitsune-ral@users.sf.net>
+*
+* This library is free software; you can redistribute it and/or
+* modify it under the terms of the GNU Lesser General Public
+* License as published by the Free Software Foundation; either
+* version 2.1 of the License, or (at your option) any later version.
+*
+* This library is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this library; if not, write to the Free Software
+* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#pragma once
+
+#include <QtCore/QJsonValue>
+#include <QtCore/QJsonArray>
+#include <QtCore/QDate>
+
+namespace QMatrixClient
+{
+ template <typename T>
+ inline QJsonValue toJson(T val)
+ {
+ return QJsonValue(val);
+ }
+
+ template <typename T>
+ inline QJsonValue toJson(const QVector<T>& vals)
+ {
+ QJsonArray ar;
+ std::copy(vals.begin(), vals.end(), std::back_inserter(ar));
+ return ar;
+ }
+
+ inline QJsonValue toJson(const QStringList& strings)
+ {
+ return QJsonArray::fromStringList(strings);
+ }
+
+ template <typename T>
+ inline T fromJson(const QJsonValue& jv)
+ {
+ return QVariant(jv).value<T>();
+ }
+
+ template <>
+ inline int fromJson<int>(const QJsonValue& jv)
+ {
+ return jv.toInt();
+ }
+
+ template <>
+ inline qint64 fromJson<qint64>(const QJsonValue& jv)
+ {
+ return static_cast<qint64>(jv.toDouble());
+ }
+
+ template <>
+ inline double fromJson<double>(const QJsonValue& jv)
+ {
+ return jv.toDouble();
+ }
+
+ template <>
+ inline QString fromJson<QString>(const QJsonValue& jv)
+ {
+ return jv.toString();
+ }
+
+ template <>
+ inline QDateTime fromJson<QDateTime>(const QJsonValue& jv)
+ {
+ return QDateTime::fromMSecsSinceEpoch(fromJson<qint64>(jv), Qt::UTC);
+ }
+
+ template <>
+ inline QDate fromJson<QDate>(const QJsonValue& jv)
+ {
+ return QDateTime::fromMSecsSinceEpoch(
+ fromJson<qint64>(jv), Qt::UTC).date();
+ }
+} // namespace QMatrixClient \ No newline at end of file
diff --git a/jobs/preamble.mustache b/jobs/preamble.mustache
new file mode 100644
index 00000000..f14d58d2
--- /dev/null
+++ b/jobs/preamble.mustache
@@ -0,0 +1,18 @@
+/******************************************************************************
+* Copyright (C) 2016-2017 {{copyrightName}} {{copyrightEmail}}
+*
+* This library is free software; you can redistribute it and/or
+* modify it under the terms of the GNU Lesser General Public
+* License as published by the Free Software Foundation; either
+* version 2.1 of the License, or (at your option) any later version.
+*
+* This library is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this library; if not, write to the Free Software
+* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
diff --git a/jobs/{{base}}.cpp.mustache b/jobs/{{base}}.cpp.mustache
new file mode 100644
index 00000000..05c865b3
--- /dev/null
+++ b/jobs/{{base}}.cpp.mustache
@@ -0,0 +1,75 @@
+{{#@filePartial}}preamble{{/@filePartial}}
+
+#include "{{filenameBase}}.h"
+
+{{#operations}}
+#include "../converters.h"
+
+#include <QtCore/QStringBuilder>
+
+using namespace QMatrixClient;
+
+ {{#returns?}}
+class {{#@cap}}{{operationId}}{{/@cap}}Job::Private
+{
+ public:
+ {{#returns}}
+ {{type}} {{name}};
+ {{/returns}}
+};
+ {{/returns?}}
+
+ {{#operation}}
+{{#@cap}}{{operationId}}{{/@cap}}Job::{{#@cap}}{{operationId}}{{/@cap}}Job(const ConnectionData* connection{{#allParams}}, {{!}}
+ {{#avoidCopy?}}const {{dataType}}&{{/avoidCopy?}}
+ {{^avoidCopy?}}{{dataType}}{{/avoidCopy?}} {{paramName}}
+ {{/allParams}})
+ : BaseJob(connection, HttpVerb::{{#@cap}}{{#@tolower}}{{httpMethod}}{{/@tolower}}{{/@cap}}, "{{#@cap}}{{operationId}}{{/@cap}}Job"
+ , {{#pathParts}}{{part}}{{#hasMore}} % {{/hasMore}}{{/pathParts}}
+ , Query { {{#queryParams}}
+ { "{{baseName}}", toJson({{paramName}}).toString() }{{#hasMore}}, {{/hasMore}}
+ {{/queryParams}} }
+ , Data { {{#bodyParams}}
+ { "{{baseName}}", toJson({{paramName}}) }{{#hasMore}}, {{/hasMore}}
+ {{/bodyParams}} }
+ {{#skipAuth}}, false{{/skipAuth}}
+ ){{#returns?}}, d(new Private){{/returns?}}
+{ }
+ {{/operation}}
+
+ {{#returns?}}
+{{className}}Job::~{{className}}Job()
+{
+ delete d;
+}
+
+ {{#returns}}
+{{type}} {{className}}Job::{{name}}() const
+{
+ return d->{{name}};
+}
+ {{/returns}}
+
+ {{#returnFile?}}
+BaseJob::Status {{className}}Job::parseReply(QByteArray data)
+{
+ {{#returns}}{{name}}{{/returns}} = data;
+ return Success;
+}
+ {{/returnFile?}}
+ {{^returnFile?}}
+BaseJob::Status {{className}}Job::parseJson(const QJsonDocument& data)
+{
+ auto json = data.object();
+ {{#returns}}
+ {{#required?}}
+ if (!json.contains("{{name}}")
+ return { JsonParseError, "{{name}} not found in the response" };
+ {{/required?}}
+ d->{{name}} = fromJson<{{type}}>(json.value("{{name}}"));
+ {{/returns}}
+ return Success;
+}
+ {{/returnFile?}}
+ {{/returns?}}
+{{/operations}}
diff --git a/jobs/{{base}}.h.mustache b/jobs/{{base}}.h.mustache
new file mode 100644
index 00000000..6fb9caef
--- /dev/null
+++ b/jobs/{{base}}.h.mustache
@@ -0,0 +1,65 @@
+{{#@filePartial}}preamble{{/@filePartial}}
+
+#pragma once
+
+{{#operations}}
+#include "../basejob.h"
+{{/operations}}
+
+{{#imports}}
+#include {{.}}
+{{/imports}}
+
+{{#models}}
+#include <QtCore/QJsonValue> {{! FIXME: This should probably go inside imports }}
+{{/models}}
+
+namespace QMatrixClient
+{
+{{#models}}
+ // Data structures
+ {{#model}}
+ struct {{classname}}
+ {
+ {{#vars}}
+ {{datatype}} {{name}};
+ {{/vars}}
+ operator QJsonValue() const { return {}; }
+ };
+ {{/model}}
+{{/models}}
+{{#operations}}
+ // Operations
+ {{#operation}}
+ class {{#@cap}}{{operationId}}{{/@cap}}Job : public BaseJob
+ {
+ public:
+ {{#@cap}}{{operationId}}{{/@cap}}Job(const ConnectionData* connection
+ {{#allParams}}
+ , {{!}}
+ {{#avoidCopy?}}const {{dataType}}&{{/avoidCopy?}}
+ {{^avoidCopy?}}{{dataType}}{{/avoidCopy?}} {{paramName}}
+ {{/allParams}});
+ {{#returns?}}
+ virtual {{className}}Job();
+
+ {{#returns}}
+ {{type}} {{name}}() const;
+ {{/returns}}
+
+ protected:
+ {{#returnFile?}}
+ Status parseReply(QByteArray data) override;
+ {{/returnFile?}}
+ {{^returnFile}}
+ Status parseJson(const JsonDocument& data) override;
+ {{/returnFile}}
+
+ private:
+ class Private;
+ Private* d;
+ {{/returns?}}
+ };
+ {{/operation}}
+{{/operations}}
+} // namespace QMatrixClient