Schema: Add feature to [switch] to allow a [case] to match the absence of the key

Use this to support the default [goal] name
This commit is contained in:
Celtic Minstrel 2018-03-29 00:25:14 -04:00
parent 6d4b789e5b
commit 644ff73c92
2 changed files with 13 additions and 1 deletions

View file

@ -8,9 +8,9 @@
[else]
[switch]
key=name
# TODO: The default is target_unit
[case]
value=target,target_unit
trigger_if_missing=yes
{FILTER_TAG criteria unit ()}
[/case]
[case]

View file

@ -502,6 +502,7 @@ void class_tag::add_switch(const config& switch_cfg)
{
config default_cfg;
const std::string key = switch_cfg["key"];
bool allow_missing = false;
for(const auto& case_cfg : switch_cfg.child_range("case")) {
if(case_cfg.has_attribute("value")) {
const std::vector<std::string> values = utils::split(case_cfg["value"], ',', utils::STRIP_SPACES);
@ -516,6 +517,11 @@ void class_tag::add_switch(const config& switch_cfg)
}
default_cfg.add_child("not")[key] = value;
}
if(!allow_missing && case_cfg["trigger_if_missing"].to_bool()) {
config& missing_filter = filter.add_child("or").add_child("not");
missing_filter["glob_on_" + key] = "*";
allow_missing = true;
}
conditions_.emplace_back(case_cfg, filter);
} else {
// Match if the attribute is missing
@ -525,6 +531,12 @@ void class_tag::add_switch(const config& switch_cfg)
conditions_.back().set_name(name);
}
if(switch_cfg.has_child("else")) {
if(allow_missing) {
// If a [case] matches the absence of the key, then [else] should not
// The previous [not] keys already failed if it had a value matched by another [case]
// So just add an [and] tag that matches any other value
default_cfg.add_child("and")["glob_on_" + key] = "*";
}
conditions_.emplace_back(switch_cfg.child("else"), default_cfg);
const std::string name = formatter() << get_name() << "[else]";
conditions_.back().set_name(name);