Skip to content

Commit 4f9c668

Browse files
committed
feat(config): improve config handling
2 parents 557ba29 + 2fbabf4 commit 4f9c668

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

include/vix/config/Config.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ namespace vix::config
4242
/** @brief Load or reload the configuration file. */
4343
void loadConfig();
4444

45+
void set(const std::string &dottedKey, const nlohmann::json &value);
46+
4547
#if VIX_CORE_WITH_MYSQL
4648
namespace sql
4749
{

src/config/Config.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,33 @@ namespace vix::config
143143
return DEFAULT_DB_PASS;
144144
}
145145

146+
void Config::set(const std::string &dottedKey, const nlohmann::json &value)
147+
{
148+
if (dottedKey.empty())
149+
{
150+
return;
151+
}
152+
153+
nlohmann::json *node = &rawConfig_;
154+
std::size_t start = 0;
155+
156+
while (start < dottedKey.size())
157+
{
158+
const std::size_t dot = dottedKey.find('.', start);
159+
const std::string key =
160+
dottedKey.substr(start, dot == std::string::npos ? std::string::npos : dot - start);
161+
162+
if (dot == std::string::npos)
163+
{
164+
(*node)[key] = value;
165+
return;
166+
}
167+
168+
node = &((*node)[key]);
169+
start = dot + 1;
170+
}
171+
}
172+
146173
const std::string &Config::getDbHost() const noexcept
147174
{
148175
return db_host;

0 commit comments

Comments
 (0)