Skip to content

Commit dfdb817

Browse files
committed
feat: wip started to work on mapper
1 parent b10bb12 commit dfdb817

File tree

2 files changed

+225
-9
lines changed

2 files changed

+225
-9
lines changed

reader/ts/src/parser.ts

Lines changed: 223 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
import {Reader} from './reader.js';
2-
import {DataType, FlowType, RuntimeFunctionDefinition} from "@code0-tech/sagittarius-graphql-types";
2+
import {
3+
DataType, DataTypeIdentifier,
4+
DataTypeRule,
5+
DataTypeRuleConnection, DataTypeRulesConfig,
6+
DataTypeRulesVariant,
7+
DataTypeVariant,
8+
FlowType, FlowTypeSetting,
9+
FunctionDefinition, ParameterDefinitionConnection,
10+
TranslationConnection
11+
} from "@code0-tech/sagittarius-graphql-types";
12+
import {
13+
DefinitionDataType,
14+
DefinitionDataType_Variant,
15+
DefinitionDataTypeRule
16+
} from "@code0-tech/tucana/pb/shared.data_type_pb.js";
17+
import {Translation} from "@code0-tech/tucana/pb/shared.translation_pb.js"
18+
import {FlowType as TucanaFlowType, FlowTypeSetting as TucanaFlowTypeSetting} from "@code0-tech/tucana/pb/shared.flow_definition_pb.js"
19+
import {RuntimeFunctionDefinition as TucanaFunction, RuntimeParameterDefinition} from "@code0-tech/tucana/pb/shared.runtime_function_pb.js"
320
import {Feature, Meta, MetaType} from "./types.js";
421

