From 1e0e50266b8af91a49163a0b7ba9defa3932d6ce Mon Sep 17 00:00:00 2001 From: TheDoctor314 Date: Tue, 28 Sep 2021 21:31:40 +0530 Subject: [PATCH] Add get_with_prefix() Returns a list of key-value pairs starting with a prefix. If you have in config: ``` env-FOO = bar env-CAT = dog ``` then `get_with_prefix("env-")` will return `[{"FOO", "bar"}, {"CAT", "dog"}]` --- include/components/config.hpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/include/components/config.hpp b/include/components/config.hpp index b052f567..1b0a5b98 100644 --- a/include/components/config.hpp +++ b/include/components/config.hpp @@ -111,6 +111,31 @@ class config { } } + /** + * Get list of key-value pairs starting with a prefix by section. + * + * Eg: if you have in config `env-FOO = bar`, + * get_with_prefix(section, "env-") will return [{"FOO", "bar"}] + */ + vector> get_with_prefix(const string& section, const string& key_prefix) const { + auto it = m_sections.find(section); + if (it == m_sections.end()) { + throw key_error("Missing section \"" + section + "\""); + } + + vector> list; + for (const auto& kv_pair : it->second) { + const auto& key = kv_pair.first; + + if (key.substr(0, key_prefix.size()) == key_prefix) { + const auto& val = kv_pair.second; + list.emplace_back(key.substr(key_prefix.size()), val); + } + } + + return list; + } + /** * Get list of values for the current bar by name */