/* * Copyright (c) Facebook, Inc. and its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // @author Nicholas Ormrod #pragma once #include #include #include #include #include #include #include #include #include #include namespace folly { template T convertTo(const dynamic&); template dynamic toDynamic(const T&); } // namespace folly /** * convertTo returns a well-typed representation of the input dynamic. * * Example: * * dynamic d = dynamic::array( * dynamic::array(1, 2, 3), * dynamic::array(4, 5)); // a vector of vector of int * auto vvi = convertTo>>(d); * * See docs/DynamicConverter.md for supported types and customization */ namespace folly { /////////////////////////////////////////////////////////////////////////////// // traits namespace dynamicconverter_detail { BOOST_MPL_HAS_XXX_TRAIT_DEF(value_type) BOOST_MPL_HAS_XXX_TRAIT_DEF(iterator) BOOST_MPL_HAS_XXX_TRAIT_DEF(mapped_type) BOOST_MPL_HAS_XXX_TRAIT_DEF(key_type) template struct iterator_class_is_container { typedef std::reverse_iterator some_iterator; enum { value = has_value_type::value && std::is_constructible::value }; }; template using class_is_container = Conjunction, iterator_class_is_container>; template using is_range = StrictConjunction, has_iterator>; template using is_container = StrictConjunction, class_is_container>; template using is_map = StrictConjunction, has_mapped_type>; template using is_associative = StrictConjunction, has_key_type>; } // namespace dynamicconverter_detail /////////////////////////////////////////////////////////////////////////////// // custom iterators /** * We have iterators that dereference to dynamics, but need iterators * that dereference to typename T. * * Implementation details: * 1. We cache the value of the dereference operator. This is necessary * because boost::iterator_adaptor requires *it to return a * reference. * 2. For const reasons, we cannot call operator= to refresh the * cache: we must call the destructor then placement new. */ namespace dynamicconverter_detail { template struct Dereferencer { static inline void derefToCache( Optional* /* mem */, const dynamic::const_item_iterator& /* it */) { throw_exception("array", dynamic::Type::OBJECT); } static inline void derefToCache( Optional* mem, const dynamic::const_iterator& it) { mem->emplace(convertTo(*it)); } }; template struct Dereferencer> { static inline void derefToCache( Optional>* mem, const dynamic::const_item_iterator& it) { mem->emplace(convertTo(it->first), convertTo(it->second)); } // Intentional duplication of the code in Dereferencer template static inline void derefToCache( Optional* mem, const dynamic::const_iterator& it) { mem->emplace(convertTo(*it)); } }; template class Transformer : public boost:: iterator_adaptor, It, typename T::value_type> { friend class boost::iterator_core_access; typedef typename T::value_type ttype; mutable Optional cache_; void increment() { ++this->base_reference(); cache_ = none; } ttype& dereference() const { if (!cache_) { Dereferencer::derefToCache(&cache_, this->base_reference()); } return cache_.value(); } public: explicit Transformer(const It& it) : Transformer::iterator_adaptor_(it) {} }; // conversion factory template inline std::move_iterator> conversionIterator(const It& it) { return std::make_move_iterator(Transformer(it)); } } // namespace dynamicconverter_detail /////////////////////////////////////////////////////////////////////////////// // DynamicConverter specializations /** * Each specialization of DynamicConverter has the function * 'static T convert(const dynamic&);' */ // default - intentionally unimplemented template struct DynamicConverter; // boolean template <> struct DynamicConverter { static bool convert(const dynamic& d) { return d.asBool(); } }; // integrals template struct DynamicConverter< T, typename std::enable_if< std::is_integral::value && !std::is_same::value>::type> { static T convert(const dynamic& d) { return folly::to(d.asInt()); } }; // enums template struct DynamicConverter< T, typename std::enable_if::value>::type> { static T convert(const dynamic& d) { using type = typename std::underlying_type::type; return static_cast(DynamicConverter::convert(d)); } }; // floating point template struct DynamicConverter< T, typename std::enable_if::value>::type> { static T convert(const dynamic& d) { return folly::to(d.asDouble()); } }; // fbstring template <> struct DynamicConverter { static folly::fbstring convert(const dynamic& d) { return d.asString(); } }; // std::string template <> struct DynamicConverter { static std::string convert(const dynamic& d) { return d.asString(); } }; // std::pair template struct DynamicConverter> { static std::pair convert(const dynamic& d) { if (d.isArray() && d.size() == 2) { return std::make_pair(convertTo(d[0]), convertTo(d[1])); } else if (d.isObject() && d.size() == 1) { auto it = d.items().begin(); return std::make_pair(convertTo(it->first), convertTo(it->second)); } else { throw_exception("array (size 2) or object (size 1)", d.type()); } } }; // non-associative containers template struct DynamicConverter< C, typename std::enable_if< dynamicconverter_detail::is_container::value && !dynamicconverter_detail::is_associative::value>::type> { static C convert(const dynamic& d) { if (d.isArray()) { return C( dynamicconverter_detail::conversionIterator(d.begin()), dynamicconverter_detail::conversionIterator(d.end())); } else if (d.isObject()) { return C( dynamicconverter_detail::conversionIterator(d.items().begin()), dynamicconverter_detail::conversionIterator(d.items().end())); } else { throw_exception("object or array", d.type()); } } }; // associative containers template struct DynamicConverter< C, typename std::enable_if< dynamicconverter_detail::is_container::value && dynamicconverter_detail::is_associative::value>::type> { static C convert(const dynamic& d) { C ret; // avoid direct initialization due to unordered_map's constructor // causing memory corruption if the iterator throws an exception if (d.isArray()) { ret.insert( dynamicconverter_detail::conversionIterator(d.begin()), dynamicconverter_detail::conversionIterator(d.end())); } else if (d.isObject()) { ret.insert( dynamicconverter_detail::conversionIterator(d.items().begin()), dynamicconverter_detail::conversionIterator(d.items().end())); } else { throw_exception("object or array", d.type()); } return ret; } }; /////////////////////////////////////////////////////////////////////////////// // DynamicConstructor specializations /** * Each specialization of DynamicConstructor has the function * 'static dynamic construct(const C&);' */ // default template struct DynamicConstructor { static dynamic construct(const C& x) { return dynamic(x); } }; // identity template struct DynamicConstructor< C, typename std::enable_if::value>::type> { static dynamic construct(const C& x) { return x; } }; // enums template struct DynamicConstructor< C, typename std::enable_if::value>::type> { static dynamic construct(const C& x) { return dynamic(to_underlying(x)); } }; // maps template struct DynamicConstructor< C, typename std::enable_if< !std::is_same::value && dynamicconverter_detail::is_map::value>::type> { static dynamic construct(const C& x) { dynamic d = dynamic::object; for (const auto& pair : x) { d.insert(toDynamic(pair.first), toDynamic(pair.second)); } return d; } }; // other ranges template struct DynamicConstructor< C, typename std::enable_if< !std::is_same::value && !dynamicconverter_detail::is_map::value && !std::is_constructible::value && dynamicconverter_detail::is_range::value>::type> { static dynamic construct(const C& x) { dynamic d = dynamic::array; for (const auto& item : x) { d.push_back(toDynamic(item)); } return d; } }; // pair template struct DynamicConstructor, void> { static dynamic construct(const std::pair& x) { dynamic d = dynamic::array; d.push_back(toDynamic(x.first)); d.push_back(toDynamic(x.second)); return d; } }; // vector template <> struct DynamicConstructor, void> { static dynamic construct(const std::vector& x) { dynamic d = dynamic::array; // Intentionally specifying the type as bool here. // std::vector's iterators return a proxy which is a prvalue // and hence cannot bind to an lvalue reference such as auto& for (bool item : x) { d.push_back(toDynamic(item)); } return d; } }; /////////////////////////////////////////////////////////////////////////////// // implementation template T convertTo(const dynamic& d) { return DynamicConverter::type>::convert(d); } template dynamic toDynamic(const T& x) { return DynamicConstructor::type>::construct(x); } } // namespace folly