Permalink
Browse files

Fixed error with empty default value assignment

  • Loading branch information...
1 parent 486539e commit 71aaf015fb5f325f77618cd769b87fa7d4aca53c @saleyn committed Jul 15, 2014
@@ -449,6 +449,8 @@ def process_options(self, f, root, level=0, arg='m_options'):
elif max : err = "'max': %s " % max
elif maxlen : err = "'max_length': %s " % maxlen
+ if default != None: required = 'false'
+
for n in node.xpath("./name"):
f.write('%sl_names.insert("%s");\n' % \
(ws1, self.value_to_string(n.attrib.get('val'), tp)))
@@ -468,17 +470,25 @@ def process_options(self, f, root, level=0, arg='m_options'):
valtp = 'string' if not tp and subopts else valtype;
+ if default == None:
+ defval = "v()"
+ elif len(default) == 0:
+ defval = "variant(\"\")"
+ else:
+ defval = ('v("%s")' % default)
+
f.write("%sadd_option(%s,\n" % (ws1, arg))
f.write("%sconfig::option(%s(), %s, %s,\n"
- '%s "%s", %s, %s, %s,\n'
- "%s v(%s), v(%s), v(%s), l_names, l_values, l_children%d));\n"
+ '%s "%s", %s /*unique*/, %s /*required*/, %s,\n'
+ "%s %s /*default*/, v(%s) /*min*/, v(%s) /*max*/,\n"
+ "%s l_names, l_values, l_children%d));\n"
"%s}\n" % (
ws2, format_name(name), str_tp, self.string_to_type(valtp),
ws2, desc, unique, required, macros,
- ws2,('"%s"' % default) if default else "",
+ ws2, defval,
('"%s"' % min) if min else "",
('"%s"' % max) if max else "",
- level,
+ ws2, level,
ws))
@@ -185,6 +185,8 @@ namespace config {
variant_set value_choices;
option_type_t value_type;
+ // NB: Default value is a tree because variant_tree.get_child()
+ // may need to return a default child tree node as a const expression
variant_tree_base default_value;
variant min_value;
variant max_value;
@@ -317,12 +319,22 @@ namespace config {
/// @return config option details
std::string usage(const std::string& a_indent=std::string()) const;
+ /// @return default variant for a given option's path.
+ const variant_tree_base& def(
+ const tree_path& a_path,
+ const tree_path& a_root_path = tree_path()
+ ) const throw (variant_tree_error);
+
/// Get default value for a named option.
/// @return default value for a given option's path.
- const variant_tree_base& default_value(
+ template <class T>
+ T def_value(
const tree_path& a_path,
const tree_path& a_root_path = tree_path()
- ) const throw (variant_tree_error);
+ ) const throw (variant_tree_error)
+ {
+ return def(a_path, a_root_path).data().get<T>();
+ }
/// Find option's metadata
const option* find(const tree_path& a_path,
@@ -410,7 +410,7 @@ class basic_variant_tree : public basic_variant_tree_base<Ch>
if (r) return *r;
if (m_schema_validator)
// default_value will throw if there's no such path
- return m_schema_validator->default_value(path, m_root_path);
+ return m_schema_validator->def(path, m_root_path);
throw variant_tree_bad_path(
"Cannot get child - path not found", m_root_path / path);
}
@@ -197,7 +197,7 @@ const option& validator::get(const tree_path& a_path, const tree_path& a_root) c
throw variant_tree_error(m_root / ep, "Configuration option not found!");
}
-const variant_tree_base& validator::default_value
+const variant_tree_base& validator::def
(
const tree_path& a_path,
const tree_path& a_root_path
@@ -32,6 +32,9 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include <utxx/config_validator.hpp>
#include <utxx/variant_tree_parser.hpp>
+#include <boost/algorithm/string/split.hpp>
+#include <boost/algorithm/string.hpp>
+
// The file below is auto-generated by running
// config_validator.xsl on test_config_validator.xml
#include "generated/test_config_validator.generated.hpp"
@@ -131,7 +134,23 @@ BOOST_AUTO_TEST_CASE( test_config_validator0 )
"tmp_str: string\n"
" Default: \"$TMP\"\n"
"\n";
- BOOST_REQUIRE_EQUAL(expected, s.str());
+ std::string got(s.str());
+ if (expected != got) {
+ std::vector<std::string> res1, res2;
+ boost::split(res1, expected, boost::is_any_of("\n"), boost::algorithm::token_compress_off);
+ boost::split(res2, got, boost::is_any_of("\n"), boost::algorithm::token_compress_off);
+ std::vector<std::string>::iterator it1 = res1.begin(), it2 = res2.begin();
+ std::vector<std::string>::iterator e1 = res1.end(), e2 = res2.end();
+ int i = 1;
+ for (; it1 != e1 && it2 != e2; ++it1, ++it2, ++i)
+ if (*it1 != *it2)
+ break;
+ if (it1 != e1)
+ BOOST_MESSAGE("Expected[" << i << "]: " << *it1);
+ if (it2 != e2)
+ BOOST_MESSAGE("Got [" << i << "]: " << *it2);
+ }
+ BOOST_REQUIRE_EQUAL(expected, got);
}
BOOST_AUTO_TEST_CASE( test_config_validator1 )
@@ -475,11 +494,12 @@ BOOST_AUTO_TEST_CASE( test_config_validator_def )
{
const test::cfg_validator* l_validator = test::cfg_validator::instance();
BOOST_REQUIRE_EQUAL("test", l_validator->root().dump());
- BOOST_REQUIRE_EQUAL(variant("123.124.125.012"), l_validator->default_value("test.address").data());
- BOOST_REQUIRE_EQUAL(variant(true), l_validator->default_value("test.enabled").data());
- BOOST_REQUIRE_EQUAL(variant(1.5), l_validator->default_value("test.cost").data());
- BOOST_REQUIRE_EQUAL(variant("x"), l_validator->default_value("test.section2.abc").data());
- BOOST_REQUIRE_THROW(l_validator->default_value("a.b.c"), variant_tree_error);
+ BOOST_REQUIRE_EQUAL(variant("123.124.125.012"), l_validator->def("test.address").data());
+ BOOST_REQUIRE_EQUAL("123.124.125.012", l_validator->def_value<std::string>("test.address"));
+ BOOST_REQUIRE_EQUAL(true, l_validator->def_value<bool>("test.enabled"));
+ BOOST_REQUIRE_EQUAL(1.5, l_validator->def_value<double>("test.cost"));
+ BOOST_REQUIRE_EQUAL(variant("x"), l_validator->def("test.section2.abc").data());
+ BOOST_REQUIRE_THROW(l_validator->def("a.b.c"), variant_tree_error);
variant_tree l_config("", l_validator);
bool b = l_config.get<bool>("test.enabled");
@@ -490,13 +510,13 @@ BOOST_AUTO_TEST_CASE( test_config_validator_def )
BOOST_REQUIRE_EQUAL("enabled", l_opt->name);
BOOST_REQUIRE_EQUAL(true, l_opt->default_value.data().to_bool());
try {
- l_validator->default_value("name", "test.country");
+ l_validator->def("name", "test.country");
BOOST_REQUIRE(false);
} catch (variant_tree_error& e) {
BOOST_REQUIRE_EQUAL("test.country.name", e.path());
}
try {
- l_validator->default_value("", "test.country.name");
+ l_validator->def("", "test.country.name");
} catch (variant_tree_error& e) {
BOOST_REQUIRE_EQUAL("test.country.name", e.path());
}

0 comments on commit 71aaf01

Please sign in to comment.