Revision control

Copy as Markdown

{{>licenseInfo}}
/**
* Representing a Server configuration.
*/
#ifndef {{prefix}}_SERVERVCONFIGURATION_H
#define {{prefix}}_SERVERVCONFIGURATION_H
#include "{{prefix}}ServerVariable.h"
#include <QtCore/qstring.h>
#include <QtCore/qmap.h>
#include <QtCore/qregularexpression.h>
#include <QtCore/qurl.h>
#include <stdexcept>
{{#cppNamespaceDeclarations}}
namespace {{this}} {
{{/cppNamespaceDeclarations}}
class {{prefix}}ServerConfiguration {
public:
/**
* @param url A URL to the target host.
* @param description A description of the host designated by the URL.
* @param variables A map between a variable name and its value. The value is used for substitution in the server's URL template.
*/
{{prefix}}ServerConfiguration(const QUrl &url, const QString &description, const QMap<QString, {{prefix}}ServerVariable> &variables)
: m_description(description),
m_variables(variables),
m_url(url) {}
{{prefix}}ServerConfiguration() {}
~{{prefix}}ServerConfiguration() {}
/**
* Original URL
*
* @return Original URL.
*/
const QUrl serverUrl() const { return m_url; }
/**
* Format URL template using server's variables.
*
* @return Formatted URL.
*/
QString URL() {
QString url = m_url.toString();
if (!m_variables.empty()) {
// go through variables and replace placeholders
for (auto const& v : m_variables.keys()) {
QString name = v;
{{prefix}}ServerVariable serverVariable = m_variables.value(v);
QString value = serverVariable.m_defaultValue;
if (!serverVariable.m_enumValues.empty() && !serverVariable.m_enumValues.contains(value)) {
throw std::runtime_error(QString("The variable " + name + " in the server URL has invalid value " + value + ".").toUtf8().toStdString());
}
QRegularExpression regex(QString("\\{" + name + "\\}"));
url = url.replace(regex, value);
}
return url;
}
return url;
}
{{prefix}}ServerVariable::ServerError setDefaultValue(const QString &variable, const QString &value) {
if (m_variables.contains(variable))
return m_variables[variable].setDefaultValue(value);
return {{prefix}}ServerVariable::ServerError::ServerVariableNotFound;
}
QString m_description;
QMap<QString, {{prefix}}ServerVariable> m_variables;
QUrl m_url;
};
{{#cppNamespaceDeclarations}}
} // namespace {{this}}
{{/cppNamespaceDeclarations}}
#endif // {{prefix}}_SERVERVCONFIGURATION_H