Skip to content

Commit a164874

Browse files
Nick Lefeverfacebook-github-bot
authored andcommitted
Add codegen for EnumType diffing (#52241)
Summary: Pull Request resolved: #52241 Add support for converting string and int32 enum types to `folly::dynamic` and generating the correct property diffing for it conditionally adding the prop value to the prop diff result. This diff updates the template to convert the enum back to the original string representation provided from the JS side based on the current generated C++ enum value. The string enum re-uses the existing `toString` conversion. The number enum generates the switch-case mapping required to map back the C++ enum value to the original value assigned to it. Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D77234070 fbshipit-source-id: 8c669d5b2e21bd6022c6ba36149465495e4d4bf3
1 parent 0031377 commit a164874

8 files changed

Lines changed: 148 additions & 0 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,13 @@ folly::dynamic EnumPropNativeComponentViewProps::getDiffProps(
306306
}
307307
folly::dynamic result = HostPlatformViewProps::getDiffProps(prevProps);
308308
309+
if (alignment != oldProps->alignment) {
310+
result[\\"alignment\\"] = toDynamic(alignment);
311+
}
309312
313+
if (intervals != oldProps->intervals) {
314+
result[\\"intervals\\"] = toDynamic(intervals);
315+
}
310316
return result;
311317
}
312318
#endif

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,12 @@ static inline std::string toString(const EnumPropNativeComponentViewAlignment &v
360360
case EnumPropNativeComponentViewAlignment::BottomRight: return \\"bottom-right\\";
361361
}
362362
}
363+
364+
#ifdef RN_SERIALIZABLE_STATE
365+
static inline folly::dynamic toDynamic(const EnumPropNativeComponentViewAlignment &value) {
366+
return toString(value);
367+
}
368+
#endif
363369
enum class EnumPropNativeComponentViewIntervals { Intervals0 = 0, Intervals15 = 15, Intervals30 = 30, Intervals60 = 60 };
364370
365371
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, EnumPropNativeComponentViewIntervals &result) {
@@ -391,6 +397,17 @@ static inline std::string toString(const EnumPropNativeComponentViewIntervals &v
391397
}
392398
}
393399
400+
#ifdef RN_SERIALIZABLE_STATE
401+
static inline folly::dynamic toDynamic(const EnumPropNativeComponentViewIntervals &value) {
402+
switch (value) {
403+
case EnumPropNativeComponentViewIntervals::Intervals0: return 0;
404+
case EnumPropNativeComponentViewIntervals::Intervals15: return 15;
405+
case EnumPropNativeComponentViewIntervals::Intervals30: return 30;
406+
case EnumPropNativeComponentViewIntervals::Intervals60: return 60;
407+
}
408+
}
409+
#endif
410+
394411
class EnumPropNativeComponentViewProps final : public ViewProps {
395412
public:
396413
EnumPropNativeComponentViewProps() = default;
@@ -798,6 +815,12 @@ static inline std::string toString(const ObjectPropsNativeComponentStringEnumPro
798815
case ObjectPropsNativeComponentStringEnumProp::Large: return \\"large\\";
799816
}
800817
}
818+
819+
#ifdef RN_SERIALIZABLE_STATE
820+
static inline folly::dynamic toDynamic(const ObjectPropsNativeComponentStringEnumProp &value) {
821+
return toString(value);
822+
}
823+
#endif
801824
enum class ObjectPropsNativeComponentIntEnumProp { IntEnumProp0 = 0, IntEnumProp1 = 1 };
802825
803826
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsNativeComponentIntEnumProp &result) {
@@ -820,6 +843,15 @@ static inline std::string toString(const ObjectPropsNativeComponentIntEnumProp &
820843
case ObjectPropsNativeComponentIntEnumProp::IntEnumProp1: return \\"1\\";
821844
}
822845
}
846+
847+
#ifdef RN_SERIALIZABLE_STATE
848+
static inline folly::dynamic toDynamic(const ObjectPropsNativeComponentIntEnumProp &value) {
849+
switch (value) {
850+
case ObjectPropsNativeComponentIntEnumProp::IntEnumProp0: return 0;
851+
case ObjectPropsNativeComponentIntEnumProp::IntEnumProp1: return 1;
852+
}
853+
}
854+
#endif
823855
struct ObjectPropsNativeComponentObjectPropStruct {
824856
std::string stringProp{\\"\\"};
825857
bool booleanProp{false};

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,13 @@ folly::dynamic EnumPropNativeComponentViewProps::getDiffProps(
306306
}
307307
folly::dynamic result = HostPlatformViewProps::getDiffProps(prevProps);
308308
309+
if (alignment != oldProps->alignment) {
310+
result[\\"alignment\\"] = toDynamic(alignment);
311+
}
309312
313+
if (intervals != oldProps->intervals) {
314+
result[\\"intervals\\"] = toDynamic(intervals);
315+
}
310316
return result;
311317
}
312318
#endif

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,12 @@ static inline std::string toString(const EnumPropNativeComponentViewAlignment &v
360360
case EnumPropNativeComponentViewAlignment::BottomRight: return \\"bottom-right\\";
361361
}
362362
}
363+
364+
#ifdef RN_SERIALIZABLE_STATE
365+
static inline folly::dynamic toDynamic(const EnumPropNativeComponentViewAlignment &value) {
366+
return toString(value);
367+
}
368+
#endif
363369
enum class EnumPropNativeComponentViewIntervals { Intervals0 = 0, Intervals15 = 15, Intervals30 = 30, Intervals60 = 60 };
364370
365371
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, EnumPropNativeComponentViewIntervals &result) {
@@ -391,6 +397,17 @@ static inline std::string toString(const EnumPropNativeComponentViewIntervals &v
391397
}
392398
}
393399
400+
#ifdef RN_SERIALIZABLE_STATE
401+
static inline folly::dynamic toDynamic(const EnumPropNativeComponentViewIntervals &value) {
402+
switch (value) {
403+
case EnumPropNativeComponentViewIntervals::Intervals0: return 0;
404+
case EnumPropNativeComponentViewIntervals::Intervals15: return 15;
405+
case EnumPropNativeComponentViewIntervals::Intervals30: return 30;
406+
case EnumPropNativeComponentViewIntervals::Intervals60: return 60;
407+
}
408+
}
409+
#endif
410+
394411
class EnumPropNativeComponentViewProps final : public ViewProps {
395412
public:
396413
EnumPropNativeComponentViewProps() = default;
@@ -798,6 +815,12 @@ static inline std::string toString(const ObjectPropsNativeComponentStringEnumPro
798815
case ObjectPropsNativeComponentStringEnumProp::Large: return \\"large\\";
799816
}
800817
}
818+
819+
#ifdef RN_SERIALIZABLE_STATE
820+
static inline folly::dynamic toDynamic(const ObjectPropsNativeComponentStringEnumProp &value) {
821+
return toString(value);
822+
}
823+
#endif
801824
enum class ObjectPropsNativeComponentIntEnumProp { IntEnumProp0 = 0, IntEnumProp1 = 1 };
802825
803826
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsNativeComponentIntEnumProp &result) {
@@ -820,6 +843,15 @@ static inline std::string toString(const ObjectPropsNativeComponentIntEnumProp &
820843
case ObjectPropsNativeComponentIntEnumProp::IntEnumProp1: return \\"1\\";
821844
}
822845
}
846+
847+
#ifdef RN_SERIALIZABLE_STATE
848+
static inline folly::dynamic toDynamic(const ObjectPropsNativeComponentIntEnumProp &value) {
849+
switch (value) {
850+
case ObjectPropsNativeComponentIntEnumProp::IntEnumProp0: return 0;
851+
case ObjectPropsNativeComponentIntEnumProp::IntEnumProp1: return 1;
852+
}
853+
}
854+
#endif
823855
struct ObjectPropsNativeComponentObjectPropStruct {
824856
std::string stringProp{\\"\\"};
825857
bool booleanProp{false};

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,17 @@ function generatePropsDiffString(
131131
}
132132
case 'ArrayTypeAnnotation':
133133
case 'ObjectTypeAnnotation':
134+
return '';
134135
case 'StringEnumTypeAnnotation':
136+
return `
137+
if (${prop.name} != oldProps->${prop.name}) {
138+
result["${prop.name}"] = toDynamic(${prop.name});
139+
}`;
135140
case 'Int32EnumTypeAnnotation':
141+
return `
142+
if (${prop.name} != oldProps->${prop.name}) {
143+
result["${prop.name}"] = toDynamic(${prop.name});
144+
}`;
136145
case 'MixedTypeAnnotation':
137146
default:
138147
// TODO: Implement diffProps for complex types

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,26 @@ static inline std::string toString(const ${enumName} &value) {
115115
${toCases}
116116
}
117117
}
118+
119+
#ifdef RN_SERIALIZABLE_STATE
120+
static inline folly::dynamic toDynamic(const ${enumName} &value) {
121+
return toString(value);
122+
}
123+
#endif
118124
`.trim();
119125

120126
const IntEnumTemplate = ({
121127
enumName,
122128
values,
123129
fromCases,
124130
toCases,
131+
toDynamicCases,
125132
}: {
126133
enumName: string,
127134
values: string,
128135
fromCases: string,
129136
toCases: string,
137+
toDynamicCases: string,
130138
}) =>
131139
`
132140
enum class ${enumName} { ${values} };
@@ -144,6 +152,14 @@ static inline std::string toString(const ${enumName} &value) {
144152
${toCases}
145153
}
146154
}
155+
156+
#ifdef RN_SERIALIZABLE_STATE
157+
static inline folly::dynamic toDynamic(const ${enumName} &value) {
158+
switch (value) {
159+
${toDynamicCases}
160+
}
161+
}
162+
#endif
147163
`.trim();
148164

149165
const StructTemplate = ({
@@ -401,6 +417,16 @@ function generateIntEnum(
401417
)
402418
.join('\n' + ' ');
403419

420+
const toDynamicCases = values
421+
.map(
422+
value =>
423+
`case ${enumName}::${toIntEnumValueName(
424+
prop.name,
425+
value,
426+
)}: return ${value};`,
427+
)
428+
.join('\n' + ' ');
429+
404430
const valueVariables = values
405431
.map(val => `${toIntEnumValueName(prop.name, val)} = ${val}`)
406432
.join(', ');
@@ -410,6 +436,7 @@ function generateIntEnum(
410436
values: valueVariables,
411437
fromCases,
412438
toCases,
439+
toDynamicCases,
413440
});
414441
}
415442

packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsCpp-test.js.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,9 @@ folly::dynamic Int32EnumPropsNativeComponentProps::getDiffProps(
949949
}
950950
folly::dynamic result = HostPlatformViewProps::getDiffProps(prevProps);
951951
952+
if (maxInterval != oldProps->maxInterval) {
953+
result[\\"maxInterval\\"] = toDynamic(maxInterval);
954+
}
952955
return result;
953956
}
954957
#endif
@@ -1357,6 +1360,9 @@ folly::dynamic StringEnumPropsNativeComponentProps::getDiffProps(
13571360
}
13581361
folly::dynamic result = HostPlatformViewProps::getDiffProps(prevProps);
13591362
1363+
if (alignment != oldProps->alignment) {
1364+
result[\\"alignment\\"] = toDynamic(alignment);
1365+
}
13601366
return result;
13611367
}
13621368
#endif

packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsH-test.js.snap

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,16 @@ static inline std::string toString(const Int32EnumPropsNativeComponentMaxInterva
933933
}
934934
}
935935
936+
#ifdef RN_SERIALIZABLE_STATE
937+
static inline folly::dynamic toDynamic(const Int32EnumPropsNativeComponentMaxInterval &value) {
938+
switch (value) {
939+
case Int32EnumPropsNativeComponentMaxInterval::MaxInterval0: return 0;
940+
case Int32EnumPropsNativeComponentMaxInterval::MaxInterval1: return 1;
941+
case Int32EnumPropsNativeComponentMaxInterval::MaxInterval2: return 2;
942+
}
943+
}
944+
#endif
945+
936946
class Int32EnumPropsNativeComponentProps final : public ViewProps {
937947
public:
938948
Int32EnumPropsNativeComponentProps() = default;
@@ -1182,6 +1192,12 @@ static inline std::string toString(const ObjectPropsStringEnumProp &value) {
11821192
case ObjectPropsStringEnumProp::Option1: return \\"option1\\";
11831193
}
11841194
}
1195+
1196+
#ifdef RN_SERIALIZABLE_STATE
1197+
static inline folly::dynamic toDynamic(const ObjectPropsStringEnumProp &value) {
1198+
return toString(value);
1199+
}
1200+
#endif
11851201
enum class ObjectPropsIntEnumProp { IntEnumProp0 = 0 };
11861202
11871203
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsIntEnumProp &result) {
@@ -1200,6 +1216,14 @@ static inline std::string toString(const ObjectPropsIntEnumProp &value) {
12001216
case ObjectPropsIntEnumProp::IntEnumProp0: return \\"0\\";
12011217
}
12021218
}
1219+
1220+
#ifdef RN_SERIALIZABLE_STATE
1221+
static inline folly::dynamic toDynamic(const ObjectPropsIntEnumProp &value) {
1222+
switch (value) {
1223+
case ObjectPropsIntEnumProp::IntEnumProp0: return 0;
1224+
}
1225+
}
1226+
#endif
12031227
struct ObjectPropsObjectPropObjectArrayPropStruct {
12041228
std::vector<std::string> array{};
12051229
};
@@ -1496,6 +1520,12 @@ static inline std::string toString(const StringEnumPropsNativeComponentAlignment
14961520
}
14971521
}
14981522
1523+
#ifdef RN_SERIALIZABLE_STATE
1524+
static inline folly::dynamic toDynamic(const StringEnumPropsNativeComponentAlignment &value) {
1525+
return toString(value);
1526+
}
1527+
#endif
1528+
14991529
class StringEnumPropsNativeComponentProps final : public ViewProps {
15001530
public:
15011531
StringEnumPropsNativeComponentProps() = default;

0 commit comments

Comments
 (0)