Fabric: New, much fancier, approach to parse dynamic props

Summary:
The new approach uses C++ overloading feature instead of specifying exact types in macros manually.
*Almost* macro-free!

Reviewed By: mdvacca

Differential Revision: D7738584

fbshipit-source-id: 85f8e4c1037b452df5e73b093dced9392cb2f73e
This commit is contained in:
Valentin Shergin 2018-04-26 17:51:59 -07:00 committed by Facebook Github Bot
parent f8ab0e0b08
commit edc6cb5711
4 changed files with 64 additions and 39 deletions

View File

@ -7,6 +7,7 @@
#include "Props.h"
#include <fabric/core/propsConversions.h>
#include <folly/dynamic.h>
namespace facebook {
@ -15,14 +16,7 @@ namespace react {
void Props::apply(const RawProps &rawProps) {
ensureUnsealed();
for (auto const &pair : rawProps) {
auto const &name = pair.first;
auto const &value = pair.second;
if (name == "nativeID") {
nativeId_ = value.asString();
}
}
applyRawProp(rawProps, "nativeID", nativeId_);
}
const std::string &Props::getNativeId() const {

View File

@ -0,0 +1,52 @@
/**
* 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
#include <folly/dynamic.h>
#include <fabric/graphics/Color.h>
#include <fabric/graphics/Geometry.h>
#include <fabric/graphics/graphicValuesConversions.h>
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 APPLY_RAW_PROP_TEMPLATE(type, converter) \
inline static void applyRawProp(const RawProps &rawProps, const std::string &name, type &property) { \
auto &&iterator = rawProps.find(name); \
if (iterator != rawProps.end()) { \
property = converter(iterator->second); \
} \
} \
\
inline static void applyRawProp(const RawProps &rawProps, const std::string &name, folly::Optional<type> &property) { \
auto &&iterator = rawProps.find(name); \
if (iterator != rawProps.end()) { \
auto &&value = iterator->second; \
if (value.isNull()) { \
property = {}; \
} else { \
property = converter(value); \
} \
} \
}
APPLY_RAW_PROP_TEMPLATE(bool, boolFromDynamic)
APPLY_RAW_PROP_TEMPLATE(int, intFromDynamic)
APPLY_RAW_PROP_TEMPLATE(Float, floatFromDynamic)
APPLY_RAW_PROP_TEMPLATE(std::string, stringFromDynamic)
APPLY_RAW_PROP_TEMPLATE(SharedColor, colorFromDynamic)
APPLY_RAW_PROP_TEMPLATE(Point, pointFromDynamic)
APPLY_RAW_PROP_TEMPLATE(Size, sizeFromDynamic)
} // namespace react
} // namespace facebook

View File

@ -9,6 +9,7 @@
#include <fabric/debug/DebugStringConvertibleItem.h>
#include <fabric/graphics/graphicValuesConversions.h>
#include <fabric/core/propsConversions.h>
namespace facebook {
namespace react {
@ -17,32 +18,10 @@ void ViewProps::apply(const RawProps &rawProps) {
Props::apply(rawProps);
YogaStylableProps::apply(rawProps);
for (auto const &pair : rawProps) {
auto const &name = pair.first;
auto const &value = pair.second;
#pragma mark View Specific Properties
if (name == "zIndex") {
zIndex_ = value.asInt();
continue;
}
if (name == "opacity") {
opacity_ = value.asDouble();
continue;
}
if (name == "color") {
foregroundColor_ = colorFromDynamic(value);
continue;
}
if (name == "backgroundColor") {
backgroundColor_ = colorFromDynamic(value);
continue;
}
}
applyRawProp(rawProps, "zIndex", zIndex_);
applyRawProp(rawProps, "opacity", opacity_);
applyRawProp(rawProps, "color", foregroundColor_);
applyRawProp(rawProps, "backgroundColor", backgroundColor_);
}
#pragma mark - Getters

View File

@ -33,19 +33,19 @@ public:
SharedColor getForegroundColor() const;
SharedColor getBackgroundColor() const;
#pragma mark - DebugStringConvertible
SharedDebugStringConvertibleList getDebugProps() const override;
private:
int zIndex_ {0};
float opacity_ {1.0};
Float opacity_ {1.0};
SharedColor foregroundColor_ {nullptr};
SharedColor backgroundColor_ {nullptr};
SharedColor shadowColor_ {nullptr};
Point shadowOffset_ {0, 0};
#pragma mark - DebugStringConvertible
SharedDebugStringConvertibleList getDebugProps() const override;
};
} // namespace react