From e88735c87c71aaffd33d559af6aa1a1247dd253d Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Thu, 7 Jul 2016 01:27:05 -0700 Subject: [PATCH] Handle conversion from float in toDynamic() Summary: This adds the necessary ConversionHelper to enable float-to-double conversion when using `toDynamic` on any type that contains `float`. Fixes the added test case, which previously failed to compile. Reviewed By: yfeldblum Differential Revision: D3525942 fbshipit-source-id: d904dde5585316ea9a15e21430e91ac4e33116b9 --- folly/dynamic-inl.h | 17 ++++++++++------- folly/test/DynamicConverterTest.cpp | 6 ++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/folly/dynamic-inl.h b/folly/dynamic-inl.h index 920f627c..08374f7b 100644 --- a/folly/dynamic-inl.h +++ b/folly/dynamic-inl.h @@ -107,14 +107,17 @@ namespace detail { > { typedef int64_t type; }; - template + template <> + struct ConversionHelper { + typedef double type; + }; + template struct ConversionHelper< - T, - typename std::enable_if< - (!std::is_integral::value || std::is_same::value) && - !std::is_same::value - >::type - > { + T, + typename std::enable_if< + (!std::is_integral::value || std::is_same::value) && + !std::is_same::value && + !std::is_same::value>::type> { typedef T type; }; template diff --git a/folly/test/DynamicConverterTest.cpp b/folly/test/DynamicConverterTest.cpp index 039ce198..1d45e67d 100644 --- a/folly/test/DynamicConverterTest.cpp +++ b/folly/test/DynamicConverterTest.cpp @@ -343,6 +343,12 @@ TEST(DynamicConverter, construct) { EXPECT_EQ(d, toDynamic(c)); } + { + vector c{1.0f, 2.0f, 4.0f}; + dynamic d = dynamic::array(1.0, 2.0, 4.0); + EXPECT_EQ(d, toDynamic(c)); + } + { map c { { 2, 4 }, { 3, 9 } }; dynamic d = dynamic::object(2, 4)(3, 9); -- 2.34.1