-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildParts.js
360 lines (299 loc) · 9.99 KB
/
buildParts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
const { convertGraphQLToFQL } = require("./graphqlToFQLConverter");
const {
capitalizeFirstLetter,
removeQuotes,
checkBool,
} = require("./helper");
const { operatorMap } = require("./constants");
const FUNCTION_CALL = ' => ';
const PREFIX = 'RE_';
const OBJECT = 'object';
const FACT = 'fact';
const VARIABLE = 'variable';
const CONDITION = 'condition';
const getQueries = (data, queries) => {
if (!data)
return;
for (let index = 0; index < data.length; index++) {
const { all, any } = data[index];
if (all)
getQueries(all, queries);
else if (any)
getQueries(any, queries);
else {
const { source, target } = data[index];
if (source?.type?.toLowerCase() === FACT)
queries.push(source.value);
if (target?.type?.toLowerCase() === FACT)
queries.push(target.value);
}
}
}
const getCorrectOperator = operator => operatorMap[operator.toLowerCase()] || operator;
const getTargetString = (operatorString, target) => {
const { type, value } = target;
if (type.toLowerCase() === FACT)
return operatorString === '=='
? `${convertGraphQLToFQL(value)}`
: `(${convertGraphQLToFQL(value)})`;
else if (type.toLowerCase() === 'string')
return operatorString === '==' ? `"${value}"` : `("${value}")`
return value;
}
const createObjectMap = (data) => {
let topLevelMap = new Map();
let objectName;
let inputObject = {};
let factMap = new Map();
let conditionMap = new Map();
let oldName = Array.from(data.keys())[2].split('.');
oldName.pop();
oldName = oldName.join('.');
for (const [key, value] of data.entries()) {
const keys = key.split('.');
keys.pop();
const updatedAmount = keys.length;
const updatedKey = keys[updatedAmount - 1];
const updatedName = keys.join('.');
const checkName = key.slice(0, updatedName.length);
const valueName = key.slice(updatedName.length + 1, key.length);
if (updatedKey) {
// Create object name
if(updatedKey === 'source') {
if (valueName === 'value') {
objectName = createObjectName(value);
}
inputObject.source = {
...inputObject.source,
[valueName]: value
};
} else if (updatedKey === 'target') {
inputObject.target = {
...inputObject.target,
[valueName]: value
};
} else if (valueName === 'comparator') {
inputObject = {
...inputObject,
[valueName]: value
};
}
// Check if the whole object is set
// To create the sub objects
// And set it to the correct place
if (
inputObject.source?.type
&& inputObject.source?.name
&& inputObject.source?.value
&& 'comparator' in inputObject
&& inputObject.target?.type
&& inputObject.target?.value
) {
factMap = buildFactPart(inputObject.source);
conditionMap = buildConditionPart(factMap, inputObject);
// Add object name to map
// So we can find and replace it later
if (checkName === updatedName) {
let correctName = updatedName.split('.');
correctName.pop();
correctName = correctName.join('.')
topLevelMap.set(correctName, conditionMap);
// Reset object after adding result data
inputObject = {};
}
}
}
// necessary for usage of old/new map
oldName = updatedName !== '' || updatedName !== undefined ? updatedName : oldName;
}
return topLevelMap;
}
function createObjectName(source) {
const sourceString = convertGraphQLToFQL(source);
const collection = sourceString.split('.')[0];
// Create function name - Object
const udfSplit = sourceString.split('.');
udfSplit.pop()
let object = udfSplit.join('.');
let searchParamSplit = object.split('(');
searchParamSplit = searchParamSplit.at(searchParamSplit.length - 1).split(' ');
const searchParams = [];
const variableNames = [];
const fixedValues = [];
searchParamSplit.forEach(searchParamPart => {
if (searchParamPart.includes('.')) {
searchParams.push(capitalizeFirstLetter(searchParamPart.substring(2)));
} else if (searchParamPart.includes('&&')) {
searchParams.push('And');
} else if (searchParamPart.includes('$')) {
let tempName = searchParamPart.replace('$', '');
const tempNameWithoutBrackets = tempName.replace(')', '');
tempName = `${collection}${capitalizeFirstLetter(tempNameWithoutBrackets)}`;
variableNames.push(tempName);
// replace var name in query
// with dynamic generated var name
object = object.replaceAll(`$${tempNameWithoutBrackets}`, tempName);
} else if (searchParamPart.includes('"')) {
fixedValues.push(
searchParamPart.replaceAll('"', '').replaceAll(')', '')
);
}
});
let objectName = `${udfSplit[0]}By`;
let index = 0;
searchParams.forEach(param => {
objectName += param
// If gql contains fixed value
// Add dynamically the value to name
if (fixedValues.length > 0) {
if (index === 0) {
objectName += `-${fixedValues[index++]}`;
} else if (param !== 'And') {
objectName += `-${fixedValues[index++]}`;
}
}
});
objectName = objectName.replace(/ /g, '');
return {object, variableNames, objectName};
}
const buildFactPart = (source) => {
const sourceString = convertGraphQLToFQL(source.value);
let {object, variableNames, objectName} = createObjectName(source.value);
// Create function names - Fact
let factName = sourceString.split('.');
const collectionCapitalized = capitalizeFirstLetter(factName[0]);
const sourceType = capitalizeFirstLetter(factName[factName.length - 1]);
factName = `fact${collectionCapitalized}${sourceType}`;
// Check for variable usage in gql query
let factValue = sourceString.split('.')
factValue = factValue[factValue.length-1]
let fact;
if (!variableNames.length) {
object = `()${FUNCTION_CALL}${object}`;
fact = `()${FUNCTION_CALL}${objectName}().${factValue}`;
} else {
const updatedVariableName = variableNames.join(',');
const updatedObject = object.replaceAll('$', '');
object = `(${updatedVariableName})${FUNCTION_CALL}${updatedObject}`;
fact = `(${updatedVariableName})${FUNCTION_CALL}${objectName}(${updatedVariableName}).${factValue}`;
}
const resultMap = new Map();
resultMap.set(OBJECT, {[objectName]: object});
resultMap.set(FACT, {[factName]: fact});
resultMap.set(VARIABLE, variableNames);
return resultMap;
}
const buildConditionPart = (inputMap, inputObject) => {
let condition;
let conditionName;
const comparatorString = inputObject.comparator;
const source = inputObject.source;
const target = inputObject.target;
const operatorString = getCorrectOperator(comparatorString);
const targetString = getTargetString(operatorString, target);
const objectObject = inputMap.get(OBJECT);
let [objectName] = Object.keys(objectObject);
const factObject = inputMap.get(FACT);
let [factName] = Object.keys(factObject);
const variableNames = inputMap.get(VARIABLE);
const collection = objectName.split('By')[0];
const collectionCapitalized = capitalizeFirstLetter(collection);
const sourceType = capitalizeFirstLetter(source.name);
// Create function names - Condition
const formattedComparator = capitalizeFirstLetter(comparatorString);
const formattedTarget = capitalizeFirstLetter(removeQuotes(targetString));
if (checkBool(target)) {
conditionName = target.value ? `${CONDITION}${collectionCapitalized}Has${sourceType}` : `${CONDITION}${collectionCapitalized}HasNo${sourceType}`;
} else {
conditionName = `${CONDITION}${collectionCapitalized}${sourceType}${formattedComparator}${formattedTarget}`;
}
if (!variableNames.size) {
condition = `()${FUNCTION_CALL}${factName}() ${operatorString} ${targetString}`;
} else {
const updatedVariableName = variableNames.join(',');
condition = `(${updatedVariableName})${FUNCTION_CALL}${factName}(${updatedVariableName}) ${operatorString} ${targetString}`;
}
inputMap.set(CONDITION, {[conditionName]: condition});
return inputMap;
}
const buildRulePart = (inputMap, ruleName) => {
const updatedRuleName = `${PREFIX}Rule${capitalizeFirstLetter(ruleName)}`;
const resultMap = new Map();
resultMap.set(OBJECT, {});
resultMap.set(FACT, {});
resultMap.set(CONDITION, {});
inputMap.forEach((value, key) => {
Object.assign(resultMap.get(OBJECT), value.get(OBJECT));
Object.assign(resultMap.get(FACT), value.get(FACT));
Object.assign(resultMap.get(CONDITION), value.get(CONDITION));
});
const openBracket = '(';
const closeBracket = ')';
const [firstKey] = inputMap.keys();
const [lastKey] = [...inputMap].at(-1);
let oldKey = firstKey;
let updatedKey = firstKey;
let ruleString;
let counterBrackets = 0;
let allUsedVariables = new Set();
let temp;
ruleString = openBracket;
counterBrackets++;
inputMap.forEach((value, key) => {
allUsedVariables.add(value.get(VARIABLE));
// create correct function call
// incl. necessary parameter
temp = Object.keys(value.get(CONDITION));
temp += Object.values(value.get(CONDITION));
temp = temp.substring(0, temp.indexOf(FUNCTION_CALL));
// check for position
if (key === oldKey && key.length === oldKey.length) {
ruleString += temp;
if (updatedKey.startsWith('all')) {
ruleString += ' && ';
} else {
ruleString += ' || ';
}
} else {
// get correct position from key
if (key.length !== oldKey.length) {
updatedKey = key.slice(oldKey.length + 1);
}
// add brackets and operator
// only if it's not the last key
if(key !== lastKey) {
// only add a bracket if it's a new array
if(key.length !== oldKey.length) {
ruleString += openBracket;
counterBrackets++;
}
ruleString += temp;
if (updatedKey.startsWith('all')) {
ruleString += ' && ';
} else {
ruleString += ' || ';
}
} else {
ruleString += temp;
}
}
oldKey = key;
})
// add the correct amount of brackets
const brackets = closeBracket.repeat(counterBrackets);
ruleString += brackets;
const rule = `(${[...allUsedVariables].flat().join(',')})${FUNCTION_CALL}${ruleString}`;
resultMap.set('rule', {[updatedRuleName]: rule});
return resultMap;
}
const createFunction = (functionName, functionBody) => `Function.create({
name: '${functionName}',
body: '${functionBody}'
})`;
module.exports = {
createObjectMap,
buildFactPart,
buildConditionPart,
buildRulePart,
createFunction
}