Skip to content

Commit b50ad49

Browse files
Nick Lefeverfacebook-github-bot
authored andcommitted
Add codegen for ObjectType diffing (#52243)
Summary: Pull Request resolved: #52243 Building on the availability of `toDynamic` conversion methods for all supported property types, this diff adds support for diffing of `ObjectType` props. The template adds the generation of a default comparator for the generated C++ struct. The struct also gains a `toDynamic` conversion method that will convert each property of the object type to a `folly::dynamic` value. Primitive types make use of the implicit conversion supported by `folly::dynamic`, all other types are converted using `toDynamic`. The `toDynamic` logic is implemented as a method defined on the struct to avoid increased binary size when required multiple times by the prop diffing implementation. The external `toDynamic` conversion function calls the struct method directly. This enables support for converting object types using object types within their props. Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D77234064 fbshipit-source-id: 21deb3104303aa374fb65b969af57a6aca6db38c
1 parent 8c806ec commit b50ad49

8 files changed

Lines changed: 451 additions & 1 deletion

File tree

packages/react-native-codegen/e2e/deep_imports/__tests__/components/__snapshots__/GeneratePropsCpp-test.js.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,8 +858,17 @@ folly::dynamic ObjectPropsNativeComponentProps::getDiffProps(
858858
}
859859
folly::dynamic result = HostPlatformViewProps::getDiffProps(prevProps);
860860
861+
if (objectProp != oldProps->objectProp) {
862+
result[\\"objectProp\\"] = toDynamic(objectProp);
863+
}
861864
865+
if (objectArrayProp != oldProps->objectArrayProp) {
866+
result[\\"objectArrayProp\\"] = toDynamic(objectArrayProp);
867+
}
862868
869+
if (objectPrimitiveRequiredProp != oldProps->objectPrimitiveRequiredProp) {
870+
result[\\"objectPrimitiveRequiredProp\\"] = toDynamic(objectPrimitiveRequiredProp);
871+
}
863872
return result;
864873
}
865874
#endif

