sf-plugin-data 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +618 -240
- package/docs/artifact-format.md +528 -633
- package/docs/capabilities.md +1160 -168
- package/docs/git-workflow.md +135 -12
- package/docs/handover.md +7826 -12
- package/lib/api.d.ts +13 -0
- package/lib/api.js +10 -0
- package/lib/artifacts/code-fields.d.ts +49 -0
- package/lib/artifacts/code-fields.js +142 -0
- package/lib/artifacts/data-graph-document.d.ts +29 -0
- package/lib/artifacts/data-graph-document.js +52 -0
- package/lib/artifacts/data-space-field.d.ts +65 -0
- package/lib/artifacts/data-space-field.js +98 -0
- package/lib/artifacts/data-stream-document.d.ts +264 -0
- package/lib/artifacts/data-stream-document.js +680 -0
- package/lib/artifacts/data-transform-document.d.ts +152 -0
- package/lib/artifacts/data-transform-document.js +560 -0
- package/lib/artifacts/dependency-selection.d.ts +51 -0
- package/lib/artifacts/dependency-selection.js +154 -0
- package/lib/artifacts/dlo-document.d.ts +93 -0
- package/lib/artifacts/dlo-document.js +253 -0
- package/lib/artifacts/enum-case.d.ts +39 -0
- package/lib/artifacts/enum-case.js +67 -0
- package/lib/artifacts/git-deploy-selection.d.ts +68 -0
- package/lib/artifacts/git-deploy-selection.js +474 -0
- package/lib/artifacts/identity-resolution-document.d.ts +133 -0
- package/lib/artifacts/identity-resolution-document.js +625 -0
- package/lib/artifacts/mapping-document.d.ts +1 -0
- package/lib/artifacts/mapping-document.js +1 -0
- package/lib/artifacts/native-calculated-insight-document.d.ts +142 -0
- package/lib/artifacts/native-calculated-insight-document.js +321 -0
- package/lib/artifacts/native-data-graph-document.d.ts +124 -0
- package/lib/artifacts/native-data-graph-document.js +780 -0
- package/lib/artifacts/native-dmo-document.d.ts +151 -0
- package/lib/artifacts/native-dmo-document.js +313 -0
- package/lib/artifacts/native-mapping-document.d.ts +67 -0
- package/lib/artifacts/native-mapping-document.js +187 -0
- package/lib/artifacts/native-relationship-document.d.ts +76 -0
- package/lib/artifacts/native-relationship-document.js +232 -0
- package/lib/artifacts/path-selector.d.ts +55 -0
- package/lib/artifacts/path-selector.js +167 -0
- package/lib/artifacts/read-deploy-input.d.ts +222 -6
- package/lib/artifacts/read-deploy-input.js +1213 -53
- package/lib/artifacts/segment-document.d.ts +183 -0
- package/lib/artifacts/segment-document.js +570 -0
- package/lib/artifacts/verbatim-document.d.ts +203 -0
- package/lib/artifacts/verbatim-document.js +339 -0
- package/lib/artifacts/write-data-graph.d.ts +17 -0
- package/lib/artifacts/write-data-graph.js +162 -0
- package/lib/artifacts/write-data-stream.d.ts +11 -0
- package/lib/artifacts/write-data-stream.js +175 -0
- package/lib/artifacts/write-data-transform.d.ts +11 -0
- package/lib/artifacts/write-data-transform.js +180 -0
- package/lib/artifacts/write-dlo.d.ts +11 -0
- package/lib/artifacts/write-dlo.js +158 -0
- package/lib/artifacts/write-identity-resolution.d.ts +18 -0
- package/lib/artifacts/write-identity-resolution.js +165 -0
- package/lib/artifacts/write-mapping-definition.d.ts +17 -0
- package/lib/artifacts/write-mapping-definition.js +127 -0
- package/lib/artifacts/write-member-export.d.ts +88 -0
- package/lib/artifacts/write-member-export.js +199 -0
- package/lib/artifacts/write-native-calculated-insight.d.ts +21 -0
- package/lib/artifacts/write-native-calculated-insight.js +176 -0
- package/lib/artifacts/write-native-dmo.d.ts +11 -0
- package/lib/artifacts/write-native-dmo.js +158 -0
- package/lib/artifacts/write-native-mapping.d.ts +11 -0
- package/lib/artifacts/write-native-mapping.js +157 -0
- package/lib/artifacts/write-native-relationship.d.ts +11 -0
- package/lib/artifacts/write-native-relationship.js +157 -0
- package/lib/artifacts/write-segment.d.ts +26 -0
- package/lib/artifacts/write-segment.js +212 -0
- package/lib/commands/data360/delete.d.ts +280 -0
- package/lib/commands/data360/delete.js +2347 -0
- package/lib/commands/data360/deploy.d.ts +235 -16
- package/lib/commands/data360/deploy.js +2339 -102
- package/lib/commands/data360/list/dataspace.d.ts +53 -0
- package/lib/commands/data360/list/dataspace.js +109 -0
- package/lib/commands/data360/publish.d.ts +93 -0
- package/lib/commands/data360/publish.js +298 -0
- package/lib/commands/data360/retrieve.d.ts +283 -14
- package/lib/commands/data360/retrieve.js +1945 -95
- package/lib/commands/data360/segment/members/export.d.ts +101 -0
- package/lib/commands/data360/segment/members/export.js +320 -0
- package/lib/errors/codes/calculated-insight.d.ts +2 -0
- package/lib/errors/codes/calculated-insight.js +25 -0
- package/lib/errors/codes/data-graph.d.ts +2 -0
- package/lib/errors/codes/data-graph.js +22 -0
- package/lib/errors/codes/data-stream.d.ts +2 -0
- package/lib/errors/codes/data-stream.js +70 -0
- package/lib/errors/codes/data-transform.d.ts +2 -0
- package/lib/errors/codes/data-transform.js +35 -0
- package/lib/errors/codes/dependency.d.ts +2 -0
- package/lib/errors/codes/dependency.js +11 -0
- package/lib/errors/codes/dlo.d.ts +2 -0
- package/lib/errors/codes/dlo.js +31 -0
- package/lib/errors/codes/dmo.d.ts +2 -0
- package/lib/errors/codes/dmo.js +28 -0
- package/lib/errors/codes/general.d.ts +2 -0
- package/lib/errors/codes/general.js +58 -0
- package/lib/errors/codes/identity-resolution.d.ts +2 -0
- package/lib/errors/codes/identity-resolution.js +24 -0
- package/lib/errors/codes/mapping.d.ts +2 -0
- package/lib/errors/codes/mapping.js +42 -0
- package/lib/errors/codes/relationship.d.ts +2 -0
- package/lib/errors/codes/relationship.js +21 -0
- package/lib/errors/codes/segment.d.ts +2 -0
- package/lib/errors/codes/segment.js +49 -0
- package/lib/errors/registry.d.ts +60 -0
- package/lib/errors/registry.js +123 -0
- package/lib/exit-code.d.ts +10 -0
- package/lib/exit-code.js +24 -0
- package/lib/plugin.d.ts +1 -0
- package/lib/plugin.js +3 -1
- package/lib/services/acquire-retrieve-selection.d.ts +43 -0
- package/lib/services/acquire-retrieve-selection.js +145 -0
- package/lib/services/bind-identity-resolution-dependencies.d.ts +53 -0
- package/lib/services/bind-identity-resolution-dependencies.js +238 -0
- package/lib/services/bind-segment-dependencies.d.ts +49 -0
- package/lib/services/bind-segment-dependencies.js +115 -0
- package/lib/services/bounded-reads.d.ts +17 -0
- package/lib/services/bounded-reads.js +42 -0
- package/lib/services/calculated-insight-sql-checks.d.ts +33 -0
- package/lib/services/calculated-insight-sql-checks.js +193 -0
- package/lib/services/command-dlo.d.ts +48 -0
- package/lib/services/command-dlo.js +103 -0
- package/lib/services/command-native-data-graph.d.ts +54 -0
- package/lib/services/command-native-data-graph.js +485 -0
- package/lib/services/command-native-data-stream.d.ts +104 -0
- package/lib/services/command-native-data-stream.js +257 -0
- package/lib/services/command-native-dmo.d.ts +124 -0
- package/lib/services/command-native-dmo.js +338 -0
- package/lib/services/command-native-mapping.d.ts +65 -0
- package/lib/services/command-native-mapping.js +129 -0
- package/lib/services/command-native-relationship.d.ts +46 -0
- package/lib/services/command-native-relationship.js +103 -0
- package/lib/services/cross-dataspace-deploy.d.ts +111 -0
- package/lib/services/cross-dataspace-deploy.js +493 -0
- package/lib/services/cross-dataspace-rename.d.ts +74 -0
- package/lib/services/cross-dataspace-rename.js +1314 -0
- package/lib/services/cross-dataspace-sql.d.ts +109 -0
- package/lib/services/cross-dataspace-sql.js +1301 -0
- package/lib/services/cross-space-context.d.ts +23 -0
- package/lib/services/cross-space-context.js +38 -0
- package/lib/services/crud-plan.d.ts +137 -0
- package/lib/services/crud-plan.js +408 -0
- package/lib/services/data-space-check.d.ts +92 -0
- package/lib/services/data-space-check.js +169 -0
- package/lib/services/data-space-prefix.d.ts +77 -0
- package/lib/services/data-space-prefix.js +127 -0
- package/lib/services/data-transform-space.d.ts +31 -0
- package/lib/services/data-transform-space.js +79 -0
- package/lib/services/delete-calculated-insight.d.ts +44 -0
- package/lib/services/delete-calculated-insight.js +101 -0
- package/lib/services/delete-data-transform.d.ts +52 -0
- package/lib/services/delete-data-transform.js +222 -0
- package/lib/services/delete-dmo-field.d.ts +211 -0
- package/lib/services/delete-dmo-field.js +472 -0
- package/lib/services/delete-identity-resolution.d.ts +43 -0
- package/lib/services/delete-identity-resolution.js +295 -0
- package/lib/services/delete-segment.d.ts +42 -0
- package/lib/services/delete-segment.js +248 -0
- package/lib/services/deploy-result.d.ts +54 -0
- package/lib/services/deploy-result.js +60 -0
- package/lib/services/escaped-copy.d.ts +23 -0
- package/lib/services/escaped-copy.js +88 -0
- package/lib/services/execute-calculated-insight-deploy.d.ts +42 -0
- package/lib/services/execute-calculated-insight-deploy.js +96 -0
- package/lib/services/execute-data-transform-deploy.d.ts +56 -0
- package/lib/services/execute-data-transform-deploy.js +168 -0
- package/lib/services/execute-native-identity-resolution-deploy.d.ts +48 -0
- package/lib/services/execute-native-identity-resolution-deploy.js +360 -0
- package/lib/services/execute-segment-deploy.d.ts +45 -0
- package/lib/services/execute-segment-deploy.js +393 -0
- package/lib/services/list-data-spaces.d.ts +17 -0
- package/lib/services/list-data-spaces.js +24 -0
- package/lib/services/local-file-removal.d.ts +36 -0
- package/lib/services/local-file-removal.js +103 -0
- package/lib/services/native-calculated-insight.d.ts +87 -0
- package/lib/services/native-calculated-insight.js +164 -0
- package/lib/services/native-crm-data-stream.d.ts +91 -0
- package/lib/services/native-crm-data-stream.js +272 -0
- package/lib/services/native-data-graph-context.d.ts +12 -0
- package/lib/services/native-data-graph-context.js +139 -0
- package/lib/services/native-data-graph-crd.d.ts +261 -0
- package/lib/services/native-data-graph-crd.js +866 -0
- package/lib/services/native-data-stream.d.ts +329 -0
- package/lib/services/native-data-stream.js +1830 -0
- package/lib/services/native-dlo.d.ts +163 -0
- package/lib/services/native-dlo.js +402 -0
- package/lib/services/native-dmo.d.ts +191 -0
- package/lib/services/native-dmo.js +614 -0
- package/lib/services/native-mapping.d.ts +255 -0
- package/lib/services/native-mapping.js +1226 -0
- package/lib/services/native-relationship.d.ts +132 -0
- package/lib/services/native-relationship.js +512 -0
- package/lib/services/operation-diagnostic.d.ts +83 -0
- package/lib/services/operation-diagnostic.js +272 -0
- package/lib/services/preflight-calculated-insight-deploy.d.ts +58 -0
- package/lib/services/preflight-calculated-insight-deploy.js +95 -0
- package/lib/services/preflight-data-transform-deploy.d.ts +33 -0
- package/lib/services/preflight-data-transform-deploy.js +104 -0
- package/lib/services/preflight-identity-resolution-deploy.d.ts +107 -0
- package/lib/services/preflight-identity-resolution-deploy.js +281 -0
- package/lib/services/preflight-segment-deploy.d.ts +31 -0
- package/lib/services/preflight-segment-deploy.js +109 -0
- package/lib/services/publish-calculated-insight.d.ts +40 -0
- package/lib/services/publish-calculated-insight.js +95 -0
- package/lib/services/publish-segment.d.ts +65 -0
- package/lib/services/publish-segment.js +194 -0
- package/lib/services/readback-pace.d.ts +16 -0
- package/lib/services/readback-pace.js +23 -0
- package/lib/services/retrieve-data-transform.d.ts +26 -0
- package/lib/services/retrieve-data-transform.js +65 -0
- package/lib/services/retrieve-identity-resolution.d.ts +40 -0
- package/lib/services/retrieve-identity-resolution.js +87 -0
- package/lib/services/retrieve-inventory.d.ts +32 -0
- package/lib/services/retrieve-inventory.js +151 -0
- package/lib/services/retrieve-segment.d.ts +120 -0
- package/lib/services/retrieve-segment.js +502 -0
- package/lib/services/run-retrieve.d.ts +24 -0
- package/lib/services/run-retrieve.js +51 -0
- package/lib/services/segment-lookup.d.ts +46 -0
- package/lib/services/segment-lookup.js +108 -0
- package/lib/services/segment-members.d.ts +144 -0
- package/lib/services/segment-members.js +402 -0
- package/lib/services/select-native-retrieve.d.ts +60 -0
- package/lib/services/select-native-retrieve.js +226 -0
- package/lib/services/settle-calculated-insight.d.ts +22 -0
- package/lib/services/settle-calculated-insight.js +53 -0
- package/lib/services/sql-tokens.d.ts +58 -0
- package/lib/services/sql-tokens.js +147 -0
- package/lib/transport/api-version.d.ts +36 -0
- package/lib/transport/api-version.js +89 -0
- package/lib/transport/bound-ssot.d.ts +27 -0
- package/lib/transport/bound-ssot.js +116 -0
- package/lib/transport/calculated-insight-rest.d.ts +28 -0
- package/lib/transport/calculated-insight-rest.js +62 -0
- package/lib/transport/connect-client.d.ts +94 -32
- package/lib/transport/connect-client.js +318 -178
- package/lib/transport/connect-diagnostic.d.ts +89 -0
- package/lib/transport/connect-diagnostic.js +331 -0
- package/lib/transport/data-graph-rest.d.ts +27 -0
- package/lib/transport/data-graph-rest.js +76 -0
- package/lib/transport/data-stream-rest.d.ts +62 -0
- package/lib/transport/data-stream-rest.js +348 -0
- package/lib/transport/data-transform-rest.d.ts +25 -0
- package/lib/transport/data-transform-rest.js +100 -0
- package/lib/transport/dlo-native-rest.d.ts +22 -0
- package/lib/transport/dlo-native-rest.js +89 -0
- package/lib/transport/dlo-rest.d.ts +23 -0
- package/lib/transport/dlo-rest.js +135 -0
- package/lib/transport/dmo-native-rest.d.ts +18 -0
- package/lib/transport/dmo-native-rest.js +55 -0
- package/lib/transport/dmo-rest.d.ts +30 -0
- package/lib/transport/dmo-rest.js +163 -0
- package/lib/transport/mapping-rest.d.ts +63 -0
- package/lib/transport/mapping-rest.js +341 -0
- package/lib/transport/project-metadata.d.ts +36 -0
- package/lib/transport/project-metadata.js +118 -0
- package/lib/transport/relationship-rest.d.ts +43 -0
- package/lib/transport/relationship-rest.js +201 -0
- package/package.json +29 -6
- package/lib/artifacts/dmo-document.d.ts +0 -181
- package/lib/artifacts/dmo-document.js +0 -848
- package/lib/artifacts/dmo-update-delta.d.ts +0 -28
- package/lib/artifacts/dmo-update-delta.js +0 -86
- package/lib/artifacts/write-other-dmo.d.ts +0 -17
- package/lib/artifacts/write-other-dmo.js +0 -140
- package/lib/services/execute-dmo-create.d.ts +0 -24
- package/lib/services/execute-dmo-create.js +0 -118
- package/lib/services/execute-dmo-deploy.d.ts +0 -33
- package/lib/services/execute-dmo-deploy.js +0 -197
- package/lib/services/preflight-dmo-deploy.d.ts +0 -69
- package/lib/services/preflight-dmo-deploy.js +0 -161
- package/lib/services/retrieve-other-dmo.d.ts +0 -46
- package/lib/services/retrieve-other-dmo.js +0 -140
|
@@ -0,0 +1,1314 @@
|
|
|
1
|
+
import { unescapeSalesforceHtml } from '../artifacts/native-calculated-insight-document.js';
|
|
2
|
+
import { htmlEntityOutsideTexts, translateSql, translateSqlExpression, } from './cross-dataspace-sql.js';
|
|
3
|
+
import { placeDmo, translateFieldName, translateRulesetName, translateSegmentName, translateUniqueName, } from 'salesforce-data360-rules';
|
|
4
|
+
export { pairedByRulesets, placeDmo, translateDmoName, translateFieldName, translateRulesetName, translateSegmentName, translateUniqueName, } from 'salesforce-data360-rules';
|
|
5
|
+
const dmoSuffix = '__dlm';
|
|
6
|
+
const nameValue = /^[A-Za-z][A-Za-z0-9_]*__(?:c|dlm)$/u;
|
|
7
|
+
// A field named with the qualifier of a join side (`R.FirstName__c`).
|
|
8
|
+
const qualifiedField = /^([A-Za-z][A-Za-z0-9_]*)\.([A-Za-z][A-Za-z0-9_]*__c)$/u;
|
|
9
|
+
/** Collects the rewrites and unplaced names of one file. */
|
|
10
|
+
class Recorder {
|
|
11
|
+
context;
|
|
12
|
+
rewrites = [];
|
|
13
|
+
unresolved = [];
|
|
14
|
+
/** Every JSON path the translation looked at, changed or kept on purpose. */
|
|
15
|
+
visited = new Set();
|
|
16
|
+
/** SQL texts, checked token by token by their own translation (not by the full-coverage check). */
|
|
17
|
+
ownCheck = new Set();
|
|
18
|
+
constructor(context) {
|
|
19
|
+
this.context = context;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Mark a value as looked at and kept as it is.
|
|
23
|
+
* @param {string} at JSON path.
|
|
24
|
+
*/
|
|
25
|
+
keep(at) {
|
|
26
|
+
this.visited.add(at);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Mark every value below a path as looked at and kept (compared values are data).
|
|
30
|
+
* @param {unknown} value Value.
|
|
31
|
+
* @param {string} at Its JSON path.
|
|
32
|
+
*/
|
|
33
|
+
keepAll(value, at) {
|
|
34
|
+
this.visited.add(at);
|
|
35
|
+
if (Array.isArray(value))
|
|
36
|
+
for (const [index, entry] of value.entries())
|
|
37
|
+
this.keepAll(entry, `${at}[${index}]`);
|
|
38
|
+
else if (value && typeof value === 'object')
|
|
39
|
+
for (const [key, entry] of Object.entries(value))
|
|
40
|
+
this.keepAll(entry, `${at}.${key}`);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Record a name the translation leaves out (generated for one instance).
|
|
44
|
+
* @param {Record<string, unknown>} node Object holding the key, changed in place.
|
|
45
|
+
* @param {string} key Key to remove.
|
|
46
|
+
* @param {string} at JSON path of the key.
|
|
47
|
+
*/
|
|
48
|
+
leaveOut(node, key, at) {
|
|
49
|
+
if (!Object.hasOwn(node, key))
|
|
50
|
+
return;
|
|
51
|
+
const value = node[key];
|
|
52
|
+
delete node[key];
|
|
53
|
+
if (typeof value === 'string' && value)
|
|
54
|
+
this.rewrites.push({ at, from: value, to: '(left out)', rule: 'generated' });
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Place a name by a rule function, recording the rewrite or the unplaced name.
|
|
58
|
+
* @param {string} at JSON path.
|
|
59
|
+
* @param {string} name Source name.
|
|
60
|
+
* @param {(context: CrossSpaceContext, name: string) => Placed | NotPlaced} rule Rule.
|
|
61
|
+
* @returns {string} Target name (the source name when it is not placed).
|
|
62
|
+
*/
|
|
63
|
+
named(at, name, rule) {
|
|
64
|
+
const placed = rule(this.context, name);
|
|
65
|
+
if ('reason' in placed) {
|
|
66
|
+
this.visited.add(at);
|
|
67
|
+
this.unresolved.push({ at, name, reason: placed.reason });
|
|
68
|
+
return name;
|
|
69
|
+
}
|
|
70
|
+
return this.change(at, name, placed.name, placed.rule);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Record a value change.
|
|
74
|
+
* @param {string} at JSON path.
|
|
75
|
+
* @param {string} from Old value.
|
|
76
|
+
* @param {string} to New value.
|
|
77
|
+
* @param {CrossSpaceRule} [rule] Rule; none means unchanged.
|
|
78
|
+
* @returns {string} The new value.
|
|
79
|
+
*/
|
|
80
|
+
change(at, from, to, rule) {
|
|
81
|
+
this.visited.add(at);
|
|
82
|
+
if (rule !== undefined && from !== to)
|
|
83
|
+
this.rewrites.push({ at, from, to, rule });
|
|
84
|
+
return to;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Place a DMO name, recording the rewrite or the unplaced name.
|
|
88
|
+
* @param {string} at JSON path.
|
|
89
|
+
* @param {string} name Source name.
|
|
90
|
+
* @returns {Placed | undefined} Placement, or undefined when not placed.
|
|
91
|
+
*/
|
|
92
|
+
dmo(at, name) {
|
|
93
|
+
const placed = placeDmo(this.context, name);
|
|
94
|
+
this.visited.add(at);
|
|
95
|
+
if ('reason' in placed) {
|
|
96
|
+
this.unresolved.push({ at, name, reason: placed.reason });
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
this.change(at, name, placed.name, placed.rule);
|
|
100
|
+
return placed;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Place a field name of a placed DMO.
|
|
104
|
+
* @param {string} at JSON path.
|
|
105
|
+
* @param {Placed | undefined} owner The placed DMO, or undefined when it was not placed.
|
|
106
|
+
* @param {string} field Source field name.
|
|
107
|
+
* @returns {string} Target field name (the source name when its DMO was not placed).
|
|
108
|
+
*/
|
|
109
|
+
field(at, owner, field) {
|
|
110
|
+
this.visited.add(at);
|
|
111
|
+
if (!owner)
|
|
112
|
+
return field;
|
|
113
|
+
const placed = translateFieldName(this.context, owner.kind, field);
|
|
114
|
+
return this.change(at, field, placed.name, placed.rule);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Rewrite one SQL text by the rules (see translateSql), recording every change and every name
|
|
118
|
+
* it cannot place with its file, line and column.
|
|
119
|
+
* @param {string} at JSON path of the SQL.
|
|
120
|
+
* @param {string} sql SQL text.
|
|
121
|
+
* @param {string} file The SQL file it is read from, as positions are reported.
|
|
122
|
+
* @param {string} item What the SQL belongs to, as a refusal names it (`data transform`).
|
|
123
|
+
* @param {ReadonlyMap<string, {name: string; rule?: CrossSpaceRule}>} [outputs] When the columns
|
|
124
|
+
* the SQL puts out land in the fields of its output object by name (a DCSQL node): each field of
|
|
125
|
+
* that object (source name, lowercase) with its target name and rule. Each output column is then
|
|
126
|
+
* checked against its field (see translateSql), and `*` over a table that is not a DLO is not
|
|
127
|
+
* placed. For an object whose fields are not listed (an intermediate model's relation), the
|
|
128
|
+
* field of each name by the object's rule and whether those fields can change.
|
|
129
|
+
* @returns {string} The rewritten SQL.
|
|
130
|
+
*/
|
|
131
|
+
sql(at, sql, file, item, outputs) {
|
|
132
|
+
this.visited.add(at);
|
|
133
|
+
this.ownCheck.add(at);
|
|
134
|
+
const listed = outputs instanceof Map ? outputs : undefined;
|
|
135
|
+
const outputField = listed
|
|
136
|
+
? (name) => listed.get(name.toLowerCase())
|
|
137
|
+
: outputs && 'field' in outputs
|
|
138
|
+
? outputs.field
|
|
139
|
+
: undefined;
|
|
140
|
+
const result = translateSql(sql, file, sqlNamer(this.context), outputField, item);
|
|
141
|
+
this.rewrites.push(...result.rewrites);
|
|
142
|
+
this.unresolved.push(...result.unresolved);
|
|
143
|
+
if (outputs && result.outputs) {
|
|
144
|
+
const fieldsChange = listed
|
|
145
|
+
? [...listed].some(([source, field]) => field.name.toLowerCase() !== source)
|
|
146
|
+
: 'fieldsChange' in outputs && outputs.fieldsChange;
|
|
147
|
+
for (const star of result.outputs.stars)
|
|
148
|
+
if (!star.dlo || fieldsChange)
|
|
149
|
+
this.unresolved.push({
|
|
150
|
+
at: star.at,
|
|
151
|
+
name: star.name,
|
|
152
|
+
reason: star.dlo
|
|
153
|
+
? "this * puts out the columns of a DLO, which keep their names while the output object's fields change in the target data space; list the columns with AS and the output field name"
|
|
154
|
+
: 'this * puts out the columns of a table that is not a DLO, whose names can change in the target data space while the output object keeps its fields; list the columns, each with AS and the output field name',
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
return result.sql;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Rewrite one SQL expression of an STL node (a formula, a SQL filter) by the node's lineage. A
|
|
161
|
+
* value as an older retrieve wrote it, HTML-escaped as the GET returns it (`"`, `'`:
|
|
162
|
+
* an entity outside texts), is read unescaped; a value that mixes both forms is not placed. The
|
|
163
|
+
* result is always written plain: Salesforce stores what it is sent and never decodes entities.
|
|
164
|
+
* Line and column positions refer to the unescaped expression.
|
|
165
|
+
* @param {string} at JSON path of the expression.
|
|
166
|
+
* @param {string} text Expression as the file holds it.
|
|
167
|
+
* @param {Lineage} lineage Whose columns the node reads without a qualifier.
|
|
168
|
+
* @param {ReadonlyMap<string, Lineage>} [qualifiers] The qualifiers of the join above the node.
|
|
169
|
+
* @returns {string} The rewritten expression, plain.
|
|
170
|
+
*/
|
|
171
|
+
expression(at, text, lineage, qualifiers) {
|
|
172
|
+
this.visited.add(at);
|
|
173
|
+
const escaped = htmlEntityOutsideTexts(text) !== undefined;
|
|
174
|
+
const plain = escaped ? unescapeSalesforceHtml(text) : text;
|
|
175
|
+
if (escaped && escapeSalesforceHtml(plain) !== text) {
|
|
176
|
+
this.unresolved.push({
|
|
177
|
+
at,
|
|
178
|
+
name: text,
|
|
179
|
+
reason: 'the expression mixes HTML-escaped and plain characters, so it cannot be decoded exactly; write it plain',
|
|
180
|
+
});
|
|
181
|
+
return text;
|
|
182
|
+
}
|
|
183
|
+
const base = sqlNamer(this.context);
|
|
184
|
+
const namer = {
|
|
185
|
+
leftover: base.leftover,
|
|
186
|
+
spelling: base.spelling,
|
|
187
|
+
column: (field, qualifier) => {
|
|
188
|
+
if (qualifier === undefined)
|
|
189
|
+
return this.placeLineage(lineage, field);
|
|
190
|
+
if (qualifiers?.has(qualifier))
|
|
191
|
+
return this.placeLineage(qualifiers.get(qualifier), field);
|
|
192
|
+
return {
|
|
193
|
+
reason: `the qualifier ${qualifier} names no side of the join this expression reads from, so the object of its field is not known and its spelling cannot be placed`,
|
|
194
|
+
};
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
const result = translateSqlExpression(plain, at, namer);
|
|
198
|
+
this.rewrites.push(...result.rewrites);
|
|
199
|
+
this.unresolved.push(...result.unresolved);
|
|
200
|
+
return result.sql;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Place a segment name.
|
|
204
|
+
* @param {string} at JSON path.
|
|
205
|
+
* @param {string} name Source name.
|
|
206
|
+
* @returns {string} Target name (the source name when it is not placed).
|
|
207
|
+
*/
|
|
208
|
+
segment(at, name) {
|
|
209
|
+
return this.named(at, name, translateSegmentName);
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Place a field whose DMO may be unknown (a transform node after a join): towards another data
|
|
213
|
+
* space every field drops `ssot__` whatever its DMO; otherwise the field must keep its name in
|
|
214
|
+
* this direction whatever its DMO, or it is not placed.
|
|
215
|
+
* A qualified field (`R.FirstName__c`, the qualifier of a join side) keeps its qualifier.
|
|
216
|
+
* @param {string} at JSON path.
|
|
217
|
+
* @param {Placed | 'dlo' | undefined} owner The placed DMO, `dlo` for a DLO, or undefined when unknown.
|
|
218
|
+
* @param {string} field Source field name (without the qualifier).
|
|
219
|
+
* @param {string} [qualifier] The join side qualifier written before the field.
|
|
220
|
+
* @returns {string} Target value.
|
|
221
|
+
*/
|
|
222
|
+
lineageField(at, owner, field, qualifier) {
|
|
223
|
+
const placed = this.placeLineage(owner, field);
|
|
224
|
+
const whole = qualifier === undefined ? field : `${qualifier}.${field}`;
|
|
225
|
+
if ('reason' in placed) {
|
|
226
|
+
this.visited.add(at);
|
|
227
|
+
this.unresolved.push({ at, name: whole, reason: placed.reason });
|
|
228
|
+
return whole;
|
|
229
|
+
}
|
|
230
|
+
return this.change(at, whole, qualifier === undefined ? placed.name : `${qualifier}.${placed.name}`, placed.rule);
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* The target spelling of a field by its lineage, without recording it.
|
|
234
|
+
* @param {Placed | 'dlo' | undefined} owner The placed DMO, `dlo` for a DLO, or undefined when unknown.
|
|
235
|
+
* @param {string} field Source field name.
|
|
236
|
+
* @returns {{name: string; rule?: CrossSpaceRule} | NotPlaced} Target field name, or why not.
|
|
237
|
+
*/
|
|
238
|
+
placeLineage(owner, field) {
|
|
239
|
+
if (owner === 'dlo')
|
|
240
|
+
return { name: field };
|
|
241
|
+
if (owner)
|
|
242
|
+
return translateFieldName(this.context, owner.kind, field);
|
|
243
|
+
if (this.context.source === 'default')
|
|
244
|
+
return translateFieldName(this.context, 'custom', field);
|
|
245
|
+
if (!fieldNeverChanges(this.context, field))
|
|
246
|
+
return {
|
|
247
|
+
reason: 'the object this field belongs to is not known here, so its spelling cannot be placed',
|
|
248
|
+
};
|
|
249
|
+
return { name: field };
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* HTML-escape a text the way Connect REST returns every string without the plain-text request
|
|
254
|
+
* header (`"`, `'`, `\`), as older plugin versions wrote STL expressions into files.
|
|
255
|
+
* @param {string} text Plain text.
|
|
256
|
+
* @returns {string} Escaped text.
|
|
257
|
+
*/
|
|
258
|
+
function escapeSalesforceHtml(text) {
|
|
259
|
+
return text
|
|
260
|
+
.replaceAll('&', '&')
|
|
261
|
+
.replaceAll('<', '<')
|
|
262
|
+
.replaceAll('>', '>')
|
|
263
|
+
.replaceAll('"', '"')
|
|
264
|
+
.replaceAll("'", ''')
|
|
265
|
+
.replaceAll('\\', '\');
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Whether a field name keeps its spelling for every kind of DMO in this direction, so it can be
|
|
269
|
+
* placed without knowing its DMO.
|
|
270
|
+
* @param {CrossSpaceContext} context Translation context.
|
|
271
|
+
* @param {string} field Field name.
|
|
272
|
+
* @returns {boolean} Whether it never changes.
|
|
273
|
+
*/
|
|
274
|
+
function fieldNeverChanges(context, field) {
|
|
275
|
+
if (field.startsWith('KQ_'))
|
|
276
|
+
return true;
|
|
277
|
+
if (context.source === 'default')
|
|
278
|
+
return !field.startsWith('ssot__');
|
|
279
|
+
return context.target !== 'default';
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Translate segment criteria: every `objectApiName` by the DMO rule and every `fieldApiName` by
|
|
283
|
+
* the field rule of the DMO named beside it (or above it). Any other DMO or field name in the
|
|
284
|
+
* criteria is placed the same way; a field name without a DMO to place it by is not placed,
|
|
285
|
+
* unless it keeps its name in this direction whatever its DMO. Compared values (`values`,
|
|
286
|
+
* `value`) are data and stay as they are.
|
|
287
|
+
* @param {unknown} value Criteria node.
|
|
288
|
+
* @param {string} at JSON path of the node.
|
|
289
|
+
* @param {Placed | undefined} owner The nearest DMO above, placed.
|
|
290
|
+
* @param {Recorder} recorder Collects changes.
|
|
291
|
+
* @param {CrossSpaceContext} context Translation context.
|
|
292
|
+
* @returns {unknown} Translated node.
|
|
293
|
+
*/
|
|
294
|
+
function translateCriteria(value, at, owner, recorder, context) {
|
|
295
|
+
if (Array.isArray(value))
|
|
296
|
+
return value.map((entry, index) => translateCriteria(entry, `${at}[${index}]`, owner, recorder, context));
|
|
297
|
+
if (!value || typeof value !== 'object') {
|
|
298
|
+
if (typeof value !== 'string' || !nameValue.test(value))
|
|
299
|
+
return value;
|
|
300
|
+
if (value.endsWith(dmoSuffix))
|
|
301
|
+
return recorder.dmo(at, value)?.name ?? value;
|
|
302
|
+
if (owner)
|
|
303
|
+
return recorder.field(at, owner, value);
|
|
304
|
+
if (!fieldNeverChanges(context, value))
|
|
305
|
+
recorder.unresolved.push({
|
|
306
|
+
at,
|
|
307
|
+
name: value,
|
|
308
|
+
reason: 'no DMO is named beside or above this field, so its spelling cannot be placed',
|
|
309
|
+
});
|
|
310
|
+
return value;
|
|
311
|
+
}
|
|
312
|
+
const node = value;
|
|
313
|
+
const object = node.objectApiName;
|
|
314
|
+
let here = owner;
|
|
315
|
+
const result = {};
|
|
316
|
+
if (typeof object === 'string') {
|
|
317
|
+
const placed = recorder.dmo(`${at}.objectApiName`, object);
|
|
318
|
+
here = placed;
|
|
319
|
+
// A DMO that could not be placed: its fields are not placed either (already refused).
|
|
320
|
+
if (!placed)
|
|
321
|
+
here = undefined;
|
|
322
|
+
}
|
|
323
|
+
for (const [key, child] of Object.entries(node)) {
|
|
324
|
+
const path = `${at}.${key}`;
|
|
325
|
+
if (key === 'objectApiName' && typeof child === 'string') {
|
|
326
|
+
result[key] = here?.name ?? child;
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
// Compared values are data, not names.
|
|
330
|
+
if (key === 'values' || key === 'value') {
|
|
331
|
+
recorder.keepAll(child, path);
|
|
332
|
+
result[key] = child;
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
// The DMO beside it was not placed (already refused): its field is left as it is.
|
|
336
|
+
if (key === 'fieldApiName' && typeof object === 'string' && !here) {
|
|
337
|
+
result[key] = child;
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
result[key] = translateCriteria(child, path, here, recorder, context);
|
|
341
|
+
}
|
|
342
|
+
return result;
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* STL node actions the translation can place: loads, joins and outputs, and the expression nodes
|
|
346
|
+
* whose shapes are known (live API 68, DB entry data360:dataspace-ci-spelling-stl-nodes-api68).
|
|
347
|
+
*/
|
|
348
|
+
const translatableNodes = new Set([
|
|
349
|
+
'load',
|
|
350
|
+
'outputD360',
|
|
351
|
+
'join',
|
|
352
|
+
'formula',
|
|
353
|
+
'filter',
|
|
354
|
+
'sqlFilter',
|
|
355
|
+
'aggregate',
|
|
356
|
+
'appendV2',
|
|
357
|
+
]);
|
|
358
|
+
/**
|
|
359
|
+
* The definitions of a transform file: its `definition`, or each entry of `definitions`.
|
|
360
|
+
* @param {Record<string, unknown>} document Transform file.
|
|
361
|
+
* @returns {Array<Record<string, unknown>>} Definitions.
|
|
362
|
+
*/
|
|
363
|
+
function transformDefinitions(document) {
|
|
364
|
+
const input = document.definition;
|
|
365
|
+
if (Array.isArray(input.definitions))
|
|
366
|
+
return input.definitions;
|
|
367
|
+
return [input.definition];
|
|
368
|
+
}
|
|
369
|
+
/** The keys of a DCSQL manifest node that hold SQL text. */
|
|
370
|
+
const dcsqlSqlKeys = ['compiled_code', 'raw_code'];
|
|
371
|
+
/**
|
|
372
|
+
* The model nodes of a DCSQL definition (`manifest.nodes`), or undefined when it has none.
|
|
373
|
+
* @param {Record<string, unknown>} definition DCSQL definition.
|
|
374
|
+
* @returns {Record<string, Record<string, unknown>> | undefined} Nodes by key.
|
|
375
|
+
*/
|
|
376
|
+
function dcsqlNodes(definition) {
|
|
377
|
+
const manifest = definition.manifest;
|
|
378
|
+
const nodes = manifest?.nodes;
|
|
379
|
+
if (!nodes || typeof nodes !== 'object' || Array.isArray(nodes))
|
|
380
|
+
return undefined;
|
|
381
|
+
return nodes;
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Why a DCSQL definition cannot be translated: no model nodes, or a node without SQL text.
|
|
385
|
+
* @param {Record<string, unknown>} definition DCSQL definition.
|
|
386
|
+
* @returns {string | undefined} Reason.
|
|
387
|
+
*/
|
|
388
|
+
function dcsqlUnsupported(definition) {
|
|
389
|
+
const nodes = dcsqlNodes(definition);
|
|
390
|
+
if (!nodes || Object.keys(nodes).length === 0)
|
|
391
|
+
return 'the DCSQL definition holds no manifest nodes the translation can read';
|
|
392
|
+
for (const [key, node] of Object.entries(nodes)) {
|
|
393
|
+
if (!node || typeof node !== 'object' || Array.isArray(node))
|
|
394
|
+
return `the DCSQL manifest node ${key} is not an object the translation can read`;
|
|
395
|
+
if (typeof node.compiled_code !== 'string')
|
|
396
|
+
return `the DCSQL manifest node ${key} holds no SQL in compiled_code`;
|
|
397
|
+
for (const sqlKey of dcsqlSqlKeys)
|
|
398
|
+
if (node[sqlKey] !== undefined && typeof node[sqlKey] !== 'string')
|
|
399
|
+
return `the DCSQL manifest node ${key} holds a ${sqlKey} that is not SQL text`;
|
|
400
|
+
}
|
|
401
|
+
return undefined;
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Why a transform file cannot be translated yet: neither STL nor DCSQL, a DCSQL definition without
|
|
405
|
+
* SQL to read, or an STL node whose action the translation does not know.
|
|
406
|
+
* @param {Record<string, unknown>} document Transform file.
|
|
407
|
+
* @returns {string | undefined} Reason.
|
|
408
|
+
*/
|
|
409
|
+
function transformUnsupported(document) {
|
|
410
|
+
for (const definition of transformDefinitions(document)) {
|
|
411
|
+
const type = definition?.type;
|
|
412
|
+
if (type === 'DCSQL') {
|
|
413
|
+
const reason = dcsqlUnsupported(definition);
|
|
414
|
+
if (reason !== undefined)
|
|
415
|
+
return reason;
|
|
416
|
+
continue;
|
|
417
|
+
}
|
|
418
|
+
if (type !== 'STL')
|
|
419
|
+
return `data transforms of type ${String(type)} cannot be translated; only STL and DCSQL transforms can`;
|
|
420
|
+
const nodes = definition.nodes;
|
|
421
|
+
if (!nodes || typeof nodes !== 'object' || Array.isArray(nodes))
|
|
422
|
+
return 'the STL definition holds no nodes the translation can read';
|
|
423
|
+
for (const [key, node] of Object.entries(nodes)) {
|
|
424
|
+
const action = node?.action;
|
|
425
|
+
if (typeof action !== 'string' || !translatableNodes.has(action))
|
|
426
|
+
return `the STL node ${key} (${String(action)}) is of a kind the translation does not know; only load, join, formula, filter, sqlFilter, aggregate, appendV2 and output nodes can be translated`;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
return undefined;
|
|
430
|
+
}
|
|
431
|
+
/** Segment types whose names the translation can place: criteria, child segments or SQL models. */
|
|
432
|
+
const supportedSegmentTypes = new Set(['UI', 'Waterfall', 'DBT']);
|
|
433
|
+
/**
|
|
434
|
+
* Why a file of this kind cannot be translated yet, or undefined when it can.
|
|
435
|
+
* @param {Record<string, unknown>} document Admitted file.
|
|
436
|
+
* @returns {string | undefined} Reason.
|
|
437
|
+
*/
|
|
438
|
+
export function crossSpaceUnsupported(document) {
|
|
439
|
+
switch (document.kind) {
|
|
440
|
+
case 'dmo':
|
|
441
|
+
case 'mapping':
|
|
442
|
+
case 'relationship':
|
|
443
|
+
case 'dlo': {
|
|
444
|
+
return undefined;
|
|
445
|
+
}
|
|
446
|
+
case 'segment': {
|
|
447
|
+
const type = document.spec?.segmentType;
|
|
448
|
+
return typeof type === 'string' && supportedSegmentTypes.has(type)
|
|
449
|
+
? undefined
|
|
450
|
+
: `segments of type ${String(type)} cannot be translated; only UI, Waterfall and SQL (DBT) segments can`;
|
|
451
|
+
}
|
|
452
|
+
case 'native-data-graph':
|
|
453
|
+
case 'identity-resolution': {
|
|
454
|
+
return undefined;
|
|
455
|
+
}
|
|
456
|
+
case 'data-transform': {
|
|
457
|
+
return transformUnsupported(document);
|
|
458
|
+
}
|
|
459
|
+
case 'calculated-insight': {
|
|
460
|
+
return undefined;
|
|
461
|
+
}
|
|
462
|
+
case 'data-stream': {
|
|
463
|
+
return 'data streams cannot be deployed into another data space with this option yet';
|
|
464
|
+
}
|
|
465
|
+
default: {
|
|
466
|
+
return `files of kind ${String(document.kind)} cannot be translated into another data space`;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* The DMOs of a non-default data space that a selected DMO file of that same data space shows to
|
|
472
|
+
* be custom: a field with creationType Custom and none with Standard (as retrieve writes them). A
|
|
473
|
+
* DMO file of another data space is no evidence.
|
|
474
|
+
* @param {ReadonlyArray<Record<string, unknown>>} documents Admitted files.
|
|
475
|
+
* @param {string} source The data space of the files being translated.
|
|
476
|
+
* @returns {Set<string>} DMO names.
|
|
477
|
+
*/
|
|
478
|
+
export function customDmosOf(documents, source) {
|
|
479
|
+
const names = new Set();
|
|
480
|
+
for (const document of documents) {
|
|
481
|
+
if (document.kind !== 'dmo' || document.c__dataSpace !== source)
|
|
482
|
+
continue;
|
|
483
|
+
const fields = document.spec?.fields;
|
|
484
|
+
const name = document.identity?.apiName;
|
|
485
|
+
if (!Array.isArray(fields) || typeof name !== 'string')
|
|
486
|
+
continue;
|
|
487
|
+
const types = new Set(fields.map((field) => field?.creationType));
|
|
488
|
+
if (types.has('Custom') && !types.has('Standard'))
|
|
489
|
+
names.add(name);
|
|
490
|
+
}
|
|
491
|
+
return names;
|
|
492
|
+
}
|
|
493
|
+
/** Graph-level names Salesforce generates for one graph instance (helper DMOs, mapping, transform). */
|
|
494
|
+
const graphGeneratedKeys = [
|
|
495
|
+
'idDmoName',
|
|
496
|
+
'valuesDmoName',
|
|
497
|
+
'dayZeroMappingDevName',
|
|
498
|
+
'dayZeroTransformDevName',
|
|
499
|
+
'incrementalMappingDevName',
|
|
500
|
+
'incrementalTransformDevName',
|
|
501
|
+
];
|
|
502
|
+
/**
|
|
503
|
+
* Translate one data graph node and the nodes below it: its object, joins, fields, criteria and
|
|
504
|
+
* top-K order. The node's JSON path is rebuilt from the translated object names, and its developer
|
|
505
|
+
* name as Salesforce builds it, `<graph>_<object without __dlm>` (live API 68: a create replaces
|
|
506
|
+
* an authored one with that, and a retrieve reads it back so); other generated names (fragment
|
|
507
|
+
* DMO, join and field developer names) and the reference name Salesforce sets are left out.
|
|
508
|
+
* @param {Record<string, unknown>} node Node, changed in place.
|
|
509
|
+
* @param {string} at JSON path of the node.
|
|
510
|
+
* @param {Placed | undefined} parent The parent node's placed DMO.
|
|
511
|
+
* @param {string | undefined} parentJsonPath The parent's translated jsonPath (undefined for the root).
|
|
512
|
+
* @param {string | undefined} graph The graph's target name, or undefined when it was not placed.
|
|
513
|
+
* @param {Recorder} recorder Collects changes.
|
|
514
|
+
* @param {CrossSpaceContext} context Translation context.
|
|
515
|
+
* @returns {Placed | undefined} The node's placed DMO.
|
|
516
|
+
*/
|
|
517
|
+
function translateGraphNode(node, at, parent, parentJsonPath, graph, recorder, context) {
|
|
518
|
+
const owner = recorder.dmo(`${at}.name`, String(node.name));
|
|
519
|
+
if (owner)
|
|
520
|
+
node.name = owner.name;
|
|
521
|
+
if (typeof node.projectedName === 'string')
|
|
522
|
+
node.projectedName =
|
|
523
|
+
recorder.dmo(`${at}.projectedName`, node.projectedName)?.name ?? node.projectedName;
|
|
524
|
+
const jsonPath = parentJsonPath === undefined ? '$' : `${parentJsonPath}.${String(node.name)}`;
|
|
525
|
+
if (typeof node.jsonPath === 'string')
|
|
526
|
+
node.jsonPath = recorder.change(`${at}.jsonPath`, node.jsonPath, jsonPath, owner?.rule);
|
|
527
|
+
if (typeof node.devName === 'string') {
|
|
528
|
+
// Not placed (its object or the graph name is already refused): it is kept as it is.
|
|
529
|
+
if (owner && graph !== undefined)
|
|
530
|
+
node.devName = recorder.change(`${at}.devName`, node.devName, `${graph}_${owner.name.slice(0, -dmoSuffix.length)}`, 'generated');
|
|
531
|
+
else
|
|
532
|
+
recorder.keep(`${at}.devName`);
|
|
533
|
+
}
|
|
534
|
+
recorder.leaveOut(node, 'fragmentDMOName', `${at}.fragmentDMOName`);
|
|
535
|
+
recorder.leaveOut(node, 'referenceDeveloperName', `${at}.referenceDeveloperName`);
|
|
536
|
+
for (const [index, field] of (node.fields ?? []).entries()) {
|
|
537
|
+
const fieldAt = `${at}.fields[${index}]`;
|
|
538
|
+
recorder.leaveOut(field, 'devName', `${fieldAt}.devName`);
|
|
539
|
+
for (const key of ['sourceFieldName', 'keyQualifierName'])
|
|
540
|
+
if (typeof field[key] === 'string')
|
|
541
|
+
field[key] = recorder.field(`${fieldAt}.${key}`, owner, field[key]);
|
|
542
|
+
}
|
|
543
|
+
for (const [index, join] of (node.path ?? []).entries()) {
|
|
544
|
+
const joinAt = `${at}.path[${index}]`;
|
|
545
|
+
recorder.leaveOut(join, 'devName', `${joinAt}.devName`);
|
|
546
|
+
if (typeof join.fieldName === 'string')
|
|
547
|
+
join.fieldName = recorder.field(`${joinAt}.fieldName`, owner, join.fieldName);
|
|
548
|
+
if (typeof join.parentFieldName === 'string')
|
|
549
|
+
join.parentFieldName = recorder.field(`${joinAt}.parentFieldName`, parent, join.parentFieldName);
|
|
550
|
+
}
|
|
551
|
+
for (const key of ['recencyCriteria', 'filterCriteria'])
|
|
552
|
+
if (node[key] !== undefined && node[key] !== null)
|
|
553
|
+
node[key] = translateCriteria(node[key], `${at}.${key}`, owner, recorder, context);
|
|
554
|
+
const top = node.topKFilterCriteria;
|
|
555
|
+
if (top && Array.isArray(top.orderCriteria))
|
|
556
|
+
for (const [index, order] of top.orderCriteria.entries())
|
|
557
|
+
if (typeof order.fieldApiName === 'string')
|
|
558
|
+
order.fieldApiName = recorder.field(`${at}.topKFilterCriteria.orderCriteria[${index}].fieldApiName`, owner, order.fieldApiName);
|
|
559
|
+
for (const [index, child] of (node.relatedObjects ?? []).entries())
|
|
560
|
+
translateGraphNode(child, `${at}.relatedObjects[${index}]`, owner, jsonPath, graph, recorder, context);
|
|
561
|
+
return owner;
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Translate a data graph file: its name (prefix rule), data space, root and every node below it.
|
|
565
|
+
* The graph-level generated names are left out.
|
|
566
|
+
* @param {Record<string, unknown>} copy Graph file copy, changed in place.
|
|
567
|
+
* @param {Recorder} recorder Collects changes.
|
|
568
|
+
* @param {CrossSpaceContext} context Translation context.
|
|
569
|
+
*/
|
|
570
|
+
function translateGraph(copy, recorder, context) {
|
|
571
|
+
const definition = copy.definition;
|
|
572
|
+
const graph = translateUniqueName(context, String(definition.name));
|
|
573
|
+
definition.name = recorder.named('definition.name', String(definition.name), translateUniqueName);
|
|
574
|
+
if (typeof definition.dataspaceName === 'string')
|
|
575
|
+
definition.dataspaceName = recorder.change('definition.dataspaceName', definition.dataspaceName, context.target, 'data-space');
|
|
576
|
+
for (const key of graphGeneratedKeys)
|
|
577
|
+
recorder.leaveOut(definition, key, `definition.${key}`);
|
|
578
|
+
const root = definition.sourceObject;
|
|
579
|
+
const placed = translateGraphNode(root, 'definition.sourceObject', undefined, undefined, 'reason' in graph ? undefined : graph.name, recorder, context);
|
|
580
|
+
recorder.change('definition.primaryObjectName', String(definition.primaryObjectName), String(root.name), placed?.rule);
|
|
581
|
+
definition.primaryObjectName = root.name;
|
|
582
|
+
}
|
|
583
|
+
/**
|
|
584
|
+
* Place one field reference of a ruleset (`{parentApiName, apiName}`): its DMO, then its field.
|
|
585
|
+
* @param {Record<string, unknown>} field Field reference, changed in place.
|
|
586
|
+
* @param {string} at JSON path.
|
|
587
|
+
* @param {Recorder} recorder Collects changes.
|
|
588
|
+
*/
|
|
589
|
+
function translateRulesetField(field, at, recorder) {
|
|
590
|
+
const parent = recorder.dmo(`${at}.parentApiName`, String(field.parentApiName));
|
|
591
|
+
if (parent)
|
|
592
|
+
field.parentApiName = parent.name;
|
|
593
|
+
field.apiName = recorder.field(`${at}.apiName`, parent, String(field.apiName));
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* Mark the DLO sources of a reconciliation setting as kept: a DLO keeps its name in every data space.
|
|
597
|
+
* @param {Record<string, unknown>} setting Reconciliation rule or field override.
|
|
598
|
+
* @param {string} at JSON path.
|
|
599
|
+
* @param {Recorder} recorder Collects changes.
|
|
600
|
+
*/
|
|
601
|
+
function keepSources(setting, at, recorder) {
|
|
602
|
+
for (const [index] of (setting.sources ?? []).entries())
|
|
603
|
+
recorder.keep(`${at}.sources[${index}].apiName`);
|
|
604
|
+
}
|
|
605
|
+
/**
|
|
606
|
+
* Translate an identity resolution ruleset file: its name (`<prefix>_<type>_<rulesetId>`, the
|
|
607
|
+
* ruleset ID kept), secondary DMO, match and reconciliation rules and the owner and entity of
|
|
608
|
+
* each output by the standard DMO rules. DLO sources keep their names.
|
|
609
|
+
* @param {Record<string, unknown>} copy Ruleset file copy, changed in place.
|
|
610
|
+
* @param {Recorder} recorder Collects changes.
|
|
611
|
+
*/
|
|
612
|
+
function translateRuleset(copy, recorder) {
|
|
613
|
+
const identity = copy.identity;
|
|
614
|
+
const from = identity.apiName;
|
|
615
|
+
identity.apiName = recorder.named('identity.apiName', from, translateRulesetName);
|
|
616
|
+
const spec = copy.spec;
|
|
617
|
+
const secondary = spec.secondaryDmo;
|
|
618
|
+
secondary.apiName =
|
|
619
|
+
recorder.dmo('spec.secondaryDmo.apiName', secondary.apiName)?.name ?? secondary.apiName;
|
|
620
|
+
for (const [ruleIndex, rule] of (spec.matchRules ?? []).entries())
|
|
621
|
+
for (const [index, criterion] of (rule.criteria ?? []).entries())
|
|
622
|
+
translateRulesetField(criterion.field, `spec.matchRules[${ruleIndex}].criteria[${index}].field`, recorder);
|
|
623
|
+
for (const [ruleIndex, rule] of (spec.reconciliationRules ?? []).entries()) {
|
|
624
|
+
const at = `spec.reconciliationRules[${ruleIndex}]`;
|
|
625
|
+
const entity = rule.entity;
|
|
626
|
+
entity.apiName = recorder.dmo(`${at}.entity.apiName`, entity.apiName)?.name ?? entity.apiName;
|
|
627
|
+
keepSources(rule, at, recorder);
|
|
628
|
+
for (const [index, override] of (rule.fields ?? []).entries()) {
|
|
629
|
+
translateRulesetField(override.field, `${at}.fields[${index}].field`, recorder);
|
|
630
|
+
keepSources(override, `${at}.fields[${index}]`, recorder);
|
|
631
|
+
}
|
|
632
|
+
for (const [index, output] of (rule.outputs ?? []).entries()) {
|
|
633
|
+
const owner = output.owner;
|
|
634
|
+
owner.apiName = recorder.change(`${at}.outputs[${index}].owner.apiName`, owner.apiName, owner.apiName === from ? identity.apiName : owner.apiName, 'name-prefix');
|
|
635
|
+
const outputEntity = output.entity;
|
|
636
|
+
outputEntity.apiName =
|
|
637
|
+
recorder.dmo(`${at}.outputs[${index}].entity.apiName`, outputEntity.apiName)?.name ??
|
|
638
|
+
outputEntity.apiName;
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* Place a DLO or DMO name of a transform node: a DLO keeps its name in every data space.
|
|
644
|
+
* @param {string} at JSON path.
|
|
645
|
+
* @param {string} name Object name.
|
|
646
|
+
* @param {Recorder} recorder Collects changes.
|
|
647
|
+
* @returns {{name: string; lineage: Lineage}} Target name and lineage.
|
|
648
|
+
*/
|
|
649
|
+
function transformObject(at, name, recorder) {
|
|
650
|
+
if (name.endsWith('__dll')) {
|
|
651
|
+
recorder.keep(at);
|
|
652
|
+
return { name, lineage: 'dlo' };
|
|
653
|
+
}
|
|
654
|
+
const placed = recorder.dmo(at, name);
|
|
655
|
+
return { name: placed?.name ?? name, lineage: placed };
|
|
656
|
+
}
|
|
657
|
+
/**
|
|
658
|
+
* Translate every field name in a value (a string or a list of them) by its lineage. A field
|
|
659
|
+
* written with a join side's qualifier (`R.FirstName__c`) is placed by that side's lineage; a
|
|
660
|
+
* qualifier that names no side of the join the value comes from is not placed.
|
|
661
|
+
* @param {unknown} value Field name or list.
|
|
662
|
+
* @param {string} at JSON path.
|
|
663
|
+
* @param {Lineage} lineage Whose fields they are.
|
|
664
|
+
* @param {Recorder} recorder Collects changes.
|
|
665
|
+
* @param {Map<string, Lineage>} [qualifiers] The qualifiers of the join the value comes from.
|
|
666
|
+
* @returns {unknown} Translated value.
|
|
667
|
+
*/
|
|
668
|
+
function lineageFields(value, at, lineage, recorder, qualifiers) {
|
|
669
|
+
if (typeof value === 'string') {
|
|
670
|
+
if (nameValue.test(value))
|
|
671
|
+
return recorder.lineageField(at, lineage, value);
|
|
672
|
+
const qualified = qualifiedField.exec(value);
|
|
673
|
+
if (!qualified)
|
|
674
|
+
return value;
|
|
675
|
+
const [, qualifier, field] = qualified;
|
|
676
|
+
if (qualifiers?.has(qualifier))
|
|
677
|
+
return recorder.lineageField(at, qualifiers.get(qualifier), field, qualifier);
|
|
678
|
+
recorder.visited.add(at);
|
|
679
|
+
recorder.unresolved.push({
|
|
680
|
+
at,
|
|
681
|
+
name: value,
|
|
682
|
+
reason: `the qualifier ${qualifier} names no side of the join this value comes from, so the object of its field is not known and its spelling cannot be placed`,
|
|
683
|
+
});
|
|
684
|
+
return value;
|
|
685
|
+
}
|
|
686
|
+
if (Array.isArray(value))
|
|
687
|
+
return value.map((entry, index) => lineageFields(entry, `${at}[${index}]`, lineage, recorder, qualifiers));
|
|
688
|
+
return value;
|
|
689
|
+
}
|
|
690
|
+
/**
|
|
691
|
+
* Place the name of a column a node adds (a formula field, an aggregation). It is a user name, but
|
|
692
|
+
* every later node reads it by the lineage rule of its node (see lineageFields), so it takes that
|
|
693
|
+
* spelling too: from `default` an `ssot__` name drops it like any field, a plain name stays. Where
|
|
694
|
+
* the lineage rule would give a plain new name a spelling of the source object's own fields (an
|
|
695
|
+
* identity resolution output towards `default` gets `ssot__`), the new name is not placed: it is
|
|
696
|
+
* not a field of that object, and keeping it would leave the later references apart from it.
|
|
697
|
+
* @param {unknown} value Column name.
|
|
698
|
+
* @param {string} at JSON path.
|
|
699
|
+
* @param {Lineage} input Lineage of the node's one source.
|
|
700
|
+
* @param {Recorder} recorder Collects changes.
|
|
701
|
+
* @param {CrossSpaceContext} context Translation context.
|
|
702
|
+
* @returns {unknown} Target name.
|
|
703
|
+
*/
|
|
704
|
+
function addedColumn(value, at, input, recorder, context) {
|
|
705
|
+
if (typeof value !== 'string' || !nameValue.test(value))
|
|
706
|
+
return lineageFields(value, at, input, recorder);
|
|
707
|
+
const placed = recorder.placeLineage(input, value);
|
|
708
|
+
const own = translateFieldName(context, 'custom', value);
|
|
709
|
+
if (!('reason' in placed) && placed.name !== own.name) {
|
|
710
|
+
recorder.visited.add(at);
|
|
711
|
+
recorder.unresolved.push({
|
|
712
|
+
at,
|
|
713
|
+
name: value,
|
|
714
|
+
reason: `this node adds the column ${value}, and the later nodes read it by the rule of the object the rows come from, which would spell it ${placed.name}, a field of that object it is not; deploy this transform in its own data space`,
|
|
715
|
+
});
|
|
716
|
+
return value;
|
|
717
|
+
}
|
|
718
|
+
return recorder.lineageField(at, input, value);
|
|
719
|
+
}
|
|
720
|
+
/**
|
|
721
|
+
* Translate the names of a formula, filter, sqlFilter or aggregate node by the lineage of its one
|
|
722
|
+
* source: whole values (filter fields, aggregate sources and groupings) as fields, SQL text
|
|
723
|
+
* (formula and SQL filter expressions) name by name. The columns a node adds (formula field
|
|
724
|
+
* names, aggregation names) are user names: see addedColumn.
|
|
725
|
+
* @param {unknown} action Node action.
|
|
726
|
+
* @param {Record<string, unknown>} parameters Node parameters, changed in place.
|
|
727
|
+
* @param {string} at JSON path of the parameters.
|
|
728
|
+
* @param {Lineage} input Lineage of the node's one source; undefined after a join or with several.
|
|
729
|
+
* @param {ReadonlyMap<string, Lineage>} [qualifiers] The qualifiers of the join above the node.
|
|
730
|
+
* @param {Recorder} recorder Collects changes.
|
|
731
|
+
* @param {CrossSpaceContext} context Translation context.
|
|
732
|
+
*/
|
|
733
|
+
function translateExpressionNode(action, parameters, at, input, qualifiers, recorder, context) {
|
|
734
|
+
const entries = (key) => Array.isArray(parameters[key]) ? parameters[key] : [];
|
|
735
|
+
switch (action) {
|
|
736
|
+
case 'formula': {
|
|
737
|
+
const sql = parameters.expressionType === 'SQL';
|
|
738
|
+
for (const [index, field] of entries('fields').entries()) {
|
|
739
|
+
const fieldAt = `${at}.fields[${index}]`;
|
|
740
|
+
field.name = addedColumn(field.name, `${fieldAt}.name`, input, recorder, context);
|
|
741
|
+
if (typeof field.formulaExpression !== 'string')
|
|
742
|
+
continue;
|
|
743
|
+
if (sql)
|
|
744
|
+
field.formulaExpression = recorder.expression(`${fieldAt}.formulaExpression`, field.formulaExpression, input, qualifiers);
|
|
745
|
+
else {
|
|
746
|
+
recorder.visited.add(`${fieldAt}.formulaExpression`);
|
|
747
|
+
recorder.unresolved.push({
|
|
748
|
+
at: `${fieldAt}.formulaExpression`,
|
|
749
|
+
name: field.formulaExpression,
|
|
750
|
+
reason: `formula expressions of type ${String(parameters.expressionType)} cannot be translated; only SQL expressions can`,
|
|
751
|
+
});
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
return;
|
|
755
|
+
}
|
|
756
|
+
case 'filter': {
|
|
757
|
+
for (const [index, expression] of entries('filterExpressions').entries())
|
|
758
|
+
expression.field = lineageFields(expression.field, `${at}.filterExpressions[${index}].field`, input, recorder, qualifiers);
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
761
|
+
case 'sqlFilter': {
|
|
762
|
+
if (typeof parameters.sqlFilterExpression === 'string')
|
|
763
|
+
parameters.sqlFilterExpression = recorder.expression(`${at}.sqlFilterExpression`, parameters.sqlFilterExpression, input, qualifiers);
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
766
|
+
case 'aggregate': {
|
|
767
|
+
for (const [index, aggregation] of entries('aggregations').entries()) {
|
|
768
|
+
aggregation.source = lineageFields(aggregation.source, `${at}.aggregations[${index}].source`, input, recorder, qualifiers);
|
|
769
|
+
aggregation.name = addedColumn(aggregation.name, `${at}.aggregations[${index}].name`, input, recorder, context);
|
|
770
|
+
}
|
|
771
|
+
parameters.groupings = lineageFields(parameters.groupings, `${at}.groupings`, input, recorder, qualifiers);
|
|
772
|
+
return;
|
|
773
|
+
}
|
|
774
|
+
default: {
|
|
775
|
+
return;
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
/**
|
|
780
|
+
* Translate one STL definition: load and output objects and fields, output field mappings, join
|
|
781
|
+
* keys and the sample data space, the names in formula, filter, sqlFilter, aggregate and appendV2
|
|
782
|
+
* nodes; output object definitions by their name.
|
|
783
|
+
* @param {Record<string, unknown>} definition STL definition, changed in place.
|
|
784
|
+
* @param {string} at JSON path.
|
|
785
|
+
* @param {Recorder} recorder Collects changes.
|
|
786
|
+
* @param {CrossSpaceContext} context Translation context.
|
|
787
|
+
*/
|
|
788
|
+
function translateStl(definition, at, recorder, context) {
|
|
789
|
+
// The STL definition repeats the transform's name.
|
|
790
|
+
if (typeof definition.name === 'string')
|
|
791
|
+
definition.name = recorder.named(`${at}.name`, definition.name, translateUniqueName);
|
|
792
|
+
const nodes = definition.nodes;
|
|
793
|
+
const lineage = new Map();
|
|
794
|
+
const sourcesOf = (node) => Array.isArray(node.sources) ? node.sources : [];
|
|
795
|
+
for (const [key, node] of Object.entries(nodes)) {
|
|
796
|
+
if (node.action !== 'load')
|
|
797
|
+
continue;
|
|
798
|
+
const nodeAt = `${at}.nodes.${key}.parameters`;
|
|
799
|
+
const parameters = node.parameters;
|
|
800
|
+
const dataset = parameters.dataset;
|
|
801
|
+
const object = typeof dataset?.name === 'string'
|
|
802
|
+
? transformObject(`${nodeAt}.dataset.name`, dataset.name, recorder)
|
|
803
|
+
: undefined;
|
|
804
|
+
if (dataset && object)
|
|
805
|
+
dataset.name = object.name;
|
|
806
|
+
lineage.set(key, object?.lineage);
|
|
807
|
+
parameters.fields = lineageFields(parameters.fields, `${nodeAt}.fields`, object?.lineage, recorder);
|
|
808
|
+
const sample = parameters.sampleDetails;
|
|
809
|
+
if (sample && typeof sample.dataspace === 'string')
|
|
810
|
+
sample.dataspace = recorder.change(`${nodeAt}.sampleDetails.dataspace`, sample.dataspace, context.target, 'data-space');
|
|
811
|
+
if (sample && sample.sortBy !== undefined)
|
|
812
|
+
sample.sortBy = lineageFields(sample.sortBy, `${nodeAt}.sampleDetails.sortBy`, object?.lineage, recorder);
|
|
813
|
+
}
|
|
814
|
+
// A node takes the lineage of its one source. A join or append mixes its sources: its columns
|
|
815
|
+
// keep one spelling only when every source is a DLO, or every source a DMO of the same kind
|
|
816
|
+
// (the field rule reads nothing else); otherwise its lineage is unknown. Each node's lineage is
|
|
817
|
+
// worked out once (the loads are all placed above); a result reached through a cycle is not kept.
|
|
818
|
+
const memo = new Map();
|
|
819
|
+
let cycles = 0;
|
|
820
|
+
const lineageOf = (key, seen = new Set()) => {
|
|
821
|
+
if (lineage.has(key))
|
|
822
|
+
return lineage.get(key);
|
|
823
|
+
if (memo.has(key))
|
|
824
|
+
return memo.get(key);
|
|
825
|
+
const node = nodes[key];
|
|
826
|
+
if (!node)
|
|
827
|
+
return undefined;
|
|
828
|
+
if (seen.has(key)) {
|
|
829
|
+
cycles++;
|
|
830
|
+
return undefined;
|
|
831
|
+
}
|
|
832
|
+
seen.add(key);
|
|
833
|
+
const before = cycles;
|
|
834
|
+
const sides = sourcesOf(node).map((source) => lineageOf(source, new Set(seen)));
|
|
835
|
+
const [first] = sides;
|
|
836
|
+
const same = first !== undefined &&
|
|
837
|
+
sides.every((side) => side === 'dlo' || first === 'dlo' ? side === first : side?.kind === first.kind);
|
|
838
|
+
const result = same ? first : undefined;
|
|
839
|
+
if (cycles === before)
|
|
840
|
+
memo.set(key, result);
|
|
841
|
+
return result;
|
|
842
|
+
};
|
|
843
|
+
// The qualifiers of the nearest join above a node (`leftQualifier`, `rightQualifier`), each with
|
|
844
|
+
// the lineage of its side.
|
|
845
|
+
const qualifiersOf = (key, seen = new Set()) => {
|
|
846
|
+
const node = nodes[key];
|
|
847
|
+
if (!node || seen.has(key))
|
|
848
|
+
return undefined;
|
|
849
|
+
seen.add(key);
|
|
850
|
+
const sources = sourcesOf(node);
|
|
851
|
+
if (node.action !== 'join')
|
|
852
|
+
return sources.length === 1 ? qualifiersOf(sources[0], seen) : undefined;
|
|
853
|
+
const parameters = (node.parameters ?? {});
|
|
854
|
+
const found = new Map();
|
|
855
|
+
for (const [side, name] of ['leftQualifier', 'rightQualifier'].entries()) {
|
|
856
|
+
const qualifier = parameters[name];
|
|
857
|
+
if (typeof qualifier === 'string' && qualifier)
|
|
858
|
+
found.set(qualifier, lineageOf(sources[side] ?? ''));
|
|
859
|
+
}
|
|
860
|
+
return found;
|
|
861
|
+
};
|
|
862
|
+
for (const [key, node] of Object.entries(nodes)) {
|
|
863
|
+
const nodeAt = `${at}.nodes.${key}.parameters`;
|
|
864
|
+
const parameters = (node.parameters ?? {});
|
|
865
|
+
const sources = sourcesOf(node);
|
|
866
|
+
if (node.action === 'join')
|
|
867
|
+
for (const [name, value] of Object.entries(parameters)) {
|
|
868
|
+
const side = /left/iu.test(name) ? 0 : /right/iu.test(name) ? 1 : undefined;
|
|
869
|
+
parameters[name] = lineageFields(value, `${nodeAt}.${name}`, side === undefined ? undefined : lineageOf(sources[side] ?? ''), recorder, side === undefined ? undefined : qualifiersOf(sources[side] ?? ''));
|
|
870
|
+
}
|
|
871
|
+
const input = sources.length === 1 ? lineageOf(sources[0]) : undefined;
|
|
872
|
+
const qualifiers = sources.length === 1 ? qualifiersOf(sources[0]) : undefined;
|
|
873
|
+
if (node.action === 'appendV2') {
|
|
874
|
+
for (const [index, pair] of (parameters.fieldMappings ?? []).entries())
|
|
875
|
+
for (const [side, name] of ['top', 'bottom'].entries())
|
|
876
|
+
pair[name] = lineageFields(pair[name], `${nodeAt}.fieldMappings[${index}].${name}`, lineageOf(sources[side] ?? ''), recorder, qualifiersOf(sources[side] ?? ''));
|
|
877
|
+
continue;
|
|
878
|
+
}
|
|
879
|
+
if (node.action !== 'outputD360') {
|
|
880
|
+
translateExpressionNode(node.action, parameters, nodeAt, input, qualifiers, recorder, context);
|
|
881
|
+
continue;
|
|
882
|
+
}
|
|
883
|
+
const output = typeof parameters.name === 'string'
|
|
884
|
+
? transformObject(`${nodeAt}.name`, parameters.name, recorder)
|
|
885
|
+
: undefined;
|
|
886
|
+
if (output)
|
|
887
|
+
parameters.name = output.name;
|
|
888
|
+
for (const [index, pair] of (parameters.fieldsMappings ?? []).entries()) {
|
|
889
|
+
pair.sourceField = lineageFields(pair.sourceField, `${nodeAt}.fieldsMappings[${index}].sourceField`, input, recorder, qualifiers);
|
|
890
|
+
pair.targetField = lineageFields(pair.targetField, `${nodeAt}.fieldsMappings[${index}].targetField`, output?.lineage, recorder);
|
|
891
|
+
}
|
|
892
|
+
if (parameters.dedupOrder !== undefined)
|
|
893
|
+
parameters.dedupOrder = lineageFields(parameters.dedupOrder, `${nodeAt}.dedupOrder`, output?.lineage, recorder);
|
|
894
|
+
}
|
|
895
|
+
translateOutputObjects(definition, at, recorder, context);
|
|
896
|
+
}
|
|
897
|
+
/**
|
|
898
|
+
* Translate the output object definitions of a transform definition by their name: a DLO keeps
|
|
899
|
+
* its name and fields, a DMO is placed with its fields; a `dataspaceName` names the target.
|
|
900
|
+
* @param {Record<string, unknown>} definition Transform definition, changed in place.
|
|
901
|
+
* @param {string} at JSON path of the definition.
|
|
902
|
+
* @param {Recorder} recorder Collects changes.
|
|
903
|
+
* @param {CrossSpaceContext} context Translation context.
|
|
904
|
+
* @returns {Map<string, Map<string, {name: string; rule?: CrossSpaceRule}>>} Per output object
|
|
905
|
+
* (source name, lowercase) each field (source name, lowercase) with its target name and rule.
|
|
906
|
+
*/
|
|
907
|
+
function translateOutputObjects(definition, at, recorder, context) {
|
|
908
|
+
const objects = new Map();
|
|
909
|
+
for (const [index, object] of (definition.outputDataObjects ?? []).entries()) {
|
|
910
|
+
const objectAt = `${at}.outputDataObjects[${index}]`;
|
|
911
|
+
const fields = new Map();
|
|
912
|
+
if (typeof object.name === 'string')
|
|
913
|
+
objects.set(object.name.toLowerCase(), fields);
|
|
914
|
+
const placed = typeof object.name === 'string'
|
|
915
|
+
? transformObject(`${objectAt}.name`, object.name, recorder)
|
|
916
|
+
: undefined;
|
|
917
|
+
if (placed)
|
|
918
|
+
object.name = placed.name;
|
|
919
|
+
if (typeof object.dataspaceName === 'string')
|
|
920
|
+
object.dataspaceName = recorder.change(`${objectAt}.dataspaceName`, object.dataspaceName, context.target, 'data-space');
|
|
921
|
+
for (const [fieldIndex, field] of (object.fields ?? []).entries()) {
|
|
922
|
+
const source = field.name;
|
|
923
|
+
field.name = lineageFields(field.name, `${objectAt}.fields[${fieldIndex}].name`, placed?.lineage, recorder);
|
|
924
|
+
if (typeof source === 'string' && typeof field.name === 'string') {
|
|
925
|
+
const rule = recorder.rewrites.find((entry) => entry.at === `${objectAt}.fields[${fieldIndex}].name`)?.rule;
|
|
926
|
+
fields.set(source.toLowerCase(), { name: field.name, ...(rule ? { rule } : {}) });
|
|
927
|
+
}
|
|
928
|
+
for (const key of ['keyQualifierField', 'keyQualifierName'])
|
|
929
|
+
if (typeof field[key] === 'string')
|
|
930
|
+
recorder.keep(`${objectAt}.fields[${fieldIndex}].${key}`);
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
return objects;
|
|
934
|
+
}
|
|
935
|
+
/**
|
|
936
|
+
* Translate one DCSQL definition: its name, the output objects (see translateOutputObjects), and
|
|
937
|
+
* per manifest node the SQL (`compiled_code`, and `raw_code` when present) by the SQL rules and
|
|
938
|
+
* the output object (`relation_name`) by its name. The columns a node's SQL puts out land by name
|
|
939
|
+
* in the fields of the output object its `relation_name` names, so each is checked against that
|
|
940
|
+
* object's fields (see translateSql). A node whose `relation_name` names no output object of the
|
|
941
|
+
* definition is an intermediate model: its `relation_name` follows the object rules, and its
|
|
942
|
+
* columns are checked against the fields of that relation by its own rule (other nodes read them
|
|
943
|
+
* so). A node without a `relation_name` is not placed. The
|
|
944
|
+
* node's `name` is kept, its `config` checked (see dcsqlConfig); anything else name-like is left
|
|
945
|
+
* to the full-coverage check.
|
|
946
|
+
* @param {Record<string, unknown>} definition DCSQL definition, changed in place.
|
|
947
|
+
* @param {string} at JSON path.
|
|
948
|
+
* @param {Recorder} recorder Collects changes.
|
|
949
|
+
* @param {CrossSpaceContext} context Translation context.
|
|
950
|
+
*/
|
|
951
|
+
function translateDcsql(definition, at, recorder, context) {
|
|
952
|
+
if (typeof definition.name === 'string')
|
|
953
|
+
definition.name = recorder.named(`${at}.name`, definition.name, translateUniqueName);
|
|
954
|
+
const objects = translateOutputObjects(definition, at, recorder, context);
|
|
955
|
+
for (const [key, node] of Object.entries(dcsqlNodes(definition) ?? {})) {
|
|
956
|
+
const nodeAt = `${at}.manifest.nodes.${key}`;
|
|
957
|
+
const relation = typeof node.relation_name === 'string' ? node.relation_name : undefined;
|
|
958
|
+
const placedRelation = relation === undefined
|
|
959
|
+
? undefined
|
|
960
|
+
: transformObject(`${nodeAt}.relation_name`, relation, recorder);
|
|
961
|
+
// A relation_name that names no output object is an intermediate model: its columns land by
|
|
962
|
+
// name in that relation, whose fields other nodes read by the relation's own rule, so each
|
|
963
|
+
// column is checked against that rule (a DLO or custom DMO relation keeps every name).
|
|
964
|
+
const lineage = placedRelation?.lineage;
|
|
965
|
+
const fields = relation === undefined
|
|
966
|
+
? new Map()
|
|
967
|
+
: (objects.get(relation.toLowerCase()) ?? {
|
|
968
|
+
field: (name) => {
|
|
969
|
+
const placed = recorder.placeLineage(lineage, name);
|
|
970
|
+
return 'reason' in placed ? undefined : placed;
|
|
971
|
+
},
|
|
972
|
+
fieldsChange: lineage !== 'dlo' && lineage?.kind !== 'custom',
|
|
973
|
+
});
|
|
974
|
+
if (relation === undefined)
|
|
975
|
+
recorder.unresolved.push({
|
|
976
|
+
at: `${nodeAt}.relation_name`,
|
|
977
|
+
name: '(none)',
|
|
978
|
+
reason: "the node's relation_name is missing, so it is not known whether its SQL columns land in an output object, and the column names cannot be placed",
|
|
979
|
+
});
|
|
980
|
+
for (const sqlKey of dcsqlSqlKeys)
|
|
981
|
+
if (typeof node[sqlKey] === 'string')
|
|
982
|
+
node[sqlKey] = recorder.sql(`${nodeAt}.${sqlKey}`, node[sqlKey], `${nodeAt}.${sqlKey}`, 'data transform', fields);
|
|
983
|
+
if (placedRelation)
|
|
984
|
+
node.relation_name = placedRelation.name;
|
|
985
|
+
if (typeof node.name === 'string')
|
|
986
|
+
recorder.keep(`${nodeAt}.name`);
|
|
987
|
+
if (node.config !== undefined)
|
|
988
|
+
dcsqlConfig(node.config, `${nodeAt}.config`, recorder);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
// An object or field name inside a DCSQL config value.
|
|
992
|
+
const configNameLike = /__(?:c|dlm|dll|cio)\b|ssot__/iu;
|
|
993
|
+
/**
|
|
994
|
+
* Check the `config` of a DCSQL node. The only key retrieve shows is `materialized` (`table`),
|
|
995
|
+
* which holds no name; a name-like value under any key (`unique_key: 'Id__c'`) is not placed,
|
|
996
|
+
* since which object's field it means is not known to the translation. Everything else is kept.
|
|
997
|
+
* @param {unknown} value The config value, or a part of it.
|
|
998
|
+
* @param {string} at Its JSON path.
|
|
999
|
+
* @param {Recorder} recorder Collects changes.
|
|
1000
|
+
*/
|
|
1001
|
+
function dcsqlConfig(value, at, recorder) {
|
|
1002
|
+
recorder.keep(at);
|
|
1003
|
+
if (Array.isArray(value))
|
|
1004
|
+
for (const [index, entry] of value.entries())
|
|
1005
|
+
dcsqlConfig(entry, `${at}[${index}]`, recorder);
|
|
1006
|
+
else if (value && typeof value === 'object')
|
|
1007
|
+
for (const [key, entry] of Object.entries(value))
|
|
1008
|
+
dcsqlConfig(entry, `${at}.${key}`, recorder);
|
|
1009
|
+
else if (typeof value === 'string' && configNameLike.test(value))
|
|
1010
|
+
recorder.unresolved.push({
|
|
1011
|
+
at,
|
|
1012
|
+
name: value,
|
|
1013
|
+
reason: "this DCSQL config value holds an object or field name; the translation does not place names in a node's config, so it cannot be placed",
|
|
1014
|
+
});
|
|
1015
|
+
}
|
|
1016
|
+
/**
|
|
1017
|
+
* Translate an STL data transform file: its name (prefix rule), data space (a create in a
|
|
1018
|
+
* non-default data space needs `dataSpaceName`) and each STL definition.
|
|
1019
|
+
* @param {Record<string, unknown>} copy Transform file copy, changed in place.
|
|
1020
|
+
* @param {Recorder} recorder Collects changes.
|
|
1021
|
+
* @param {CrossSpaceContext} context Translation context.
|
|
1022
|
+
*/
|
|
1023
|
+
function translateTransform(copy, recorder, context) {
|
|
1024
|
+
const input = copy.definition;
|
|
1025
|
+
input.name = recorder.named('definition.name', String(input.name), translateUniqueName);
|
|
1026
|
+
if (typeof input.dataSpaceName === 'string' || context.target !== 'default')
|
|
1027
|
+
input.dataSpaceName = recorder.change('definition.dataSpaceName', typeof input.dataSpaceName === 'string' ? input.dataSpaceName : '(none)', context.target, 'data-space');
|
|
1028
|
+
const translate = (definition, at) => {
|
|
1029
|
+
if (definition.type === 'DCSQL')
|
|
1030
|
+
translateDcsql(definition, at, recorder, context);
|
|
1031
|
+
else
|
|
1032
|
+
translateStl(definition, at, recorder, context);
|
|
1033
|
+
};
|
|
1034
|
+
if (Array.isArray(input.definitions))
|
|
1035
|
+
for (const [index, entry] of input.definitions.entries())
|
|
1036
|
+
translate(entry, `definition.definitions[${index}]`);
|
|
1037
|
+
else
|
|
1038
|
+
translate(input.definition, 'definition.definition');
|
|
1039
|
+
}
|
|
1040
|
+
/**
|
|
1041
|
+
* The full-coverage check of a translated graph, ruleset or transform: Salesforce accepts a graph
|
|
1042
|
+
* left in the source spelling in another data space (live API 68), so a missed reference would
|
|
1043
|
+
* not fail. Every value with a name-like part (`…__c`, `…__dlm`, also inside `R.FirstName__c`)
|
|
1044
|
+
* the translation did not look at, and every value with a part still in the source spelling
|
|
1045
|
+
* (`ssot__` towards a non-default data space, another data space's prefix), is a name that
|
|
1046
|
+
* cannot be placed. Labels and descriptions are text.
|
|
1047
|
+
* @param {unknown} value Translated document or part of it.
|
|
1048
|
+
* @param {string} at JSON path.
|
|
1049
|
+
* @param {Recorder} recorder The translation's record.
|
|
1050
|
+
* @param {CrossSpaceContext} context Translation context.
|
|
1051
|
+
* @param {Set<string>} reported Paths already reported.
|
|
1052
|
+
*/
|
|
1053
|
+
function coverageGaps(value, at, recorder, context, reported) {
|
|
1054
|
+
if (Array.isArray(value)) {
|
|
1055
|
+
for (const [index, entry] of value.entries())
|
|
1056
|
+
coverageGaps(entry, `${at}[${index}]`, recorder, context, reported);
|
|
1057
|
+
return;
|
|
1058
|
+
}
|
|
1059
|
+
if (value && typeof value === 'object') {
|
|
1060
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
1061
|
+
if (key === 'label' || key === 'description')
|
|
1062
|
+
continue;
|
|
1063
|
+
coverageGaps(entry, at ? `${at}.${key}` : key, recorder, context, reported);
|
|
1064
|
+
}
|
|
1065
|
+
return;
|
|
1066
|
+
}
|
|
1067
|
+
if (typeof value !== 'string' || reported.has(at))
|
|
1068
|
+
return;
|
|
1069
|
+
// A compound value (`R.FirstName__c`, an expression) is checked token by token.
|
|
1070
|
+
const tokens = value.split(/[^A-Za-z0-9_]+/u).filter(Boolean);
|
|
1071
|
+
const foreign = tokens.some((token) => context.spaces.some((space) => space.prefix !== undefined &&
|
|
1072
|
+
space.name !== context.target &&
|
|
1073
|
+
token.startsWith(`${space.prefix}_`) &&
|
|
1074
|
+
!token.endsWith('__dll')));
|
|
1075
|
+
const leftover = (context.target !== 'default' && value.includes('ssot__')) || foreign;
|
|
1076
|
+
if (!leftover && (recorder.visited.has(at) || !tokens.some((token) => nameValue.test(token))))
|
|
1077
|
+
return;
|
|
1078
|
+
reported.add(at);
|
|
1079
|
+
recorder.unresolved.push({
|
|
1080
|
+
at,
|
|
1081
|
+
name: value,
|
|
1082
|
+
reason: leftover
|
|
1083
|
+
? `it is still spelled as in ${context.source} after the translation`
|
|
1084
|
+
: 'the translation does not cover this reference, so it cannot be placed',
|
|
1085
|
+
});
|
|
1086
|
+
}
|
|
1087
|
+
/**
|
|
1088
|
+
* The rule engine as the SQL rewrite uses it. A `__dlm` table is placed by the DMO rules (and the
|
|
1089
|
+
* ruleset list), a `__cio` table by the prefix rule of insights, and a `__dll` table keeps its
|
|
1090
|
+
* name. A column of a table the rules do not know is placed only when it never changes in this
|
|
1091
|
+
* direction. A name still holding `ssot__` towards a non-default data space, or another data
|
|
1092
|
+
* space's prefix, is left over.
|
|
1093
|
+
* @param {CrossSpaceContext} context Translation context.
|
|
1094
|
+
* @returns {SqlNamer} The namer.
|
|
1095
|
+
*/
|
|
1096
|
+
export function sqlNamer(context) {
|
|
1097
|
+
return {
|
|
1098
|
+
object: (name) => {
|
|
1099
|
+
if (name.endsWith('__dll'))
|
|
1100
|
+
return 'dlo';
|
|
1101
|
+
if (name.endsWith('__cio'))
|
|
1102
|
+
return translateUniqueName(context, name);
|
|
1103
|
+
return placeDmo(context, name);
|
|
1104
|
+
},
|
|
1105
|
+
field: (kind, field) => translateFieldName(context, kind, field),
|
|
1106
|
+
anyField: (field) => {
|
|
1107
|
+
if (context.source === 'default')
|
|
1108
|
+
return translateFieldName(context, 'custom', field);
|
|
1109
|
+
if (!fieldNeverChanges(context, field))
|
|
1110
|
+
return { reason: 'its spelling depends on the object it belongs to' };
|
|
1111
|
+
return { name: field };
|
|
1112
|
+
},
|
|
1113
|
+
// SQL reads names without regard to case, so the leftover check does too.
|
|
1114
|
+
leftover: (name) => {
|
|
1115
|
+
const lower = name.toLowerCase();
|
|
1116
|
+
const foreign = context.spaces.some((space) => space.prefix !== undefined &&
|
|
1117
|
+
space.name !== context.target &&
|
|
1118
|
+
lower.startsWith(`${space.prefix.toLowerCase()}_`) &&
|
|
1119
|
+
!lower.endsWith('__dll'));
|
|
1120
|
+
return (context.target !== 'default' && lower.includes('ssot__')) || foreign
|
|
1121
|
+
? `it is still spelled as in ${context.source} after the translation`
|
|
1122
|
+
: undefined;
|
|
1123
|
+
},
|
|
1124
|
+
spelling: (name) => {
|
|
1125
|
+
const marker = caseMarker(context, name);
|
|
1126
|
+
return marker === undefined
|
|
1127
|
+
? undefined
|
|
1128
|
+
: `it holds ${marker.written} where the rules read ${marker.rule}; SQL ignores the case, the rules do not, so the name is not placed: write ${marker.rule}`;
|
|
1129
|
+
},
|
|
1130
|
+
};
|
|
1131
|
+
}
|
|
1132
|
+
/**
|
|
1133
|
+
* The part of a name the rules read (`__c`, `__dlm`, `ssot__`, `KQ_`, `Unified`, a data space
|
|
1134
|
+
* prefix) when it is written in another case, such as `SSOT__ID__C`.
|
|
1135
|
+
* @param {CrossSpaceContext} context Translation context.
|
|
1136
|
+
* @param {string} name Name as written.
|
|
1137
|
+
* @returns {{written: string; rule: string} | undefined} The part as written and as the rules read it.
|
|
1138
|
+
*/
|
|
1139
|
+
function caseMarker(context, name) {
|
|
1140
|
+
const lower = name.toLowerCase();
|
|
1141
|
+
const suffix = /__(?:c|dlm|cio|dll)$/iu.exec(name)?.[0];
|
|
1142
|
+
if (suffix !== undefined && suffix !== suffix.toLowerCase())
|
|
1143
|
+
return { written: suffix, rule: suffix.toLowerCase() };
|
|
1144
|
+
for (let at = lower.indexOf('ssot__'); at !== -1; at = lower.indexOf('ssot__', at + 1))
|
|
1145
|
+
if (name.slice(at, at + 6) !== 'ssot__')
|
|
1146
|
+
return { written: name.slice(at, at + 6), rule: 'ssot__' };
|
|
1147
|
+
if (lower.startsWith('kq_') && !name.startsWith('KQ_'))
|
|
1148
|
+
return { written: name.slice(0, 3), rule: 'KQ_' };
|
|
1149
|
+
if (!lower.endsWith('__dlm') && !lower.endsWith('__cio'))
|
|
1150
|
+
return undefined;
|
|
1151
|
+
if (lower.startsWith('unified') && !name.startsWith('Unified'))
|
|
1152
|
+
return { written: name.slice(0, 7), rule: 'Unified' };
|
|
1153
|
+
if (lower.startsWith('unifiedlink') && !name.startsWith('UnifiedLink'))
|
|
1154
|
+
return { written: name.slice(0, 11), rule: 'UnifiedLink' };
|
|
1155
|
+
const base = name.slice(0, -5);
|
|
1156
|
+
if (base.toLowerCase().endsWith('identitylink') && !base.endsWith('IdentityLink'))
|
|
1157
|
+
return { written: base.slice(-12), rule: 'IdentityLink' };
|
|
1158
|
+
for (const space of context.spaces) {
|
|
1159
|
+
if (space.prefix === undefined)
|
|
1160
|
+
continue;
|
|
1161
|
+
const prefix = `${space.prefix}_`;
|
|
1162
|
+
if (lower.startsWith(prefix.toLowerCase()) && !name.startsWith(prefix))
|
|
1163
|
+
return { written: name.slice(0, prefix.length), rule: prefix };
|
|
1164
|
+
}
|
|
1165
|
+
return undefined;
|
|
1166
|
+
}
|
|
1167
|
+
/** Keys of a calculated insight file that hold no names: settings and text. */
|
|
1168
|
+
const insightPlainKeys = [
|
|
1169
|
+
'definitionType',
|
|
1170
|
+
'description',
|
|
1171
|
+
'displayName',
|
|
1172
|
+
'historyPublishScheduleInterval',
|
|
1173
|
+
'historyPublishScheduleTimeZone',
|
|
1174
|
+
'historyPublishTime',
|
|
1175
|
+
'isFromPackage',
|
|
1176
|
+
'publishScheduleEndDate',
|
|
1177
|
+
'publishScheduleInterval',
|
|
1178
|
+
'publishScheduleStartDateTime',
|
|
1179
|
+
'publishScheduleTimeZone',
|
|
1180
|
+
'queryDialect',
|
|
1181
|
+
];
|
|
1182
|
+
/**
|
|
1183
|
+
* Translate one calculated insight (its JSON with the SQL joined) into the target data space: its
|
|
1184
|
+
* name and a source insight by the prefix rule, its data space, and every table and column of its
|
|
1185
|
+
* SQL (classic and DC_SQL alike). Salesforce accepts an insight left in the source spelling and
|
|
1186
|
+
* then fails it a few minutes after activation without a message (live API 68), so every name-like
|
|
1187
|
+
* value the translation does not account for is a name it cannot place.
|
|
1188
|
+
* @param {Record<string, unknown>} native Insight document (not changed).
|
|
1189
|
+
* @param {CrossSpaceContext} context From which data space to which one.
|
|
1190
|
+
* @returns {CrossSpaceTranslation} The translated insight.
|
|
1191
|
+
*/
|
|
1192
|
+
export function translateCalculatedInsight(native, context) {
|
|
1193
|
+
const recorder = new Recorder(context);
|
|
1194
|
+
const copy = structuredClone(native);
|
|
1195
|
+
const source = String(native.apiName);
|
|
1196
|
+
copy.apiName = recorder.named('apiName', source, translateUniqueName);
|
|
1197
|
+
copy.dataSpace = recorder.change('c__dataSpace', String(native.dataSpace), context.target, 'data-space');
|
|
1198
|
+
recorder.keep('dataSpace');
|
|
1199
|
+
if (typeof copy.sourceCiDevName === 'string' && copy.sourceCiDevName)
|
|
1200
|
+
copy.sourceCiDevName = recorder.named('sourceCiDevName', copy.sourceCiDevName, translateUniqueName);
|
|
1201
|
+
else
|
|
1202
|
+
recorder.keep('sourceCiDevName');
|
|
1203
|
+
copy.expression = recorder.sql('expression', String(native.expression), `${source}.calculatedInsight-meta.sql`, 'calculated insight');
|
|
1204
|
+
for (const key of insightPlainKeys)
|
|
1205
|
+
recorder.keep(key);
|
|
1206
|
+
// The SQL had its own token-level check; the other keys are checked here.
|
|
1207
|
+
const keys = { ...copy };
|
|
1208
|
+
delete keys.expression;
|
|
1209
|
+
coverageGaps(keys, '', recorder, context, new Set(recorder.unresolved.map((entry) => entry.at)));
|
|
1210
|
+
return { document: copy, rewrites: recorder.rewrites, unresolved: recorder.unresolved };
|
|
1211
|
+
}
|
|
1212
|
+
/**
|
|
1213
|
+
* Translate one admitted file of the source data space into the target data space, in memory.
|
|
1214
|
+
* DMO: its name and field names. Mapping: its target DMO and target fields (the source DLO side
|
|
1215
|
+
* and the mapping's own name stay; the mapping is found by its binding). Relationship: both
|
|
1216
|
+
* sides. DLO: the target data space is added to its list. UI segment: its name, DMO and criteria
|
|
1217
|
+
* names; Waterfall: its name, DMO and child segments.
|
|
1218
|
+
* @param {Record<string, unknown>} document Admitted file (not changed).
|
|
1219
|
+
* @param {CrossSpaceContext} context From which data space to which one.
|
|
1220
|
+
* @returns {CrossSpaceTranslation | CrossSpaceUnsupported} The translated copy, or why not.
|
|
1221
|
+
*/
|
|
1222
|
+
export function translateDocument(document, context) {
|
|
1223
|
+
const unsupported = crossSpaceUnsupported(document);
|
|
1224
|
+
if (unsupported !== undefined)
|
|
1225
|
+
return { unsupported };
|
|
1226
|
+
const recorder = new Recorder(context);
|
|
1227
|
+
const copy = structuredClone(document);
|
|
1228
|
+
if (copy.kind === 'dlo') {
|
|
1229
|
+
const listed = copy.c__dataSpace;
|
|
1230
|
+
if (!listed.includes(context.target)) {
|
|
1231
|
+
copy.c__dataSpace = [...listed, context.target];
|
|
1232
|
+
recorder.change('c__dataSpace', listed.join(', '), [...listed, context.target].join(', '), 'dlo-membership');
|
|
1233
|
+
}
|
|
1234
|
+
return { document: copy, rewrites: recorder.rewrites, unresolved: recorder.unresolved };
|
|
1235
|
+
}
|
|
1236
|
+
copy.c__dataSpace = recorder.change('c__dataSpace', String(document.c__dataSpace), context.target, 'data-space');
|
|
1237
|
+
if (copy.kind === 'native-data-graph' ||
|
|
1238
|
+
copy.kind === 'identity-resolution' ||
|
|
1239
|
+
copy.kind === 'data-transform') {
|
|
1240
|
+
if (copy.kind === 'native-data-graph')
|
|
1241
|
+
translateGraph(copy, recorder, context);
|
|
1242
|
+
else if (copy.kind === 'identity-resolution')
|
|
1243
|
+
translateRuleset(copy, recorder);
|
|
1244
|
+
else
|
|
1245
|
+
translateTransform(copy, recorder, context);
|
|
1246
|
+
// Structural keys that hold no names are looked at by the shape: kind and versions.
|
|
1247
|
+
for (const key of ['schemaVersion', 'kind', 'apiVersion'])
|
|
1248
|
+
recorder.keep(key);
|
|
1249
|
+
coverageGaps(copy, '', recorder, context, new Set([...recorder.unresolved.map((entry) => entry.at), ...recorder.ownCheck]));
|
|
1250
|
+
return { document: copy, rewrites: recorder.rewrites, unresolved: recorder.unresolved };
|
|
1251
|
+
}
|
|
1252
|
+
const identity = copy.identity;
|
|
1253
|
+
const spec = copy.spec;
|
|
1254
|
+
switch (copy.kind) {
|
|
1255
|
+
case 'dmo': {
|
|
1256
|
+
const owner = recorder.dmo('identity.apiName', identity.apiName);
|
|
1257
|
+
if (owner)
|
|
1258
|
+
identity.apiName = owner.name;
|
|
1259
|
+
const fields = spec.fields;
|
|
1260
|
+
for (const [index, field] of fields.entries())
|
|
1261
|
+
for (const [key, value] of Object.entries(field))
|
|
1262
|
+
if (typeof value === 'string' && nameValue.test(value) && value.endsWith('__c'))
|
|
1263
|
+
field[key] = recorder.field(`spec.fields[${index}].${key}`, owner, value);
|
|
1264
|
+
break;
|
|
1265
|
+
}
|
|
1266
|
+
case 'mapping': {
|
|
1267
|
+
const target = recorder.dmo('spec.targetEntityDeveloperName', String(spec.targetEntityDeveloperName));
|
|
1268
|
+
if (target)
|
|
1269
|
+
spec.targetEntityDeveloperName = target.name;
|
|
1270
|
+
const pairs = spec.fieldMappings;
|
|
1271
|
+
for (const [index, pair] of pairs.entries())
|
|
1272
|
+
pair.targetFieldDeveloperName = recorder.field(`spec.fieldMappings[${index}].targetFieldDeveloperName`, target, String(pair.targetFieldDeveloperName));
|
|
1273
|
+
break;
|
|
1274
|
+
}
|
|
1275
|
+
case 'relationship': {
|
|
1276
|
+
for (const side of ['source', 'target']) {
|
|
1277
|
+
const objectKey = `${side}ObjectName`;
|
|
1278
|
+
const fieldKey = `${side}FieldName`;
|
|
1279
|
+
const object = String(spec[objectKey]);
|
|
1280
|
+
// The target of a relationship may be a CRM object (Account), which has no data space.
|
|
1281
|
+
if (side === 'target' && !object.endsWith(dmoSuffix))
|
|
1282
|
+
continue;
|
|
1283
|
+
const placed = recorder.dmo(`spec.${objectKey}`, object);
|
|
1284
|
+
if (placed)
|
|
1285
|
+
spec[objectKey] = placed.name;
|
|
1286
|
+
spec[fieldKey] = recorder.field(`spec.${fieldKey}`, placed, String(spec[fieldKey]));
|
|
1287
|
+
}
|
|
1288
|
+
break;
|
|
1289
|
+
}
|
|
1290
|
+
case 'calculated-insight': {
|
|
1291
|
+
return {
|
|
1292
|
+
unsupported: 'a calculated insight is translated from its JSON and SQL together',
|
|
1293
|
+
};
|
|
1294
|
+
}
|
|
1295
|
+
default: {
|
|
1296
|
+
const sourceName = identity.apiName;
|
|
1297
|
+
identity.apiName = recorder.segment('identity.apiName', identity.apiName);
|
|
1298
|
+
const segmentOn = spec.segmentOn;
|
|
1299
|
+
const on = recorder.dmo('spec.segmentOn.apiName', segmentOn.apiName);
|
|
1300
|
+
if (on)
|
|
1301
|
+
segmentOn.apiName = on.name;
|
|
1302
|
+
if (spec.includeCriteria !== undefined)
|
|
1303
|
+
spec.includeCriteria = translateCriteria(spec.includeCriteria, 'spec.includeCriteria', undefined, recorder, context);
|
|
1304
|
+
if (Array.isArray(spec.childSegments))
|
|
1305
|
+
spec.childSegments = spec.childSegments.map((child, index) => recorder.segment(`spec.childSegments[${index}]`, child));
|
|
1306
|
+
if (spec.segmentType === 'DBT' && Array.isArray(spec.models))
|
|
1307
|
+
for (const [index, model] of spec.models.entries()) {
|
|
1308
|
+
recorder.keep(`spec.models[${index}].name`);
|
|
1309
|
+
model.sql = recorder.sql(`spec.models[${index}].sql`, String(model.sql), `${sourceName}.segment.${String(model.name)}.sql`, 'DBT segment');
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
return { document: copy, rewrites: recorder.rewrites, unresolved: recorder.unresolved };
|
|
1314
|
+
}
|