1
0
Fork 0
mirror of https://github.com/HenkKalkwater/harbour-sailfin.git synced 2025-09-05 10:12:46 +00:00

Extract OpenAPI writeflns to template files

With a few exceptions if the code would actually get larger
This commit is contained in:
Chris Josten 2021-03-19 23:01:29 +01:00
parent 96e19548d7
commit 05f79197eb
4 changed files with 166 additions and 133 deletions

View file

@ -0,0 +1,34 @@
class {{className}}Class {
Q_GADGET
public:
enum Value {
EnumNotSet,
{{#each values as |value|}}
{{value}},
{{/each}}
};
Q_ENUM(Value)
private:
explicit {{className}}Class();
};
typedef {{className}} Class::Value {{className}};
template <>
{{className}} fromJsonValue<{{className}}>(QJsonValue source) {
if (!source.isString()) return {{className}}Class::EnumNotSet;
QString str = source.toString();
{{#each values as |value|}}
if (str == QStringLiteral("{{value}}")) {
return {{className}}Class::{{value}};
}
{{/each}}
return {{className}}Class::EnumNotSet;
}

View file

@ -0,0 +1,57 @@
{{#each userImports as |userClass|}}
{{#if userClass != className}}
class {{className}};
{{/if}}
{{/each}}
class {{className}} : public QObject {
Q_OBJECT
public:
explicit {{className}}(QObject *parent = nullptr);
static {{className}} *fromJSON(QJsonObject source, QObject *parent = nullptr);
void updateFromJSON(QJsonObject source, bool emitSignals = true);
QJsonObject toJSON();
// Properties
{{#each properties as |p|}}
{{#if p.description.length > 0}}
/**
* @brief {{p.description}}
*/
{{/if}}
Q_PROPERTY({{p.typeNameWithQualifiers}} {{p.name}} READ {{p.name}} WRITE set{{p.writeName}} NOTIFY {{p.name}}Changed)
{{/each}}
// Getters
{{#each properties as |p|}}
{{p.typeNameWithQualifiers}} {{p.name}}() const;
void set{{p.writeName}}({{p.typeNameWithQualifiers}} new{{p.writeName}});
{{/each}}
// Signals
signals:
{{#each properties as |p|}}
void {{p.name}}Changed({{p.typeNameWithQualifiers}} new{{p.writeName}});
{{/each}}
protected:
{{#each properties as |p|}}
{{#if p.defaultInitializer.length > 0}}
{{p.typeNameWithQualifiers}} {{p.memberName}} = {{p.defaultInitializer}};
{{else}}
{{p.typeNameWithQualifiers}} {{p.memberName}};
{{/if}}
{{/each}}
};

View file

@ -0,0 +1,49 @@
{{className}}::{{className}}(QObject *parent) : QObject(parent) {}
{{className}} *{{className}}::fromJSON(QJsonObject source, QObject *parent) {
{{className}} *instance = new {{className}}(parent);
instance->updateFromJSON(source, false);
return instance;
}
void {{className}}::updateFromJSON(QJsonObject source, bool emitSignals) {
{{#each properties as |property|}}
{{property.memberName}} = fromJsonValue<{{property.typeNameWithQualifiers}}>(source["{{property.originalName}}"]);
{{/each}}
if (emitSignals) {
{{#each properties as |property|}}
emit {{property.name}}Changed({{property.memberName}});
{{/each}}
}
}
QJsonObject {{className}}::toJSON() {
QJsonObject result;
{{#each properties as |property|}}
result["{{property.originalName}}"] = toJsonValue<{{property.typeNameWithQualifiers}}>({{property.memberName}});
{{/each}}
return result;
}"
{{#each properties as |property|}}
{{property.typeNameWithQualifiers}} {{className}}::{{property.name}}() const { return {{property.memberName}}; }
void {{className}}::set{{property.writeName}}({{property.typeNameWithQualifiers}} new{{property.writeName}}) {
{{property.memberName}} = new{{property.writeName}};
emit {{property.name}}Changed(new{{property.writeName}});
}
{{/each}}