Skip to content

Refactor ViewManagerInterfaces codegen to generate kotlin classes (#51735) #52545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,15 @@ const FileTemplate = ({
* ${'@'}generated by codegen project: GeneratePropsJavaInterface.js
*/

package ${packageName};
package ${packageName}

${imports}

public interface ${className}<T extends ${extendClasses}> extends ViewManagerWithGeneratedInterface {
public interface ${className}<T: ${extendClasses}>: ViewManagerWithGeneratedInterface {
${methods}
}
`;

function addNullable(imports: Set<string>) {
imports.add('import androidx.annotation.Nullable;');
}

function getJavaValueForProp(
prop: NamedShape<PropTypeAnnotation>,
imports: Set<string>,
Expand All @@ -70,65 +66,52 @@ function getJavaValueForProp(
switch (typeAnnotation.type) {
case 'BooleanTypeAnnotation':
if (typeAnnotation.default === null) {
addNullable(imports);
return '@Nullable Boolean value';
return 'value: Boolean?';
} else {
return 'boolean value';
return 'value: Boolean';
}
case 'StringTypeAnnotation':
addNullable(imports);
return '@Nullable String value';
return 'value: String?';
case 'Int32TypeAnnotation':
return 'int value';
return 'value: Int';
case 'DoubleTypeAnnotation':
return 'double value';
return 'value: Double';
case 'FloatTypeAnnotation':
if (typeAnnotation.default === null) {
addNullable(imports);
return '@Nullable Float value';
return 'value: Float?';
} else {
return 'float value';
return 'value: Float';
}
case 'ReservedPropTypeAnnotation':
switch (typeAnnotation.name) {
case 'ColorPrimitive':
addNullable(imports);
return '@Nullable Integer value';
return 'value: Int?';
case 'ImageSourcePrimitive':
addNullable(imports);
return '@Nullable ReadableMap value';
return 'value: ReadableMap?';
case 'ImageRequestPrimitive':
addNullable(imports);
return '@Nullable ReadableMap value';
return 'value: ReadableMap?';
case 'PointPrimitive':
addNullable(imports);
return '@Nullable ReadableMap value';
return 'value: ReadableMap?';
case 'EdgeInsetsPrimitive':
addNullable(imports);
return '@Nullable ReadableMap value';
return 'value: ReadableMap?';
case 'DimensionPrimitive':
addNullable(imports);
return '@Nullable YogaValue value';
return 'value: YogaValue?';
default:
(typeAnnotation.name: empty);
throw new Error('Received unknown ReservedPropTypeAnnotation');
}
case 'ArrayTypeAnnotation': {
addNullable(imports);
return '@Nullable ReadableArray value';
return 'value: ReadableArray?';
}
case 'ObjectTypeAnnotation': {
addNullable(imports);
return '@Nullable ReadableMap value';
return 'value: ReadableMap?';
}
case 'StringEnumTypeAnnotation':
addNullable(imports);
return '@Nullable String value';
return 'value: String?';
case 'Int32EnumTypeAnnotation':
addNullable(imports);
return '@Nullable Integer value';
return 'value: Int?';
case 'MixedTypeAnnotation':
return 'Dynamic value';
return 'value: Dynamic';
default:
(typeAnnotation: empty);
throw new Error('Received invalid typeAnnotation');
Expand All @@ -142,9 +125,9 @@ function generatePropsString(component: ComponentShape, imports: Set<string>) {

return component.props
.map(prop => {
return `void set${toSafeJavaString(
return `public fun set${toSafeJavaString(
prop.name,
)}(T view, ${getJavaValueForProp(prop, imports)});`;
)}(view: T, ${getJavaValueForProp(prop, imports)}): Unit`;
})
.join('\n' + ' ');
}
Expand All @@ -156,19 +139,19 @@ function getCommandArgJavaType(param: NamedShape<CommandParamTypeAnnotation>) {
case 'ReservedTypeAnnotation':
switch (typeAnnotation.name) {
case 'RootTag':
return 'double';
return 'Double';
default:
(typeAnnotation.name: empty);
throw new Error(`Receieved invalid type: ${typeAnnotation.name}`);
}
case 'BooleanTypeAnnotation':
return 'boolean';
return 'Boolean';
case 'DoubleTypeAnnotation':
return 'double';
return 'Double';
case 'FloatTypeAnnotation':
return 'float';
return 'Float';
case 'Int32TypeAnnotation':
return 'int';
return 'Int';
case 'StringTypeAnnotation':
return 'String';
case 'ArrayTypeAnnotation':
Expand All @@ -184,11 +167,11 @@ function getCommandArguments(
componentName: string,
): string {
return [
'T view',
'view: T',
...command.typeAnnotation.params.map(param => {
const commandArgJavaType = getCommandArgJavaType(param);

return `${commandArgJavaType} ${param.name}`;
return `${param.name}: ${commandArgJavaType}`;
}),
].join(', ');
}
Expand All @@ -201,10 +184,10 @@ function generateCommandsString(
.map(command => {
const safeJavaName = toSafeJavaString(command.name, false);

return `void ${safeJavaName}(${getCommandArguments(
return `public fun ${safeJavaName}(${getCommandArguments(
command,
componentName,
)});`;
)}): Unit`;
})
.join('\n' + ' ');
}
Expand Down Expand Up @@ -287,7 +270,7 @@ module.exports = {
.trimRight(),
});

files.set(`${outputDir}/${className}.java`, replacedTemplate);
files.set(`${outputDir}/${className}.kt`, replacedTemplate);
});
});

Expand Down
Loading
Loading