Commit 981ed984 authored by Ashoat Tevosyan's avatar Ashoat Tevosyan Committed by Jordan DeLong

Add DynamicConverters for const strings.

Summary: These are necessary for string-keyed maps, since maps const their keys.

Test Plan: I wrote a test.

Reviewed By: njormrod@fb.com

FB internal diff: D582283
parent 49417268
......@@ -268,7 +268,7 @@ struct DynamicConverter<C,
template <typename T>
T convertTo(const dynamic& d) {
return DynamicConverter<T>::convert(d);
return DynamicConverter<typename std::remove_cv<T>::type>::convert(d);
}
} // namespace folly
......
......@@ -141,6 +141,18 @@ TEST(DynamicConverter, simple_map) {
EXPECT_EQ(i2, i2b);
}
TEST(DynamicConverter, map_keyed_by_string) {
dynamic d1 = dynamic::object("1", "one")("2", "two");
auto i1 = convertTo<std::map<std::string, std::string>>(d1);
decltype(i1) i1b = { { "1", "one" }, { "2", "two" } };
EXPECT_EQ(i1, i1b);
dynamic d2 = { { "3", "three" }, { "4", "four" } };
auto i2 = convertTo<std::unordered_map<std::string, std::string>>(d2);
decltype(i2) i2b = { { "3", "three" }, { "4", "four" } };
EXPECT_EQ(i2, i2b);
}
TEST(DynamicConverter, nested_containers) {
dynamic d1 = { { 1 }, { }, { 2, 3 } };
auto i1 = convertTo<folly::fbvector<std::vector<uint8_t>>>(d1);
......@@ -227,6 +239,30 @@ TEST(DynamicConverter, crazy) {
EXPECT_EQ(f1, i);
}
TEST(DynamicConverter, consts) {
dynamic d1 = 7.5;
auto i1 = convertTo<const double>(d1);
EXPECT_EQ(7.5, i1);
dynamic d2 = "Hello";
auto i2 = convertTo<const std::string>(d2);
decltype(i2) i2b = "Hello";
EXPECT_EQ(i2b, i2);
dynamic d3 = true;
auto i3 = convertTo<const bool>(d3);
EXPECT_EQ(true, i3);
dynamic d4 = "true";
auto i4 = convertTo<const bool>(d4);
EXPECT_EQ(true, i4);
dynamic d5 = { 1, 2 };
auto i5 = convertTo<const std::pair<const int, const int>>(d5);
decltype(i5) i5b = { 1, 2 };
EXPECT_EQ(i5b, i5);
}
struct Token {
int kind_;
fbstring lexeme_;
......
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