Commit a4d0e2ef authored by Dominik Charousset's avatar Dominik Charousset

Drop the implicit global category

parent bff39e81
......@@ -302,7 +302,9 @@ actor_system_config& actor_system_config::set_impl(string_view name,
auto opt = custom_options_.qualified_name_lookup(name);
if (opt != nullptr && opt->check(value) == none) {
opt->store(value);
auto& dict = content[opt->category()].as_dictionary();
auto category = opt->category();
auto& dict = category == "global" ? content
: content[category].as_dictionary();
dict[opt->long_name()] = std::move(value);
}
return *this;
......
......@@ -124,17 +124,15 @@ auto config_option_set::parse(settings& config, argument_iterator first,
auto opt_name = opt.long_name();
auto opt_ctg = opt.category();
// Try inserting a new submap into the config or fill existing one.
auto& entry = config[opt_ctg];
if (!holds_alternative<config_value::dictionary>(entry))
entry = config_value::dictionary{};
auto& submap = get<config_value::dictionary>(entry);
auto& entry = opt_ctg == "global" ? config
: config[opt_ctg].as_dictionary();
// Flags only consume the current element.
if (opt.is_flag()) {
if (arg_begin != arg_end)
return pec::illegal_argument;
config_value cfg_true{true};
opt.store(cfg_true);
submap[opt_name] = cfg_true;
entry[opt_name] = cfg_true;
} else {
if (arg_begin == arg_end)
return pec::missing_argument;
......@@ -158,7 +156,7 @@ auto config_option_set::parse(settings& config, argument_iterator first,
}
}
opt.store(*val);
submap[opt_name] = std::move(*val);
entry[opt_name] = std::move(*val);
}
return pec::success;
};
......
......@@ -170,13 +170,18 @@ void ini_consumer::key(std::string name) {
void ini_consumer::value_impl(config_value&& x) {
using dict_type = config_value::dictionary;
auto dict = get_if<dict_type>(&x);
auto& dst = cfg_.emplace(current_key, dict_type{}).first->second;
if (dict != nullptr && !dict->empty() && holds_alternative<dict_type>(dst)) {
auto& dst_dict = get<dict_type>(dst);
// We need to "merge" values into the destination, because it can already
// contain any number of unrelated entries.
if (current_key != "global") {
auto& dst = cfg_.emplace(current_key, dict_type{}).first->second;
if (dict != nullptr && !dict->empty() && holds_alternative<dict_type>(dst)) {
auto& dst_dict = get<dict_type>(dst);
// We need to "merge" values into the destination, because it can already
// contain any number of unrelated entries.
for (auto& entry : *dict)
dst_dict.insert_or_assign(entry.first, std::move(entry.second));
}
} else {
for (auto& entry : *dict)
dst_dict.insert_or_assign(entry.first, std::move(entry.second));
cfg_.insert_or_assign(entry.first, std::move(entry.second));
}
}
......
......@@ -18,6 +18,8 @@
#include "caf/settings.hpp"
#include "caf/string_algorithms.hpp"
namespace caf {
std::string get_or(const settings& xs, string_view name,
......@@ -32,6 +34,12 @@ config_value& put_impl(settings& dict, const std::vector<string_view>& path,
config_value& value) {
// Sanity check.
CAF_ASSERT(!path.empty());
// TODO: We implicitly swallow the `global.` suffix as a hotfix, but we
// actually should drop `global.` on the upper layers.
if (path.front() == "global") {
std::vector<string_view> new_path{path.begin() + 1, path.end()};
return put_impl(dict, new_path, value);
}
// Navigate path.
auto last = path.end();
auto back = last - 1;
......
......@@ -101,18 +101,18 @@ CAF_TEST(parse with ref syncing) {
CAF_CHECK_EQUAL(get<int>(cfg, "foo.i"), 42);
}
CAF_TEST(implicit global) {
CAF_TEST(drop global) {
opts.add<int>("value", "some value").add<bool>("help", "print help text");
CAF_MESSAGE("test long option with argument");
settings cfg;
auto res = opts.parse(cfg, {"--value=42"});
CAF_CHECK_EQUAL(res.first, pec::success);
CAF_CHECK_EQUAL(get_if<int>(&cfg, "global.value"), 42);
CAF_CHECK_EQUAL(get_if<int>(&cfg, "value"), 42);
CAF_MESSAGE("test long option flag");
cfg.clear();
res = opts.parse(cfg, {"--help"});
CAF_CHECK_EQUAL(res.first, pec::success);
CAF_CHECK_EQUAL(get_or(cfg, "global.help", false), true);
CAF_CHECK_EQUAL(get_or(cfg, "help", false), true);
}
CAF_TEST(atom parameters) {
......@@ -123,7 +123,7 @@ CAF_TEST(atom parameters) {
auto res = opts.parse(cfg, std::move(args));
if (res.first != pec::success)
return res.first;
auto atm = get_if<atom_value>(&cfg, "global.value");
auto atm = get_if<atom_value>(&cfg, "value");
if (atm == none)
return sec::invalid_argument;
return *atm;
......
......@@ -82,9 +82,9 @@ CAF_TEST(ini_consumer) {
res.e = str.end();
detail::parser::read_ini(res, consumer);
CAF_CHECK_EQUAL(res.code, pec::success);
CAF_CHECK_EQUAL(get<bool>(config, "global.is_server"), true);
CAF_CHECK_EQUAL(get<uint16_t>(config, "global.port"), 4242u);
CAF_CHECK_EQUAL(get<ls>(config, "global.nodes"), ls({"sun", "venus"}));
CAF_CHECK_EQUAL(get<bool>(config, "is_server"), true);
CAF_CHECK_EQUAL(get<uint16_t>(config, "port"), 4242u);
CAF_CHECK_EQUAL(get<ls>(config, "nodes"), ls({"sun", "venus"}));
CAF_CHECK_EQUAL(get<string>(config, "logger.file-name"), "foobar.ini");
CAF_CHECK_EQUAL(get<timespan>(config, "scheduler.timing"), timespan(2000));
CAF_CHECK_EQUAL(get<atom_value>(config, "scheduler.impl"), atom("foo"));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment