From febe6997b6f2a9c1710143a2ade31c2e2b8fea27 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Thu, 26 Jan 2017 19:33:14 +0100 Subject: [PATCH] feat(config): Add fallback support to local value references --- include/components/config.hpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/include/components/config.hpp b/include/components/config.hpp index 6c71a23d..0b2d9501 100644 --- a/include/components/config.hpp +++ b/include/components/config.hpp @@ -227,8 +227,11 @@ class config { /** * Dereference local value reference defined using: * ${root.key} + * ${root.key:fallback} * ${self.key} + * ${self.key:fallback} * ${section.key} + * ${section.key:fallback} */ template T dereference_local(string section, const string& key, const string& current_section) const { @@ -245,7 +248,13 @@ class config { T result{convert(string{string_value})}; return dereference(string(section), move(key), move(string_value), move(result)); } catch (const key_error& err) { - throw value_error("Unexisting reference defined at [" + section + "." + key + "]"); + size_t pos; + if ((pos = key.find(':')) != string::npos) { + string fallback = key.substr(pos + 1); + m_log.info("The reference ${%s.%s} does not exist, using defined fallback value \"%s\"", section, key.substr(0, pos), fallback); + return convert(move(fallback)); + } + throw value_error("The reference ${" + section + "." + key + "} does not exist (no fallback set)"); } } @@ -259,7 +268,7 @@ class config { size_t pos; string env_default; - if ((pos = var.find(":")) != string::npos) { + if ((pos = var.find(':')) != string::npos) { env_default = var.substr(pos + 1); var.erase(pos); } @@ -286,7 +295,7 @@ class config { size_t pos; #if not WITH_XRM m_log.warn("No built-in support for xrdb (requires xcb-util-xrm). Using default value for `%s`", var); - if ((pos = var.find(":")) != string::npos) { + if ((pos = var.find(':')) != string::npos) { return convert(var.substr(pos + 1)); } return convert(""); @@ -296,7 +305,7 @@ class config { } string fallback; - if ((pos = var.find(":")) != string::npos) { + if ((pos = var.find(':')) != string::npos) { fallback = var.substr(pos + 1); var.erase(pos); } @@ -324,7 +333,7 @@ class config { T dereference_file(string var) const { size_t pos; string fallback; - if ((pos = var.find(":")) != string::npos) { + if ((pos = var.find(':')) != string::npos) { fallback = var.substr(pos + 1); var.erase(pos); }