Skip to content

Commit 718a119

Browse files
committed
feat: add getSubFlows and createSubFlowIfCompatible functions to extract SubFlowValues from compatible function declarations
1 parent 0d898c6 commit 718a119

1 file changed

Lines changed: 51 additions & 5 deletions

File tree

src/util/subflows.util.ts

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@ import ts from "typescript";
22
import {FunctionDefinition, SubFlowValue, SubFlowValueSetting} from "@code0-tech/sagittarius-graphql-types";
33
import {isSubFlow} from "./schema.util";
44

5+
/**
6+
* Extracts and creates a list of SubFlowValue objects from function declarations
7+
* that are compatible with the given parameter type.
8+
*
9+
* This function filters function declarations based on type compatibility and
10+
* converts matching functions into SubFlowValue objects that can be used as
11+
* sub-flows in a larger workflow.
12+
*
13+
* @param {ts.TypeChecker} checker - The TypeScript type checker for type analysis
14+
* @param {ts.FunctionDeclaration[]} functionDeclarations - Array of function declarations to analyze
15+
* @param {FunctionDefinition[]} functions - Array of function definitions for reference lookup
16+
* @param {ts.Type} paramType - The target parameter type that functions must be compatible with
17+
*
18+
* @returns {SubFlowValue[]} An array of SubFlowValue objects for compatible functions,
19+
* or an empty array if the paramType is not a sub-flow type
20+
*/
521
export const getSubFlows = (
622
checker: ts.TypeChecker,
723
functionDeclarations: ts.FunctionDeclaration[],
@@ -20,24 +36,41 @@ export const getSubFlows = (
2036

2137
}
2238

39+
/**
40+
* Verifies that a function declaration is compatible with a given parameter type
41+
* and creates a SubFlowValue if the types match.
42+
*
43+
* Type compatibility is checked by comparing the function type against the expected
44+
* parameter type (e.g., a function signature like (number: number) => void).
45+
* If compatible, the function is converted into a SubFlowValue object with its
46+
* associated parameter settings.
47+
*
48+
* @param {ts.TypeChecker} checker - The TypeScript type checker for type analysis
49+
* @param {ts.FunctionDeclaration} func - The function declaration to check
50+
* @param {FunctionDefinition[]} functions - Array of function definitions for lookup
51+
* @param {ts.Type} paramType - The expected parameter type to check compatibility against
52+
*
53+
* @returns {SubFlowValue | null} A SubFlowValue object if the function is compatible
54+
* with the paramType and a matching function definition exists, or null otherwise
55+
*/
2356
const createSubFlowIfCompatible = (
2457
checker: ts.TypeChecker,
2558
func: ts.FunctionDeclaration,
2659
functions: FunctionDefinition[],
2760
paramType: ts.Type
2861
): SubFlowValue | null => {
2962

30-
// 2. Hole den Typ der Funktion (nicht nur den Return Type!)
63+
// Get the full function type from the declaration
3164
const functionType = checker.getTypeAtLocation(func);
3265

33-
// 3. Prüfe ob functionType zu paramType assignierbar ist
34-
// paramType ist z.B. (number: number) => void
35-
// functionType sollte auch (number: number) => void (oder kompatibel) sein
66+
// Check if the function type is assignable to the parameter type
67+
// This ensures the function signature matches the expected interface
68+
// e.g., paramType might be (number: number) => void, and functionType should be compatible
3669
if (!checker.isTypeAssignableTo(functionType, paramType)) {
3770
return null;
3871
}
3972

40-
// 4. Wenn kompatibel: Erstelle SubFlowValue
73+
// If type-compatible, find the function definition and create the SubFlowValue
4174
const functionName = normalizeFunctionName(func.name?.getText());
4275
const functionDefinition = functions.find((f) => f.identifier === functionName);
4376

@@ -77,6 +110,19 @@ const normalizeFunctionName = (rawName: string | undefined): string => {
77110
.replace("_", "::");
78111
}
79112

113+
/**
114+
* Constructs a SubFlowValue object from a function definition.
115+
*
116+
* This function transforms a FunctionDefinition into a SubFlowValue with all
117+
* its parameter settings properly configured. Each parameter from the function
118+
* definition is converted into a SubFlowValueSetting object that includes
119+
* metadata such as identifier, default values, hidden status, and optional status.
120+
*
121+
* @param {FunctionDefinition} functionDefinition - The function definition to convert
122+
*
123+
* @returns {SubFlowValue} A SubFlowValue object with the function definition
124+
* and all parameter settings configured
125+
*/
80126
const buildSubFlowValue = (functionDefinition: FunctionDefinition): SubFlowValue => {
81127
return {
82128
__typename: "SubFlowValue",

0 commit comments

Comments
 (0)