-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathedmx-to-vdm.ts
138 lines (124 loc) · 3.84 KB
/
edmx-to-vdm.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import {
VdmNavigationProperty,
VdmComplexType,
VdmEntity,
VdmFunctionImport
} from '../../../vdm-types';
import { ServiceNameFormatter } from '../../../service-name-formatter';
import { JoinedEntityMetadata, ParsedServiceMetadata } from '../common';
import {
joinEntityMetadata,
createEntityClassNames,
transformEntity,
navigationPropertyBase,
parseReturnType,
swaggerDefinitionForFunctionImport,
transformFunctionImportBase
} from '../common/edmx-to-vdm';
import {
stripNamespace,
isCollectionType
} from '../../../edmx-to-vdm/edmx-to-vdm-util';
import { EdmxEntityType, EdmxEntitySet, EdmxMetadata } from './parser-types';
/* eslint-disable valid-jsdoc */
// TODO: This should be removed once derived types are considered.
function isDerivedNavBindingPath(path: string): boolean {
return path.includes('/');
}
function navigationProperties(
entityMetadata: JoinedEntityMetadata,
classNames: { [originalName: string]: string },
formatter: ServiceNameFormatter
): VdmNavigationProperty[] {
const entityType = entityMetadata.entityType as EdmxEntityType;
const entitySet = entityMetadata.entitySet as EdmxEntitySet;
return entitySet.NavigationPropertyBinding.filter(
navBinding => !isDerivedNavBindingPath(navBinding.Path)
).map(navBinding => {
const navProp = entityType.NavigationProperty.find(
n => n.Name === navBinding.Path
);
if (!navProp) {
throw new Error(
`Could not find navigation property ${navBinding.Path} in entity type ${entityType.Name}.`
);
}
const isCollection = isCollectionType(navProp.Type);
return {
...navigationPropertyBase(
navProp.Name,
entityMetadata.entitySet.Name,
formatter
),
from: entityMetadata.entityType.Name,
to: navBinding.Target,
toEntityClassName: classNames[navBinding.Target],
multiplicity: isCollection ? '1 - *' : '1 - 1',
isMultiLink: isCollection,
isCollection
};
});
}
/**
* @deprecated Since version 1.25.0 due to major generator refactoring.
*/
export function transformEntitiesV4(
serviceMetadata: ParsedServiceMetadata,
complexTypes: VdmComplexType[],
formatter: ServiceNameFormatter
): Omit<VdmEntity, 'entityTypeNamespace'>[] {
const entitiesMetadata = joinEntityMetadata(serviceMetadata);
const classNames = createEntityClassNames(entitiesMetadata, formatter);
return entitiesMetadata.map(entityMetadata => ({
...transformEntity(entityMetadata, classNames, complexTypes, formatter),
navigationProperties: navigationProperties(
entityMetadata,
classNames,
formatter
)
}));
}
/**
* @deprecated Since version 1.25.0 due to major generator refactoring.
*/
export function transformFunctionImportsV4(
serviceMetadata: ParsedServiceMetadata,
entities: VdmEntity[],
complexTypes: VdmComplexType[],
formatter: ServiceNameFormatter
): VdmFunctionImport[] {
const edmxMetadata = serviceMetadata.edmx as EdmxMetadata;
const edmxFunctionImports = edmxMetadata.functionImports;
const edmxFunctions = edmxMetadata.functions;
return edmxFunctionImports.map(f => {
const edmxFunction = edmxFunctions.find(
fn => stripNamespace(f.Function) === fn.Name
);
if (!edmxFunction) {
throw Error(
`Unable to find a function with name: ${f.Function}, but specified in function import ${f.Name}`
);
}
const httpMethod = 'get';
const swaggerDefinition = swaggerDefinitionForFunctionImport(
serviceMetadata,
f.Name,
httpMethod
);
return {
...transformFunctionImportBase(
f,
edmxFunction.Parameter,
swaggerDefinition,
formatter
),
httpMethod,
returnType: parseReturnType(
edmxFunction.ReturnType?.Type,
entities,
complexTypes
),
returnTypeEdmx: edmxFunction.ReturnType?.Type
};
});
}