diff --git a/ReactCommon/fabric/core/propsConversions.h b/ReactCommon/fabric/core/propsConversions.h index 240f70ce126716..edca1fe8e0ae28 100644 --- a/ReactCommon/fabric/core/propsConversions.h +++ b/ReactCommon/fabric/core/propsConversions.h @@ -15,35 +15,6 @@ namespace facebook { namespace react { -inline bool boolFromDynamic(const folly::dynamic &value) { return value.getBool(); } -inline int intFromDynamic(const folly::dynamic &value) { return value.getInt(); } -inline Float floatFromDynamic(const folly::dynamic &value) { return value.getDouble(); } -inline std::string stringFromDynamic(const folly::dynamic &value) { return value.getString(); } - -#define CONVERT_RAW_PROP_TEMPLATE(type, converter) \ -inline static type convertRawProp(const RawProps &rawProps, const std::string &name, const type &defaultValue) { \ - auto &&iterator = rawProps.find(name); \ - if (iterator != rawProps.end()) { \ - return converter(iterator->second); \ - } else { \ - return defaultValue; \ - } \ -} \ -\ -inline static folly::Optional convertRawProp(const RawProps &rawProps, const std::string &name, const folly::Optional &defaultValue) { \ - auto &&iterator = rawProps.find(name); \ - if (iterator != rawProps.end()) { \ - auto &&value = iterator->second; \ - if (value.isNull()) { \ - return {}; \ - } else { \ - return converter(value); \ - } \ - } else { \ - return defaultValue; \ - } \ -} - inline void fromDynamic(const folly::dynamic &value, bool &result) { result = value.getBool(); } inline void fromDynamic(const folly::dynamic &value, int &result) { result = value.getInt(); } inline void fromDynamic(const folly::dynamic &value, std::string &result) { result = value.getString(); } @@ -51,30 +22,33 @@ inline void fromDynamic(const folly::dynamic &value, std::string &result) { resu template inline T convertRawProp(const RawProps &rawProps, const std::string &name, const T &defaultValue) { auto &&iterator = rawProps.find(name); - if (iterator != rawProps.end()) { - T result = defaultValue; - fromDynamic(iterator->second, result); - return result; - } else { + if (iterator == rawProps.end()) { return defaultValue; } + + auto &&value = iterator->second; + T result; + fromDynamic(value, result); + return result; } template inline static folly::Optional convertRawProp(const RawProps &rawProps, const std::string &name, const folly::Optional &defaultValue) { auto &&iterator = rawProps.find(name); - if (iterator != rawProps.end()) { - auto &&value = iterator->second; - if (value.isNull()) { - return defaultValue; - } else { - T result; - fromDynamic(value, result); - return result; - } - } else { + if (iterator == rawProps.end()) { return defaultValue; } + + auto &&value = iterator->second; + T result; + + // Special case for optionals: `null` always means `no value`. + if (value.isNull()) { + return {}; + } + + fromDynamic(value, result); + return result; } } // namespace react diff --git a/ReactCommon/fabric/debug/debugStringConvertibleUtils.cpp b/ReactCommon/fabric/debug/debugStringConvertibleUtils.cpp deleted file mode 100644 index de9817d4999f44..00000000000000 --- a/ReactCommon/fabric/debug/debugStringConvertibleUtils.cpp +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Copyright (c) 2015-present, Facebook, Inc. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#include "debugStringConvertibleUtils.h" - -namespace facebook { -namespace react { - -SharedDebugStringConvertibleList operator+(const SharedDebugStringConvertibleList &lhs, const SharedDebugStringConvertibleList &rhs) { - SharedDebugStringConvertibleList result = {}; - std::move(lhs.begin(), lhs.end(), std::back_inserter(result)); - std::move(rhs.begin(), rhs.end(), std::back_inserter(result)); - return result; -} - -SharedDebugStringConvertible debugStringConvertibleItem(std::string name, DebugStringConvertible value, std::string defaultValue) { - return debugStringConvertibleItem(name, value.getDebugDescription(), defaultValue); -} - -} // namespace react -} // namespace facebook diff --git a/ReactCommon/fabric/debug/debugStringConvertibleUtils.h b/ReactCommon/fabric/debug/debugStringConvertibleUtils.h index 22eca9acfa907b..836945018b3266 100644 --- a/ReactCommon/fabric/debug/debugStringConvertibleUtils.h +++ b/ReactCommon/fabric/debug/debugStringConvertibleUtils.h @@ -19,6 +19,12 @@ namespace facebook { namespace react { +inline std::string toString(const std::string &value) { return value; } +inline std::string toString(const int &value) { return folly::to(value); } +inline std::string toString(const bool &value) { return folly::to(value); } +inline std::string toString(const float &value) { return folly::to(value); } +inline std::string toString(const double &value) { return folly::to(value); } + template inline SharedDebugStringConvertible debugStringConvertibleItem(std::string name, T value, T defaultValue = {}) { if (value == defaultValue) { @@ -33,38 +39,20 @@ inline SharedDebugStringConvertible debugStringConvertibleItem(std::string name, if (!value.has_value()) { return nullptr; } + return debugStringConvertibleItem(name, value.value_or(defaultValue), defaultValue); } -SharedDebugStringConvertibleList operator+(const SharedDebugStringConvertibleList &lhs, const SharedDebugStringConvertibleList &rhs); -SharedDebugStringConvertible debugStringConvertibleItem(std::string name, DebugStringConvertible value, std::string defaultValue = ""); - -#define IS_EQUAL(a, b) ((a) == (b)) -#define IS_EQUAL_FLOAT(a, b) ((isnan(a) == isnan(b)) || ((a) == (b))) - -#define DEBUG_STRING_CONVERTIBLE_TEMPLATE(type, converter) \ -DEBUG_STRING_CONVERTIBLE_TEMPLATE_EX(type, converter, {}, IS_EQUAL) - -#define DEBUG_STRING_CONVERTIBLE_TEMPLATE_EX(type, converter, defaults, comparator) \ -inline SharedDebugStringConvertible debugStringConvertibleItem(std::string name, type value, type defaultValue = defaults) { \ - if (comparator(value, defaultValue)) { \ - return nullptr; \ - } \ - return std::make_shared(name, converter(value)); \ -} \ -\ -inline SharedDebugStringConvertible debugStringConvertibleItem(std::string name, folly::Optional value, type defaultValue = defaults) { \ - if (value.has_value()) { \ - return nullptr; \ - } \ - return debugStringConvertibleItem(name, value.value_or(defaultValue), defaultValue); \ +inline SharedDebugStringConvertibleList operator+(const SharedDebugStringConvertibleList &lhs, const SharedDebugStringConvertibleList &rhs) { + SharedDebugStringConvertibleList result = {}; + std::move(lhs.begin(), lhs.end(), std::back_inserter(result)); + std::move(rhs.begin(), rhs.end(), std::back_inserter(result)); + return result; } -DEBUG_STRING_CONVERTIBLE_TEMPLATE(std::string, ) -DEBUG_STRING_CONVERTIBLE_TEMPLATE(int, folly::to) -DEBUG_STRING_CONVERTIBLE_TEMPLATE(bool, folly::to) -DEBUG_STRING_CONVERTIBLE_TEMPLATE_EX(float, folly::to, std::numeric_limits::quiet_NaN(), IS_EQUAL_FLOAT) -DEBUG_STRING_CONVERTIBLE_TEMPLATE_EX(double, folly::to, std::numeric_limits::quiet_NaN(), IS_EQUAL_FLOAT) +inline SharedDebugStringConvertible debugStringConvertibleItem(std::string name, DebugStringConvertible value, std::string defaultValue) { + return debugStringConvertibleItem(name, value.getDebugDescription(), defaultValue); +} } // namespace react } // namespace facebook