522
export const Definition = (rootPath: string): Feature[] => {
@@ -32,22 +49,221 @@ function appendMeta(feature: Feature, meta: Meta): void {
3249
try {
3350
switch (meta.type) {
3451
case MetaType.DataType: {
35-
const parsed = JSON.parse(definition) as DataType;
36-
feature.dataTypes.push(parsed);
52+
const parsed = JSON.parse(definition) as DefinitionDataType;
53+
const dataType: DataType = {
54+
genericKeys: parsed.genericKeys,
55+
identifier: parsed.identifier,
56+
name: createTranslation(parsed.name),
57+
rules: createRules(parsed.rules),
58+
variant: getDataTypeVariant(parsed.variant)
59+
}
60+
feature.dataTypes.push(dataType);
3761
break;
3862
}
3963
case MetaType.FlowType: {
40-
const parsed = JSON.parse(definition) as FlowType;
41-
feature.flowTypes.push(parsed);
64+
const parsed = JSON.parse(definition) as TucanaFlowType;
65+
const flowType: FlowType = {
66+
identifier: parsed.identifier,
67+
inputType: getDataType(parsed.inputTypeIdentifier),
68+
returnType: getDataType(parsed.returnTypeIdentifier),
69+
flowTypeSettings: createFlowTypeSetting(parsed.settings),
70+
names: createTranslation(parsed.name),
71+
descriptions: createTranslation(parsed.description),
72+
editable: parsed.editable
73+
}
74+
feature.flowTypes.push(flowType);
4275
break;
4376
}
4477
case MetaType.RuntimeFunction: {
45-
const parsed = JSON.parse(definition) as RuntimeFunctionDefinition;
46-
feature.runtimeFunctions.push(parsed);
78+
const parsed = JSON.parse(definition) as TucanaFunction;
79+
const functionDefinition: FunctionDefinition = {
80+
genericKeys: parsed.genericKeys,
81+
names: createTranslation(parsed.name),
82+
descriptions: createTranslation(parsed.description),
83+
documentations: createTranslation(parsed.documentation),
84+
deprecationMessages: createTranslation(parsed.deprecationMessage),
85+
throwsError: parsed.throwsError,
86+
returnType: getDataTypeIdentifier(parsed.returnTypeIdentifier),
87+
parameterDefinitions: getParameterDefinitionConnection(parsed.runtimeParameterDefinitions),
88+
}
89+
90+
feature.runtimeFunctions.push(functionDefinition);
4791
break;
4892
}
4993
}
5094
} catch (err: any) {
5195
console.error(`Error parsing ${meta.type} ${meta.name} ${definition}:`, err);
5296
}
97+
}
98+
99+
function createFlowTypeSetting(settings: TucanaFlowTypeSetting[]): FlowTypeSetting[] {
100+
return settings.map(setting => {
101+
return {
102+
names: createTranslation(setting.name),
103+
descriptions: createTranslation(setting.description),
104+
dataType: getDataType(setting.dataTypeIdentifier),
105+
identifier: setting.identifier,
106+
unique: setting.unique
107+
}
108+
})
109+
}
110+
111+
function getParameterDefinitionConnection(def: RuntimeParameterDefinition[]): ParameterDefinitionConnection {
112+
return {
113+
count: def.length,
114+
nodes: def.map(node => {
115+
return {
116+
names: createTranslation(node.name),
117+
descriptions: createTranslation(node.description),
118+
documentations: createTranslation(node.documentation),
119+
dataType: getDataTypeIdentifier(node.dataTypeIdentifier)
120+
}
121+
})
122+
}
123+
}
124+
125+
function getDataType(identifier: string | undefined): DataType {
126+
// TODO
127+
// @ts-ignore
128+
return null
129+
}
130+
131+
function getDataTypeIdentifier(identifier: string): DataTypeIdentifier {
132+
// TODO
133+
// @ts-ignore
134+
return null
135+
}
136+
137+
function createTranslation(translation: Translation[]): TranslationConnection {
138+
return {
139+
count: translation.length,
140+
nodes: translation,
141+
}
142+
}
143+
144+
function mapDefinitionRuleToDataTypeRule(
145+
rule: DefinitionDataTypeRule
146+
): DataTypeRulesConfig | null {
147+
const { config } = rule;
148+
149+
switch (config.oneofKind) {
150+
case "containsKey":
151+
return {
152+
__typename: "DataTypeRulesContainsKeyConfig",
153+
key: config.containsKey.key,
154+
dataTypeIdentifier: config.containsKey.dataTypeIdentifier,
155+
};
156+
157+
case "containsType":
158+
return {
159+
__typename: "DataTypeRulesContainsTypeConfig",
160+
dataTypeIdentifier: config.containsType.dataTypeIdentifier,
161+
};
162+
163+
case "itemOfCollection":
164+
return {
165+
__typename: "DataTypeRulesItemOfCollectionConfig",
166+
items: config.itemOfCollection.items,
167+
};
168+
169+
case "numberRange":
170+
return {
171+
__typename: "DataTypeRulesNumberRangeConfig",
172+
from: Number(config.numberRange.from),
173+
to: Number(config.numberRange.to),
174+
steps: config.numberRange.steps ? Number(config.numberRange.steps) : undefined,
175+
};
176+
177+
case "regex":
178+
return {
179+
__typename: "DataTypeRulesRegexConfig",
180+
pattern: config.regex.pattern,
181+
};
182+
183+
case "inputTypes":
184+
return {
185+
__typename: "DataTypeRulesInputTypesConfig",
186+
inputTypes: config.inputTypes.inputTypes.map(input => ({
187+
__typename: "DataTypeRulesInputTypeConfig",
188+
inputType: input.dataTypeIdentifier
189+
? { identifier: input.dataTypeIdentifier.type.dataTypeIdentifier ?? undefined }
190+
: undefined,
191+
dataTypeIdentifier: input.dataTypeIdentifier,
192+
})),
193+
};
194+
195+
case "returnType":
196+
return {
197+
__typename: "DataTypeRulesReturnTypeConfig",
198+
dataTypeIdentifier: config.returnType.dataTypeIdentifier,
199+
};
200+
201+
case "parentType":
202+
return {
203+
__typename: "DataTypeRulesParentTypeConfig",
204+
dataTypeIdentifier: config.parentType.parentType,
205+
};
206+
207+
default:
208+
return null;
209+
}
210+
}
211+
212+
213+
function createRules(rule: DefinitionDataTypeRule[]): DataTypeRuleConnection {
214+
return {
215+
count: rule.length,
216+
nodes: rule.map(r => {
217+
const rule: DataTypeRule = {
218+
variant: getRuleTypeValue(r.config.oneofKind),
219+
config: mapDefinitionRuleToDataTypeRule(r)
220+
}
221+
return rule;
222+
}
223+
)
224+
}
225+
}
226+
227+
function getRuleTypeValue(rule: string | unknown): DataTypeRulesVariant {
228+
switch (rule) {
229+
case "containsKey":
230+
return DataTypeRulesVariant.ContainsKey;
231+
case "containsType":
232+
return DataTypeRulesVariant.ContainsType;
233+
case "itemOfCollection":
234+
return DataTypeRulesVariant.ItemOfCollection;
235+
case "numberRange":
236+
return DataTypeRulesVariant.NumberRange;
237+
case "regex":
238+
return DataTypeRulesVariant.Regex;
239+
case "inputTypes":
240+
return DataTypeRulesVariant.InputType;
241+
case "returnType":
242+
return DataTypeRulesVariant.ReturnType;
243+
case "parentType":
244+
return DataTypeRulesVariant.ParentType;
245+
default:
246+
throw new Error(`Unknown rule: ${rule}`);
247+
}
248+
}
249+
250+
function getDataTypeVariant(variant: DefinitionDataType_Variant): DataTypeVariant {
251+
switch (variant) {
252+
case DefinitionDataType_Variant.ARRAY:
253+
return DataTypeVariant.Array
254+
case DefinitionDataType_Variant.DATATYPE:
255+
return DataTypeVariant.DataType;
256+
case DefinitionDataType_Variant.ERROR:
257+
return DataTypeVariant.Error;
258+
case DefinitionDataType_Variant.NODE:
259+
return DataTypeVariant.Node;
260+
case DefinitionDataType_Variant.OBJECT:
261+
return DataTypeVariant.Object;
262+
case DefinitionDataType_Variant.PRIMITIVE:
263+
return DataTypeVariant.Primitive;
264+
case DefinitionDataType_Variant.TYPE:
265+
return DataTypeVariant.Type;
266+
default:
267+
throw new Error(`Unknown variant: ${variant}`);
268+
}
53269
}

reader/ts/src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {DataType, FlowType, RuntimeFunctionDefinition} from "@code0-tech/sagittarius-graphql-types";
1+
import {DataType, FlowType, FunctionDefinition} from "@code0-tech/sagittarius-graphql-types";
22

33
export enum MetaType {
44
FlowType = 'FlowType',
@@ -16,5 +16,5 @@ export interface Feature {
1616
name: string;
1717
dataTypes: DataType[];
1818
flowTypes: FlowType[];
19-
runtimeFunctions: RuntimeFunctionDefinition[];
19+
runtimeFunctions: FunctionDefinition[];
2020
}

0 commit comments

Comments
 (0)