template updates, tests

This commit is contained in:
Tony Tam
2015-05-15 11:27:03 -07:00
parent 622d883344
commit b4674d6fc5
23 changed files with 1088 additions and 153 deletions

View File

@@ -130,11 +130,13 @@ public class Qt5CPPGenerator extends DefaultCodegen implements CodegenConfig {
typeMapping.put("boolean", "bool");
typeMapping.put("array", "QList");
typeMapping.put("map", "QMap");
// typeMapping.put("number", "Long");
typeMapping.put("file", "SWGHttpRequestInputFileElement");
typeMapping.put("object", PREFIX + "Object");
importMapping = new HashMap<String, String>();
importMapping.put("SWGHttpRequestInputFileElement", "#include \"" + PREFIX + "HttpRequest.h\"");
namespaces = new HashMap<String, String> ();
foundationClasses.add("QString");
@@ -210,7 +212,7 @@ public class Qt5CPPGenerator extends DefaultCodegen implements CodegenConfig {
else if (p instanceof MapProperty) {
MapProperty mp = (MapProperty) p;
Property inner = mp.getAdditionalProperties();
return getSwaggerType(p) + "<String, " + getTypeDeclaration(inner) + ">*";
return getSwaggerType(p) + "<QString, " + getTypeDeclaration(inner) + ">*";
}
if(foundationClasses.contains(swaggerType))
return swaggerType + "*";
@@ -243,7 +245,7 @@ public class Qt5CPPGenerator extends DefaultCodegen implements CodegenConfig {
else if (p instanceof MapProperty) {
MapProperty ap = (MapProperty) p;
String inner = getSwaggerType(ap.getAdditionalProperties());
return "NULL";
return "new QMap<QString, " + inner + ">()";
}
else if (p instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) p;

View File

@@ -26,7 +26,7 @@ void HttpRequestInput::add_var(QString key, QString value) {
}
void HttpRequestInput::add_file(QString variable_name, QString local_filename, QString request_filename, QString mime_type) {
HttpRequestInputFileElement file;
SWGHttpRequestInputFileElement file;
file.variable_name = variable_name;
file.local_filename = local_filename;
file.request_filename = request_filename;
@@ -173,7 +173,7 @@ void HttpRequestWorker::execute(HttpRequestInput *input) {
}
// add files
for (QList<HttpRequestInputFileElement>::iterator file_info = input->files.begin(); file_info != input->files.end(); file_info++) {
for (QList<SWGHttpRequestInputFileElement>::iterator file_info = input->files.begin(); file_info != input->files.end(); file_info++) {
QFileInfo fi(file_info->local_filename);
// ensure necessary variables are available

View File

@@ -13,11 +13,10 @@
#include <QNetworkAccessManager>
#include <QNetworkReply>
enum HttpRequestVarLayout {NOT_SET, ADDRESS, URL_ENCODED, MULTIPART};
class HttpRequestInputFileElement {
class SWGHttpRequestInputFileElement {
public:
QString variable_name;
@@ -36,7 +35,7 @@ public:
HttpRequestVarLayout var_layout;
QMap<QString, QString> vars;
QMap<QString, QString> headers;
QList<HttpRequestInputFileElement> files;
QList<SWGHttpRequestInputFileElement> files;
QByteArray request_body;
HttpRequestInput();

View File

@@ -1,5 +1,6 @@
#include "{{classname}}.h"
#include "{{prefix}}Helpers.h"
#include "{{prefix}}ModelFactory.h"
#include <QJsonArray>
#include <QJsonDocument>
@@ -17,32 +18,97 @@ namespace Swagger {
{{#operations}}
{{#operation}}
void
{{classname}}::{{nickname}}({{#allParams}}{{dataType}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {
{{classname}}::{{nickname}}({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {
QString fullPath;
fullPath.append(this->host).append(this->basePath).append("{{path}}");
HttpRequestWorker *worker = new HttpRequestWorker();
HttpRequestInput input(fullPath, "{{httpMethod}}");
{{#pathParams}}
QString {{paramName}}PathParam("{"); {{paramName}}PathParam.append("{{paramName}}").append("}");
fullPath.replace({{paramName}}PathParam, stringValue({{paramName}}));
{{/pathParams}}
{{#queryParams}}if(fullPath.compare("?") > 0) fullPath.append("?");
else fullPath.append("&");
{{#formParams}}{{^isFile}}
if({{paramName}} != NULL) {
input.add_var("{{paramName}}", *{{paramName}});
}
{{/isFile}}{{/formParams}}
{{#queryParams}}
{{^collectionFormat}}
if(fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?");
fullPath.append(QUrl::toPercentEncoding("{{paramName}}"))
.append("=")
.append(QUrl::toPercentEncoding(stringValue({{paramName}})));
{{/collectionFormat}}
{{#collectionFormat}}
if({{{paramName}}}->size() > 0) {
if(QString("{{collectionFormat}}").indexOf("multi") == 0) {
foreach({{{baseType}}} t, *{{paramName}}) {
if(fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?");
fullPath.append("{{{paramName}}}=").append(stringValue(t));
}
}
else if (QString("{{collectionFormat}}").indexOf("ssv") == 0) {
if(fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?");
fullPath.append("{{paramName}}=");
qint32 count = 0;
foreach({{{baseType}}} t, *{{paramName}}) {
if(count > 0) {
fullPath.append(" ");
}
fullPath.append(stringValue(t));
}
}
else if (QString("{{collectionFormat}}").indexOf("tsv") == 0) {
if(fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?");
fullPath.append("{{paramName}}=");
qint32 count = 0;
foreach({{{baseType}}} t, *{{paramName}}) {
if(count > 0) {
fullPath.append("\t");
}
fullPath.append(stringValue(t));
}
}
}
{{/collectionFormat}}
{{/queryParams}}
HttpRequestWorker *worker = new HttpRequestWorker();
HttpRequestInput input(fullPath, "{{httpMethod}}");
{{#bodyParams}}
// body
input.request_body.append({{paramName}}.asJson());
{{/bodyParams}}
{{#isContainer}}
QJsonArray* {{paramName}}Array = new QJsonArray();
toJsonArray((QList<void*>*){{paramName}}, {{paramName}}Array, QString("body"), QString("SWGUser*"));
QJsonDocument doc(*{{paramName}}Array);
QByteArray bytes = doc.toJson();
input.request_body.append(bytes);
{{/isContainer}}
{{^isContainer}}
QString output = {{paramName}}.asJson();
input.request_body.append(output);
{{/isContainer}}{{/bodyParams}}
{{#headerParams}}
input.headers
// TODO: add header support
{{/headerParams}}
connect(worker,
@@ -71,7 +137,7 @@ void
QJsonArray jsonArray = doc.array();
foreach(QJsonValue obj, jsonArray) {
{{returnBaseType}}* o = new {{returnBaseType}}();
{{{returnBaseType}}}* o = new {{returnBaseType}}();
QJsonObject jv = obj.toObject();
QJsonObject * ptr = (QJsonObject*)&jv;
o->fromJsonObject(*ptr);
@@ -80,13 +146,29 @@ void
{{/isListContainer}}
{{^isListContainer}}{{#returnTypeIsPrimitive}}
{{returnType}} output; // TODO
{{{returnType}}} output; // TODO add primitive output support
{{/returnTypeIsPrimitive}}
{{#isMapContainer}}
{{{returnType}}} output = {{{defaultResponse}}};
QString json(worker->response);
QByteArray array (json.toStdString().c_str());
QJsonDocument doc = QJsonDocument::fromJson(array);
QJsonObject obj = doc.object();
foreach(QString key, obj.keys()) {
qint32* val;
setValue(&val, obj[key], "{{returnBaseType}}", "");
output->insert(key, *val);
}
{{/isMapContainer}}
{{^isMapContainer}}
{{^returnTypeIsPrimitive}}QString json(worker->response);
{{returnType}} output = new {{returnBaseType}}(&json);
{{{returnType}}} output = static_cast<{{{returnType}}}>(create(json, QString("{{{returnBaseType}}}")));
{{/returnTypeIsPrimitive}}
{{/isMapContainer}}
{{/isListContainer}}{{/returnType}}
worker->deleteLater();

View File

@@ -154,6 +154,11 @@ stringValue(qint32 value) {
return QString::number(value);
}
QString
stringValue(qint64 value) {
return QString::number(value);
}
QString
stringValue(bool value) {
return QString(value ? "true" : "false");

View File

@@ -10,6 +10,7 @@ namespace Swagger {
bool isCompatibleJsonValue(QString type);
QString stringValue(QString* value);
QString stringValue(qint32 value);
QString stringValue(qint64 value);
QString stringValue(bool value);
}

View File

@@ -5,14 +5,25 @@
#include "{{classname}}.h"{{/model}}{{/models}}
namespace Swagger {
void*
create(QString type) {
inline void* create(QString type) {
{{#models}}{{#model}}if(QString("{{classname}}").compare(type) == 0) {
return new {{classname}}();
}
{{/model}}{{/models}}
return NULL;
}
inline void* create(QString json, QString type) {
void* val = create(type);
if(val != NULL) {
SWGObject* obj = static_cast<SWGObject*>(val);
return obj->fromJson(json);
}
if(type.startsWith("QString")) {
return new QString();
}
return NULL;
}
} /* namespace Swagger */
#endif /* ModelFactory_H_ */