Fabric: Final cleanup of define-based props treatment

Summary: Oh, my! No more `#define`s related to props conversions and debug-printing.

Reviewed By: fkgozali

Differential Revision: D7958250

fbshipit-source-id: 86950070c55f134aa3a575b9fd68fc90d865cf44
This commit is contained in:
Valentin Shergin 2018-05-14 15:43:57 -07:00 committed by Facebook Github Bot
parent 120dcec621
commit 1f9676a1cb
3 changed files with 33 additions and 96 deletions

View File

@ -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<type> convertRawProp(const RawProps &rawProps, const std::string &name, const folly::Optional<type> &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 <typename T>
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 <typename T>
inline static folly::Optional<T> convertRawProp(const RawProps &rawProps, const std::string &name, const folly::Optional<T> &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

View File

@ -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

View File

@ -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<std::string>(value); }
inline std::string toString(const bool &value) { return folly::to<std::string>(value); }
inline std::string toString(const float &value) { return folly::to<std::string>(value); }
inline std::string toString(const double &value) { return folly::to<std::string>(value); }
template <typename T>
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<DebugStringConvertibleItem>(name, converter(value)); \
} \
\
inline SharedDebugStringConvertible debugStringConvertibleItem(std::string name, folly::Optional<type> 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<std::string>)
DEBUG_STRING_CONVERTIBLE_TEMPLATE(bool, folly::to<std::string>)
DEBUG_STRING_CONVERTIBLE_TEMPLATE_EX(float, folly::to<std::string>, std::numeric_limits<float>::quiet_NaN(), IS_EQUAL_FLOAT)
DEBUG_STRING_CONVERTIBLE_TEMPLATE_EX(double, folly::to<std::string>, std::numeric_limits<float>::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