-
Notifications
You must be signed in to change notification settings - Fork 45
/
edmx-parser.ts
93 lines (90 loc) · 2.54 KB
/
edmx-parser.ts
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
import { PathLike } from 'fs';
import { basename } from 'path';
import { createLogger, ODataVersion } from '@sap-cloud-sdk/util';
import { forceArray } from '../../../generator-utils';
import {
EdmxMetadataBase,
EdmxEntityTypeBase,
EdmxComplexTypeBase,
EdmxFunctionImportBase
} from './parser-types';
/* eslint-disable valid-jsdoc */
const logger = createLogger({
package: 'generator',
messageContext: 'edmx-parser'
});
/**
* @deprecated Since version 1.25.0 due to major generator refactoring.
*/
export function parseBaseMetadata(
root,
oDataVersion: ODataVersion,
edmxPath: PathLike
): EdmxMetadataBase {
return {
path: edmxPath,
oDataVersion,
fileName: basename(edmxPath.toString()).split('.')[0],
namespace: root.Namespace,
selfLink: parseLink(root)
};
}
/**
* @deprecated Since version 1.25.0 due to major generator refactoring.
*/
export function parseEntityTypes(root): EdmxEntityTypeBase[] {
return forceArray(root.EntityType).map(e => {
if (!e.Key) {
e.Key = {};
}
e.Key.PropertyRef = forceArray(e.Key.PropertyRef);
e.NavigationProperty = forceArray(e.NavigationProperty);
e.Property = forceArray(e.Property);
return e;
});
}
/**
* @deprecated Since version 1.25.0 due to major generator refactoring.
*/
export function parseFunctionImportsBase(root): EdmxFunctionImportBase[] {
return forceArray(root.EntityContainer.FunctionImport).map(f => {
f.Parameter = forceArray(f.Parameter);
return f;
});
}
/**
* @deprecated Since version 1.25.0 due to major generator refactoring.
*/
export function parseComplexTypes(root): EdmxComplexTypeBase[] {
return forceArray(root.ComplexType).map(c => {
c.Property = forceArray(c.Property);
return c;
});
}
function parseLink(root): string | undefined {
const links = forceArray(root['atom:link']);
const selfLink = links.find(link => link.rel === 'self');
if (selfLink) {
return selfLink.href;
}
}
/**
* @deprecated Since version 1.25.0 due to major generator refactoring.
*/
export function getRoot(edmx) {
const schema = edmx['edmx:Edmx']['edmx:DataServices'].Schema;
if (schema.length > 1) {
if (schema.length > 2) {
throw new Error(
'There are more than two schemas in the input metadata file.'
);
}
// We assume SFSF edmx files to always have multiple schema tags
logger.info(`${schema.length} schemas found. Schemas will be merged.`);
return schema.reduce(
(mergedSchemas, schemaEntry) => ({ ...mergedSchemas, ...schemaEntry }),
{}
);
}
return schema;
}