Skip to content

Commit

Permalink
Rewrite array transform to utilise existing transform
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Lucas committed Jun 13, 2024
1 parent 37c9a2a commit 2e9286f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 37 deletions.
63 changes: 40 additions & 23 deletions packages/openapi-ts/src/compiler/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ export const createDateTransformMutation = ({

export const createArrayTransformMutation = ({
path,
statements,
transformer,
// statements,
}: {
path: string[];
statements: ts.Statement[];
transformer: string;
// statements: ts.Statement[];
}): ts.Statement => {
const safeAccessExpression = path
.slice(1)
Expand Down Expand Up @@ -82,32 +84,47 @@ export const createArrayTransformMutation = ({
ts.factory.createCallChain(
ts.factory.createPropertyAccessChain(
safeAccessExpression,
undefined,
ts.factory.createToken(ts.SyntaxKind.QuestionDotToken),
ts.factory.createIdentifier('forEach'),
),
ts.factory.createToken(ts.SyntaxKind.QuestionDotToken),
undefined,
undefined,
[
ts.factory.createArrowFunction(
undefined,
undefined,
[
ts.factory.createParameterDeclaration(
undefined,
undefined,
ts.factory.createIdentifier('item'),
undefined,
undefined,
undefined,
),
],
undefined,
ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
ts.factory.createBlock(statements, true),
),
],
[ts.factory.createIdentifier(transformer)],
),
),

// TODO: might need this but clean up otherwise
// ts.factory.createExpressionStatement(
// ts.factory.createCallChain(
// ts.factory.createPropertyAccessChain(
// safeAccessExpression,
// undefined,
// ts.factory.createIdentifier('forEach'),
// ),
// undefined,
// [ts.factory.createIdentifier(transformer)]
// // undefined,
// // [
// // ts.factory.createArrowFunction(
// // undefined,
// // undefined,
// // [
// // ts.factory.createParameterDeclaration(
// // undefined,
// // undefined,
// // ts.factory.createIdentifier('item'),
// // undefined,
// // undefined,
// // undefined,
// // ),
// // ],
// // undefined,
// // ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
// // ts.factory.createBlock(statements, true),
// // ),
// // ],
// ),
// ),
],
true,
),
Expand Down
29 changes: 15 additions & 14 deletions packages/openapi-ts/src/utils/write/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,17 @@ const processType = (client: Client, model: Model, onNode: OnNode) => {
const generateTransform = (client: Client, model: Model, onNode: OnNode) => {
const config = getConfig();
if (config.types.dates === 'types+transform') {
const mapTypeToTransformStatements = (
localPath: string[],
if (model.meta?.hasTransformer) {
// Transform already created (maybe the model is used in other models) so we just bail here
return;
}

const generateForProperty = (
rootPath: string[],
property: Model,
isArrayElement?: boolean,
) => {
const path = [...localPath, property.name];
const path = [...rootPath, property.name];

if (
property.type === 'string' &&
Expand All @@ -214,7 +219,7 @@ const generateTransform = (client: Client, model: Model, onNode: OnNode) => {
return [];
} else {
// otherwise we recurse in case it's an object/array, and if it's not that will just bail with []
return generateTransformStatements(path, property);
return generateForModel(path, property);
}
};

Expand All @@ -235,31 +240,27 @@ const generateTransform = (client: Client, model: Model, onNode: OnNode) => {
);
}

const statements = mapTypeToTransformStatements(['item'], nextModel);

if (statements.length === 0) {
return [];
}
generateTransform(client, nextModel, onNode);

return [
compiler.transform.arrayTransformMutation({
path: localPath,
statements,
transformer: nextModel.meta!.name,
}),
];
}

throw new Error('Unsupported array type');
}

function generateTransformStatements(
function generateForModel(
localPath: string[],
localModel: Model,
): ts.Statement[] {
switch (localModel.export) {
case 'interface':
return localModel.properties.flatMap((property) =>
mapTypeToTransformStatements(localPath, property),
generateForProperty(localPath, property),
);
case 'array':
return generateForArray(localPath, localModel);
Expand All @@ -270,11 +271,11 @@ const generateTransform = (client: Client, model: Model, onNode: OnNode) => {
}
}

const transforms = generateTransformStatements(['data'], model);
const transforms = generateForModel(['data'], model);
if (transforms.length > 0) {
const transformFunction = compiler.transform.transformMutationFunction({
modelName: model.name,
statements: generateTransformStatements(['data'], model),
statements: generateForModel(['data'], model),
});

client.types[model.meta!.name].hasTransformer = true;
Expand Down

0 comments on commit 2e9286f

Please sign in to comment.