forked from oasis-tcs/odata-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagram.js
More file actions
184 lines (173 loc) · 5.04 KB
/
diagram.js
File metadata and controls
184 lines (173 loc) · 5.04 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* Resource diagram for an OData model using https://yuml.me
*
* Latest version: https://github.com/oasis-tcs/odata-openapi/blob/master/lib/edm.js
*/
const { isIdentifier, nameParts } = require("./edm");
{
isIdentifier;
}
module.exports = { resourceDiagram };
/**
* Construct resource diagram
* @param {object} model EDM for CSDL document
* @return {string} resource diagram
*/
function resourceDiagram(model) {
let diagram = "";
let comma = "";
//TODO: make colors configurable
let color = {
resource: "{bg:lawngreen}",
entityType: "{bg:lightslategray}",
complexType: "",
external: "{bg:whitesmoke}",
};
for (const [qualifiedName, type] of model.structuredTypes) {
const typeName = nameParts(qualifiedName).name;
diagram +=
comma +
(type.$BaseType
? "[" +
nameParts(type.$BaseType).name +
(type.$Kind == "EntityType" ? color.entityType : color.complexType) +
"]^"
: "") +
"[" +
typeName +
(type.$Kind == "EntityType" ? color.entityType : color.complexType) +
"]";
for (const [name, property] of model.directPropertiesOfStructuredType(
type,
)) {
const targetNP = nameParts(property.$Type ?? "Edm.String");
if (
property.$Kind == "NavigationProperty" ||
targetNP.qualifier != "Edm"
) {
const target = model.element(property.$Type);
const bidirectional =
property.$Partner &&
target &&
target[property.$Partner] &&
target[property.$Partner].$Partner == name;
// Note: if the partner has the same name then it will also be depicted
if (!bidirectional || name <= property.$Partner) {
diagram +=
",[" +
typeName +
"]" +
(property.$Kind != "NavigationProperty" || property.$ContainsTarget
? "++"
: bidirectional
? cardinality(target[property.$Partner])
: "") +
"-" +
cardinality(property) +
(property.$Kind != "NavigationProperty" || bidirectional
? ""
: ">") +
"[" +
(target
? targetNP.name +
(type.$Kind == "EntityType"
? color.entityType
: color.complexType)
: property.$Type + color.external) +
"]";
}
}
}
comma = ",";
}
for (const [name, resource] of model.resources.reverse()) {
if (resource.$Type) {
diagram +=
comma +
"[" +
name +
"%20" +
color.resource +
"]" + // additional space in case entity set and type have same name
"++-" +
cardinality(resource) +
">[" +
nameParts(resource.$Type).name +
"]";
} else {
if (resource.$Action) {
diagram += comma + "[" + name + color.resource + "]";
const overload = model
.element(resource.$Action)
.find((o) => !o.$IsBound);
diagram += overloadDiagram(name, overload);
} else if (resource.$Function) {
diagram += comma + "[" + name + color.resource + "]";
const overloads = model.element(resource.$Function);
if (overloads) {
const unbound = overloads.filter((o) => !o.$IsBound);
// TODO: loop over all overloads, add new source box after first arrow
diagram += overloadDiagram(name, unbound[0]);
}
}
}
}
if (diagram != "") {
diagram =
"\n\n## Entity Data Model\n\n\n### Legend\n";
}
return diagram;
/**
* Diagram representation of property cardinality
* @param {object} typedElement Typed model element, e.g. property
* @param {boolean} one Explicitly represent to-1
* @return {string} cardinality
*/
function cardinality(typedElement) {
return typedElement.$Collection
? "*"
: typedElement.$Nullable
? "0..1"
: "";
}
function overloadDiagram(name, overload) {
let diag = "";
if (overload.$ReturnType) {
const type = model.element(overload.$ReturnType.$Type ?? "Edm.String");
if (type) {
diag +=
"-" +
cardinality(overload.$ReturnType, true) +
">[" +
nameParts(overload.$ReturnType.$Type).name +
"]";
}
}
for (const param of overload.$Parameter ?? []) {
const type = model.element(param.$Type ?? "Edm.String");
if (type) {
diag +=
comma +
"[" +
name +
color.resource +
"]in-" +
cardinality(param.$Type) +
">[" +
nameParts(param.$Type).name +
"]";
}
}
return diag;
}
}