mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
Refactor Settings DB and Handling (#1786)
* Refactor config API
* Corrections
* Test Qt 6.8
* Revert "Test Qt 6.8"
This reverts commit eceebec49e
.
* Corrections 2
* Update Changelog
* Add configFilter element for getconfig call
* Do not create errors for DB updates when in read-only mode
* Have configuration migration and validation before Hyperion starts
* Correct Tests
* Corrections
* Add migration items
* Correct windows build
* Ensure that first instance as default one exists
* Remove dependency between AuthManager and SSDPHandler
* Correct typos
* Address CodeQL findings
* Replace CamkeSettings by Presets and provide debug scenarios
This commit is contained in:
@@ -7,155 +7,251 @@
|
||||
//qt includes
|
||||
#include <QRegularExpression>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonParseError>
|
||||
#include <QStringList>
|
||||
|
||||
namespace JsonUtils {
|
||||
|
||||
QPair<bool, QStringList> readFile(const QString& path, QJsonObject& obj, Logger* log, bool ignError)
|
||||
QPair<bool, QStringList> readFile(const QString& path, QJsonObject& obj, Logger* log, bool ignError)
|
||||
{
|
||||
QJsonValue value(obj);
|
||||
|
||||
QPair<bool, QStringList> result = readFile(path, value,log, ignError);
|
||||
obj = value.toObject();
|
||||
return result;
|
||||
}
|
||||
|
||||
QPair<bool, QStringList> readFile(const QString& path, QJsonValue& obj, Logger* log, bool ignError)
|
||||
{
|
||||
QString data;
|
||||
if(!FileUtils::readFile(path, data, log, ignError))
|
||||
{
|
||||
QString data;
|
||||
if(!FileUtils::readFile(path, data, log, ignError))
|
||||
return qMakePair(false, QStringList(QString("Error reading file: %1").arg(path)));
|
||||
}
|
||||
|
||||
QPair<bool, QStringList> parsingResult = JsonUtils::parse(path, data, obj, log);
|
||||
return parsingResult;
|
||||
}
|
||||
|
||||
|
||||
bool readSchema(const QString& path, QJsonObject& obj, Logger* log)
|
||||
{
|
||||
QJsonObject schema;
|
||||
if(!readFile(path, schema, log).first)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!resolveRefs(schema, obj, log))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
QPair<bool, QStringList> parse(const QString& path, const QString& data, QJsonObject& obj, Logger* log)
|
||||
{
|
||||
QJsonValue value(obj);
|
||||
|
||||
QPair<bool, QStringList> result = JsonUtils::parse(path, data, value, log);
|
||||
obj = value.toObject();
|
||||
return result;
|
||||
}
|
||||
|
||||
QPair<bool, QStringList> parse(const QString& path, const QString& data, QJsonArray& arr, Logger* log)
|
||||
{
|
||||
QJsonValue value(arr);
|
||||
|
||||
QPair<bool, QStringList> result = JsonUtils::parse(path, data, value, log);
|
||||
arr = value.toArray();
|
||||
return result;
|
||||
}
|
||||
|
||||
QPair<bool, QStringList> parse(const QString& path, const QString& data, QJsonValue& value, Logger* log)
|
||||
{
|
||||
QJsonDocument doc;
|
||||
QPair<bool, QStringList> parsingResult = JsonUtils::parse(path, data, doc, log);
|
||||
value = doc.object();
|
||||
return parsingResult;
|
||||
}
|
||||
|
||||
QPair<bool, QStringList> parse(const QString& path, const QString& data, QJsonDocument& doc, Logger* log)
|
||||
{
|
||||
QStringList errorList;
|
||||
|
||||
QJsonParseError error;
|
||||
doc = QJsonDocument::fromJson(data.toUtf8(), &error);
|
||||
|
||||
if (error.error != QJsonParseError::NoError)
|
||||
{
|
||||
int errorLine = 1;
|
||||
int errorColumn = 1;
|
||||
|
||||
int lastNewlineIndex = data.lastIndexOf("\n", error.offset - 1);
|
||||
if (lastNewlineIndex != -1)
|
||||
{
|
||||
return qMakePair(false, QStringList(QString("Error reading file: %1").arg(path)));
|
||||
errorColumn = error.offset - lastNewlineIndex ;
|
||||
}
|
||||
errorLine += data.left(error.offset).count('\n');
|
||||
|
||||
QPair<bool, QStringList> parsingResult = JsonUtils::parse(path, data, obj, log);
|
||||
return parsingResult;
|
||||
const QString errorMessage = QString("JSON parse error @(%1): %2, line: %3, column: %4, Data: '%5'")
|
||||
.arg(path)
|
||||
.arg(error.errorString())
|
||||
.arg(errorLine)
|
||||
.arg(errorColumn)
|
||||
.arg(data);
|
||||
errorList.push_back(errorMessage);
|
||||
Error(log, "%s", QSTRING_CSTR(errorMessage));
|
||||
|
||||
return qMakePair(false, errorList);
|
||||
}
|
||||
return qMakePair(true, errorList);
|
||||
}
|
||||
|
||||
QPair<bool, QStringList> validate(const QString& file, const QJsonValue& json, const QString& schemaPath, Logger* log)
|
||||
{
|
||||
// get the schema data
|
||||
QJsonObject schema;
|
||||
|
||||
QPair<bool, QStringList> readResult = readFile(schemaPath, schema, log);
|
||||
if(!readResult.first)
|
||||
{
|
||||
return readResult;
|
||||
}
|
||||
|
||||
bool readSchema(const QString& path, QJsonObject& obj, Logger* log)
|
||||
QPair<bool, QStringList> validationResult = validate(file, json, schema, log);
|
||||
return validationResult;
|
||||
}
|
||||
|
||||
QPair<bool, QStringList> validate(const QString& file, const QJsonValue& json, const QJsonObject& schema, Logger* log)
|
||||
{
|
||||
QStringList errorList;
|
||||
|
||||
QJsonSchemaChecker schemaChecker;
|
||||
schemaChecker.setSchema(schema);
|
||||
if (!schemaChecker.validate(json).first)
|
||||
{
|
||||
QJsonObject schema;
|
||||
if(!readFile(path, schema, log).first)
|
||||
return false;
|
||||
|
||||
if(!resolveRefs(schema, obj, log))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
QPair<bool, QStringList> parse(const QString& path, const QString& data, QJsonObject& obj, Logger* log)
|
||||
{
|
||||
QJsonDocument doc;
|
||||
QPair<bool, QStringList> parsingResult = JsonUtils::parse(path, data, doc, log);
|
||||
obj = doc.object();
|
||||
return parsingResult;
|
||||
}
|
||||
|
||||
QPair<bool, QStringList> parse(const QString& path, const QString& data, QJsonArray& arr, Logger* log)
|
||||
{
|
||||
QJsonDocument doc;
|
||||
|
||||
QPair<bool, QStringList> parsingResult = JsonUtils::parse(path, data, doc, log);
|
||||
arr = doc.array();
|
||||
return parsingResult;
|
||||
}
|
||||
|
||||
QPair<bool, QStringList> parse(const QString& path, const QString& data, QJsonDocument& doc, Logger* log)
|
||||
{
|
||||
QStringList errorList;
|
||||
|
||||
QJsonParseError error;
|
||||
doc = QJsonDocument::fromJson(data.toUtf8(), &error);
|
||||
|
||||
if (error.error != QJsonParseError::NoError)
|
||||
const QStringList &errors = schemaChecker.getMessages();
|
||||
for (const auto& error : errors)
|
||||
{
|
||||
int errorLine = 1;
|
||||
int errorColumn = 1;
|
||||
|
||||
int lastNewlineIndex = data.lastIndexOf("\n", error.offset - 1);
|
||||
if (lastNewlineIndex != -1)
|
||||
{
|
||||
errorColumn = error.offset - lastNewlineIndex ;
|
||||
}
|
||||
errorLine += data.left(error.offset).count('\n');
|
||||
|
||||
const QString errorMessage = QString("JSON parse error: %1, line: %2, column: %3, Data: '%4'")
|
||||
.arg(error.errorString())
|
||||
.arg(errorLine)
|
||||
.arg(errorColumn)
|
||||
.arg(data);
|
||||
QString errorMessage = QString("JSON parse error @(%1) - %2")
|
||||
.arg(file, error);
|
||||
errorList.push_back(errorMessage);
|
||||
Error(log, "%s", QSTRING_CSTR(errorMessage));
|
||||
|
||||
return qMakePair(false, errorList);
|
||||
}
|
||||
return qMakePair(true, errorList);
|
||||
return qMakePair(false, errorList);
|
||||
}
|
||||
return qMakePair(true, errorList);
|
||||
}
|
||||
|
||||
QPair<bool, QStringList> validate(const QString& file, const QJsonObject& json, const QString& schemaPath, Logger* log)
|
||||
QPair<bool, QStringList> correct(const QString& file, QJsonValue& json, const QJsonObject& schema, Logger* log)
|
||||
{
|
||||
bool wasCorrected {false};
|
||||
QStringList corrections;
|
||||
QJsonValue correctJson;
|
||||
|
||||
QJsonSchemaChecker schemaChecker;
|
||||
schemaChecker.setSchema(schema);
|
||||
if (!schemaChecker.validate(json).first)
|
||||
{
|
||||
// get the schema data
|
||||
QJsonObject schema;
|
||||
Warning(log, "Fixing JSON data!");
|
||||
json = schemaChecker.getAutoCorrectedConfig(json);
|
||||
wasCorrected = true;
|
||||
|
||||
QPair<bool, QStringList> readResult = readFile(schemaPath, schema, log);
|
||||
if(!readResult.first)
|
||||
const QStringList &correctionMessages = schemaChecker.getMessages();
|
||||
for (const auto& correction : correctionMessages)
|
||||
{
|
||||
return readResult;
|
||||
QString message = QString("JSON fix @(%1) - %2")
|
||||
.arg(file, correction);
|
||||
corrections.push_back(message);
|
||||
Warning(log, "%s", QSTRING_CSTR(message));
|
||||
}
|
||||
|
||||
QPair<bool, QStringList> validationResult = validate(file, json, schema, log);
|
||||
return validationResult;
|
||||
}
|
||||
return qMakePair(wasCorrected, corrections);
|
||||
}
|
||||
|
||||
QPair<bool, QStringList> validate(const QString& file, const QJsonObject& json, const QJsonObject& schema, Logger* log)
|
||||
bool write(const QString& filename, const QJsonObject& json, Logger* log)
|
||||
{
|
||||
QJsonDocument doc;
|
||||
|
||||
doc.setObject(json);
|
||||
QByteArray data = doc.toJson(QJsonDocument::Indented);
|
||||
|
||||
return FileUtils::writeFile(filename, data, log);
|
||||
}
|
||||
|
||||
bool resolveRefs(const QJsonObject& schema, QJsonObject& obj, Logger* log)
|
||||
{
|
||||
for (QJsonObject::const_iterator i = schema.begin(); i != schema.end(); ++i)
|
||||
{
|
||||
QStringList errorList;
|
||||
QString attribute = i.key();
|
||||
const QJsonValue & attributeValue = *i;
|
||||
|
||||
QJsonSchemaChecker schemaChecker;
|
||||
schemaChecker.setSchema(schema);
|
||||
if (!schemaChecker.validate(json).first)
|
||||
if (attribute == "$ref" && attributeValue.isString())
|
||||
{
|
||||
const QStringList &errors = schemaChecker.getMessages();
|
||||
for (const auto& error : errors)
|
||||
if(!readSchema(":/" + attributeValue.toString(), obj, log))
|
||||
{
|
||||
QString errorMessage = QString("JSON parse error: %1")
|
||||
.arg(error);
|
||||
errorList.push_back(errorMessage);
|
||||
Error(log, "%s", QSTRING_CSTR(errorMessage));
|
||||
}
|
||||
return qMakePair(false, errorList);
|
||||
}
|
||||
return qMakePair(true, errorList);
|
||||
}
|
||||
|
||||
bool write(const QString& filename, const QJsonObject& json, Logger* log)
|
||||
{
|
||||
QJsonDocument doc;
|
||||
|
||||
doc.setObject(json);
|
||||
QByteArray data = doc.toJson(QJsonDocument::Indented);
|
||||
|
||||
if(!FileUtils::writeFile(filename, data, log))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool resolveRefs(const QJsonObject& schema, QJsonObject& obj, Logger* log)
|
||||
{
|
||||
for (QJsonObject::const_iterator i = schema.begin(); i != schema.end(); ++i)
|
||||
{
|
||||
QString attribute = i.key();
|
||||
const QJsonValue & attributeValue = *i;
|
||||
|
||||
if (attribute == "$ref" && attributeValue.isString())
|
||||
{
|
||||
if(!readSchema(":/" + attributeValue.toString(), obj, log))
|
||||
{
|
||||
Error(log,"Error while getting schema ref: %s",QSTRING_CSTR(QString(":/" + attributeValue.toString())));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (attributeValue.isObject())
|
||||
obj.insert(attribute, resolveRefs(attributeValue.toObject(), obj, log));
|
||||
else
|
||||
{
|
||||
obj.insert(attribute, attributeValue);
|
||||
Error(log,"Error while getting schema ref: %s",QSTRING_CSTR(QString(":/" + attributeValue.toString())));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
else if (attributeValue.isObject())
|
||||
{
|
||||
obj.insert(attribute, resolveRefs(attributeValue.toObject(), obj, log));
|
||||
}
|
||||
else
|
||||
{
|
||||
obj.insert(attribute, attributeValue);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
QString jsonValueToQString(const QJsonValue &value, QJsonDocument::JsonFormat format)
|
||||
{
|
||||
switch (value.type()) {
|
||||
case QJsonValue::Object:
|
||||
{
|
||||
return QJsonDocument(value.toObject()).toJson(format);
|
||||
}
|
||||
case QJsonValue::Array:
|
||||
{
|
||||
return QJsonDocument(value.toArray()).toJson(format);
|
||||
}
|
||||
case QJsonValue::String:
|
||||
case QJsonValue::Double:
|
||||
case QJsonValue::Bool:
|
||||
{
|
||||
return value.toString();
|
||||
}
|
||||
case QJsonValue::Null:
|
||||
{
|
||||
return "Null";
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
|
||||
QJsonObject mergeJsonObjects(const QJsonObject &obj1, const QJsonObject &obj2, bool overrideObj1)
|
||||
{
|
||||
QJsonObject result = obj1;
|
||||
|
||||
for (auto it = obj2.begin(); it != obj2.end(); ++it) {
|
||||
if (result.contains(it.key())) {
|
||||
if (overrideObj1)
|
||||
{
|
||||
result[it.key()] = it.value();
|
||||
}
|
||||
} else {
|
||||
result[it.key()] = it.value();
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
@@ -40,7 +40,7 @@ QStringList QJsonSchemaChecker::getMessages() const
|
||||
return _messages;
|
||||
}
|
||||
|
||||
QPair<bool, bool> QJsonSchemaChecker::validate(const QJsonObject& value, bool ignoreRequired)
|
||||
QPair<bool, bool> QJsonSchemaChecker::validate(const QJsonValue& value, bool ignoreRequired)
|
||||
{
|
||||
// initialize state
|
||||
_ignoreRequired = ignoreRequired;
|
||||
@@ -56,7 +56,7 @@ QPair<bool, bool> QJsonSchemaChecker::validate(const QJsonObject& value, bool ig
|
||||
return QPair<bool, bool>(!_error, !_schemaError);
|
||||
}
|
||||
|
||||
QJsonObject QJsonSchemaChecker::getAutoCorrectedConfig(const QJsonObject& value, bool ignoreRequired)
|
||||
QJsonValue QJsonSchemaChecker::getAutoCorrectedConfig(const QJsonValue& value, bool ignoreRequired)
|
||||
{
|
||||
_ignoreRequired = ignoreRequired;
|
||||
const QStringList sequence = QStringList() << "remove" << "modify" << "create";
|
||||
|
Reference in New Issue
Block a user