Skip to content

Commit

Permalink
Fabric: Final cleanup of define-based props treatment
Browse files Browse the repository at this point in the history
Summary: Oh, my! No more `#define`s related to props conversions and debug-printing.

Reviewed By: fkgozali

Differential Revision: D7958250

fbshipit-source-id: 86950070c55f134aa3a575b9fd68fc90d865cf44
  • Loading branch information
shergin authored and facebook-github-bot committed May 14, 2018
1 parent 120dcec commit 1f9676a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 96 deletions.
62 changes: 18 additions & 44 deletions ReactCommon/fabric/core/propsConversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,66 +15,40 @@
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(); }

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
Expand Down
25 changes: 0 additions & 25 deletions ReactCommon/fabric/debug/debugStringConvertibleUtils.cpp

This file was deleted.

42 changes: 15 additions & 27 deletions ReactCommon/fabric/debug/debugStringConvertibleUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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

0 comments on commit 1f9676a

Please sign in to comment.