-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtemplate-processor.js
98 lines (78 loc) · 3.34 KB
/
template-processor.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
// Enhanced class based on cds v5.5.5 @sap/cds/libx/_runtime/common/utils/templateProcessor
const DELIMITER = require("@sap/cds/libx/_runtime/common/utils/templateDelimiter");
const _formatRowContext = (tKey, keyNames, row) => {
const keyValuePairs = keyNames.map((key) => `${key}=${row[key]}`);
const keyValuePairsSerialized = keyValuePairs.join(",");
return `${tKey}(${keyValuePairsSerialized})`;
};
const _processElement = (processFn, row, key, elements, isRoot, pathSegments, picked = {}) => {
const element = elements[key];
const { plain } = picked;
// do not change-track personal data
const isPersonalData = element && Object.keys(element).some(key => key.startsWith('@PersonalData'));
if (plain && !isPersonalData) {
/**
* @type import('../../types/api').templateProcessorProcessFnArgs
*/
const elementInfo = { row, key, element, plain, isRoot, pathSegments };
processFn(elementInfo);
}
};
const _processRow = (processFn, row, template, tKey, tValue, isRoot, pathOptions) => {
const { template: subTemplate, picked } = tValue;
const key = tKey.split(DELIMITER).pop();
const { segments: pathSegments } = pathOptions;
if (!subTemplate && pathSegments) {
pathSegments.push(key);
}
_processElement(processFn, row, key, template.target.elements, isRoot, pathSegments, picked);
// process deep
if (subTemplate) {
let subRows = row && row[key];
subRows = Array.isArray(subRows) ? subRows : [subRows];
// Build entity path
subRows.forEach((subRow) => {
if (subRow && row && row._path) {
/** Enhancement by SME: Support CAP Change Histroy
* Construct path from root entity to current entity.
*/
const serviceNodeName = template.target.elements[key].target;
subRow._path = `${row._path}/${serviceNodeName}(${subRow.ID})`;
}
});
_processComplex(processFn, subRows, subTemplate, key, pathOptions);
}
};
const _processComplex = (processFn, rows, template, tKey, pathOptions) => {
if (rows.length === 0) {
return;
}
const segments = pathOptions.segments;
let keyNames;
for (const row of rows) {
if (row == null) {
continue;
}
const args = { processFn, row, template, isRoot: false, pathOptions };
if (pathOptions.includeKeyValues) {
keyNames = keyNames || (template.target.keys && Object.keys(template.target.keys)) || [];
pathOptions.rowKeysGenerator(keyNames, row, template);
const pathSegment = _formatRowContext(tKey, keyNames, { ...row, ...pathOptions.extraKeys });
args.pathOptions.segments = segments ? [...segments, pathSegment] : [pathSegment];
}
templateProcessor(args);
}
};
/**
* @param {import("../../types/api").TemplateProcessor} args
*/
const templateProcessor = ({ processFn, row, template, isRoot = true, pathOptions = {} }) => {
const segments = pathOptions.segments && [...pathOptions.segments];
for (const [tKey, tValue] of template.elements) {
if (segments) {
pathOptions.segments = [...segments];
}
_processRow(processFn, row, template, tKey, tValue, isRoot, pathOptions);
}
};
module.exports = templateProcessor;