From 92fc32aa053ac8401ad8c9f55dcfa1e48ae8fc1d Mon Sep 17 00:00:00 2001 From: Ruslan Shestopalyuk Date: Fri, 27 Jan 2023 05:19:17 -0800 Subject: [PATCH] Add missing C++ include for prop conversion of complex array type (#35984) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/35984 [Changelog][Internal] Codegen for props parsing was failing to add a required include for the case when the type is an array of objects, which in turn use non-trivial types. Something like: ``` export type NativeProps = $ReadOnly<{ ...ViewProps, bounds: $ReadOnlyArray< $ReadOnly<{ height?: Float, left?: Float, top?: Float, width?: Float, }>, >, }>; ``` would cause compilation errors on C++ side, since the required header for the `Float` conversion wasn't included. Reviewed By: cipolleschi Differential Revision: D42781128 fbshipit-source-id: d5b133b931a60e414761db0b3ed09893d3fcc9aa --- .../components/ArrayPropsNativeComponent.js | 1 + .../GeneratePropsCpp-test.js.snap | 3 +- .../__snapshots__/GeneratePropsH-test.js.snap | 34 +++++++++++++++++++ .../GeneratePropsJavaDelegate-test.js.snap | 3 ++ .../GeneratePropsJavaInterface-test.js.snap | 1 + .../GenerateViewConfigJs-test.js.snap | 1 + .../components/ComponentsGeneratorUtils.js | 3 ++ .../__snapshots__/GeneratePropsH-test.js.snap | 2 ++ 8 files changed, 47 insertions(+), 1 deletion(-) diff --git a/packages/react-native-codegen/e2e/__test_fixtures__/components/ArrayPropsNativeComponent.js b/packages/react-native-codegen/e2e/__test_fixtures__/components/ArrayPropsNativeComponent.js index fc24aadc4aeb29..b65f3ce50b3914 100644 --- a/packages/react-native-codegen/e2e/__test_fixtures__/components/ArrayPropsNativeComponent.js +++ b/packages/react-native-codegen/e2e/__test_fixtures__/components/ArrayPropsNativeComponent.js @@ -35,6 +35,7 @@ type NativeProps = $ReadOnly<{| // edgeInsets?: $ReadOnlyArray, sizes?: WithDefault<$ReadOnlyArray<'small' | 'large'>, 'small'>, object?: $ReadOnlyArray<$ReadOnly<{|prop: string|}>>, + arrayOfObjects?: $ReadOnlyArray<$ReadOnly<{|prop1: Float, prop2: Int32|}>>, |}>; export default (codegenNativeComponent( diff --git a/packages/react-native-codegen/e2e/__tests__/components/__snapshots__/GeneratePropsCpp-test.js.snap b/packages/react-native-codegen/e2e/__tests__/components/__snapshots__/GeneratePropsCpp-test.js.snap index 63443148619e08..6de527c44d3460 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/__snapshots__/GeneratePropsCpp-test.js.snap +++ b/packages/react-native-codegen/e2e/__tests__/components/__snapshots__/GeneratePropsCpp-test.js.snap @@ -33,7 +33,8 @@ ArrayPropsNativeComponentViewProps::ArrayPropsNativeComponentViewProps( srcs(convertRawProp(context, rawProps, \\"srcs\\", sourceProps.srcs, {})), points(convertRawProp(context, rawProps, \\"points\\", sourceProps.points, {})), sizes(convertRawProp(context, rawProps, \\"sizes\\", sourceProps.sizes, {static_cast(ArrayPropsNativeComponentViewSizes::Small)})), - object(convertRawProp(context, rawProps, \\"object\\", sourceProps.object, {})) + object(convertRawProp(context, rawProps, \\"object\\", sourceProps.object, {})), + arrayOfObjects(convertRawProp(context, rawProps, \\"arrayOfObjects\\", sourceProps.arrayOfObjects, {})) {} } // namespace react diff --git a/packages/react-native-codegen/e2e/__tests__/components/__snapshots__/GeneratePropsH-test.js.snap b/packages/react-native-codegen/e2e/__tests__/components/__snapshots__/GeneratePropsH-test.js.snap index ba79b2cf4149fb..91c0ca90fd83f6 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/__snapshots__/GeneratePropsH-test.js.snap +++ b/packages/react-native-codegen/e2e/__tests__/components/__snapshots__/GeneratePropsH-test.js.snap @@ -17,6 +17,7 @@ Object { #include #include #include +#include #include #include #include @@ -106,6 +107,38 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu } } + +struct ArrayPropsNativeComponentViewArrayOfObjectsStruct { + Float prop1; + int prop2; +}; + +static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ArrayPropsNativeComponentViewArrayOfObjectsStruct &result) { + auto map = (butter::map)value; + + auto tmp_prop1 = map.find(\\"prop1\\"); + if (tmp_prop1 != map.end()) { + fromRawValue(context, tmp_prop1->second, result.prop1); + } + auto tmp_prop2 = map.find(\\"prop2\\"); + if (tmp_prop2 != map.end()) { + fromRawValue(context, tmp_prop2->second, result.prop2); + } +} + +static inline std::string toString(const ArrayPropsNativeComponentViewArrayOfObjectsStruct &value) { + return \\"[Object ArrayPropsNativeComponentViewArrayOfObjectsStruct]\\"; +} + +static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, std::vector &result) { + auto items = (std::vector)value; + for (const auto &item : items) { + ArrayPropsNativeComponentViewArrayOfObjectsStruct newItem; + fromRawValue(context, item, newItem); + result.emplace_back(newItem); + } +} + class JSI_EXPORT ArrayPropsNativeComponentViewProps final : public ViewProps { public: ArrayPropsNativeComponentViewProps() = default; @@ -122,6 +155,7 @@ class JSI_EXPORT ArrayPropsNativeComponentViewProps final : public ViewProps { std::vector points{}; ArrayPropsNativeComponentViewSizesMask sizes{static_cast(ArrayPropsNativeComponentViewSizes::Small)}; std::vector object{}; + std::vector arrayOfObjects{}; }; } // namespace react diff --git a/packages/react-native-codegen/e2e/__tests__/components/__snapshots__/GeneratePropsJavaDelegate-test.js.snap b/packages/react-native-codegen/e2e/__tests__/components/__snapshots__/GeneratePropsJavaDelegate-test.js.snap index 6e97f60966f91d..461895f3b0d44e 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/__snapshots__/GeneratePropsJavaDelegate-test.js.snap +++ b/packages/react-native-codegen/e2e/__tests__/components/__snapshots__/GeneratePropsJavaDelegate-test.js.snap @@ -53,6 +53,9 @@ public class ArrayPropsNativeComponentViewManagerDelegate { void setPoints(T view, @Nullable ReadableArray value); void setSizes(T view, @Nullable ReadableArray value); void setObject(T view, @Nullable ReadableArray value); + void setArrayOfObjects(T view, @Nullable ReadableArray value); } ", } diff --git a/packages/react-native-codegen/e2e/__tests__/components/__snapshots__/GenerateViewConfigJs-test.js.snap b/packages/react-native-codegen/e2e/__tests__/components/__snapshots__/GenerateViewConfigJs-test.js.snap index 12fa79b3d4f46b..400735e1e9ce73 100644 --- a/packages/react-native-codegen/e2e/__tests__/components/__snapshots__/GenerateViewConfigJs-test.js.snap +++ b/packages/react-native-codegen/e2e/__tests__/components/__snapshots__/GenerateViewConfigJs-test.js.snap @@ -38,6 +38,7 @@ export const __INTERNAL_VIEW_CONFIG = { points: true, sizes: true, object: true, + arrayOfObjects: true, }, }; diff --git a/packages/react-native-codegen/src/generators/components/ComponentsGeneratorUtils.js b/packages/react-native-codegen/src/generators/components/ComponentsGeneratorUtils.js index 5e6b6c9aeb3b69..104b35d4529351 100644 --- a/packages/react-native-codegen/src/generators/components/ComponentsGeneratorUtils.js +++ b/packages/react-native-codegen/src/generators/components/ComponentsGeneratorUtils.js @@ -270,8 +270,11 @@ function getLocalImports( typeAnnotation.type === 'ArrayTypeAnnotation' && typeAnnotation.elementType.type === 'ObjectTypeAnnotation' ) { + imports.add('#include '); const objectProps = typeAnnotation.elementType.properties; + // $FlowFixMe[incompatible-call] the type is guaranteed to be ObjectTypeAnnotation const objectImports = getImports(objectProps); + // $FlowFixMe[incompatible-call] the type is guaranteed to be ObjectTypeAnnotation const localImports = getLocalImports(objectProps); // $FlowFixMe[method-unbinding] added when improving typing for this parameters objectImports.forEach(imports.add, imports); diff --git a/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsH-test.js.snap b/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsH-test.js.snap index 7f8a84fe002064..30fd96825792c3 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsH-test.js.snap +++ b/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsH-test.js.snap @@ -17,6 +17,7 @@ Map { #include #include #include +#include #include #include #include @@ -234,6 +235,7 @@ Map { #include #include #include +#include #include #include #include