-
Notifications
You must be signed in to change notification settings - Fork 45
/
edmx-parser.ts
49 lines (45 loc) · 1.24 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
/* eslint-disable valid-jsdoc */
import { PathLike, readFileSync } from 'fs';
import path from 'path';
import { parse } from 'fast-xml-parser';
import { ODataVersion } from '@sap-cloud-sdk/util';
import {
EdmxMetadata as EdmxMetadataV2,
isV2Metadata,
parseEdmxV2
} from './v2';
import { EdmxMetadata as EdmxMetadataV4, parseEdmxV4 } from './v4';
import { parseBaseMetadata, getRoot } from './common';
/**
* @deprecated Since version 1.25.0 due to major generator refactoring.
*/
export function parseEdmxFromPath(
edmxPath: PathLike
): EdmxMetadataV2 | EdmxMetadataV4 {
const edmxFile = readFileSync(path.resolve(edmxPath.toString()), {
encoding: 'utf-8'
});
return parseEdmxFile(edmxFile, edmxPath);
}
function parseEdmxFile(
edmx: string,
edmxPath: PathLike
): EdmxMetadataV2 | EdmxMetadataV4 {
const parsedEdmx = parse(edmx, {
ignoreAttributes: false,
attributeNamePrefix: ''
});
const root = getRoot(parsedEdmx);
const metaData = parseBaseMetadata(
root,
getODataVersion(parsedEdmx),
edmxPath
);
return {
...metaData,
...(isV2Metadata(metaData) ? parseEdmxV2(root) : parseEdmxV4(root))
};
}
function getODataVersion(edmx): ODataVersion {
return edmx['edmx:Edmx'].Version === '4.0' ? 'v4' : 'v2';
}