From f8e187f69922979c386394e6b39fcbab0c0d4ef3 Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Wed, 12 Mar 2014 17:27:09 -0700 Subject: [PATCH] Make sure folly::dynamic arrays can be used with folly::gen Summary: Define a value_type for folly::dynamic to make arrays work with folly::gen::from. Test Plan: - added new unit test - fbconfig folly/test && fbmake runtests Reviewed By: tjackson@fb.com FB internal diff: D1216358 --- folly/dynamic.h | 1 + folly/test/DynamicTest.cpp | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/folly/dynamic.h b/folly/dynamic.h index 8a017852..1494a557 100644 --- a/folly/dynamic.h +++ b/folly/dynamic.h @@ -110,6 +110,7 @@ private: typedef std::vector Array; public: typedef Array::const_iterator const_iterator; + typedef dynamic value_type; struct const_key_iterator; struct const_value_iterator; struct const_item_iterator; diff --git a/folly/test/DynamicTest.cpp b/folly/test/DynamicTest.cpp index 123e5d5c..18dcbcf5 100644 --- a/folly/test/DynamicTest.cpp +++ b/folly/test/DynamicTest.cpp @@ -20,6 +20,7 @@ #include "folly/Benchmark.h" #include "folly/dynamic.h" +#include "folly/gen/Base.h" #include "folly/json.h" using folly::dynamic; @@ -305,6 +306,13 @@ TEST(Dynamic, GetPtr) { EXPECT_EQ(dynamic(2), *cobject.get_ptr("two")); } +TEST(Dynamic, ArrayGenerator) { + // Make sure arrays can be used with folly::gen. + using namespace folly::gen; + dynamic arr { 1, 2, 3, 4 }; + EXPECT_EQ(from(arr) | take(3) | member(&dynamic::asInt) | sum, 6); +} + int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); google::ParseCommandLineFlags(&argc, &argv, true); -- 2.34.1