Permalink
Browse files

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
  • Loading branch information...
1 parent 4941726 commit 981ed98473314f0e3df5e0c4d47c8574d9523289 Ashoat Tevosyan committed with jdelong Sep 24, 2012
Showing with 37 additions and 1 deletion.
  1. +1 −1 folly/DynamicConverter.h
  2. +36 −0 folly/test/DynamicConverterTest.cpp
View
@@ -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_;

0 comments on commit 981ed98

Please sign in to comment.