revert recursive find function

I found that in a user-code (I'm also one of the users of this library),
this new feature sometimes causes an error. Some of my code won't
compile because of this change. Since toml::table is convertible to
toml::value *implicitly*, if toml::find(table, key, tablename) was
called, the overload resolution becomes ambiguous with toml::find(
value, key1, key2). But dropping support for toml::find(toml::table,
key, tablename) is a breaking change. So I concluded that now is not
the right time yet.
This commit is contained in:
ToruNiina
2019-06-16 19:52:59 +09:00
parent b2daf916b3
commit cbaaaaca7c
3 changed files with 20 additions and 172 deletions

View File

@@ -12,9 +12,8 @@
#include <deque>
#include <array>
BOOST_AUTO_TEST_CASE(test_find_for_value)
BOOST_AUTO_TEST_CASE(test_find)
{
// value itself is not a table
{
toml::value v(true);
bool thrown = false;
@@ -28,87 +27,22 @@ BOOST_AUTO_TEST_CASE(test_find_for_value)
}
BOOST_CHECK(thrown);
}
// the value corresponding to the key is not the expected type
{
toml::value v{{"key", 42}};
bool thrown = false;
try
{
toml::find<toml::boolean>(v, "key");
}
catch(toml::type_error const& te)
{
thrown = true;
}
BOOST_CHECK(thrown);
toml::table v{{"num", 42}};
BOOST_CHECK_EQUAL(42, toml::find<int>(v, "num"));
toml::find<toml::integer>(v, "num") = 54;
BOOST_CHECK_EQUAL(54, toml::find<int>(v, "num"));
}
{
toml::value v = toml::table{{"num", 42}};
BOOST_CHECK_EQUAL(42, toml::find<int>(v, "num"));
// reference that can be used to modify the content
auto& num = toml::find<toml::integer>(v, "num");
num = 54;
toml::find<toml::integer>(v, "num") = 54;
BOOST_CHECK_EQUAL(54, toml::find<int>(v, "num"));
}
// recursively search tables
{
toml::value v = toml::table{
{"a", toml::table{
{"b", toml::table{
{"c", toml::table{
{"d", 42}
}}
}}
}}
};
BOOST_CHECK_EQUAL(42, toml::find<int>(v, "a", "b", "c", "d"));
// reference that can be used to modify the content
auto& num = toml::find<toml::integer>(v, "a", "b", "c", "d");
num = 54;
BOOST_CHECK_EQUAL(54, toml::find<int>(v, "a", "b", "c", "d"));
const std::string a("a"), b("b"), c("c"), d("d");
auto& num2 = toml::find<toml::integer>(v, a, b, c, d);
num2 = 42;
BOOST_CHECK_EQUAL(42, toml::find<int>(v, a, b, c, d));
}
}
BOOST_AUTO_TEST_CASE(test_find_for_table)
{
// the value corresponding to the key is not the expected type
{
toml::table v{{"key", 42}};
bool thrown = false;
try
{
toml::find<toml::boolean>(v, "key");
}
catch(toml::type_error const& te)
{
thrown = true;
}
BOOST_CHECK(thrown);
}
{
toml::table v{{"num", 42}};
BOOST_CHECK_EQUAL(42, toml::find<int>(v, "num"));
// reference that can be used to modify the content
auto& num = toml::find<toml::integer>(v, "num");
num = 54;
BOOST_CHECK_EQUAL(54, toml::find<int>(v, "num"));
}
// recursive search is not provided for tables.
}
BOOST_AUTO_TEST_CASE(test_get_or)
{
// requires conversion int -> uint