core: allow specifying special keys with []

fixes #35
This commit is contained in:
Vaxry 2024-03-28 16:44:44 +00:00
parent b3e430f81f
commit 95471ec86f
3 changed files with 34 additions and 3 deletions

View File

@ -301,7 +301,37 @@ CParseResult CConfig::configSetValueSafe(const std::string& command, const std::
const auto VALUEONLYNAME = command.starts_with(catPrefix) ? command.substr(catPrefix.length()) : command;
auto VALUEIT = impl->values.find(valueName);
// FIXME: this will bug with nested.
if (valueName.contains('[') && valueName.contains(']')) {
const auto L = valueName.find_first_of('[');
const auto R = valueName.find_last_of(']');
if (L < R) {
const auto CATKEY = valueName.substr(L + 1, R - L - 1);
impl->currentSpecialKey = CATKEY;
valueName = valueName.substr(0, L) + valueName.substr(R + 1);
// if it doesn't exist, make it
for (auto& sc : impl->specialCategoryDescriptors) {
if (sc->key.empty() || !valueName.starts_with(sc->name))
continue;
// bingo
const auto PCAT = impl->specialCategories.emplace_back(std::make_unique<SSpecialCategory>()).get();
PCAT->descriptor = sc.get();
PCAT->name = sc->name;
PCAT->key = sc->key;
addSpecialConfigValue(sc->name.c_str(), sc->key.c_str(), CConfigValue(CATKEY.c_str()));
applyDefaultsToCat(*PCAT);
PCAT->values[sc->key].setFrom(CATKEY);
}
}
}
auto VALUEIT = impl->values.find(valueName);
if (VALUEIT == impl->values.end()) {
// it might be in a special category
bool found = false;

View File

@ -40,8 +40,7 @@ special {
value = $SPECIALVAL1
}
special {
key = b
special[b] {
value = 2
}

View File

@ -199,6 +199,8 @@ int main(int argc, char** argv, char** envp) {
EXPECT(std::any_cast<int64_t>(config.getSpecialConfigValue("special", "value", "b")), 2);
EXPECT(std::any_cast<int64_t>(config.getSpecialConfigValue("specialGeneric:one", "value")), 1);
EXPECT(std::any_cast<int64_t>(config.getSpecialConfigValue("specialGeneric:two", "value")), 2);
EXPECT(config.parseDynamic("special[b]:value = 3").error, false);
EXPECT(std::any_cast<int64_t>(config.getSpecialConfigValue("special", "value", "b")), 3);
// test dynamic special variable
EXPECT(config.parseDynamic("$SPECIALVAL1 = 2").error, false);