1
1
#include " classbuilder.h"
2
2
#include < QJsonArray>
3
- #include < QDebug>
4
3
5
4
ClassBuilder::ClassBuilder () :
6
5
classes(),
7
6
methods(),
8
- defaultExcept(" QObject*" )
7
+ defaultExcept(QStringLiteral( " QObject*" ) )
9
8
{}
10
9
11
10
void ClassBuilder::build ()
12
11
{
13
- if (root[" type" ].toString () == " api" )
12
+ if (root[QStringLiteral ( " type" ) ].toString () == QStringLiteral ( " api" ) )
14
13
generateApi ();
15
- else if (root[" type" ].toString () == " class" )
14
+ else if (root[QStringLiteral ( " type" ) ].toString () == QStringLiteral ( " class" ) )
16
15
generateClass ();
17
16
else
18
- throw QStringLiteral (" REST_API_CLASSES must be either of type api or class" );
17
+ throw tr (" REST_API_CLASSES must be either of type api or class" );
19
18
}
20
19
21
20
QString ClassBuilder::specialPrefix ()
@@ -25,19 +24,17 @@ QString ClassBuilder::specialPrefix()
25
24
26
25
QString ClassBuilder::expr (const QString &expression)
27
26
{
28
- if (expression.startsWith (' $' ))
27
+ if (expression.startsWith (QLatin1Char ( ' $' ) ))
29
28
return expression.mid (1 );
30
29
else
31
- return ' "' + expression + ' "' ;
30
+ return QLatin1Char ( ' "' ) + expression + QLatin1Char ( ' "' ) ;
32
31
}
33
32
34
33
void ClassBuilder::generateClass ()
35
34
{
36
- qInfo () << " generating class:" << className;
37
-
38
35
readClasses ();
39
36
readMethods ();
40
- auto parent = root[" parent" ].toString (" QObject" );
37
+ auto parent = root[QStringLiteral ( " parent" ) ].toString (QStringLiteral ( " QObject" ) );
41
38
42
39
// write header
43
40
writeClassBeginDeclaration (parent);
@@ -51,11 +48,9 @@ void ClassBuilder::generateClass()
51
48
52
49
void ClassBuilder::generateApi ()
53
50
{
54
- qInfo () << " generating api:" << className;
55
-
56
51
readClasses ();
57
52
readMethods ();
58
- auto parent = root[" parent" ].toString (" QObject" );
53
+ auto parent = root[QStringLiteral ( " parent" ) ].toString (QStringLiteral ( " QObject" ) );
59
54
60
55
// write header
61
56
writeClassBeginDeclaration (parent);
@@ -77,7 +72,7 @@ void ClassBuilder::generateApi()
77
72
writeClassMainDefinition (parent);
78
73
79
74
// write API generation
80
- auto globalName = root[" globalName" ].toString ();
75
+ auto globalName = root[QStringLiteral ( " globalName" ) ].toString ();
81
76
if (!globalName.isEmpty ())
82
77
writeGlobalApiGeneration (globalName);
83
78
else
@@ -87,10 +82,10 @@ void ClassBuilder::generateApi()
87
82
void ClassBuilder::writeClassBeginDeclaration (const QString &parent)
88
83
{
89
84
auto includes = readIncludes ();
90
- includes.append (" QtRestClient/restclient.h" );
91
- includes.append (" QtRestClient/restclass.h" );
92
- includes.append (" QtCore/qstring.h" );
93
- includes.append (" QtCore/qstringlist.h" );
85
+ includes.append (QStringLiteral ( " QtRestClient/restclient.h" ) );
86
+ includes.append (QStringLiteral ( " QtRestClient/restclass.h" ) );
87
+ includes.append (QStringLiteral ( " QtCore/qstring.h" ) );
88
+ includes.append (QStringLiteral ( " QtCore/qstringlist.h" ) );
94
89
95
90
writeIncludes (header, includes);
96
91
header << " class " << exportedClassName << " : public " << parent << " \n "
@@ -123,7 +118,7 @@ void ClassBuilder::writeClassBeginDefinition()
123
118
<< " #include <QtCore/qtimer.h>\n "
124
119
<< " #include <QtCore/qpointer.h>\n "
125
120
<< " using namespace QtRestClient;\n\n "
126
- << " const QString " << className << " ::Path(" << expr (root[" path" ].toString ()) << " );\n " ;
121
+ << " const QString " << className << " ::Path(" << expr (root[QStringLiteral ( " path" ) ].toString ()) << " );\n " ;
127
122
generateFactoryDefinition ();
128
123
}
129
124
@@ -155,33 +150,33 @@ void ClassBuilder::writeClassMainDefinition(const QString &parent)
155
150
156
151
void ClassBuilder::readClasses ()
157
152
{
158
- auto cls = root[" classes" ].toObject ();
153
+ auto cls = root[QStringLiteral ( " classes" ) ].toObject ();
159
154
for (auto it = cls.constBegin (); it != cls.constEnd (); it++)
160
155
classes.insert (it.key (), it.value ().toString ());
161
156
}
162
157
163
158
void ClassBuilder::readMethods ()
164
159
{
165
- defaultExcept = root[" except" ].toString (defaultExcept);
166
- auto member = root[" methods" ].toObject ();
160
+ defaultExcept = root[QStringLiteral ( " except" ) ].toString (defaultExcept);
161
+ auto member = root[QStringLiteral ( " methods" ) ].toObject ();
167
162
for (auto it = member.constBegin (); it != member.constEnd (); it++) {
168
163
auto obj = it.value ().toObject ();
169
164
MethodInfo info;
170
- info.path = obj[" path" ].toString (info.path );
171
- info.url = obj[" url" ].toString (info.url );
165
+ info.path = obj[QStringLiteral ( " path" ) ].toString (info.path );
166
+ info.url = obj[QStringLiteral ( " url" ) ].toString (info.url );
172
167
if (!info.path .isEmpty () && !info.url .isEmpty ())
173
- throw QStringLiteral (" You can only use either path or url, not both!" );
174
- info.verb = obj[" verb" ].toString (info.verb );
175
- foreach (auto value, obj[" pathParams" ].toArray ())
168
+ throw tr (" You can only use either path or url, not both!" );
169
+ info.verb = obj[QStringLiteral ( " verb" ) ].toString (info.verb );
170
+ foreach (auto value, obj[QStringLiteral ( " pathParams" ) ].toArray ())
176
171
info.pathParams .append (value.toString ());
177
- foreach (auto value, obj[" parameters" ].toArray ())
172
+ foreach (auto value, obj[QStringLiteral ( " parameters" ) ].toArray ())
178
173
info.parameters .append (value.toString ());
179
- auto headers = obj[" headers" ].toObject ();
174
+ auto headers = obj[QStringLiteral ( " headers" ) ].toObject ();
180
175
for (auto jt = headers.constBegin (); jt != headers.constEnd (); jt++)
181
176
info.headers .insert (jt.key (), jt.value ().toString ());
182
- info.body = obj[" body" ].toString (info.body );
183
- info.returns = obj[" returns" ].toString (info.returns );
184
- info.except = obj[" except" ].toString (defaultExcept);
177
+ info.body = obj[QStringLiteral ( " body" ) ].toString (info.body );
178
+ info.returns = obj[QStringLiteral ( " returns" ) ].toString (info.returns );
179
+ info.except = obj[QStringLiteral ( " except" ) ].toString (defaultExcept);
185
180
186
181
methods.insert (it.key (), info);
187
182
}
@@ -224,12 +219,12 @@ void ClassBuilder::writeMethodDeclarations()
224
219
header << " \t QtRestClient::GenericRestReply<" << it->returns << " , " << it->except << " > *" << it.key () << " (" ;
225
220
QStringList parameters;
226
221
if (!it->body .isEmpty ())
227
- parameters.append (it->body + " __body" );
222
+ parameters.append (it->body + QStringLiteral ( " __body" ) );
228
223
foreach (auto path, it->pathParams )
229
224
parameters.append (path.write (true ));
230
225
foreach (auto param, it->parameters )
231
226
parameters.append (param.write (true ));
232
- header << parameters.join (" , " ) << " );\n " ;
227
+ header << parameters.join (QStringLiteral ( " , " ) ) << " );\n " ;
233
228
}
234
229
if (!methods.isEmpty ())
235
230
header << ' \n ' ;
@@ -283,12 +278,12 @@ void ClassBuilder::writeMethodDefinitions()
283
278
source << " \n QtRestClient::GenericRestReply<" << it->returns << " , " << it->except << " > *" << className << " ::" << it.key () << " (" ;
284
279
QStringList parameters;
285
280
if (!it->body .isEmpty ())
286
- parameters.append (it->body + " __body" );
281
+ parameters.append (it->body + QStringLiteral ( " __body" ) );
287
282
foreach (auto path, it->pathParams )
288
283
parameters.append (path.write (false ));
289
284
foreach (auto param, it->parameters )
290
285
parameters.append (param.write (false ));
291
- source << parameters.join (" , " ) << " )\n "
286
+ source << parameters.join (QStringLiteral ( " , " ) ) << " )\n "
292
287
<< " {\n " ;
293
288
294
289
// create parameters
@@ -361,7 +356,7 @@ void ClassBuilder::writeGlobalApiGeneration(const QString &globalName)
361
356
<< " \t return client;\n "
362
357
<< " }\n " ;
363
358
364
- if (root[" autoCreate" ].toBool (true )) {
359
+ if (root[QStringLiteral ( " autoCreate" ) ].toBool (true )) {
365
360
source << " \n static void __" << className << " _app_construct()\n "
366
361
<< " {\n "
367
362
<< " \t QTimer::singleShot(0, &" << className << " ::factory);\n "
@@ -373,14 +368,14 @@ void ClassBuilder::writeGlobalApiGeneration(const QString &globalName)
373
368
void ClassBuilder::writeApiCreation ()
374
369
{
375
370
source << " \t\t client = new RestClient(QCoreApplication::instance());\n "
376
- << " \t\t client->setBaseUrl(QUrl(" << expr (root[" baseUrl" ].toString ()) << " ));\n " ;
377
- auto version = root[" apiVersion" ].toString ();
371
+ << " \t\t client->setBaseUrl(QUrl(" << expr (root[QStringLiteral ( " baseUrl" ) ].toString ()) << " ));\n " ;
372
+ auto version = root[QStringLiteral ( " apiVersion" ) ].toString ();
378
373
if (!version.isEmpty ())
379
374
source << " \t\t client->setApiVersion(QVersionNumber::fromString(" << expr (version) << " ));\n " ;
380
- auto headers = root[" headers" ].toObject ();
375
+ auto headers = root[QStringLiteral ( " headers" ) ].toObject ();
381
376
for (auto it = headers.constBegin (); it != headers.constEnd (); it++)
382
377
source << " \t\t client->addGlobalHeader(\" " << it.key () << " \" , " << expr (it.value ().toString ()) << " );\n " ;
383
- auto parameters = root[" parameters" ].toObject ();
378
+ auto parameters = root[QStringLiteral ( " parameters" ) ].toObject ();
384
379
for (auto it = parameters.constBegin (); it != parameters.constEnd (); it++)
385
380
source << " \t\t client->addGlobalParameter(\" " << it.key () << " \" , " << expr (it.value ().toString ()) << " );\n " ;
386
381
}
@@ -406,20 +401,20 @@ bool ClassBuilder::writeMethodPath(const MethodInfo &info)
406
401
407
402
ClassBuilder::MethodInfo::MethodInfo () :
408
403
path(),
409
- verb(" GET" ),
404
+ verb(QStringLiteral( " GET" ) ),
410
405
pathParams(),
411
406
parameters(),
412
407
headers(),
413
408
body(),
414
- returns(" QObject*" ),
415
- except(" QObject*" )
409
+ returns(QStringLiteral( " QObject*" ) ),
410
+ except(QStringLiteral( " QObject*" ) )
416
411
{}
417
412
418
413
ClassBuilder::MethodInfo::Parameter::Parameter (const QString &data)
419
414
{
420
- auto param = data.split (' ;' );
415
+ auto param = data.split (QLatin1Char ( ' ;' ) );
421
416
if (param.size () < 2 || param.size () > 3 )
422
- throw QStringLiteral (" Element in pathParams must be of format \" name;type[;default]>\" " );
417
+ throw tr (" Element in pathParams must be of format \" name;type[;default]>\" " );
423
418
type = param[1 ];
424
419
name = param[0 ];
425
420
if (param.size () == 3 )
@@ -428,8 +423,8 @@ ClassBuilder::MethodInfo::Parameter::Parameter(const QString &data)
428
423
429
424
QString ClassBuilder::MethodInfo::Parameter::write (bool withDefault) const
430
425
{
431
- QString res = type + ' ' + name;
426
+ QString res = type + QLatin1Char ( ' ' ) + name;
432
427
if (withDefault && !defaultValue.isEmpty ())
433
- res += " = " + defaultValue;
428
+ res += QStringLiteral ( " = " ) + defaultValue;
434
429
return res;
435
430
}
0 commit comments