Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing option names, make [lokid]:rpc required #2055

Merged
merged 3 commits into from
Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions llarp/config/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,7 @@ namespace llarp
"lokid",
"rpc",
RelayOnly,
Required,
Comment{
"oxenmq control address for for communicating with oxend. Depends on oxend's",
"lmq-local-control configuration option. By default this value should be",
Expand Down
23 changes: 15 additions & 8 deletions llarp/config/definition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,8 @@ namespace llarp
const std::string& section, std::vector<std::string> comments)
{
auto& sectionComments = m_sectionComments[section];
for (size_t i = 0; i < comments.size(); ++i)
{
sectionComments.emplace_back(std::move(comments[i]));
}
for (auto& c : comments)
sectionComments.emplace_back(std::move(c));
majestrate marked this conversation as resolved.
Show resolved Hide resolved
}

void
Expand Down Expand Up @@ -198,13 +196,22 @@ namespace llarp
if (useValues and def->getNumberFound() > 0)
{
for (const auto& val : def->valuesAsString())
fmt::format_to(sect_append, "\n{}={}\n", name, val);
fmt::format_to(sect_append, "\n{}={}", name, val);
*sect_append = '\n';
}
else if (not(def->hidden and not has_comment))
else if (not def->hidden)
{
for (const auto& val : def->defaultValuesAsString())
fmt::format_to(sect_append, "\n{}{}={}\n", def->required ? "" : "#", name, val);
if (auto defaults = def->defaultValuesAsString(); not defaults.empty())
for (const auto& val : defaults)
fmt::format_to(sect_append, "\n{}{}={}", def->required ? "" : "#", name, val);
else
// We have no defaults so we append it as "#opt-name=" so that we show the option name,
// and make it simple to uncomment and edit to the desired value.
fmt::format_to(sect_append, "\n#{}=", name);
*sect_append = '\n';
}
else if (has_comment)
*sect_append = '\n';
});

if (sect_str.empty())
Expand Down
10 changes: 10 additions & 0 deletions llarp/config/definition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,16 @@ namespace llarp
OptionDefinition(std::string section_, std::string name_, Options&&... opts)
: OptionDefinitionBase(section_, name_, opts...)
{
constexpr bool has_default =
((config::is_default_array<Options> || config::is_default<Options>) || ...);
constexpr bool has_required =
(std::is_same_v<config::remove_cvref_t<Options>, config::Required_t> || ...);
constexpr bool has_hidden =
(std::is_same_v<config::remove_cvref_t<Options>, config::Hidden_t> || ...);
static_assert(
not(has_default and has_required), "Default{...} and Required are mutually exclusive");
static_assert(not(has_hidden and has_required), "Hidden and Required are mutually exclusive");

(extractDefault(std::forward<Options>(opts)), ...);
(extractAcceptor(std::forward<Options>(opts)), ...);
(extractComments(std::forward<Options>(opts)), ...);
Expand Down
11 changes: 5 additions & 6 deletions test/config/test_llarp_config_definition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ TEST_CASE("OptionDefinition acceptor throws test", "[config]")
TEST_CASE("OptionDefinition tryAccept missing option test", "[config]")
{
int unset = -1;
llarp::OptionDefinition<int> def("foo", "bar", Required, Default{1}, [&](int arg) {
llarp::OptionDefinition<int> def("foo", "bar", Required, [&](int arg) {
(void)arg;
unset = 0; // should never be called
});
Expand Down Expand Up @@ -170,7 +170,6 @@ TEST_CASE("ConfigDefinition required test", "[config]")
config.defineOption(std::make_unique<llarp::OptionDefinition<int>>(
"router",
"threads",
Default{1},
Required));

CHECK_THROWS(config.validateRequiredFields());
Expand All @@ -186,13 +185,13 @@ TEST_CASE("ConfigDefinition section test", "[config]")
config.defineOption(std::make_unique<llarp::OptionDefinition<int>>(
"foo",
"bar",
Required,
Default{1}));
Required
));
config.defineOption(std::make_unique<llarp::OptionDefinition<int>>(
"goo",
"bar",
Required,
Default{1}));
Required
));

CHECK_THROWS(config.validateRequiredFields());

Expand Down
68 changes: 38 additions & 30 deletions test/config/test_llarp_config_output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,62 @@ TEST_CASE("ConfigDefinition simple generate test", "[config]")
{
llarp::ConfigDefinition config{true};

config.defineOption<int>("foo", "bar", Required, Default{1});
config.defineOption<int>("foo", "bar", Required);
config.defineOption<int>("foo", "baz", Default{2});
config.defineOption(std::make_unique<llarp::OptionDefinition<std::string>>(
"foo", "quux", Required, Default{"hello"}));
config.defineOption(
std::make_unique<llarp::OptionDefinition<std::string>>("foo", "quux", Default{"hello"}));

config.defineOption<int>("argle", "bar", RelayOnly, Required, Default{3});
config.defineOption<int>("argle", "bar", RelayOnly, Required);
config.defineOption<int>("argle", "baz", Default{4});
config.defineOption<std::string>("argle", "quux", Required, Default{"the quick brown fox"});
config.defineOption<std::string>("argle", "quux", Default{"the quick brown fox"});

config.defineOption<int>("not", "for-routers", ClientOnly, Required, Default{1});
config.defineOption<int>("not", "for-routers", ClientOnly, Default{1});

std::string output = config.generateINIConfig();

CHECK(output == R"raw([foo]


bar=1
#bar=

#baz=2

quux=hello
#quux=hello


[argle]


bar=3
#bar=

#baz=4

quux=the quick brown fox
#quux=the quick brown fox
)raw");
}

