Commit 825f025d authored by Adam Norton's avatar Adam Norton Committed by Facebook GitHub Bot

Support DynamicConverter conversions to dynamic

Summary: Allow conversions to types containing a `dynamic`.

Reviewed By: yfeldblum

Differential Revision: D33823657

fbshipit-source-id: b09537639efad185114dfd5953c1707207ddfcdf
parent 744a0a69
......@@ -181,6 +181,12 @@ inline std::move_iterator<Transformer<T, It>> conversionIterator(const It& it) {
template <typename T, typename Enable = void>
struct DynamicConverter;
// dynamic
template <>
struct DynamicConverter<dynamic> {
static dynamic convert(const dynamic& d) { return d; }
};
// boolean
template <>
struct DynamicConverter<bool> {
......
......@@ -71,6 +71,23 @@ TEST(DynamicConverter, template_metaprogramming) {
EXPECT_EQ(r2t, true);
}
TEST(DynamicConverter, dynamics) {
dynamic d1 = 12;
auto d2 = convertTo<dynamic>(d1);
EXPECT_EQ(12, d2.getInt());
dynamic d3 = dynamic::array(1, 2, 3);
auto v = convertTo<std::vector<dynamic>>(d3);
EXPECT_EQ(1, v.at(0).getInt());
EXPECT_EQ(2, v.at(1).getInt());
EXPECT_EQ(3, v.at(2).getInt());
dynamic d4 = dynamic::object("1", 1)("2", 2.3);
auto m = convertTo<std::map<std::string, dynamic>>(d4);
EXPECT_EQ(1, m.at("1").getInt());
EXPECT_EQ(2.3, m.at("2").getDouble());
}
TEST(DynamicConverter, arithmetic_types) {
dynamic d1 = 12;
auto i1 = convertTo<int>(d1);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment