Utility class to transform strings depending on json object values. More...
#include <TransformEngine.h>
Collaboration diagram for TransformEngine:Public Member Functions | |
| bool | setTemplateString (QString templateString) |
| set the current template string | |
| TransformEngine () | |
| Constructor set up the date and time. | |
| TransformEngine (QDate currentDate, QTime currentTime) | |
| Use this constructor for test purpose only (to fix the date and be able to compare output) | |
| bool | transformToFile (const QJsonObject &data, const QString &filename, bool overwrite=true) |
| transform the current template string using the given data and save it to filename | |
| QString | transformToString (const QString &templateString, const QJsonObject &data) |
| transform the given template using the given data to a string. | |
Utility class to transform strings depending on json object values.
This is inspired by inja but is much much much more basic.
The template string can contains loops, conditions and variable fields.
Example using templateStr
and data the given json data:
then calling:
transformToString(templateStr, data)
will produce:
The following utility functions can be used in the template file (see example table below):
| Input | Function | Output |
|---|---|---|
| "my class name! 123 (test)" | title | "My Class Name! 123 (test)" |
| "my class name! 123 (test)" | lowerCamelCase | "myClassName123Test" |
| "my class name! 123 (test)" | upperCamelCase | "MyClassName123Test" |
| "my class name! 123 (test)" | lowerSnakeCase | "my_class_name_123_test" |
| "my class name! 123 (test)" | upperSnakeCase | "MY_CLASS_NAME_123_TEST" |
| "my class name! 123 (test)" | kebabCase | "my-class-name-123-test" |
| "my class name! 123 (test)" | joinKebabCase | "myclassname123test" |
Used for array to loop over each value
Loop inside loop are supported using @subfor@..@endsubfor@
In if statement you can use the contains and equals as well (this can also be used in loops):
@if@ contains($field$) ... @endif@ → check if data contains field@if@ contains($object.field$) ... @endif@ → check if data contains object that contains field@if@ equals($a$,"yes") yes @else@ no @endif@ → check if the value of data a is equals to "yes"@for@ $i$ in $a$ @if@ equals($i.b$,"foo") $index(i)$ has b==foo ($i.b$), @else@ $index(i)$ does not have b==foo, @endif@ @endfor@ → check if each value of i in a are equals to "foo"@if@ $parameter$ ...@endif@ → check if parameter can be evaluated to true, i.e.:The main algorithm has three steps
| TransformEngine::TransformEngine | ( | ) |
Constructor set up the date and time.
| TransformEngine::TransformEngine | ( | QDate | currentDate, |
| QTime | currentTime | ||
| ) |
Use this constructor for test purpose only (to fix the date and be able to compare output)
| bool TransformEngine::setTemplateString | ( | QString | templateString | ) |
set the current template string
Referenced by ExtensionGenerator::generate().
Here is the caller graph for this function:| bool TransformEngine::transformToFile | ( | const QJsonObject & | data, |
| const QString & | filename, | ||
| bool | overwrite = true |
||
| ) |
transform the current template string using the given data and save it to filename
| data | QJsonObject that contains the data used to transform the template |
| filename | the filename to use |
| overwrite | if false, the file is not overwritten if it already exists (default: true) |
References transformToString().
Referenced by ExtensionGenerator::generate().
Here is the call graph for this function:
Here is the caller graph for this function:| QString TransformEngine::transformToString | ( | const QString & | templateString, |
| const QJsonObject & | data | ||
| ) |
transform the given template using the given data to a string.
You can use this method as a utility function it does not modify the class attributes apart from temporary loop counters
Referenced by camitk::CppHotPlugAction::CppHotPlugAction(), camitk::HotPlugAction::HotPlugAction(), camitk::HotPlugActionExtension::HotPlugActionExtension(), and transformToFile().
Here is the caller graph for this function: