Allow setting more unregistered variables in Config class which can be later accessed using CONFIG_LIST
This commit is contained in:
parent
4100b4caf8
commit
83113fe697
3 changed files with 55 additions and 13 deletions
|
@ -37,6 +37,7 @@
|
|||
|
||||
#define CONFIG_STRING_DEFAULTED(PTR, KEY, DEF) ((*PTR).hasKey(KEY) ? (*PTR)[KEY].as<std::string>() : DEF)
|
||||
#define CONFIG_BOOL_DEFAULTED(PTR, KEY, DEF) ((*PTR).hasKey(KEY) ? (*PTR)[KEY].as<bool>() : DEF)
|
||||
#define CONFIG_LIST_DEFAULTED(PTR, KEY, DEF) ((*PTR).hasKey(KEY) ? (*PTR)[KEY].as<std::list<std::string> >() : DEF)
|
||||
|
||||
|
||||
namespace Transport {
|
||||
|
|
|
@ -135,18 +135,6 @@ bool Config::load(std::istream &ifs, boost::program_options::options_description
|
|||
|
||||
;
|
||||
|
||||
// Load configs passed by command line
|
||||
if (m_argc != 0 && m_argv) {
|
||||
basic_command_line_parser<char> parser = command_line_parser(m_argc, m_argv).options(opts).allow_unregistered();
|
||||
parsed_options parsed = parser.run();
|
||||
BOOST_FOREACH(option &opt, parsed.options) {
|
||||
if (opt.unregistered && !opt.value.empty()) {
|
||||
m_unregistered[opt.string_key] = variable_value(opt.value[0], false);
|
||||
}
|
||||
}
|
||||
store(parsed, m_variables);
|
||||
}
|
||||
|
||||
parsed_options parsed = parse_config_file(ifs, opts, true);
|
||||
|
||||
bool found_working = false;
|
||||
|
@ -212,15 +200,43 @@ bool Config::load(std::istream &ifs, boost::program_options::options_description
|
|||
parsed.options.push_back(boost::program_options::basic_option<char>("database.database", value));
|
||||
}
|
||||
|
||||
std::list<std::string> has_key;
|
||||
BOOST_FOREACH(option &opt, parsed.options) {
|
||||
if (opt.unregistered) {
|
||||
m_unregistered[opt.string_key] = variable_value(opt.value[0], false);
|
||||
if (std::find(has_key.begin(), has_key.end(), opt.string_key) == has_key.end()) {
|
||||
has_key.push_back(opt.string_key);
|
||||
m_unregistered[opt.string_key] = variable_value(opt.value[0], false);
|
||||
}
|
||||
else {
|
||||
std::list<std::string> list;
|
||||
try {
|
||||
list = m_unregistered[opt.string_key].as<std::list<std::string> >();
|
||||
}
|
||||
catch(...) {
|
||||
list.push_back(m_unregistered[opt.string_key].as<std::string>());
|
||||
}
|
||||
|
||||
list.push_back(opt.value[0]);
|
||||
m_unregistered[opt.string_key] = variable_value(list, false);
|
||||
}
|
||||
}
|
||||
else if (opt.value[0].find("$jid") != std::string::npos) {
|
||||
boost::replace_all(opt.value[0], "$jid", jid);
|
||||
}
|
||||
}
|
||||
|
||||
// Load configs passed by command line
|
||||
if (m_argc != 0 && m_argv) {
|
||||
basic_command_line_parser<char> parser = command_line_parser(m_argc, m_argv).options(opts).allow_unregistered();
|
||||
parsed_options parsed = parser.run();
|
||||
BOOST_FOREACH(option &opt, parsed.options) {
|
||||
if (opt.unregistered && !opt.value.empty()) {
|
||||
m_unregistered[opt.string_key] = variable_value(opt.value[0], false);
|
||||
}
|
||||
}
|
||||
store(parsed, m_variables);
|
||||
}
|
||||
|
||||
store(parsed, m_variables);
|
||||
notify(m_variables);
|
||||
|
||||
|
|
|
@ -24,7 +24,10 @@ using namespace Transport;
|
|||
|
||||
class ConfigTest : public CPPUNIT_NS :: TestFixture{
|
||||
CPPUNIT_TEST_SUITE(ConfigTest);
|
||||
CPPUNIT_TEST(setStringTwice);
|
||||
CPPUNIT_TEST(updateBackendConfig);
|
||||
CPPUNIT_TEST(unregisteredList);
|
||||
CPPUNIT_TEST(unregisteredString);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
public:
|
||||
|
@ -35,6 +38,14 @@ class ConfigTest : public CPPUNIT_NS :: TestFixture{
|
|||
|
||||
}
|
||||
|
||||
void setStringTwice() {
|
||||
char *argv[3] = {"binary", "--service.jids=localhost", NULL};
|
||||
Config cfg(2, argv);
|
||||
std::istringstream ifs("service.jids = irc.freenode.org\n");
|
||||
cfg.load(ifs);
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("localhost"), CONFIG_STRING(&cfg, "service.jids"));
|
||||
}
|
||||
|
||||
void updateBackendConfig() {
|
||||
Config cfg;
|
||||
CPPUNIT_ASSERT(!cfg.hasKey("registration.needPassword"));
|
||||
|
@ -44,6 +55,20 @@ class ConfigTest : public CPPUNIT_NS :: TestFixture{
|
|||
CPPUNIT_ASSERT_EQUAL(false, CONFIG_BOOL(&cfg, "registration.needPassword"));
|
||||
}
|
||||
|
||||
void unregisteredList() {
|
||||
Config cfg;
|
||||
std::istringstream ifs("service.irc_server = irc.freenode.org\nservice.irc_server=localhost\n");
|
||||
cfg.load(ifs);
|
||||
CPPUNIT_ASSERT_EQUAL(2, (int) CONFIG_LIST(&cfg, "service.irc_server").size());
|
||||
}
|
||||
|
||||
void unregisteredString() {
|
||||
Config cfg;
|
||||
std::istringstream ifs("service.irc_server = irc.freenode.org");
|
||||
cfg.load(ifs);
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("irc.freenode.org"), CONFIG_STRING(&cfg, "service.irc_server"));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION (ConfigTest);
|
||||
|
|
Loading…
Add table
Reference in a new issue