Forward compatibility - config substitutions:

1) Verify whether a value looks like an enum
2) Always report substitution of an enum with a boolean.
This commit is contained in:
bubnikv 2021-06-30 16:19:06 +02:00
parent bb4d0d22f9
commit 21cbcd876a
2 changed files with 15 additions and 3 deletions

View File

@ -501,6 +501,16 @@ void ConfigBase::set_deserialize(std::initializer_list<SetDeserializeItem> items
this->set_deserialize(item.opt_key, item.opt_value, substitutions_ctxt, item.append); this->set_deserialize(item.opt_key, item.opt_value, substitutions_ctxt, item.append);
} }
static inline bool looks_like_enum_value(const std::string &s)
{
if (value.empty() || value.size() > 64 || ! isalpha(value.front()))
return false;
for (const char c : s)
if (! (isalnum(c) || c == '_' || c == '-'))
return false;
return true;
}
bool ConfigBase::set_deserialize_raw(const t_config_option_key &opt_key_src, const std::string &value, ConfigSubstitutionContext& substitutions_ctxt, bool append) bool ConfigBase::set_deserialize_raw(const t_config_option_key &opt_key_src, const std::string &value, ConfigSubstitutionContext& substitutions_ctxt, bool append)
{ {
t_config_option_key opt_key = opt_key_src; t_config_option_key opt_key = opt_key_src;
@ -546,8 +556,10 @@ bool ConfigBase::set_deserialize_raw(const t_config_option_key &opt_key_src, con
// Deserialize failed, try to substitute with a default value. // Deserialize failed, try to substitute with a default value.
assert(substitutions_ctxt.rule == ForwardCompatibilitySubstitutionRule::Enable || substitutions_ctxt.rule == ForwardCompatibilitySubstitutionRule::EnableSilent); assert(substitutions_ctxt.rule == ForwardCompatibilitySubstitutionRule::Enable || substitutions_ctxt.rule == ForwardCompatibilitySubstitutionRule::EnableSilent);
if (opt_key == "gcode_flavor" && (value == "marlin2" || value == "marlinfirmware")) if (optdef->type == coEnum && opt_key == "gcode_flavor" && (value == "marlin2" || value == "marlinfirmware"))
static_cast<ConfigOptionEnum<GCodeFlavor>*>(opt)->value = gcfMarlin; static_cast<ConfigOptionEnum<GCodeFlavor>*>(opt)->value = gcfMarlin;
else if (optdef->type == coBool && looks_like_enum_value(value))
static_cast<ConfigOptionBool*>(opt)->value = boost::iequals(value, "enabled") || boost::iequals(value, "on");
else else
opt->set(optdef->default_value.get()); opt->set(optdef->default_value.get());

View File

@ -1251,11 +1251,11 @@ public:
bool deserialize(const std::string &str, bool append = false) override bool deserialize(const std::string &str, bool append = false) override
{ {
UNUSED(append); UNUSED(append);
if (str == "1" || boost::iequals(str, "enabled") || boost::iequals(str, "on")) { if (str == "1") {
this->value = true; this->value = true;
return true; return true;
} }
if (str == "0" || boost::iequals(str, "disabled") || boost::iequals(str, "off")) { if (str == "0") {
this->value = false; this->value = false;
return true; return true;
} }