Fabric: Support for optionals in `convertRawProp` and `debugStringConvertibleItem` templates

Summary:
We have to have automatic treatment for `optional` types. So, if we can process type `T` we can also automatically process `optional<T>.`
Support for optional allows us to not introduce new types (with embedded special "undefined" value) or pollute existing pure types (with special "undefined" value). (A lot of examples of those types can be found in AttributedString module.)

Reviewed By: fkgozali

Differential Revision: D7958249

fbshipit-source-id: 21af526a17dd0329e1262020cab8ecb902316654
This commit is contained in:
Valentin Shergin 2018-05-14 15:43:42 -07:00 коммит произвёл Facebook Github Bot
Родитель 03fb77cc95
Коммит 7048c9134a
2 изменённых файлов: 25 добавлений и 0 удалений

Просмотреть файл

@ -69,5 +69,22 @@ inline T convertRawProp(const RawProps &rawProps, const std::string &name, const
}
}
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 {
return defaultValue;
}
}
} // namespace react
} // namespace facebook

Просмотреть файл

@ -28,6 +28,14 @@ inline SharedDebugStringConvertible debugStringConvertibleItem(std::string name,
return std::make_shared<DebugStringConvertibleItem>(name, toString(value));
}
template <typename T>
inline SharedDebugStringConvertible debugStringConvertibleItem(std::string name, folly::Optional<T> value, T defaultValue = {}) {
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 = "");