Skip to content

Commit 04ee52b

Browse files
Shawn Shihang Weifacebook-github-bot
authored andcommitted
Revert D38938632: Add key to prop conversion errors
Differential Revision: D38938632 (facebook@7909b91) Original commit changeset: 1dc9a544ca67 Original Phabricator Diff: D38938632 (facebook@7909b91) fbshipit-source-id: ddfe699ebf2ec7b118079d9ecb9ce77f2bf4a2ac
1 parent 82ceb8c commit 04ee52b

File tree

2 files changed

+49
-27
lines changed

2 files changed

+49
-27
lines changed

ReactCommon/react/renderer/core/RawValue.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class RawValue {
9696
* Casts the value to a specified type.
9797
*/
9898
template <typename T>
99-
explicit operator T() const {
99+
explicit operator T() const noexcept {
100100
return castValue(dynamic_, (T *)nullptr);
101101
}
102102

@@ -212,36 +212,40 @@ class RawValue {
212212
return RawValue(dynamic);
213213
}
214214

215-
static bool castValue(const folly::dynamic &dynamic, bool *type) {
215+
static bool castValue(const folly::dynamic &dynamic, bool *type) noexcept {
216216
return dynamic.getBool();
217217
}
218218

219-
static int castValue(const folly::dynamic &dynamic, int *type) {
219+
static int castValue(const folly::dynamic &dynamic, int *type) noexcept {
220220
return static_cast<int>(dynamic.asInt());
221221
}
222222

223-
static int64_t castValue(const folly::dynamic &dynamic, int64_t *type) {
223+
static int64_t castValue(
224+
const folly::dynamic &dynamic,
225+
int64_t *type) noexcept {
224226
return dynamic.asInt();
225227
}
226228

227-
static float castValue(const folly::dynamic &dynamic, float *type) {
229+
static float castValue(const folly::dynamic &dynamic, float *type) noexcept {
228230
return static_cast<float>(dynamic.asDouble());
229231
}
230232

231-
static double castValue(const folly::dynamic &dynamic, double *type) {
233+
static double castValue(
234+
const folly::dynamic &dynamic,
235+
double *type) noexcept {
232236
return dynamic.asDouble();
233237
}
234238

235239
static std::string castValue(
236240
const folly::dynamic &dynamic,
237-
std::string *type) {
241+
std::string *type) noexcept {
238242
return dynamic.getString();
239243
}
240244

241245
template <typename T>
242246
static std::vector<T> castValue(
243247
const folly::dynamic &dynamic,
244-
std::vector<T> *type) {
248+
std::vector<T> *type) noexcept {
245249
react_native_assert(dynamic.isArray());
246250
auto result = std::vector<T>{};
247251
result.reserve(dynamic.size());
@@ -254,7 +258,7 @@ class RawValue {
254258
template <typename T>
255259
static std::vector<std::vector<T>> castValue(
256260
const folly::dynamic &dynamic,
257-
std::vector<std::vector<T>> *type) {
261+
std::vector<std::vector<T>> *type) noexcept {
258262
react_native_assert(dynamic.isArray());
259263
auto result = std::vector<std::vector<T>>{};
260264
result.reserve(dynamic.size());
@@ -267,7 +271,7 @@ class RawValue {
267271
template <typename T>
268272
static butter::map<std::string, T> castValue(
269273
const folly::dynamic &dynamic,
270-
butter::map<std::string, T> *type) {
274+
butter::map<std::string, T> *type) noexcept {
271275
react_native_assert(dynamic.isObject());
272276
auto result = butter::map<std::string, T>{};
273277
for (const auto &item : dynamic.items()) {

ReactCommon/react/renderer/core/propsConversions.h

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
#include <optional>
1111

1212
#include <folly/Likely.h>
13+
#include <folly/dynamic.h>
1314
#include <react/renderer/core/PropsParserContext.h>
1415
#include <react/renderer/core/RawProps.h>
15-
#include <react/renderer/core/RawPropsKey.h>
1616
#include <react/renderer/graphics/Color.h>
1717
#include <react/renderer/graphics/Geometry.h>
1818
#include <react/renderer/graphics/conversions.h>
@@ -43,18 +43,18 @@ template <typename T>
4343
void fromRawValue(
4444
const PropsParserContext &context,
4545
RawValue const &rawValue,
46-
T &result) {
47-
result = (T)rawValue;
46+
std::optional<T> &result) {
47+
T res{};
48+
fromRawValue(context, rawValue, res);
49+
result = std::optional<T>(res);
4850
}
4951

5052
template <typename T>
5153
void fromRawValue(
5254
const PropsParserContext &context,
5355
RawValue const &rawValue,
54-
std::optional<T> &result) {
55-
T resultValue;
56-
fromRawValue(context, rawValue, resultValue);
57-
result = std::optional<T>{std::move(resultValue)};
56+
T &result) {
57+
result = (T)rawValue;
5858
}
5959

6060
template <typename T>
@@ -119,6 +119,7 @@ T convertRawProp(
119119
char const *namePrefix = nullptr,
120120
char const *nameSuffix = nullptr) {
121121
const auto *rawValue = rawProps.at(name, namePrefix, nameSuffix);
122+
122123
if (LIKELY(rawValue == nullptr)) {
123124
return sourceValue;
124125
}
@@ -129,18 +130,35 @@ T convertRawProp(
129130
return defaultValue;
130131
}
131132

132-
try {
133-
T result;
134-
fromRawValue(context, *rawValue, result);
135-
return result;
136-
} catch (const std::exception &e) {
137-
// In case of errors, log the error and fall back to the default
138-
RawPropsKey key{namePrefix, name, nameSuffix};
139-
// TODO: report this using ErrorUtils so it's more visible to the user
140-
LOG(ERROR) << "Error while converting prop '"
141-
<< static_cast<std::string>(key) << "': " << e.what();
133+
T result;
134+
fromRawValue(context, *rawValue, result);
135+
return result;
136+
}
137+
138+
template <typename T>
139+
static std::optional<T> convertRawProp(
140+
const PropsParserContext &context,
141+
RawProps const &rawProps,
142+
char const *name,
143+
std::optional<T> const &sourceValue,
144+
std::optional<T> const &defaultValue,
145+
char const *namePrefix = nullptr,
146+
char const *nameSuffix = nullptr) {
147+
const auto *rawValue = rawProps.at(name, namePrefix, nameSuffix);
148+
149+
if (LIKELY(rawValue == nullptr)) {
150+
return sourceValue;
151+
}
152+
153+
// Special case: `null` always means `the prop was removed, use default
154+
// value`.
155+
if (UNLIKELY(!rawValue->hasValue())) {
142156
return defaultValue;
143157
}
158+
159+
T result;
160+
fromRawValue(context, *rawValue, result);
161+
return std::optional<T>{result};
144162
}
145163

146164
} // namespace react

0 commit comments

Comments
 (0)