Fabric: Using templates to generate convertRawProp and debugStringConvertibleItem functions

Summary:
This diff opens a diffstack where we migrate the generation of all prop conversions (convertRawProp) and pretty-printing (debugStringConvertibleItem) functions to C++ templates (instead of using `#define`s).
So, this diff implements base versions of those functions as templated functions.
For now we still need #define-based version, but eventually, we will get rid of it.

Reviewed By: fkgozali

Differential Revision: D7958247

fbshipit-source-id: 24346297c1bd17e8054758f0eb84698eebfa21e2
This commit is contained in:
Valentin Shergin 2018-05-14 15:43:40 -07:00 committed by Facebook Github Bot
parent 2b827c9f13
commit 03fb77cc95
4 changed files with 29 additions and 2 deletions

View File

@ -39,7 +39,7 @@ public:
int maximumNumberOfLines {0};
/*
* In case if a text cannot fit given boudaures, defines a place where
* In case if a text cannot fit given boundaries, defines a place where
* an ellipsize should be placed.
*/
EllipsizeMode ellipsizeMode {EllipsizeMode::Clip};

View File

@ -52,5 +52,22 @@ CONVERT_RAW_PROP_TEMPLATE(SharedColor, colorFromDynamic)
CONVERT_RAW_PROP_TEMPLATE(Point, pointFromDynamic)
CONVERT_RAW_PROP_TEMPLATE(Size, sizeFromDynamic)
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, Float &result) { result = value.getDouble(); }
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 {
return defaultValue;
}
}
} // namespace react
} // namespace facebook

View File

@ -19,6 +19,15 @@
namespace facebook {
namespace react {
template <typename T>
inline SharedDebugStringConvertible debugStringConvertibleItem(std::string name, T value, T defaultValue = {}) {
if (value == defaultValue) {
return nullptr;
}
return std::make_shared<DebugStringConvertibleItem>(name, toString(value));
}
SharedDebugStringConvertibleList operator+(const SharedDebugStringConvertibleList &lhs, const SharedDebugStringConvertibleList &rhs);
SharedDebugStringConvertible debugStringConvertibleItem(std::string name, DebugStringConvertible value, std::string defaultValue = "");

View File

@ -8,6 +8,7 @@
#include "ParagraphLocalData.h"
#include <fabric/debug/debugStringConvertibleUtils.h>
#include <fabric/attributedstring/debugStringConvertibleUtils.h>
namespace facebook {
namespace react {
@ -38,7 +39,7 @@ std::string ParagraphLocalData::getDebugName() const {
SharedDebugStringConvertibleList ParagraphLocalData::getDebugProps() const {
return {
debugStringConvertibleItem("attributedString", attributedString_)
debugStringConvertibleItem("attributedString", attributedString_, "")
};
}