Merge PR #283: Reject duplicate ids in plugin json

This commit is contained in:
Jenkins nymea 2020-06-07 19:16:23 +02:00
commit 3156264787
6 changed files with 20 additions and 11 deletions

View File

@ -1238,7 +1238,7 @@ void ThingManagerImplementation::loadPlugins()
} }
QJsonObject pluginInfo = loader.metaData().value("MetaData").toObject(); QJsonObject pluginInfo = loader.metaData().value("MetaData").toObject();
PluginMetadata metaData(pluginInfo); PluginMetadata metaData(pluginInfo, false, false);
if (!metaData.isValid()) { if (!metaData.isValid()) {
foreach (const QString &error, metaData.validationErrors()) { foreach (const QString &error, metaData.validationErrors()) {
qCWarning(dcThingManager()) << error; qCWarning(dcThingManager()) << error;
@ -1413,7 +1413,7 @@ void ThingManagerImplementation::loadConfiguredThings()
// Try to load the device class from the cache // Try to load the device class from the cache
QJsonObject pluginInfo = PluginInfoCache::loadPluginInfo(pluginId); QJsonObject pluginInfo = PluginInfoCache::loadPluginInfo(pluginId);
if (!pluginInfo.empty()) { if (!pluginInfo.empty()) {
PluginMetadata pluginMetadata(pluginInfo); PluginMetadata pluginMetadata(pluginInfo, false, false);
thingClass = pluginMetadata.thingClasses().findById(thingClassId); thingClass = pluginMetadata.thingClasses().findById(thingClassId);
if (thingClass.isValid()) { if (thingClass.isValid()) {
m_supportedThings.insert(thingClassId, thingClass); m_supportedThings.insert(thingClassId, thingClass);

View File

@ -46,7 +46,9 @@ PluginMetadata::PluginMetadata()
} }
PluginMetadata::PluginMetadata(const QJsonObject &jsonObject, bool isBuiltIn): m_isBuiltIn(isBuiltIn) PluginMetadata::PluginMetadata(const QJsonObject &jsonObject, bool isBuiltIn, bool strict):
m_isBuiltIn(isBuiltIn),
m_strictRun(strict)
{ {
parse(jsonObject); parse(jsonObject);
} }
@ -896,9 +898,12 @@ QPair<bool, Types::InputType> PluginMetadata::loadAndVerifyInputType(const QStri
bool PluginMetadata::verifyDuplicateUuid(const QUuid &uuid) bool PluginMetadata::verifyDuplicateUuid(const QUuid &uuid)
{ {
if (m_allUuids.contains(uuid)) { if (m_allUuids.contains(uuid)) {
// FIXME: Drop debug, activate return! (see .h for more context) // FIXME: Drop non-strict run! (see .h for more context)
qCWarning(dcPluginMetadata()) << "THIS PLUGIN USES DUPLICATE UUID" << uuid.toString() << "! THIS WILL STOP WORKING SOON."; if (m_strictRun) {
// return false; return false;
} else {
qCWarning(dcPluginMetadata()) << "THIS PLUGIN USES DUPLICATE UUID" << uuid.toString() << "! THIS IS NOT SUPPORTED AND MAY CAUSE RUNTIME ISSUES.";
}
} }
if (m_currentScopUuids.contains(uuid)) { if (m_currentScopUuids.contains(uuid)) {
return false; return false;

View File

@ -38,7 +38,7 @@ class PluginMetadata
{ {
public: public:
PluginMetadata(); PluginMetadata();
PluginMetadata(const QJsonObject &jsonObject, bool isBuiltIn = false); PluginMetadata(const QJsonObject &jsonObject, bool isBuiltIn = false, bool strict = true);
bool isValid() const; bool isValid() const;
QStringList validationErrors() const; QStringList validationErrors() const;
@ -80,6 +80,7 @@ private:
// As strict as possible without breaking them, but this should be removed ASAP // As strict as possible without breaking them, but this should be removed ASAP
// and only m_allUuids should be used to check for dupes // and only m_allUuids should be used to check for dupes
QList<QUuid> m_currentScopUuids; QList<QUuid> m_currentScopUuids;
bool m_strictRun = true;
QStringList m_validationErrors; QStringList m_validationErrors;
}; };

View File

@ -45,6 +45,7 @@ int main(int argc, char *argv[])
parser.addOption({{"o", "output"}, "Write generated output header to <file>.", "file"}); parser.addOption({{"o", "output"}, "Write generated output header to <file>.", "file"});
parser.addOption({{"e", "extern"}, "Write generated output header (extern definitions) to <file>.", "file"}); parser.addOption({{"e", "extern"}, "Write generated output header (extern definitions) to <file>.", "file"});
parser.addOption({{"t", "translations"}, "Write generated translations file stub to <directory>.", "directory"}); parser.addOption({{"t", "translations"}, "Write generated translations file stub to <directory>.", "directory"});
parser.addOption({{"n", "non-strict"}, "Non-strict run. Don't exit on duplicate UUID warnings."});
parser.addPositionalArgument("input", "The input json file"); parser.addPositionalArgument("input", "The input json file");
parser.process(a); parser.process(a);
@ -54,9 +55,11 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
bool strictMode = !parser.isSet("non-strict");
PluginInfoCompiler pic; PluginInfoCompiler pic;
int ret = pic.compile(parser.positionalArguments().first(), parser.value("output"), parser.value("extern"), parser.value("translations")); int ret = pic.compile(parser.positionalArguments().first(), parser.value("output"), parser.value("extern"), parser.value("translations"), strictMode);
return ret; return ret;

View File

@ -43,7 +43,7 @@ PluginInfoCompiler::PluginInfoCompiler()
} }
int PluginInfoCompiler::compile(const QString &inputFile, const QString &outputFile, const QString outputFileExtern, const QString &translationsPath) int PluginInfoCompiler::compile(const QString &inputFile, const QString &outputFile, const QString outputFileExtern, const QString &translationsPath, bool strictMode)
{ {
// First, process the input json... // First, process the input json...
QFile jsonFile(inputFile); QFile jsonFile(inputFile);
@ -81,7 +81,7 @@ int PluginInfoCompiler::compile(const QString &inputFile, const QString &outputF
} }
QJsonObject jsonObject = QJsonObject::fromVariantMap(jsonDoc.toVariant().toMap()); QJsonObject jsonObject = QJsonObject::fromVariantMap(jsonDoc.toVariant().toMap());
PluginMetadata metadata(jsonObject); PluginMetadata metadata(jsonObject, false, strictMode);
if (!metadata.isValid()) { if (!metadata.isValid()) {
foreach (const QString &error, metadata.validationErrors()) { foreach (const QString &error, metadata.validationErrors()) {
QDebug dbg = qWarning().noquote().nospace(); QDebug dbg = qWarning().noquote().nospace();

View File

@ -42,7 +42,7 @@ class PluginInfoCompiler
public: public:
PluginInfoCompiler(); PluginInfoCompiler();
int compile(const QString &inputFile, const QString &outputFile, const QString outputFileExtern, const QString &translationsPath); int compile(const QString &inputFile, const QString &outputFile, const QString outputFileExtern, const QString &translationsPath, bool strictMode);
private: private: