-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
jsonpopulator.js
328 lines (294 loc) · 12.4 KB
/
jsonpopulator.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
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const ClassDeclaration = require('../introspect/classdeclaration');
const Field = require('../introspect/field');
const RelationshipDeclaration = require('../introspect/relationshipdeclaration');
const Relationship = require('../model/relationship');
const Util = require('../util');
const ModelUtil = require('../modelutil');
const ValidationException = require('./validationexception');
const Moment = require('moment-mini');
/**
* Check if a given property name is a system property, e.g. '$class'.
* @param {String} name property name.
* @return {boolean} true for a system property; otherwise false.
* @private
*/
function isSystemProperty(name) {
return name.startsWith('$');
}
/**
* Get all properties on a resource object that both have a value and are not system properties.
* @param {Object} resourceData JSON object representation of a resource.
* @return {Array} property names.
* @private
*/
function getAssignableProperties(resourceData) {
return Object.keys(resourceData).filter((property) => {
return !isSystemProperty(property) && !Util.isNull(resourceData[property]);
});
}
/**
* Assert that all resource properties exist in a given class declaration.
* @param {Array} properties Property names.
* @param {ClassDeclaration} classDeclaration class declaration.
* @throws {ValidationException} if any properties are not defined by the class declaration.
* @private
*/
function validateProperties(properties, classDeclaration) {
const expectedProperties = classDeclaration.getProperties().map((property) => property.getName());
const invalidProperties = properties.filter((property) => !expectedProperties.includes(property));
if (invalidProperties.length > 0) {
const errorText = `Unexpected properties for type ${classDeclaration.getFullyQualifiedName()}: ` +
invalidProperties.join(', ');
throw new ValidationException(errorText);
}
}
/**
* Populates a Resource with data from a JSON object graph. The JSON objects
* should be the result of calling Serializer.toJSON and then JSON.parse.
* The parameters object should contain the keys
* 'stack' - the TypedStack of objects being processed. It should
* start with the root object from JSON.parse.
* 'factory' - the Factory instance to use for creating objects.
* 'modelManager' - the ModelManager instance to use to resolve classes
* @private
* @class
* @memberof module:concerto-core
*/
class JSONPopulator {
/**
* Constructor.
* @param {boolean} [acceptResourcesForRelationships] Permit resources in the
* place of relationships, false by default.
* @param {boolean} [ergo] target ergo.
*/
constructor(acceptResourcesForRelationships, ergo) {
this.acceptResourcesForRelationships = acceptResourcesForRelationships;
this.ergo = ergo;
}
/**
* Visitor design pattern
* @param {Object} thing - the object being visited
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
visit(thing, parameters) {
if (thing instanceof ClassDeclaration) {
return this.visitClassDeclaration(thing, parameters);
} else if (thing instanceof RelationshipDeclaration) {
return this.visitRelationshipDeclaration(thing, parameters);
} else if (thing instanceof Field) {
return this.visitField(thing, parameters);
} else {
throw new Error('Unrecognised ' + JSON.stringify(thing) );
}
}
/**
* Visitor design pattern
* @param {ClassDeclaration} classDeclaration - the object being visited
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
visitClassDeclaration(classDeclaration, parameters) {
const jsonObj = parameters.jsonStack.pop();
const resourceObj = parameters.resourceStack.pop();
const properties = getAssignableProperties(jsonObj);
validateProperties(properties, classDeclaration);
properties.forEach((property) => {
const value = jsonObj[property];
parameters.jsonStack.push(value);
const classProperty = classDeclaration.getProperty(property);
resourceObj[property] = classProperty.accept(this,parameters);
});
return resourceObj;
}
/**
* Visitor design pattern
* @param {Field} field - the object being visited
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
visitField(field, parameters) {
const jsonObj = parameters.jsonStack.pop();
let result = null;
if(field.isArray()) {
result = [];
for(let n=0; n < jsonObj.length; n++) {
const jsonItem = jsonObj[n];
result.push(this.convertItem(field,jsonItem, parameters));
}
}
else {
result = this.convertItem(field,jsonObj, parameters);
}
return result;
}
/**
*
* @param {Field} field - the field of the item being converted
* @param {Object} jsonItem - the JSON object of the item being converted
* @param {Object} parameters - the parameters
* @return {Object} - the populated object.
*/
convertItem(field, jsonItem, parameters) {
let result = null;
if(!field.isPrimitive() && !field.isTypeEnum()) {
let typeName = jsonItem.$class;
if(!typeName) {
// If the type name is not specified in the data, then use the
// type name from the model. This will only happen in the case of
// a sub resource inside another resource.
typeName = field.getFullyQualifiedTypeName();
}
// This throws if the type does not exist.
const classDeclaration = parameters.modelManager.getType(typeName);
// create a new instance, using the identifier field name as the ID.
let subResource = null;
// if this is identifiable, then we create a resource
if(!classDeclaration.isConcept()) {
subResource = parameters.factory.newResource(classDeclaration.getNamespace(),
classDeclaration.getName(), jsonItem[classDeclaration.getIdentifierFieldName()] );
}
else {
// otherwise we create a concept
subResource = parameters.factory.newConcept(classDeclaration.getNamespace(),
classDeclaration.getName() );
}
result = subResource;
parameters.resourceStack.push(subResource);
parameters.jsonStack.push(jsonItem);
classDeclaration.accept(this, parameters);
}
else {
result = this.convertToObject(field,jsonItem);
}
return result;
}
/**
* Converts a primtive object to JSON text.
*
* @param {Field} field - the field declaration of the object
* @param {Object} json - the JSON object to convert to a Concerto Object
* @return {string} the text representation
*/
convertToObject(field, json) {
let result = null;
switch(field.getType()) {
case 'DateTime':
if (Moment.isMoment(json)) {
result = json;
} else {
result = new Moment.parseZone(json);
}
break;
case 'Integer':
case 'Long':
result = this.ergo ? parseInt(json.nat) : parseInt(json);
break;
case 'Double':
result = parseFloat(json);
break;
case 'Boolean':
result = (json === true || json === 'true');
break;
case 'String':
result = json.toString();
break;
default: {
// everything else should be an enumerated value...
if (this.ergo) {
// unpack the enum
let current = json.data;
while (!current.left) {
current = current.right;
}
result = current.left;
} else {
result = json;
}
}
}
return result;
}
/**
* Visitor design pattern
* @param {RelationshipDeclaration} relationshipDeclaration - the object being visited
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
visitRelationshipDeclaration(relationshipDeclaration, parameters) {
const jsonObj = parameters.jsonStack.pop();
let result = null;
let typeFQN = relationshipDeclaration.getFullyQualifiedTypeName();
let defaultNamespace = ModelUtil.getNamespace(typeFQN);
if(!defaultNamespace) {
defaultNamespace = relationshipDeclaration.getNamespace();
}
let defaultType = ModelUtil.getShortName(typeFQN);
if(relationshipDeclaration.isArray()) {
result = [];
for(let n=0; n < jsonObj.length; n++) {
let jsonItem = jsonObj[n];
if (typeof jsonItem === 'string') {
result.push(Relationship.fromURI(parameters.modelManager, jsonItem, defaultNamespace, defaultType ));
} else {
if (!this.acceptResourcesForRelationships) {
throw new Error('Invalid JSON data. Found a value that is not a string: ' + jsonObj + ' for relationship ' + relationshipDeclaration);
}
// this isn't a relationship, but it might be an object!
if(!jsonItem.$class) {
throw new Error('Invalid JSON data. Does not contain a $class type identifier: ' + jsonItem + ' for relationship ' + relationshipDeclaration );
}
const classDeclaration = parameters.modelManager.getType(jsonItem.$class);
// create a new instance, using the identifier field name as the ID.
let subResource = parameters.factory.newResource(classDeclaration.getNamespace(),
classDeclaration.getName(), jsonItem[classDeclaration.getIdentifierFieldName()] );
parameters.jsonStack.push(jsonItem);
parameters.resourceStack.push(subResource);
classDeclaration.accept(this, parameters);
result.push(subResource);
}
}
}
else {
if (typeof jsonObj === 'string') {
result = Relationship.fromURI(parameters.modelManager, jsonObj, defaultNamespace, defaultType );
} else {
if (!this.acceptResourcesForRelationships) {
throw new Error('Invalid JSON data. Found a value that is not a string: ' + jsonObj + ' for relationship ' + relationshipDeclaration);
}
// this isn't a relationship, but it might be an object!
if(!jsonObj.$class) {
throw new Error('Invalid JSON data. Does not contain a $class type identifier: ' + jsonObj + ' for relationship ' + relationshipDeclaration );
}
const classDeclaration = parameters.modelManager.getType(jsonObj.$class);
// create a new instance, using the identifier field name as the ID.
let subResource = parameters.factory.newResource(classDeclaration.getNamespace(),
classDeclaration.getName(), jsonObj[classDeclaration.getIdentifierFieldName()] );
parameters.jsonStack.push(jsonObj);
parameters.resourceStack.push(subResource);
classDeclaration.accept(this, parameters);
result = subResource;
}
}
return result;
}
}
module.exports = JSONPopulator;