2018-04-27 00:51:59 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-05-16 20:35:33 +00:00
|
|
|
#include <folly/Optional.h>
|
2018-04-27 00:51:59 +00:00
|
|
|
#include <folly/dynamic.h>
|
|
|
|
#include <fabric/graphics/Color.h>
|
|
|
|
#include <fabric/graphics/Geometry.h>
|
2018-05-14 22:43:48 +00:00
|
|
|
#include <fabric/graphics/conversions.h>
|
2018-04-27 00:51:59 +00:00
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
2018-05-14 22:43:40 +00:00
|
|
|
inline void fromDynamic(const folly::dynamic &value, bool &result) { result = value.getBool(); }
|
2018-06-12 02:47:26 +00:00
|
|
|
inline void fromDynamic(const folly::dynamic &value, int &result) {
|
|
|
|
// All numbers from JS are treated as double, and JS cannot represent int64 in practice.
|
|
|
|
// So this always converts the value to int64 instead.
|
|
|
|
result = value.asInt();
|
|
|
|
}
|
2018-05-14 22:43:40 +00:00
|
|
|
inline void fromDynamic(const folly::dynamic &value, std::string &result) { result = value.getString(); }
|
|
|
|
|
2018-06-18 04:36:02 +00:00
|
|
|
template <typename T>
|
|
|
|
inline void fromDynamic(const folly::dynamic &value, std::vector<T> &result) {
|
|
|
|
result.clear();
|
|
|
|
T itemResult;
|
|
|
|
for (auto &itemValue : value) {
|
|
|
|
fromDynamic(itemValue, itemResult);
|
|
|
|
result.push_back(itemResult);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 22:43:40 +00:00
|
|
|
template <typename T>
|
2018-06-09 03:16:22 +00:00
|
|
|
inline T convertRawProp(
|
|
|
|
const RawProps &rawProps,
|
|
|
|
const std::string &name,
|
|
|
|
const T &sourceValue,
|
|
|
|
const T &defaultValue = T()
|
|
|
|
) {
|
2018-05-14 22:43:40 +00:00
|
|
|
auto &&iterator = rawProps.find(name);
|
2018-05-14 22:43:57 +00:00
|
|
|
if (iterator == rawProps.end()) {
|
2018-06-09 03:16:22 +00:00
|
|
|
return sourceValue;
|
2018-05-14 22:43:40 +00:00
|
|
|
}
|
2018-05-14 22:43:57 +00:00
|
|
|
|
|
|
|
auto &&value = iterator->second;
|
2018-06-09 03:16:22 +00:00
|
|
|
|
|
|
|
// Special case: `null` always means `the prop was removed, use default value`.
|
|
|
|
if (value.isNull()) {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
2018-05-14 22:43:57 +00:00
|
|
|
T result;
|
|
|
|
fromDynamic(value, result);
|
|
|
|
return result;
|
2018-05-14 22:43:40 +00:00
|
|
|
}
|
|
|
|
|
2018-05-14 22:43:42 +00:00
|
|
|
template <typename T>
|
2018-06-09 03:16:22 +00:00
|
|
|
inline static folly::Optional<T> convertRawProp(
|
|
|
|
const RawProps &rawProps,
|
|
|
|
const std::string &name,
|
|
|
|
const folly::Optional<T> &sourceValue,
|
|
|
|
const folly::Optional<T> &defaultValue = {}
|
|
|
|
) {
|
2018-05-14 22:43:42 +00:00
|
|
|
auto &&iterator = rawProps.find(name);
|
2018-05-14 22:43:57 +00:00
|
|
|
if (iterator == rawProps.end()) {
|
2018-06-09 03:16:22 +00:00
|
|
|
return sourceValue;
|
2018-05-14 22:43:42 +00:00
|
|
|
}
|
2018-05-14 22:43:57 +00:00
|
|
|
|
|
|
|
auto &&value = iterator->second;
|
|
|
|
|
2018-06-09 03:16:22 +00:00
|
|
|
// Special case: `null` always means `the prop was removed, use default value`.
|
2018-05-14 22:43:57 +00:00
|
|
|
if (value.isNull()) {
|
2018-06-09 03:16:22 +00:00
|
|
|
return defaultValue;
|
2018-05-14 22:43:57 +00:00
|
|
|
}
|
|
|
|
|
2018-06-09 03:16:22 +00:00
|
|
|
T result;
|
2018-05-14 22:43:57 +00:00
|
|
|
fromDynamic(value, result);
|
|
|
|
return result;
|
2018-05-14 22:43:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-27 00:51:59 +00:00
|
|
|
} // namespace react
|
|
|
|
} // namespace facebook
|