Skip to content

Commit

Permalink
Add RawValue support for arrays of arrays
Browse files Browse the repository at this point in the history
Summary: Adds support for dynamic value that are `std::vector<std::vector<RawValue>>`

Reviewed By: shergin, mdvacca

Differential Revision: D16936932

fbshipit-source-id: fc8d087f9705af9da56ccc1bd9a0537b2bfeb50d
  • Loading branch information
rickhanlonii authored and facebook-github-bot committed Sep 6, 2019
1 parent c232b5a commit 91d59eb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
13 changes: 13 additions & 0 deletions ReactCommon/fabric/core/primitives/RawValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,19 @@ class RawValue {
return result;
}

template <typename T>
static std::vector<std::vector<T>> castValue(
const folly::dynamic &dynamic,
std::vector<std::vector<T>> *type) noexcept {
assert(dynamic.isArray());
auto result = std::vector<std::vector<T>>{};
result.reserve(dynamic.size());
for (const auto &item : dynamic) {
result.push_back(castValue(item, (std::vector<T> *)nullptr));
}
return result;
}

template <typename T>
static better::map<std::string, T> castValue(
const folly::dynamic &dynamic,
Expand Down
25 changes: 25 additions & 0 deletions ReactCommon/fabric/core/propsConversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@ void fromRawValue(RawValue const &rawValue, std::vector<T> &result) {
result.push_back(itemResult);
}

template <typename T>
void fromRawValue(
RawValue const &rawValue,
std::vector<std::vector<T>> &result) {
if (rawValue.hasType<std::vector<std::vector<RawValue>>>()) {
auto items = (std::vector<std::vector<RawValue>>)rawValue;
auto length = items.size();
result.clear();
result.reserve(length);
for (int i = 0; i < length; i++) {
T itemResult;
fromRawValue(items.at(i), itemResult);
result.push_back(itemResult);
}
return;
}

// The case where `value` is not an array.
result.clear();
result.reserve(1);
T itemResult;
fromRawValue(rawValue, itemResult);
result.push_back(itemResult);
}

template <typename T, typename U = T>
T convertRawProp(
RawProps const &rawProps,
Expand Down

0 comments on commit 91d59eb

Please sign in to comment.