Clarity and convenience for defining config options

pull/1186/head
Stephen Shelton 4 years ago
parent 02e31f3867
commit f6d000838f
No known key found for this signature in database
GPG Key ID: EE4BADACCE8B631C

@ -18,7 +18,7 @@ ConfigDefinitionBase::ConfigDefinitionBase(std::string section_,
}
Configuration&
Configuration::addConfigOption(ConfigDefinition_ptr def)
Configuration::defineOption(ConfigDefinition_ptr def)
{
auto sectionItr = m_definitions.find(def->section);
if (sectionItr == m_definitions.end())

@ -189,7 +189,7 @@ namespace llarp
using SectionMap = std::unordered_map<std::string, DefinitionMap>;
/// A Configuration holds an ordered set of ConfigDefinitions defining the allowable values and
/// their constraints (specified through calls to addConfigOption()).
/// their constraints (specified through calls to defineOption()).
///
/// The layout and grouping of the config options are modelled after the INI file format; each
/// option has a name and is grouped under a section. Duplicate option names are allowed only if
@ -215,10 +215,19 @@ namespace llarp
/// @return `*this` for chaining calls
/// @throws std::invalid_argument if the option already exists
Configuration&
addConfigOption(ConfigDefinition_ptr def);
defineOption(ConfigDefinition_ptr def);
/// Convenience function which calls defineOption with a ConfigDefinition of the specified type
/// and with parameters passed through to ConfigDefinition's constructor.
template<typename T, typename... Params>
Configuration&
defineOption(Params&&... args)
{
return defineOption(std::make_unique<ConfigDefinition<T>>(args...));
}
/// Specify a config value for the given section and name. The value should be a valid string
/// representing the type used by the option (e.g. the type provided when addConfigOption() was
/// representing the type used by the option (e.g. the type provided when defineOption() was
/// called).
///
/// If the specified option doesn't exist, an exception will be thrown. Otherwise, the option's

@ -99,7 +99,7 @@ TEST_CASE("ConfigDefinition tryAccept missing option test", "[config]")
TEST_CASE("Configuration basic add/get test", "[config]")
{
llarp::Configuration config;
config.addConfigOption(std::make_unique<llarp::ConfigDefinition<int>>(
config.defineOption(std::make_unique<llarp::ConfigDefinition<int>>(
"router",
"threads",
false,
@ -122,7 +122,7 @@ TEST_CASE("Configuration missing def test", "[config]")
CHECK_THROWS(config.addConfigValue("foo", "bar", "5"));
CHECK_THROWS(config.getConfigValue<int>("foo", "bar") == 5);
config.addConfigOption(std::make_unique<llarp::ConfigDefinition<int>>(
config.defineOption(std::make_unique<llarp::ConfigDefinition<int>>(
"quux",
"bar",
false,
@ -135,7 +135,7 @@ TEST_CASE("Configuration missing def test", "[config]")
TEST_CASE("Configuration required test", "[config]")
{
llarp::Configuration config;
config.addConfigOption(std::make_unique<llarp::ConfigDefinition<int>>(
config.defineOption(std::make_unique<llarp::ConfigDefinition<int>>(
"router",
"threads",
true,
@ -152,13 +152,13 @@ TEST_CASE("Configuration required test", "[config]")
TEST_CASE("Configuration section test", "[config]")
{
llarp::Configuration config;
config.addConfigOption(std::make_unique<llarp::ConfigDefinition<int>>(
config.defineOption(std::make_unique<llarp::ConfigDefinition<int>>(
"foo",
"bar",
true,
false,
1));
config.addConfigOption(std::make_unique<llarp::ConfigDefinition<int>>(
config.defineOption(std::make_unique<llarp::ConfigDefinition<int>>(
"goo",
"bar",
true,
@ -183,11 +183,11 @@ TEST_CASE("Configuration acceptAllOptions test", "[config]")
std::string fooBaz = "";
llarp::Configuration config;
config.addConfigOption(std::make_unique<llarp::ConfigDefinition<int>>(
config.defineOption(std::make_unique<llarp::ConfigDefinition<int>>(
"foo", "bar", false, false, 1, [&](int arg) {
fooBar = arg;
}));
config.addConfigOption(std::make_unique<llarp::ConfigDefinition<std::string>>(
config.defineOption(std::make_unique<llarp::ConfigDefinition<std::string>>(
"foo", "baz", false, false, "no", [&](std::string arg) {
fooBaz = arg;
}));
@ -203,7 +203,7 @@ TEST_CASE("Configuration acceptAllOptions test", "[config]")
TEST_CASE("Configuration acceptAllOptions exception propagation test", "[config]")
{
llarp::Configuration config;
config.addConfigOption(std::make_unique<llarp::ConfigDefinition<int>>(
config.defineOption(std::make_unique<llarp::ConfigDefinition<int>>(
"foo", "bar", false, false, 1, [&](int arg) {
(void)arg;
throw std::runtime_error("FAIL");
@ -211,3 +211,10 @@ TEST_CASE("Configuration acceptAllOptions exception propagation test", "[config]
REQUIRE_THROWS_WITH(config.acceptAllOptions(), "FAIL");
}
TEST_CASE("Configuration defineOptions passthrough test", "[config]")
{
llarp::Configuration config;
config.defineOption<int>("foo", "bar", false, false, 1);
CHECK(config.getConfigValue<int>("foo", "bar") == 1);
}

Loading…
Cancel
Save