Skip to content

Commit c62050b

Browse files
committed
feat: enhance getSignatureSchema to handle flow signatures and improve ID sanitization
1 parent 860569a commit c62050b

3 files changed

Lines changed: 126 additions & 5 deletions

File tree

src/schema/getSignatureSchema.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ interface ParameterDependency {
2525
dependsOnIndex: number
2626
}
2727

28+
//TODO: Need to work also for flow signature
29+
//TODO: needs a way to declare custom datatypes with there schema
2830
/**
2931
* Generates node schemas for all parameters of a specified function node.
3032
*
@@ -63,8 +65,8 @@ export const getSignatureSchema = (
6365

6466
// Retrieve and identify the target node
6567
const targetNode = flow.nodes?.nodes?.find((n) => n?.id === nodeId)
66-
const functionId = `fn_${targetNode?.functionDefinition?.identifier?.replace(/::/g, "_")}`
67-
const realNodeId = `node_${sanitizeId(nodeId || "")}`
68+
const functionId = nodeId ? `fn_${targetNode?.functionDefinition?.identifier?.replace(/::/g, "_")}` : `flow`
69+
const realNodeId = nodeId ? `node_${sanitizeId(nodeId)}` : `flow_${sanitizeId(flow.id!)}`
6870

6971
// Build map of declared functions for easy lookup
7072
const declaredFunctionsMap = createFunctionMap(sourceFile)
@@ -98,8 +100,8 @@ export const getSignatureSchema = (
98100
node!,
99101
combinedParameterTypes,
100102
funktionDependencies,
101-
declaredFunctionsMap,
102-
functions,
103+
nodeId ? declaredFunctionsMap : new Map(),
104+
nodeId ? functions : [],
103105
)
104106
}
105107

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export function getSharedTypeDeclarations(dataTypes?: DataType[], genericType: s
9696
/**
9797
* Sanitizes an ID for use as a TypeScript variable name.
9898
*/
99-
export const sanitizeId = (id: string) => id.replace(/[^a-zA-Z0-9]/g, '_');
99+
export const sanitizeId = (id: string) => id?.replace(/[^a-zA-Z0-9]/g, '_');
100100

101101
/**
102102
* Generates TypeScript source code for a flow, suitable for validation and type inference.

test/schema/schema.test.ts

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import {describe, expect, it} from "vitest";
2+
import {Flow} from "@code0-tech/sagittarius-graphql-types";
3+
import {getFlowValidation, getSignatureSchema} from "../../src";
4+
import {DATA_TYPES, FUNCTION_SIGNATURES} from "../data";
5+
6+
describe("Schema", () => {
7+
8+
it('1', () => {
9+
10+
const flow: Flow = {
11+
id: "gid://sagittarius/Flow/1",
12+
startingNodeId: "gid://sagittarius/NodeFunction/1",
13+
nodes: {
14+
nodes: [
15+
{
16+
id: "gid://sagittarius/NodeFunction/1",
17+
functionDefinition: {identifier: "std::number::add"},
18+
parameters: {
19+
nodes: [
20+
{value: {__typename: "LiteralValue", value: 1}},
21+
{value: {__typename: "LiteralValue", value: 0}}
22+
]
23+
},
24+
nextNodeId: "gid://sagittarius/NodeFunction/2"
25+
},
26+
{
27+
id: "gid://sagittarius/NodeFunction/2",
28+
functionDefinition: {identifier: "std::list::for_each"},
29+
parameters: {
30+
nodes: [
31+
{
32+
value: {
33+
__typename: "LiteralValue",
34+
value: [{test: 1}]
35+
}
36+
},
37+
{
38+
value: {
39+
__typename: "NodeFunctionIdWrapper",
40+
id: "gid://sagittarius/NodeFunction/3"
41+
}
42+
}
43+
]
44+
}
45+
},
46+
{
47+
id: "gid://sagittarius/NodeFunction/3",
48+
functionDefinition: {identifier: "std::number::add"},
49+
parameters: {
50+
nodes: [
51+
{
52+
value: {
53+
__typename: "ReferenceValue",
54+
nodeFunctionId: "gid://sagittarius/NodeFunction/2",
55+
parameterIndex: 1,
56+
inputIndex: 0,
57+
referencePath: [{path: "test"}]
58+
}
59+
},
60+
{
61+
value: {__typename: "LiteralValue", value: 10}
62+
}
63+
]
64+
}
65+
}
66+
]
67+
},
68+
signature: "(test: HTTP_METHOD): void"
69+
};
70+
71+
const result = getSignatureSchema(flow, DATA_TYPES, FUNCTION_SIGNATURES);
72+
73+
//console.dir(result, {depth: null})
74+
});
75+
76+
it('2', () => {
77+
78+
const flow: Flow = {
79+
nodes: {
80+
nodes: [
81+
{
82+
id: "gid://sagittarius/NodeFunction/1",
83+
functionDefinition: {identifier: "std::list::at"},
84+
parameters: {
85+
nodes: [
86+
{value: null},
87+
{value: {__typename: "LiteralValue", value: 0}}
88+
]
89+
},
90+
nextNodeId: "gid://sagittarius/NodeFunction/2"
91+
},
92+
{
93+
id: "gid://sagittarius/NodeFunction/2",
94+
functionDefinition: {identifier: "std::number::add"},
95+
parameters: {
96+
nodes: [
97+
{
98+
value: {
99+
__typename: "ReferenceValue",
100+
nodeFunctionId: "gid://sagittarius/NodeFunction/1"
101+
}
102+
},
103+
{
104+
value: null
105+
}
106+
]
107+
}
108+
}
109+
]
110+
}
111+
};
112+
113+
const result = getSignatureSchema(flow, DATA_TYPES, FUNCTION_SIGNATURES, "gid://sagittarius/NodeFunction/2");
114+
115+
//console.dir(result, {depth: null})
116+
117+
});
118+
119+
})

0 commit comments

Comments
 (0)