Skip to content

Commit

Permalink
Updates to allow the setting of the dateTime format string (OpenAPITo…
Browse files Browse the repository at this point in the history
  • Loading branch information
etherealjoy authored and michaelpro1 committed May 7, 2020
1 parent 046f366 commit ea5f73c
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,49 @@
namespace {{this}} {
{{/cppNamespaceDeclarations}}

class {{prefix}}SerializerSettings {
public:
static void setDateTimeFormat(const QString & dtFormat){
getInstance()->dateTimeFormat = dtFormat;
}
static QString getDateTimeFormat() {
return getInstance()->dateTimeFormat;
}
static {{prefix}}SerializerSettings *getInstance(){
if(instance == nullptr){
instance = new {{prefix}}SerializerSettings();
}
return instance;
}
private:
explicit {{prefix}}SerializerSettings(){
instance = this;
dateTimeFormat.clear();
}
static {{prefix}}SerializerSettings *instance;
QString dateTimeFormat;
};

{{prefix}}SerializerSettings * {{prefix}}SerializerSettings::instance = nullptr;

bool setDateTimeFormat(const QString& dateTimeFormat){
bool success = false;
auto dt = QDateTime::fromString(QDateTime::currentDateTime().toString(dateTimeFormat), dateTimeFormat);
if(dt.isValid()){
success = true;
{{prefix}}SerializerSettings::setDateTimeFormat(dateTimeFormat);
}
return success;
}


QString toStringValue(const QString &value) {
return value;
}

QString toStringValue(const QDateTime &value) {
// ISO 8601
return value.toString("yyyy-MM-ddTHH:mm:ss[Z|[+|-]HH:mm]");
return {{prefix}}SerializerSettings::getInstance()->getDateTimeFormat().isEmpty()? value.toString(Qt::ISODate):value.toString({{prefix}}SerializerSettings::getInstance()->getDateTimeFormat());
}

QString toStringValue(const QByteArray &value) {
Expand Down Expand Up @@ -62,7 +98,7 @@ QJsonValue toJsonValue(const QString &value) {
}

QJsonValue toJsonValue(const QDateTime &value) {
return QJsonValue(value.toString(Qt::ISODate));
return QJsonValue(value.toString({{prefix}}SerializerSettings::getInstance()->getDateTimeFormat().isEmpty()?value.toString(Qt::ISODate):value.toString({{prefix}}SerializerSettings::getInstance()->getDateTimeFormat())));
}

QJsonValue toJsonValue(const QByteArray &value) {
Expand Down Expand Up @@ -115,7 +151,7 @@ bool fromStringValue(const QString &inStr, QDateTime &value) {
if (inStr.isEmpty()) {
return false;
} else {
auto dateTime = QDateTime::fromString(inStr, "yyyy-MM-ddTHH:mm:ss[Z|[+|-]HH:mm]");
auto dateTime = {{prefix}}SerializerSettings::getInstance()->getDateTimeFormat().isEmpty()?QDateTime::fromString(inStr, Qt::ISODate) :QDateTime::fromString(inStr, {{prefix}}SerializerSettings::getInstance()->getDateTimeFormat());
if (dateTime.isValid()) {
value.setDate(dateTime.date());
value.setTime(dateTime.time());
Expand Down Expand Up @@ -220,7 +256,7 @@ bool fromJsonValue(QString &value, const QJsonValue &jval) {
bool fromJsonValue(QDateTime &value, const QJsonValue &jval) {
bool ok = true;
if (!jval.isUndefined() && !jval.isNull() && jval.isString()) {
value = QDateTime::fromString(jval.toString(), Qt::ISODate);
value = {{prefix}}SerializerSettings::getInstance()->getDateTimeFormat().isEmpty()?QDateTime::fromString(jval.toString(), Qt::ISODate): QDateTime::fromString(jval.toString(), {{prefix}}SerializerSettings::getInstance()->getDateTimeFormat());
ok = value.isValid();
} else {
ok = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
namespace {{this}} {
{{/cppNamespaceDeclarations}}

bool setDateTimeFormat(const QString&);

template <typename T>
QString toStringValue(const QList<T> &val);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,49 @@
namespace {{this}} {
{{/cppNamespaceDeclarations}}

class {{prefix}}SerializerSettings {
public:
static void setDateTimeFormat(const QString & dtFormat){
getInstance()->dateTimeFormat = dtFormat;
}
static QString getDateTimeFormat() {
return getInstance()->dateTimeFormat;
}
static {{prefix}}SerializerSettings *getInstance(){
if(instance == nullptr){
instance = new {{prefix}}SerializerSettings();
}
return instance;
}
private:
explicit {{prefix}}SerializerSettings(){
instance = this;
dateTimeFormat.clear();
}
static {{prefix}}SerializerSettings *instance;
QString dateTimeFormat;
};

{{prefix}}SerializerSettings * {{prefix}}SerializerSettings::instance = nullptr;

bool setDateTimeFormat(const QString& dateTimeFormat){
bool success = false;
auto dt = QDateTime::fromString(QDateTime::currentDateTime().toString(dateTimeFormat), dateTimeFormat);
if(dt.isValid()){
success = true;
{{prefix}}SerializerSettings::setDateTimeFormat(dateTimeFormat);
}
return success;
}


QString toStringValue(const QString &value) {
return value;
}

QString toStringValue(const QDateTime &value) {
// ISO 8601
return value.toString("yyyy-MM-ddTHH:mm:ss[Z|[+|-]HH:mm]");
return {{prefix}}SerializerSettings::getInstance()->getDateTimeFormat().isEmpty()? value.toString(Qt::ISODate):value.toString({{prefix}}SerializerSettings::getInstance()->getDateTimeFormat());
}

QString toStringValue(const QByteArray &value) {
Expand Down Expand Up @@ -62,7 +98,7 @@ QJsonValue toJsonValue(const QString &value) {
}

QJsonValue toJsonValue(const QDateTime &value) {
return QJsonValue(value.toString(Qt::ISODate));
return QJsonValue(value.toString({{prefix}}SerializerSettings::getInstance()->getDateTimeFormat().isEmpty()?value.toString(Qt::ISODate):value.toString({{prefix}}SerializerSettings::getInstance()->getDateTimeFormat())));
}

QJsonValue toJsonValue(const QByteArray &value) {
Expand Down Expand Up @@ -115,7 +151,7 @@ bool fromStringValue(const QString &inStr, QDateTime &value) {
if (inStr.isEmpty()) {
return false;
} else {
auto dateTime = QDateTime::fromString(inStr, "yyyy-MM-ddTHH:mm:ss[Z|[+|-]HH:mm]");
auto dateTime = {{prefix}}SerializerSettings::getInstance()->getDateTimeFormat().isEmpty()?QDateTime::fromString(inStr, Qt::ISODate) :QDateTime::fromString(inStr, {{prefix}}SerializerSettings::getInstance()->getDateTimeFormat());
if (dateTime.isValid()) {
value.setDate(dateTime.date());
value.setTime(dateTime.time());
Expand Down Expand Up @@ -220,7 +256,7 @@ bool fromJsonValue(QString &value, const QJsonValue &jval) {
bool fromJsonValue(QDateTime &value, const QJsonValue &jval) {
bool ok = true;
if (!jval.isUndefined() && !jval.isNull() && jval.isString()) {
value = QDateTime::fromString(jval.toString(), Qt::ISODate);
value = {{prefix}}SerializerSettings::getInstance()->getDateTimeFormat().isEmpty()?QDateTime::fromString(jval.toString(), Qt::ISODate): QDateTime::fromString(jval.toString(), {{prefix}}SerializerSettings::getInstance()->getDateTimeFormat());
ok = value.isValid();
} else {
ok = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
namespace {{this}} {
{{/cppNamespaceDeclarations}}

bool setDateTimeFormat(const QString&);

template <typename T>
QString toStringValue(const QList<T> &val);

Expand Down
2 changes: 2 additions & 0 deletions samples/client/petstore/cpp-qt5/PetStore/main.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include <QCoreApplication>
#include <QTest>

#include "PFXHelpers.h"
#include "PetApiTests.h"
#include "StoreApiTests.h"
#include "UserApiTests.h"

int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
::test_namespace::setDateTimeFormat("yyyy-MM-ddTHH:mm:ss.zzzZ");
PetApiTests petApiTests;
StoreApiTests storeApiTests;
UserApiTests userApiTests;
Expand Down
44 changes: 40 additions & 4 deletions samples/client/petstore/cpp-qt5/client/PFXHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,49 @@

namespace test_namespace {

class PFXSerializerSettings {
public:
static void setDateTimeFormat(const QString & dtFormat){
getInstance()->dateTimeFormat = dtFormat;
}
static QString getDateTimeFormat() {
return getInstance()->dateTimeFormat;
}
static PFXSerializerSettings *getInstance(){
if(instance == nullptr){
instance = new PFXSerializerSettings();
}
return instance;
}
private:
explicit PFXSerializerSettings(){
instance = this;
dateTimeFormat.clear();
}
static PFXSerializerSettings *instance;
QString dateTimeFormat;
};

PFXSerializerSettings * PFXSerializerSettings::instance = nullptr;

bool setDateTimeFormat(const QString& dateTimeFormat){
bool success = false;
auto dt = QDateTime::fromString(QDateTime::currentDateTime().toString(dateTimeFormat), dateTimeFormat);
if(dt.isValid()){
success = true;
PFXSerializerSettings::setDateTimeFormat(dateTimeFormat);
}
return success;
}


QString toStringValue(const QString &value) {
return value;
}

QString toStringValue(const QDateTime &value) {
// ISO 8601
return value.toString("yyyy-MM-ddTHH:mm:ss[Z|[+|-]HH:mm]");
return PFXSerializerSettings::getInstance()->getDateTimeFormat().isEmpty()? value.toString(Qt::ISODate):value.toString(PFXSerializerSettings::getInstance()->getDateTimeFormat());
}

QString toStringValue(const QByteArray &value) {
Expand Down Expand Up @@ -70,7 +106,7 @@ QJsonValue toJsonValue(const QString &value) {
}

QJsonValue toJsonValue(const QDateTime &value) {
return QJsonValue(value.toString(Qt::ISODate));
return QJsonValue(value.toString(PFXSerializerSettings::getInstance()->getDateTimeFormat().isEmpty()?value.toString(Qt::ISODate):value.toString(PFXSerializerSettings::getInstance()->getDateTimeFormat())));
}

QJsonValue toJsonValue(const QByteArray &value) {
Expand Down Expand Up @@ -123,7 +159,7 @@ bool fromStringValue(const QString &inStr, QDateTime &value) {
if (inStr.isEmpty()) {
return false;
} else {
auto dateTime = QDateTime::fromString(inStr, "yyyy-MM-ddTHH:mm:ss[Z|[+|-]HH:mm]");
auto dateTime = PFXSerializerSettings::getInstance()->getDateTimeFormat().isEmpty()?QDateTime::fromString(inStr, Qt::ISODate) :QDateTime::fromString(inStr, PFXSerializerSettings::getInstance()->getDateTimeFormat());
if (dateTime.isValid()) {
value.setDate(dateTime.date());
value.setTime(dateTime.time());
Expand Down Expand Up @@ -228,7 +264,7 @@ bool fromJsonValue(QString &value, const QJsonValue &jval) {
bool fromJsonValue(QDateTime &value, const QJsonValue &jval) {
bool ok = true;
if (!jval.isUndefined() && !jval.isNull() && jval.isString()) {
value = QDateTime::fromString(jval.toString(), Qt::ISODate);
value = PFXSerializerSettings::getInstance()->getDateTimeFormat().isEmpty()?QDateTime::fromString(jval.toString(), Qt::ISODate): QDateTime::fromString(jval.toString(), PFXSerializerSettings::getInstance()->getDateTimeFormat());
ok = value.isValid();
} else {
ok = false;
Expand Down
2 changes: 2 additions & 0 deletions samples/client/petstore/cpp-qt5/client/PFXHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

namespace test_namespace {

bool setDateTimeFormat(const QString&);

template <typename T>
QString toStringValue(const QList<T> &val);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,49 @@

namespace OpenAPI {

class OAISerializerSettings {
public:
static void setDateTimeFormat(const QString & dtFormat){
getInstance()->dateTimeFormat = dtFormat;
}
static QString getDateTimeFormat() {
return getInstance()->dateTimeFormat;
}
static OAISerializerSettings *getInstance(){
if(instance == nullptr){
instance = new OAISerializerSettings();
}
return instance;
}
private:
explicit OAISerializerSettings(){
instance = this;
dateTimeFormat.clear();
}
static OAISerializerSettings *instance;
QString dateTimeFormat;
};

OAISerializerSettings * OAISerializerSettings::instance = nullptr;

bool setDateTimeFormat(const QString& dateTimeFormat){
bool success = false;
auto dt = QDateTime::fromString(QDateTime::currentDateTime().toString(dateTimeFormat), dateTimeFormat);
if(dt.isValid()){
success = true;
OAISerializerSettings::setDateTimeFormat(dateTimeFormat);
}
return success;
}


QString toStringValue(const QString &value) {
return value;
}

QString toStringValue(const QDateTime &value) {
// ISO 8601
return value.toString("yyyy-MM-ddTHH:mm:ss[Z|[+|-]HH:mm]");
return OAISerializerSettings::getInstance()->getDateTimeFormat().isEmpty()? value.toString(Qt::ISODate):value.toString(OAISerializerSettings::getInstance()->getDateTimeFormat());
}

QString toStringValue(const QByteArray &value) {
Expand Down Expand Up @@ -70,7 +106,7 @@ QJsonValue toJsonValue(const QString &value) {
}

QJsonValue toJsonValue(const QDateTime &value) {
return QJsonValue(value.toString(Qt::ISODate));
return QJsonValue(value.toString(OAISerializerSettings::getInstance()->getDateTimeFormat().isEmpty()?value.toString(Qt::ISODate):value.toString(OAISerializerSettings::getInstance()->getDateTimeFormat())));
}

QJsonValue toJsonValue(const QByteArray &value) {
Expand Down Expand Up @@ -123,7 +159,7 @@ bool fromStringValue(const QString &inStr, QDateTime &value) {
if (inStr.isEmpty()) {
return false;
} else {
auto dateTime = QDateTime::fromString(inStr, "yyyy-MM-ddTHH:mm:ss[Z|[+|-]HH:mm]");
auto dateTime = OAISerializerSettings::getInstance()->getDateTimeFormat().isEmpty()?QDateTime::fromString(inStr, Qt::ISODate) :QDateTime::fromString(inStr, OAISerializerSettings::getInstance()->getDateTimeFormat());
if (dateTime.isValid()) {
value.setDate(dateTime.date());
value.setTime(dateTime.time());
Expand Down Expand Up @@ -228,7 +264,7 @@ bool fromJsonValue(QString &value, const QJsonValue &jval) {
bool fromJsonValue(QDateTime &value, const QJsonValue &jval) {
bool ok = true;
if (!jval.isUndefined() && !jval.isNull() && jval.isString()) {
value = QDateTime::fromString(jval.toString(), Qt::ISODate);
value = OAISerializerSettings::getInstance()->getDateTimeFormat().isEmpty()?QDateTime::fromString(jval.toString(), Qt::ISODate): QDateTime::fromString(jval.toString(), OAISerializerSettings::getInstance()->getDateTimeFormat());
ok = value.isValid();
} else {
ok = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

namespace OpenAPI {

bool setDateTimeFormat(const QString&);

template <typename T>
QString toStringValue(const QList<T> &val);

Expand Down

0 comments on commit ea5f73c

Please sign in to comment.