packages/react-native-codegen/e2e/deep_imports/__tests__/components/__snapshots__/GeneratePropsH-test.js.snap

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ static inline std::string toString(const ArrayPropsNativeComponentViewSizesMaskW
8888
}
8989
struct ArrayPropsNativeComponentViewObjectStruct {
9090
std::string prop{};
91+
92+
#ifdef RN_SERIALIZABLE_STATE
93+
bool operator==(const ArrayPropsNativeComponentViewObjectStruct&) const = default;
94+
95+
folly::dynamic toDynamic() const {
96+
folly::dynamic result = folly::dynamic::object();
97+
result[\\"prop\\"] = prop;
98+
return result;
99+
}
100+
#endif
91101
};
92102
93103
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ArrayPropsNativeComponentViewObjectStruct &result) {
@@ -103,6 +113,12 @@ static inline std::string toString(const ArrayPropsNativeComponentViewObjectStru
103113
return \\"[Object ArrayPropsNativeComponentViewObjectStruct]\\";
104114
}
105115
116+
#ifdef RN_SERIALIZABLE_STATE
117+
static inline folly::dynamic toDynamic(const ArrayPropsNativeComponentViewObjectStruct &value) {
118+
return value.toDynamic();
119+
}
120+
#endif
121+
106122
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, std::vector<ArrayPropsNativeComponentViewObjectStruct> &result) {
107123
auto items = (std::vector<RawValue>)value;
108124
for (const auto &item : items) {
@@ -116,6 +132,17 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
116132
struct ArrayPropsNativeComponentViewArrayOfObjectsStruct {
117133
Float prop1{0.0};
118134
int prop2{0};
135+
136+
#ifdef RN_SERIALIZABLE_STATE
137+
bool operator==(const ArrayPropsNativeComponentViewArrayOfObjectsStruct&) const = default;
138+
139+
folly::dynamic toDynamic() const {
140+
folly::dynamic result = folly::dynamic::object();
141+
result[\\"prop1\\"] = prop1;
142+
result[\\"prop2\\"] = prop2;
143+
return result;
144+
}
145+
#endif
119146
};
120147
121148
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ArrayPropsNativeComponentViewArrayOfObjectsStruct &result) {
@@ -135,6 +162,12 @@ static inline std::string toString(const ArrayPropsNativeComponentViewArrayOfObj
135162
return \\"[Object ArrayPropsNativeComponentViewArrayOfObjectsStruct]\\";
136163
}
137164
165+
#ifdef RN_SERIALIZABLE_STATE
166+
static inline folly::dynamic toDynamic(const ArrayPropsNativeComponentViewArrayOfObjectsStruct &value) {
167+
return value.toDynamic();
168+
}
169+
#endif
170+
138171
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, std::vector<ArrayPropsNativeComponentViewArrayOfObjectsStruct> &result) {
139172
auto items = (std::vector<RawValue>)value;
140173
for (const auto &item : items) {
@@ -861,6 +894,21 @@ struct ObjectPropsNativeComponentObjectPropStruct {
861894
int intProp{0};
862895
ObjectPropsNativeComponentStringEnumProp stringEnumProp{ObjectPropsNativeComponentStringEnumProp::Small};
863896
ObjectPropsNativeComponentIntEnumProp intEnumProp{ObjectPropsNativeComponentIntEnumProp::IntEnumProp0};
897+
898+
#ifdef RN_SERIALIZABLE_STATE
899+
bool operator==(const ObjectPropsNativeComponentObjectPropStruct&) const = default;
900+
901+
folly::dynamic toDynamic() const {
902+
folly::dynamic result = folly::dynamic::object();
903+
result[\\"stringProp\\"] = stringProp;
904+
result[\\"booleanProp\\"] = booleanProp;
905+
result[\\"floatProp\\"] = floatProp;
906+
result[\\"intProp\\"] = intProp;
907+
result[\\"stringEnumProp\\"] = ::facebook::react::toDynamic(stringEnumProp);
908+
result[\\"intEnumProp\\"] = ::facebook::react::toDynamic(intEnumProp);
909+
return result;
910+
}
911+
#endif
864912
};
865913
866914
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsNativeComponentObjectPropStruct &result) {
@@ -896,8 +944,24 @@ static inline std::string toString(const ObjectPropsNativeComponentObjectPropStr
896944
return \\"[Object ObjectPropsNativeComponentObjectPropStruct]\\";
897945
}
898946
947+
#ifdef RN_SERIALIZABLE_STATE
948+
static inline folly::dynamic toDynamic(const ObjectPropsNativeComponentObjectPropStruct &value) {
949+
return value.toDynamic();
950+
}
951+
#endif
952+
899953
struct ObjectPropsNativeComponentObjectArrayPropStruct {
900954
std::vector<std::string> array{};
955+
956+
#ifdef RN_SERIALIZABLE_STATE
957+
bool operator==(const ObjectPropsNativeComponentObjectArrayPropStruct&) const = default;
958+
959+
folly::dynamic toDynamic() const {
960+
folly::dynamic result = folly::dynamic::object();
961+
962+
return result;
963+
}
964+
#endif
901965
};
902966
903967
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsNativeComponentObjectArrayPropStruct &result) {
@@ -913,10 +977,28 @@ static inline std::string toString(const ObjectPropsNativeComponentObjectArrayPr
913977
return \\"[Object ObjectPropsNativeComponentObjectArrayPropStruct]\\";
914978
}
915979
980+
#ifdef RN_SERIALIZABLE_STATE
981+
static inline folly::dynamic toDynamic(const ObjectPropsNativeComponentObjectArrayPropStruct &value) {
982+
return value.toDynamic();
983+
}
984+
#endif
985+
916986
struct ObjectPropsNativeComponentObjectPrimitiveRequiredPropStruct {
917987
ImageSource image{};
918988
SharedColor color{};
919989
Point point{};
990+
991+
#ifdef RN_SERIALIZABLE_STATE
992+
bool operator==(const ObjectPropsNativeComponentObjectPrimitiveRequiredPropStruct&) const = default;
993+
994+
folly::dynamic toDynamic() const {
995+
folly::dynamic result = folly::dynamic::object();
996+
result[\\"image\\"] = ::facebook::react::toDynamic(image);
997+
result[\\"color\\"] = ::facebook::react::toDynamic(color);
998+
result[\\"point\\"] = ::facebook::react::toDynamic(point);
999+
return result;
1000+
}
1001+
#endif
9201002
};
9211003
9221004
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsNativeComponentObjectPrimitiveRequiredPropStruct &result) {
@@ -939,6 +1021,12 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
9391021
static inline std::string toString(const ObjectPropsNativeComponentObjectPrimitiveRequiredPropStruct &value) {
9401022
return \\"[Object ObjectPropsNativeComponentObjectPrimitiveRequiredPropStruct]\\";
9411023
}
1024+
1025+
#ifdef RN_SERIALIZABLE_STATE
1026+
static inline folly::dynamic toDynamic(const ObjectPropsNativeComponentObjectPrimitiveRequiredPropStruct &value) {
1027+
return value.toDynamic();
1028+
}
1029+
#endif
9421030
class ObjectPropsNativeComponentProps final : public ViewProps {
9431031
public:
9441032
ObjectPropsNativeComponentProps() = default;

packages/react-native-codegen/e2e/namespaced/__tests__/components/__snapshots__/GeneratePropsCpp-test.js.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,8 +858,17 @@ folly::dynamic ObjectPropsNativeComponentProps::getDiffProps(
858858
}
859859
folly::dynamic result = HostPlatformViewProps::getDiffProps(prevProps);
860860
861+
if (objectProp != oldProps->objectProp) {
862+
result[\\"objectProp\\"] = toDynamic(objectProp);
863+
}
861864
865+
if (objectArrayProp != oldProps->objectArrayProp) {
866+
result[\\"objectArrayProp\\"] = toDynamic(objectArrayProp);
867+
}
862868
869+
if (objectPrimitiveRequiredProp != oldProps->objectPrimitiveRequiredProp) {
870+
result[\\"objectPrimitiveRequiredProp\\"] = toDynamic(objectPrimitiveRequiredProp);
871+
}
863872
return result;
864873
}
865874
#endif

packages/react-native-codegen/e2e/namespaced/__tests__/components/__snapshots__/GeneratePropsH-test.js.snap

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ static inline std::string toString(const ArrayPropsNativeComponentViewSizesMaskW
8888
}
8989
struct ArrayPropsNativeComponentViewObjectStruct {
9090
std::string prop{};
91+
92+
#ifdef RN_SERIALIZABLE_STATE
93+
bool operator==(const ArrayPropsNativeComponentViewObjectStruct&) const = default;
94+
95+
folly::dynamic toDynamic() const {
96+
folly::dynamic result = folly::dynamic::object();
97+
result[\\"prop\\"] = prop;
98+
return result;
99+
}
100+
#endif
91101
};
92102
93103
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ArrayPropsNativeComponentViewObjectStruct &result) {
@@ -103,6 +113,12 @@ static inline std::string toString(const ArrayPropsNativeComponentViewObjectStru
103113
return \\"[Object ArrayPropsNativeComponentViewObjectStruct]\\";
104114
}
105115
116+
#ifdef RN_SERIALIZABLE_STATE
117+
static inline folly::dynamic toDynamic(const ArrayPropsNativeComponentViewObjectStruct &value) {
118+
return value.toDynamic();
119+
}
120+
#endif
121+
106122
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, std::vector<ArrayPropsNativeComponentViewObjectStruct> &result) {
107123
auto items = (std::vector<RawValue>)value;
108124
for (const auto &item : items) {
@@ -116,6 +132,17 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
116132
struct ArrayPropsNativeComponentViewArrayOfObjectsStruct {
117133
Float prop1{0.0};
118134
int prop2{0};
135+
136+
#ifdef RN_SERIALIZABLE_STATE
137+
bool operator==(const ArrayPropsNativeComponentViewArrayOfObjectsStruct&) const = default;
138+
139+
folly::dynamic toDynamic() const {
140+
folly::dynamic result = folly::dynamic::object();
141+
result[\\"prop1\\"] = prop1;
142+
result[\\"prop2\\"] = prop2;
143+
return result;
144+
}
145+
#endif
119146
};
120147
121148
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ArrayPropsNativeComponentViewArrayOfObjectsStruct &result) {
@@ -135,6 +162,12 @@ static inline std::string toString(const ArrayPropsNativeComponentViewArrayOfObj
135162
return \\"[Object ArrayPropsNativeComponentViewArrayOfObjectsStruct]\\";
136163
}
137164
165+
#ifdef RN_SERIALIZABLE_STATE
166+
static inline folly::dynamic toDynamic(const ArrayPropsNativeComponentViewArrayOfObjectsStruct &value) {
167+
return value.toDynamic();
168+
}
169+
#endif
170+
138171
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, std::vector<ArrayPropsNativeComponentViewArrayOfObjectsStruct> &result) {
139172
auto items = (std::vector<RawValue>)value;
140173
for (const auto &item : items) {
@@ -861,6 +894,21 @@ struct ObjectPropsNativeComponentObjectPropStruct {
861894
int intProp{0};
862895
ObjectPropsNativeComponentStringEnumProp stringEnumProp{ObjectPropsNativeComponentStringEnumProp::Small};
863896
ObjectPropsNativeComponentIntEnumProp intEnumProp{ObjectPropsNativeComponentIntEnumProp::IntEnumProp0};
897+
898+
#ifdef RN_SERIALIZABLE_STATE
899+
bool operator==(const ObjectPropsNativeComponentObjectPropStruct&) const = default;
900+
901+
folly::dynamic toDynamic() const {
902+
folly::dynamic result = folly::dynamic::object();
903+
result[\\"stringProp\\"] = stringProp;
904+
result[\\"booleanProp\\"] = booleanProp;
905+
result[\\"floatProp\\"] = floatProp;
906+
result[\\"intProp\\"] = intProp;
907+
result[\\"stringEnumProp\\"] = ::facebook::react::toDynamic(stringEnumProp);
908+
result[\\"intEnumProp\\"] = ::facebook::react::toDynamic(intEnumProp);
909+
return result;
910+
}
911+
#endif
864912
};
865913
866914
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsNativeComponentObjectPropStruct &result) {
@@ -896,8 +944,24 @@ static inline std::string toString(const ObjectPropsNativeComponentObjectPropStr
896944
return \\"[Object ObjectPropsNativeComponentObjectPropStruct]\\";
897945
}
898946
947+
#ifdef RN_SERIALIZABLE_STATE
948+
static inline folly::dynamic toDynamic(const ObjectPropsNativeComponentObjectPropStruct &value) {
949+
return value.toDynamic();
950+
}
951+
#endif
952+
899953
struct ObjectPropsNativeComponentObjectArrayPropStruct {
900954
std::vector<std::string> array{};
955+
956+
#ifdef RN_SERIALIZABLE_STATE
957+
bool operator==(const ObjectPropsNativeComponentObjectArrayPropStruct&) const = default;
958+
959+
folly::dynamic toDynamic() const {
960+
folly::dynamic result = folly::dynamic::object();
961+
962+
return result;
963+
}
964+
#endif
901965
};
902966
903967
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsNativeComponentObjectArrayPropStruct &result) {
@@ -913,10 +977,28 @@ static inline std::string toString(const ObjectPropsNativeComponentObjectArrayPr
913977
return \\"[Object ObjectPropsNativeComponentObjectArrayPropStruct]\\";
914978
}
915979
980+
#ifdef RN_SERIALIZABLE_STATE
981+
static inline folly::dynamic toDynamic(const ObjectPropsNativeComponentObjectArrayPropStruct &value) {
982+
return value.toDynamic();
983+
}
984+
#endif
985+
916986
struct ObjectPropsNativeComponentObjectPrimitiveRequiredPropStruct {
917987
ImageSource image{};
918988
SharedColor color{};
919989
Point point{};
990+
991+
#ifdef RN_SERIALIZABLE_STATE
992+
bool operator==(const ObjectPropsNativeComponentObjectPrimitiveRequiredPropStruct&) const = default;
993+
994+
folly::dynamic toDynamic() const {
995+
folly::dynamic result = folly::dynamic::object();
996+
result[\\"image\\"] = ::facebook::react::toDynamic(image);
997+
result[\\"color\\"] = ::facebook::react::toDynamic(color);
998+
result[\\"point\\"] = ::facebook::react::toDynamic(point);
999+
return result;
1000+
}
1001+
#endif
9201002
};
9211003
9221004
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsNativeComponentObjectPrimitiveRequiredPropStruct &result) {
@@ -939,6 +1021,12 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
9391021
static inline std::string toString(const ObjectPropsNativeComponentObjectPrimitiveRequiredPropStruct &value) {
9401022
return \\"[Object ObjectPropsNativeComponentObjectPrimitiveRequiredPropStruct]\\";
9411023
}
1024+
1025+
#ifdef RN_SERIALIZABLE_STATE
1026+
static inline folly::dynamic toDynamic(const ObjectPropsNativeComponentObjectPrimitiveRequiredPropStruct &value) {
1027+
return value.toDynamic();
1028+
}
1029+
#endif
9421030
class ObjectPropsNativeComponentProps final : public ViewProps {
9431031
public:
9441032
ObjectPropsNativeComponentProps() = default;

packages/react-native-codegen/src/generators/components/GeneratePropsCpp.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,12 @@ function generatePropsDiffString(
132132
throw new Error('Received unknown ReservedPropTypeAnnotation');
133133
}
134134
case 'ArrayTypeAnnotation':
135-
case 'ObjectTypeAnnotation':
136135
return '';
136+
case 'ObjectTypeAnnotation':
137+
return `
138+
if (${prop.name} != oldProps->${prop.name}) {
139+
result["${prop.name}"] = toDynamic(${prop.name});
140+
}`;
137141
case 'StringEnumTypeAnnotation':
138142
return `
139143
if (${prop.name} != oldProps->${prop.name}) {

0 commit comments

Comments
 (0)