-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.js
333 lines (264 loc) · 10.4 KB
/
runtime.js
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
var base64 = require('base64');
var _ = require('lodash');
var helper = require('./lib/helper');
function getValue(type, from) {
switch (type) {
case 'number':
case 'number:double':
case 'integer:int32':
case 'string':
case 'boolean':
case 'Object':
return from;
case 'string:date':
if (typeof from === 'string') {
return helper.ymd2Date(from);
}
return from;
case 'string:yearmonth':
if (typeof from === 'string') {
return helper.ym2Date(from);
}
return from;
case 'string:time':
case 'string:date-time':
case 'string:datetime':
if (typeof from === 'string') {
return new Date(from);
}
return from;
default:
return json2ModelRecursive.call(this, from, type);
}
}
function json2ModelRecursive(object, className) {
if (object === undefined || object === null || typeof object.hasOwnProperty !== 'function') {
return object;
}
var self = this;
var typeClass = self.get(className);
// Check if the type is abstract
if (typeClass._abstract && typeClass._subTypeProperty && !_.isEmpty(object)) {
// Determine the subtype using the property in "subTypeProperty"
var subTypeProperty = typeClass._subTypeProperty;
var subTypes = typeClass._subTypes;
if (!self.subTypeCache[typeClass]) {
_.each(subTypes, function (subType) {
// Not all sub type are loaded
if (self.isRegistered(subType)) {
var subTypeClass = self.get(subType);
var propertyValue = subTypeClass[subTypeProperty];
self.subTypeCache[className] = self.subTypeCache[className] || {};
self.subTypeCache[className][propertyValue] = subTypeClass;
}
});
}
if (!object.hasOwnProperty(subTypeProperty)) {
throw new Error('Can not determine subtype for abstract class "{0}" because the property "{1}" was missing'.f(className, subTypeProperty));
} else if (!self.subTypeCache[className].hasOwnProperty(object[subTypeProperty])) {
throw new Error('Can not determine subtype for abstract class "{0}" by value "{1}" in property "{2}"'.f(className, object[subTypeProperty], subTypeProperty));
}
typeClass = self.subTypeCache[className][object[subTypeProperty]];
className = typeClass.name;
}
// Aware of object id
var instance;
if (object.hasOwnProperty(self.objectIDName)) {
var instanceObjectID = object[self.objectIDName];
// If object id is JWT based
if (self.jwtBasedObjectID) {
// Split JWT
var objectIDSegments = instanceObjectID.split('.');
if (objectIDSegments.length === 3) {
// Parse JWT to get subject Id
instanceObjectID = JSON.parse(base64.atob(objectIDSegments[1])).sub;
}
}
var instanceObjectIDCacheKey = instanceObjectID + '@' + className;
if (!(instanceObjectIDCacheKey in self.instanceCache)) {
self.instanceCache[instanceObjectIDCacheKey] = new (typeClass)();
}
instance = self.instanceCache[instanceObjectIDCacheKey];
} else {
instance = new (typeClass)();
}
for (var key in object) {
// Angular will add $$hashKey to object we skip them.
if (key !== '$$hashKey' && object.hasOwnProperty(key)) {
var type = typeClass._types[key];
if (!type) {
throw new Error('Can not find type for property {0}@{1}'.f(key, className));
}
if (type.endsWith('[]')) {
// Process array
_.each(object[key], function (value) {
instance._data[key] = instance._data[key] || [];
instance._data[key].push(getValue.call(self, type.replace('[]', ''), value));
});
} else {
instance._data[key] = getValue.call(self, type, object[key]);
}
}
}
// Check required fields
var missingProperties = findMissingProperties.call(self, instance, typeClass);
if (missingProperties !== false) {
throw new Error('Properties "{0}" missing in {1}@{2}'.f(missingProperties.join(','), className, JSON.stringify(instance)));
}
return instance;
}
function model2JsonRecursive(object, options, className) {
var self = this;
var result, property, types;
if (self.isModel(object)) {
className = className || object.constructor.name;
if (className !== object.constructor.name) {
if (object instanceof self.classCache[className]) {
className = object.constructor.name;
}
}
// Check required fields
var missingProperties = findMissingProperties.call(self, object, self.classCache[className]);
if (missingProperties !== false) {
throw new Error('Properties "{0}" missing in {1}@{2}'.f(missingProperties.join(','), className, JSON.stringify(object)));
}
result = _.clone(object._data);
} else {
result = object;
}
// If the object doesn't have property
if (!_.isObject(result) && !_.isArray(result)) {
return result;
}
// Load type list
types = self.get(className)._types;
// Process all properties
for (property in result) {
if (result.hasOwnProperty(property)) {
// If field is not defined in type
// Remove the data
if (!types.hasOwnProperty(property)) {
delete result[property];
continue;
}
var propertyType = types[property];
// We don't care Object arrays
if (propertyType.endsWith('[]')) {
// Process arrays
if (result[property] && result[property].length) {
if (propertyType !== 'Object[]') {
result[property] = _.transform(result[property], function (r, item) {
var json = model2JsonRecursive.call(self, item, options, propertyType.replace('[]', ''));
if (!_.isEmpty(json)) {
r.push(json);
}
});
}
} else {
// Remove empty array
delete result[property];
}
} else if (self.isModel(result[property])) {
// Process model
var json = model2JsonRecursive.call(self, result[property], options, propertyType);
if (_.isEmpty(json)) {
delete result[property];
} else {
result[property] = json;
}
} else {
// Process simple objects
switch (propertyType) {
case 'string:date':
if (typeof result[property] !== 'string') {
result[property] = helper.date2Ymd(result[property], options.jsonTimezone, options.modelTimezone);
}
break;
case 'string:yearmonth':
if (typeof result[property] !== 'string') {
result[property] = helper.date2Ym(result[property], options.jsonTimezone, options.modelTimezone);
}
break;
case 'string:time':
case 'string:date-time':
case 'string:datetime':
if (typeof result[property] !== 'string') {
result[property] = JSON.stringify(result[property]).replace(/"/g, '');
}
break;
}
}
}
}
return result;
}
function findMissingProperties(instance, classDefinition) {
var missingProperties = [];
if (classDefinition._required) {
_.each(classDefinition._required, function (requiredProperty) {
if (!(requiredProperty in instance)) {
missingProperties.push(requiredProperty);
}
});
}
return missingProperties.length > 0 ? missingProperties : false;
}
function Runtime(options) {
this.classCache = {};
this.subTypeCache = {};
this.instanceCache = {};
this.options = options || {};
}
Runtime.prototype.register = function (className, definition) {
if (!definition) {
definition = className;
className = definition.name;
}
this.classCache[className] = definition;
return definition;
};
Runtime.prototype.isRegistered = function (className) {
return (className in this.classCache);
};
Runtime.prototype.get = function (className) {
var self = this;
if (className === undefined) {
return self.classCache;
}
if (!(className in self.classCache)) {
throw new Error('Can not find class "{0}". Please ensure it has been registered.'.f(className));
}
return self.classCache[className];
};
Runtime.prototype.json2Model = function (object, className, options) {
if (!className) {
throw new Error('Please specify root class name');
}
var self = this;
options = options || {};
self.instanceCache = {};
self.objectIDName = self.objectIDName || options.objectID || 'publicID';
self.jwtBasedObjectID = self.jwtBasedObjectID || !!options.jwtBasedObjectID;
return json2ModelRecursive.call(self, object, className);
};
Runtime.prototype.model2Json = function (object, options) {
var self = this;
options = options || self.options;
return model2JsonRecursive.call(self, object, options, options.rootClass);
};
Runtime.prototype.clone = function (model) {
if (!this.isModel(model)) {
return _.cloneDeep(model);
}
var type = model.constructor.name;
return this.json2Model(_.cloneDeep(this.model2Json(model)), type);
};
Runtime.prototype.isModel = function (object) {
return (object && object.isSwaggerModel);
};
Runtime.prototype.setOptions = function (options) {
this.options = options;
this.objectIDName = options.objectID || 'publicID';
this.jwtBasedObjectID = !!options.jwtBasedObjectID;
};
module.exports = new Runtime();