TEST_CASE("ConfigDefinition useValue test", "[config]")
{
llarp::ConfigDefinition config{true};

config.defineOption<int>("foo", "bar", Required, Default{1});
config.defineOption<int>("foo", "bar", Default{1});

constexpr auto expected = "[foo]\n\n\nbar=1\n";
constexpr auto expected = R"raw([foo]


#bar=1
)raw";

CHECK(config.generateINIConfig(false) == expected);
CHECK(config.generateINIConfig(true) == expected);

config.addConfigValue("foo", "bar", "2");

constexpr auto expectedWhenValueProvided = "[foo]\n\n\nbar=2\n";
constexpr auto expectedWhenValueProvided = R"raw([foo]


bar=2
)raw";

CHECK(config.generateINIConfig(false) == expected);
CHECK(config.generateINIConfig(true) == expectedWhenValueProvided);
Expand All @@ -67,8 +75,7 @@ TEST_CASE("ConfigDefinition section comments test")

config.addSectionComments("foo", {"test comment"});
config.addSectionComments("foo", {"test comment 2"});
config.defineOption(std::make_unique<llarp::OptionDefinition<int>>(
"foo", "bar", Required, Default{1}));
config.defineOption(std::make_unique<llarp::OptionDefinition<int>>("foo", "bar", Default{1}));

std::string output = config.generateINIConfig();

Expand All @@ -77,7 +84,7 @@ TEST_CASE("ConfigDefinition section comments test")
# test comment 2


bar=1
#bar=1
)raw");
}

Expand All @@ -87,18 +94,21 @@ TEST_CASE("ConfigDefinition option comments test")

config.addOptionComments("foo", "bar", {"test comment 1"});
config.addOptionComments("foo", "bar", {"test comment 2"});
config.defineOption<int>("foo", "bar", Required, Default{1});
config.defineOption<int>("foo", "bar", Default{1});

config.defineOption<std::string>("foo", "far", Default{"abc"},
config.defineOption<std::string>(
"foo",
"far",
Default{"abc"},
Comment{
"Fill in the missing values:",
"___defg",
"Fill in the missing values:",
"___defg",
});

config.defineOption<int>("client", "omg", ClientOnly, Default{1}, Comment{"hi"});
config.defineOption<int>("relay", "ftw", RelayOnly, Default{1}, Comment{"bye"});

// has comment, so still gets shown.
// has comment, but is hidden: we show only the comment but not the value/default.
config.defineOption<int>("foo", "old-bar", Hidden, Default{456});
config.addOptionComments("foo", "old-bar", {"old bar option"});

Expand All @@ -112,14 +122,13 @@ TEST_CASE("ConfigDefinition option comments test")

# test comment 1
# test comment 2
bar=1
#bar=1

# Fill in the missing values:
# ___defg
#far=abc

# old bar option
#old-bar=456


[relay]
Expand All @@ -139,7 +148,7 @@ TEST_CASE("ConfigDefinition empty comments test")

config.addOptionComments("foo", "bar", {"option comment"});
config.addOptionComments("foo", "bar", {""});
config.defineOption<int>("foo", "bar", Required, Default{1});
config.defineOption<int>("foo", "bar", Default{1});

std::string output = config.generateINIConfig();

Expand All @@ -150,7 +159,7 @@ TEST_CASE("ConfigDefinition empty comments test")

# option comment
#
bar=1
#bar=1
)raw");
}

Expand All @@ -161,10 +170,10 @@ TEST_CASE("ConfigDefinition multi option comments")
config.addSectionComments("foo", {"foo section comment"});

config.addOptionComments("foo", "bar", {"foo bar option comment"});
config.defineOption<int>("foo", "bar", Required, Default{1});
config.defineOption<int>("foo", "bar", Default{1});

config.addOptionComments("foo", "baz", {"foo baz option comment"});
config.defineOption<int>("foo", "baz", Required, Default{1});
config.defineOption<int>("foo", "baz", Default{1});

std::string output = config.generateINIConfig();

Expand All @@ -173,10 +182,9 @@ TEST_CASE("ConfigDefinition multi option comments")


# foo bar option comment
bar=1
#bar=1

# foo baz option comment
baz=1
#baz=1
)raw");
}

Loading