Properly handle empty config default values

This commit is contained in:
Stephen Shelton 2020-04-30 14:05:05 -06:00 committed by Jeff Becker
parent dd9ab0f1d5
commit 559610ec94
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05
2 changed files with 18 additions and 9 deletions

View File

@ -182,12 +182,13 @@ namespace llarp
return addr; return addr;
}; };
conf.defineOption<std::string>("dns", "upstream-dns", true, "", [=](std::string arg) { conf.defineOption<std::string>(
m_upstreamDNS.push_back(parseAddr(arg)); "dns", "upstream-dns", false, true, nonstd::nullopt, [=](std::string arg) {
}); m_upstreamDNS.push_back(parseAddr(arg));
});
conf.defineOption<std::string>( conf.defineOption<std::string>(
"dns", "bind", false, "", [=](std::string arg) { m_bind = parseAddr(arg); }); "dns", "bind", false, nonstd::nullopt, [=](std::string arg) { m_bind = parseAddr(arg); });
} }
LinksConfig::LinkInfo LinksConfig::LinkInfo

View File

@ -2,6 +2,7 @@
#include <util/str.hpp> #include <util/str.hpp>
#include <iostream>
#include <memory> #include <memory>
#include <set> #include <set>
#include <sstream> #include <sstream>
@ -122,9 +123,9 @@ namespace llarp
if (parsedValues.size()) if (parsedValues.size())
return parsedValues[0]; return parsedValues[0];
else if (not required and not multiValued) else if (not required and not multiValued)
return defaultValue.value(); return defaultValue;
else else
return {}; return nonstd::nullopt;
} }
/// Returns the value at the given index. /// Returns the value at the given index.
@ -223,6 +224,7 @@ namespace llarp
{ {
if (multiValued) if (multiValued)
{ {
std::cout << name << " has " << parsedValues.size() << " parsedValues" << std::endl;
for (const auto& value : parsedValues) for (const auto& value : parsedValues)
{ {
acceptor(value); acceptor(value);
@ -230,10 +232,16 @@ namespace llarp
} }
else else
{ {
std::cout << name << " is NOT multi-valued" << std::endl;
auto maybe = getValue(); auto maybe = getValue();
assert(maybe.has_value()); // should be guaranteed by our earlier checks if (maybe.has_value())
// TODO: avoid copies here if possible {
acceptor(maybe.value()); acceptor(maybe.value());
}
else
{
assert(not defaultValue.has_value()); // maybe should have a value if defaultValue does
}
} }
} }
} }