-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCSDLSearch.js
More file actions
149 lines (135 loc) · 3.84 KB
/
Copy pathCSDLSearch.js
File metadata and controls
149 lines (135 loc) · 3.84 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
function searchAnnotation(collection, elementName) {
var ret = [];
if(elementName === undefined) {
return getAllValidElementsInCollection(collection);
}
else {
if(collection[elementName] !== undefined) {
ret.push(collection[elementName]);
}
}
return ret;
}
function getAllValidElementsInCollection(collection) {
let ret = [];
for(var name in collection) {
if(name[0] === '_') {
continue;
}
ret.push(collection[name]);
}
return ret;
}
function getElementIfNameEqual(element, elementName) {
if(elementName === undefined) {
return element;
}
if(elementName === element.Name) {
return element;
}
return undefined;
}
function pushIfNotUndefined(array, element) {
if(element !== undefined) {
array.push(element);
}
}
function elementInvalid(element) {
return (element === undefined || element === null || typeof element !== 'object');
}
function searchCSDL(root, elementType, elementName, includeReferences) {
var ret = [];
for(var key in root) {
if(key[0] === '_') {
continue;
}
ret = ret.concat(searchElementPart(key, root[key], elementType, elementName, includeReferences));
}
return ret;
}
function searchElementPart(key, element, elementType, elementName, includeReferences) {
let ret = [];
switch(key) {
case 'Annotations':
return processAnnotationsKey(element, elementType, elementName);
case 'References':
if(includeReferences !== true) {
return ret;
}
//Deliberate fallthrough...
default:
return processDefaultKey(element, elementType, elementName);
}
}
function processAnnotationsKey(element, elementType, elementName) {
if(elementType === 'Annotation') {
return searchAnnotation(element, elementName);
}
return [];
}
function processDefaultKey(element, elementType, elementName) {
let ret = [];
if(elementInvalid(element)) {
return ret;
}
if(elementType === undefined) {
pushIfNotUndefined(ret, getElementIfNameEqual(element, elementName));
return ret;
}
if(elementType === element.constructor.name) {
pushIfNotUndefined(ret, getElementIfNameEqual(element, elementName));
return ret;
}
return searchCSDL(element, elementType, elementName);
}
function remapNamespace(references, namespace) {
for(var i = 0; i < references.length; i++) {
if(references[i].Includes[namespace] !== undefined) {
return references[i].Includes[namespace];
}
}
return undefined;
}
function findByType(metadata, typeName) {
if(typeName.startsWith('Edm')) {
return typeName;
}
var index = typeName.lastIndexOf('.');
if(index === -1) {
return null;
}
var namespace = typeName.substring(0, index);
var justType = typeName.substring(index+1);
var schema = metadata[namespace];
if(schema === undefined) {
schema = findSchema(metadata, namespace, justType);
if(schema === undefined) {
return null;
}
}
return schema[justType];
}
function findSchema(metadata, namespace, justType) {
let schemas = metadata._options.cache.getSchemas(namespace);
if(schemas.length === 0) {
return findSchemaWithReferences(metadata, namespace, justType);
}
for(let i = 0; i < schemas.length; i++) {
if(schemas[i][justType] !== undefined) {
return schemas[i];
}
}
return undefined;
}
function findSchemaWithReferences(metadata, namespace, justType) {
if(metadata.References !== undefined) {
namespace = remapNamespace(metadata.References, namespace);
if(namespace !== undefined) {
return findSchema(metadata, namespace, justType);
}
}
return undefined;
}
module.exports.search = searchCSDL;
module.exports.findByType = findByType;
/* vim: set tabstop=2 shiftwidth=2 expandtab: */