Setting a configuration value to nil should actually erase it

also configurationVersion set is already checked in Configuration
This commit is contained in:
Kae 2023-11-02 08:23:36 +11:00
parent 0497048b44
commit bea100bde9
2 changed files with 12 additions and 6 deletions

View File

@ -46,7 +46,10 @@ void Configuration::set(String const& key, Json const& value) {
if (key == "configurationVersion") if (key == "configurationVersion")
throw ConfigurationException("cannot set configurationVersion"); throw ConfigurationException("cannot set configurationVersion");
if (value)
m_currentConfig = m_currentConfig.set(key, value); m_currentConfig = m_currentConfig.set(key, value);
else
m_currentConfig = m_currentConfig.eraseKey(key);
} }
void Configuration::setPath(String const& path, Json const& value) { void Configuration::setPath(String const& path, Json const& value) {
@ -54,7 +57,10 @@ void Configuration::setPath(String const& path, Json const& value) {
if (path.splitAny("[].").get(0) == "configurationVersion") if (path.splitAny("[].").get(0) == "configurationVersion")
throw ConfigurationException("cannot set configurationVersion"); throw ConfigurationException("cannot set configurationVersion");
if (value)
m_currentConfig = m_currentConfig.setPath(path, value); m_currentConfig = m_currentConfig.setPath(path, value);
else
m_currentConfig = m_currentConfig.erasePath(path);
} }
} }

View File

@ -177,7 +177,7 @@ LuaCallbacks LuaBindings::makeRootCallbacks() {
}); });
callbacks.registerCallback("setConfiguration", [root](String const& key, Json const& value) { callbacks.registerCallback("setConfiguration", [root](String const& key, Json const& value) {
if (key == "safeScripts" || key == "configurationVersion") if (key == "safeScripts")
throw StarException(strf("Cannot set {}", key)); throw StarException(strf("Cannot set {}", key));
else else
root->configuration()->set(key, value); root->configuration()->set(key, value);
@ -186,14 +186,14 @@ LuaCallbacks LuaBindings::makeRootCallbacks() {
callbacks.registerCallback("getConfigurationPath", [root](String const& path) -> Json { callbacks.registerCallback("getConfigurationPath", [root](String const& path) -> Json {
if (path.beginsWith("title")) if (path.beginsWith("title"))
throw StarException(strf("Cannot get {}", path)); throw ConfigurationException(strf("cannot get {}", path));
else else
return root->configuration()->getPath(path); return root->configuration()->getPath(path);
}); });
callbacks.registerCallback("setConfigurationPath", [root](String const& path, Json const& value) { callbacks.registerCallback("setConfigurationPath", [root](String const& path, Json const& value) {
if (path.beginsWith("safeScripts") || path.beginsWith("configurationVersion")) if (path.beginsWith("safeScripts"))
throw StarException(strf("Cannot set {}", path)); throw ConfigurationException(strf("cannot set {}", path));
else else
root->configuration()->setPath(path, value); root->configuration()->setPath(path, value);
}); });