From 63343dad2eed6229db7cc864de63ceb863c516ec Mon Sep 17 00:00:00 2001 From: Kevin Gozali Date: Wed, 10 Apr 2019 11:00:50 -0700 Subject: [PATCH] Fabric: properly convert prop value with object shape Summary: For props that expects a struct/object value, like scrollView's contentInset, not all keys may be present. For example: ``` ``` In this example, `left`, `bottom`, and `right` should just default to 0 (or whatever the default is in the platform). Before this fix, an exception occured when calling `fromRawValue()` because it is assuming all 4 keys are present in the prop bag. However, only `top` key was present in this example. To fix this, we have to loop through the available keys in the prop bag, then assign the values accordingly. Reviewed By: JoshuaGross Differential Revision: D14868549 fbshipit-source-id: e25208eb31f6d4061338e9cac48a93fe71859859 --- ReactCommon/fabric/graphics/conversions.h | 43 +++++++++++++++++++---- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/ReactCommon/fabric/graphics/conversions.h b/ReactCommon/fabric/graphics/conversions.h index 2b8e645d7..91406607e 100644 --- a/ReactCommon/fabric/graphics/conversions.h +++ b/ReactCommon/fabric/graphics/conversions.h @@ -73,7 +73,13 @@ inline std::string toString(const SharedColor &value) { inline void fromRawValue(const RawValue &value, Point &result) { if (value.hasType>()) { auto map = (better::map)value; - result = {map.at("x"), map.at("y")}; + for (const auto &pair : map) { + if (pair.first == "x") { + result.x = pair.second; + } else if (pair.first == "y") { + result.y = pair.second; + } + } return; } @@ -90,7 +96,13 @@ inline void fromRawValue(const RawValue &value, Point &result) { inline void fromRawValue(const RawValue &value, Size &result) { if (value.hasType>()) { auto map = (better::map)value; - result = {map.at("width"), map.at("height")}; + for (const auto &pair : map) { + if (pair.first == "width") { + result.width = pair.second; + } else if (pair.first == "height") { + result.height = pair.second; + } + } return; } @@ -112,7 +124,17 @@ inline void fromRawValue(const RawValue &value, EdgeInsets &result) { if (value.hasType>()) { auto map = (better::map)value; - result = {map.at("top"), map.at("left"), map.at("bottom"), map.at("right")}; + for (const auto &pair : map) { + if (pair.first == "top") { + result.top = pair.second; + } else if (pair.first == "left") { + result.left = pair.second; + } else if (pair.first == "bottom") { + result.bottom = pair.second; + } else if (pair.first == "right") { + result.right = pair.second; + } + } return; } @@ -134,10 +156,17 @@ inline void fromRawValue(const RawValue &value, CornerInsets &result) { if (value.hasType>()) { auto map = (better::map)value; - result = {map.at("topLeft"), - map.at("topRight"), - map.at("bottomLeft"), - map.at("bottomRight")}; + for (const auto &pair : map) { + if (pair.first == "topLeft") { + result.topLeft = pair.second; + } else if (pair.first == "topRight") { + result.topRight = pair.second; + } else if (pair.first == "bottomLeft") { + result.bottomLeft = pair.second; + } else if (pair.first == "bottomRight") { + result.bottomRight = pair.second; + } + } return; }