Infer function input in workflow step (#8308)

- add `inputSchema` column in serverless function. This is an array of
parameters, with their name and type
- on serverless function id update, get the `inputSchema` + store empty
settings in step
- from step settings, build the form 

TODO in next PR:
- use field type to decide what kind of form should be printed
- have a strategy to handle object as input



https://github.com/user-attachments/assets/ed96f919-24b5-4baf-a051-31f76f45e575
This commit is contained in:
Thomas Trompette
2024-11-05 14:57:06 +01:00
committed by GitHub
parent d1531aa1b6
commit be8141ce5e
29 changed files with 334 additions and 90 deletions

View File

@ -8,16 +8,12 @@ import {
SyntaxKind,
} from 'ts-morph';
import { FunctionParameter } from 'src/engine/metadata-modules/serverless-function/dtos/function-parameter.dto';
import { generateFakeValue } from 'src/engine/utils/generate-fake-value';
import {
CodeIntrospectionException,
CodeIntrospectionExceptionCode,
} from 'src/modules/code-introspection/code-introspection.exception';
import { generateFakeValue } from 'src/engine/utils/generate-fake-value';
type FunctionParameter = {
name: string;
type: string;
};
@Injectable()
export class CodeIntrospectionService {
@ -27,7 +23,13 @@ export class CodeIntrospectionService {
this.project = new Project();
}
public analyze(
public generateInputData(fileContent: string, fileName = 'temp.ts') {
const parameters = this.getFunctionInputSchema(fileContent, fileName);
return this.generateFakeDataFromParams(parameters);
}
public getFunctionInputSchema(
fileContent: string,
fileName = 'temp.ts',
): FunctionParameter[] {
@ -38,7 +40,7 @@ export class CodeIntrospectionService {
const functionDeclarations = sourceFile.getFunctions();
if (functionDeclarations.length > 0) {
return this.analyzeFunctions(functionDeclarations);
return this.getFunctionParameters(functionDeclarations);
}
const arrowFunctions = sourceFile.getDescendantsOfKind(
@ -46,13 +48,13 @@ export class CodeIntrospectionService {
);
if (arrowFunctions.length > 0) {
return this.analyzeArrowFunctions(arrowFunctions);
return this.getArrowFunctionParameters(arrowFunctions);
}
return [];
}
private analyzeFunctions(
private getFunctionParameters(
functionDeclarations: FunctionDeclaration[],
): FunctionParameter[] {
if (functionDeclarations.length > 1) {
@ -67,7 +69,7 @@ export class CodeIntrospectionService {
return functionDeclaration.getParameters().map(this.buildFunctionParameter);
}
private analyzeArrowFunctions(
private getArrowFunctionParameters(
arrowFunctions: ArrowFunction[],
): FunctionParameter[] {
if (arrowFunctions.length > 1) {
@ -91,23 +93,13 @@ export class CodeIntrospectionService {
};
}
public generateInputData(fileContent: string, fileName = 'temp.ts') {
const parameters = this.analyze(fileContent, fileName);
return this.generateFakeDataFromParams(parameters);
}
private generateFakeDataFromParams(
params: FunctionParameter[],
): Record<string, any> {
const data: Record<string, any> = {};
return params.reduce((acc, param) => {
acc[param.name] = generateFakeValue(param.type);
params.forEach((param) => {
const type = param.type;
data[param.name] = generateFakeValue(type);
});
return data;
return acc;
}, {});
}
}