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 380a21366b7fef..51fa83925af736 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 @@ -86,7 +86,7 @@ static inline std::string toString(const ArrayPropsNativeComponentViewSizesMaskW return result; } struct ArrayPropsNativeComponentViewObjectStruct { - std::string prop; + std::string prop{}; }; static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ArrayPropsNativeComponentViewObjectStruct &result) { @@ -113,8 +113,8 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu struct ArrayPropsNativeComponentViewArrayOfObjectsStruct { - Float prop1; - int prop2; + Float prop1{0.0}; + int prop2{0}; }; static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ArrayPropsNativeComponentViewArrayOfObjectsStruct &result) { @@ -760,12 +760,12 @@ static inline std::string toString(const ObjectPropsNativeComponentIntEnumProp & } } struct ObjectPropsNativeComponentObjectPropStruct { - std::string stringProp; - bool booleanProp; - Float floatProp; - int intProp; - ObjectPropsNativeComponentStringEnumProp stringEnumProp; - ObjectPropsNativeComponentIntEnumProp intEnumProp; + std::string stringProp{\\"\\"}; + bool booleanProp{false}; + Float floatProp{0.0}; + int intProp{0}; + ObjectPropsNativeComponentStringEnumProp stringEnumProp{ObjectPropsNativeComponentStringEnumProp::Small}; + ObjectPropsNativeComponentIntEnumProp intEnumProp{ObjectPropsNativeComponentIntEnumProp::IntEnumProp0}; }; static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsNativeComponentObjectPropStruct &result) { @@ -802,7 +802,7 @@ static inline std::string toString(const ObjectPropsNativeComponentObjectPropStr } struct ObjectPropsNativeComponentObjectArrayPropStruct { - std::vector array; + std::vector array{}; }; static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsNativeComponentObjectArrayPropStruct &result) { @@ -819,9 +819,9 @@ static inline std::string toString(const ObjectPropsNativeComponentObjectArrayPr } struct ObjectPropsNativeComponentObjectPrimitiveRequiredPropStruct { - ImageSource image; - SharedColor color; - Point point; + ImageSource image{}; + SharedColor color{}; + Point point{}; }; static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsNativeComponentObjectPrimitiveRequiredPropStruct &result) { diff --git a/packages/react-native-codegen/src/generators/components/CppHelpers.js b/packages/react-native-codegen/src/generators/components/CppHelpers.js index 9fa2154e0bcae7..a208a840e9fa27 100644 --- a/packages/react-native-codegen/src/generators/components/CppHelpers.js +++ b/packages/react-native-codegen/src/generators/components/CppHelpers.js @@ -166,6 +166,14 @@ function getEnumMaskName(enumName: string): string { return `${enumName}Mask`; } +function getDefaultInitializerString( + componentName: string, + prop: NamedShape, +): string { + const defaultValue = convertDefaultTypeToString(componentName, prop); + return `{${defaultValue}}`; +} + function convertDefaultTypeToString( componentName: string, prop: NamedShape, @@ -305,6 +313,7 @@ const IncludeTemplate = ({ }; module.exports = { + getDefaultInitializerString, convertDefaultTypeToString, getCppArrayTypeForAnnotation, getCppTypeForAnnotation, diff --git a/packages/react-native-codegen/src/generators/components/GeneratePropsH.js b/packages/react-native-codegen/src/generators/components/GeneratePropsH.js index 66edb3d640d37f..0fed9bc550682e 100644 --- a/packages/react-native-codegen/src/generators/components/GeneratePropsH.js +++ b/packages/react-native-codegen/src/generators/components/GeneratePropsH.js @@ -23,8 +23,8 @@ const { getNativeTypeFromAnnotation, } = require('./ComponentsGeneratorUtils.js'); const { - convertDefaultTypeToString, generateStructName, + getDefaultInitializerString, getEnumMaskName, toIntEnumValueName, } = require('./CppHelpers.js'); @@ -460,13 +460,21 @@ function generateEnumString( function generatePropsString( componentName: string, props: $ReadOnlyArray>, + nameParts: $ReadOnlyArray = [], ) { return props .map(prop => { - const nativeType = getNativeTypeFromAnnotation(componentName, prop, []); - const defaultValue = convertDefaultTypeToString(componentName, prop); + const nativeType = getNativeTypeFromAnnotation( + componentName, + prop, + nameParts, + ); + const defaultInitializer = getDefaultInitializerString( + componentName, + prop, + ); - return `${nativeType} ${prop.name}{${defaultValue}};`; + return `${nativeType} ${prop.name}${defaultInitializer};`; }) .join('\n' + ' '); } @@ -633,16 +641,7 @@ function generateStruct( ): void { const structNameParts = nameParts; const structName = generateStructName(componentName, structNameParts); - - const fields = properties - .map(property => { - return `${getNativeTypeFromAnnotation( - componentName, - property, - structNameParts, - )} ${property.name};`; - }) - .join('\n' + ' '); + const fields = generatePropsString(componentName, properties, nameParts); properties.forEach((property: NamedShape) => { const name = property.name; diff --git a/packages/react-native-codegen/src/generators/components/__test_fixtures__/fixtures.js b/packages/react-native-codegen/src/generators/components/__test_fixtures__/fixtures.js index 3b6915f33f9ec2..9121c8ad6878ee 100644 --- a/packages/react-native-codegen/src/generators/components/__test_fixtures__/fixtures.js +++ b/packages/react-native-codegen/src/generators/components/__test_fixtures__/fixtures.js @@ -871,6 +871,38 @@ const OBJECT_PROPS: SchemaType = { default: 0, }, }, + { + name: 'stringUserDefaultProp', + optional: true, + typeAnnotation: { + type: 'StringTypeAnnotation', + default: 'user_default', + }, + }, + { + name: 'booleanUserDefaultProp', + optional: true, + typeAnnotation: { + type: 'BooleanTypeAnnotation', + default: true, + }, + }, + { + name: 'floatUserDefaultProp', + optional: true, + typeAnnotation: { + type: 'FloatTypeAnnotation', + default: 3.14, + }, + }, + { + name: 'intUserDefaultProp', + optional: true, + typeAnnotation: { + type: 'Int32TypeAnnotation', + default: 9999, + }, + }, { name: 'stringEnumProp', optional: true, 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 bb72af650f6037..94cd317efa4c2b 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 @@ -85,7 +85,7 @@ static inline std::string toString(const ArrayPropsNativeComponentSizesMaskWrapp return result; } struct ArrayPropsNativeComponentObjectStruct { - std::string stringProp; + std::string stringProp{\\"\\"}; }; static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ArrayPropsNativeComponentObjectStruct &result) { @@ -112,7 +112,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu struct ArrayPropsNativeComponentArrayObjectStruct { - std::string stringProp; + std::string stringProp{\\"\\"}; }; static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ArrayPropsNativeComponentArrayObjectStruct &result) { @@ -139,7 +139,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu struct ArrayPropsNativeComponentArrayStruct { - std::vector object; + std::vector object{}; }; static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ArrayPropsNativeComponentArrayStruct &result) { @@ -166,7 +166,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu struct ArrayPropsNativeComponentArrayOfArrayOfObjectStruct { - std::string stringProp; + std::string stringProp{\\"\\"}; }; static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ArrayPropsNativeComponentArrayOfArrayOfObjectStruct &result) { @@ -246,9 +246,9 @@ Map { namespace facebook::react { struct ArrayPropsNativeComponentNativePrimitivesStruct { - std::vector colors; - std::vector srcs; - std::vector points; + std::vector colors{}; + std::vector srcs{}; + std::vector points{}; }; static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ArrayPropsNativeComponentNativePrimitivesStruct &result) { @@ -1104,7 +1104,7 @@ static inline std::string toString(const ObjectPropsIntEnumProp &value) { } } struct ObjectPropsObjectPropObjectArrayPropStruct { - std::vector array; + std::vector array{}; }; static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsObjectPropObjectArrayPropStruct &result) { @@ -1121,9 +1121,9 @@ static inline std::string toString(const ObjectPropsObjectPropObjectArrayPropStr } struct ObjectPropsObjectPropObjectPrimitiveRequiredPropStruct { - ImageSource image; - SharedColor color; - Point point; + ImageSource image{}; + SharedColor color{}; + Point point{}; }; static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsObjectPropObjectPrimitiveRequiredPropStruct &result) { @@ -1148,7 +1148,7 @@ static inline std::string toString(const ObjectPropsObjectPropObjectPrimitiveReq } struct ObjectPropsObjectPropNestedPropANestedPropBStruct { - std::string nestedPropC; + std::string nestedPropC{\\"\\"}; }; static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsObjectPropNestedPropANestedPropBStruct &result) { @@ -1165,7 +1165,7 @@ static inline std::string toString(const ObjectPropsObjectPropNestedPropANestedP } struct ObjectPropsObjectPropNestedPropAStruct { - ObjectPropsObjectPropNestedPropANestedPropBStruct nestedPropB; + ObjectPropsObjectPropNestedPropANestedPropBStruct nestedPropB{}; }; static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsObjectPropNestedPropAStruct &result) { @@ -1182,7 +1182,7 @@ static inline std::string toString(const ObjectPropsObjectPropNestedPropAStruct } struct ObjectPropsObjectPropNestedArrayAsPropertyArrayPropStruct { - std::string stringProp; + std::string stringProp{\\"\\"}; }; static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsObjectPropNestedArrayAsPropertyArrayPropStruct &result) { @@ -1209,7 +1209,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu struct ObjectPropsObjectPropNestedArrayAsPropertyStruct { - std::vector arrayProp; + std::vector arrayProp{}; }; static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsObjectPropNestedArrayAsPropertyStruct &result) { @@ -1226,16 +1226,20 @@ static inline std::string toString(const ObjectPropsObjectPropNestedArrayAsPrope } struct ObjectPropsObjectPropStruct { - std::string stringProp; - bool booleanProp; - Float floatProp; - int intProp; - ObjectPropsStringEnumProp stringEnumProp; - ObjectPropsIntEnumProp intEnumProp; - ObjectPropsObjectPropObjectArrayPropStruct objectArrayProp; - ObjectPropsObjectPropObjectPrimitiveRequiredPropStruct objectPrimitiveRequiredProp; - ObjectPropsObjectPropNestedPropAStruct nestedPropA; - ObjectPropsObjectPropNestedArrayAsPropertyStruct nestedArrayAsProperty; + std::string stringProp{\\"\\"}; + bool booleanProp{false}; + Float floatProp{0.0}; + int intProp{0}; + std::string stringUserDefaultProp{\\"user_default\\"}; + bool booleanUserDefaultProp{true}; + Float floatUserDefaultProp{3.14}; + int intUserDefaultProp{9999}; + ObjectPropsStringEnumProp stringEnumProp{ObjectPropsStringEnumProp::Option1}; + ObjectPropsIntEnumProp intEnumProp{ObjectPropsIntEnumProp::IntEnumProp0}; + ObjectPropsObjectPropObjectArrayPropStruct objectArrayProp{}; + ObjectPropsObjectPropObjectPrimitiveRequiredPropStruct objectPrimitiveRequiredProp{}; + ObjectPropsObjectPropNestedPropAStruct nestedPropA{}; + ObjectPropsObjectPropNestedArrayAsPropertyStruct nestedArrayAsProperty{}; }; static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsObjectPropStruct &result) { @@ -1257,6 +1261,22 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu if (tmp_intProp != map.end()) { fromRawValue(context, tmp_intProp->second, result.intProp); } + auto tmp_stringUserDefaultProp = map.find(\\"stringUserDefaultProp\\"); + if (tmp_stringUserDefaultProp != map.end()) { + fromRawValue(context, tmp_stringUserDefaultProp->second, result.stringUserDefaultProp); + } + auto tmp_booleanUserDefaultProp = map.find(\\"booleanUserDefaultProp\\"); + if (tmp_booleanUserDefaultProp != map.end()) { + fromRawValue(context, tmp_booleanUserDefaultProp->second, result.booleanUserDefaultProp); + } + auto tmp_floatUserDefaultProp = map.find(\\"floatUserDefaultProp\\"); + if (tmp_floatUserDefaultProp != map.end()) { + fromRawValue(context, tmp_floatUserDefaultProp->second, result.floatUserDefaultProp); + } + auto tmp_intUserDefaultProp = map.find(\\"intUserDefaultProp\\"); + if (tmp_intUserDefaultProp != map.end()) { + fromRawValue(context, tmp_intUserDefaultProp->second, result.intUserDefaultProp); + } auto tmp_stringEnumProp = map.find(\\"stringEnumProp\\"); if (tmp_stringEnumProp != map.end()) { fromRawValue(context, tmp_stringEnumProp->second, result.stringEnumProp); diff --git a/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsJavaPojo-test.js.snap b/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsJavaPojo-test.js.snap index 47ce16d68f18f5..fe1eaec379e592 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsJavaPojo-test.js.snap +++ b/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsJavaPojo-test.js.snap @@ -1024,6 +1024,10 @@ public class ObjectPropsPropsObjectProp { private boolean mBooleanProp; private float mFloatProp; private int mIntProp; + private @Nullable String mStringUserDefaultProp; + private boolean mBooleanUserDefaultProp; + private float mFloatUserDefaultProp; + private int mIntUserDefaultProp; private @Nullable String mStringEnumProp; private @Nullable Integer mIntEnumProp; private ObjectPropsPropsObjectPropObjectArrayProp mObjectArrayProp; @@ -1047,6 +1051,22 @@ public class ObjectPropsPropsObjectProp { return mIntProp; } @DoNotStrip + public @Nullable String getStringUserDefaultProp() { + return mStringUserDefaultProp; + } + @DoNotStrip + public boolean getBooleanUserDefaultProp() { + return mBooleanUserDefaultProp; + } + @DoNotStrip + public float getFloatUserDefaultProp() { + return mFloatUserDefaultProp; + } + @DoNotStrip + public int getIntUserDefaultProp() { + return mIntUserDefaultProp; + } + @DoNotStrip public @Nullable String getStringEnumProp() { return mStringEnumProp; }