Skip to content

Commit 266b66b

Browse files
committed
improve structute management
1 parent 3f0ecbb commit 266b66b

File tree

3 files changed

+103
-25
lines changed

3 files changed

+103
-25
lines changed

src/component.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -572,24 +572,22 @@ function getMethods(id) {
572572

573573
/**
574574
* @method getStructureProperties
575-
* @param {String} name of the property
576-
* @param {String} name of the model
575+
* @param {String} path path of the property
576+
* @param {String} name name of the model
577577
* @returns {Array} list of property schema of the structure type
578578
* @private
579579
* @description Get the schema of a structure
580580
*/
581-
function getStructureProperties(propertyName, model) {
582-
var modelDef = null;
581+
function getStructureProperties(path, model) {
583582
var type = null;
584583
var structure = null;
585584
var result = [];
586585
var propNames = [];
587586

588-
modelDef = $metamodel.getModel(model);
589-
type = modelDef[propertyName].type;
587+
type = $metamodel.getModelPathType(model, path);
590588
structure = $metamodel.getType(type);
591589

592-
if (structure.schema) {
590+
if (structure && structure.schema) {
593591
propNames = Object.keys(structure.schema);
594592
propNames.forEach(function(name) {
595593
structure.schema[name].name = name;
@@ -1159,7 +1157,10 @@ function addProperties(model, Class, classId) {
11591157
* Some checks can be done in order to see if the set of properties is compliant with the model.
11601158
*/
11611159
function addStructure(path, name, model, id) {
1162-
var properties = getStructureProperties(name, model);
1160+
var properties = getStructureProperties(
1161+
path ? path + '.' + name : name,
1162+
model
1163+
);
11631164
var sructure = {};
11641165

11651166
properties.forEach(function property(prop) {
@@ -1172,7 +1173,7 @@ function addStructure(path, name, model, id) {
11721173
propertyType = prop.type;
11731174
propertyReadOnly = prop.readOnly;
11741175

1175-
if (propertyType === 'array') {
1176+
if (Array.isArray(propertyType) || propertyType === 'array') {
11761177
// in case of array, return a sub array
11771178
proxy = function proxy(position, value) {
11781179
var search = [];
@@ -1194,11 +1195,11 @@ function addStructure(path, name, model, id) {
11941195
}
11951196
});
11961197

1197-
return true;
1198+
return result;
11981199
}
11991200

12001201
if (path) {
1201-
parentPath = parentPath + '.' + name;
1202+
parentPath = path + '.' + name;
12021203
} else {
12031204
parentPath = name;
12041205
}
@@ -1219,7 +1220,12 @@ function addStructure(path, name, model, id) {
12191220
} else {
12201221
if (Array.isArray(position)) {
12211222
// we replace the collection
1222-
if (_isValidCollection(position, 'any')) {
1223+
if (
1224+
_isValidCollection(
1225+
position,
1226+
Array.isArray(propertyType) ? propertyType[0] : 'any'
1227+
)
1228+
) {
12231229
search = $db[model].find({
12241230
_id: id
12251231
});
@@ -1272,7 +1278,12 @@ function addStructure(path, name, model, id) {
12721278
if (propertyReadOnly) {
12731279
$log.readOnlyProperty(id, this.constructor.name, propertyName);
12741280
} else {
1275-
if ($metamodel.isValidType(value, 'any')) {
1281+
if (
1282+
$metamodel.isValidType(
1283+
value,
1284+
Array.isArray(propertyType) ? propertyType[0] : 'any'
1285+
)
1286+
) {
12761287
search = $db[model].find({
12771288
_id: id
12781289
});
@@ -1308,7 +1319,7 @@ function addStructure(path, name, model, id) {
13081319
this.constructor.name,
13091320
propertyName,
13101321
value,
1311-
propertyType
1322+
Array.isArray(propertyType) ? propertyType[0] : 'any'
13121323
);
13131324
}
13141325
}
@@ -1330,7 +1341,7 @@ function addStructure(path, name, model, id) {
13301341
var fullPath = '';
13311342

13321343
if (path) {
1333-
parentPath = parentPath + '.' + name;
1344+
parentPath = path + '.' + name;
13341345
} else {
13351346
parentPath = name;
13361347
}
@@ -1355,7 +1366,7 @@ function addStructure(path, name, model, id) {
13551366
JSON.stringify(getStructureValue(model, id, fullPath))
13561367
);
13571368
break;
1358-
case $metamodel.isStructure(propertyName, model):
1369+
case $metamodel.isStructure(fullPath, model):
13591370
propertyValue = addStructure(
13601371
parentPath,
13611372
propertyName,

src/metamodel.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,22 +1869,20 @@ exports.isMethod = function isMethod(name, id) {
18691869

18701870
/**
18711871
* @method isStructure
1872-
* @param {String} name name of the propertys
1873-
* @param {String} id component id
1872+
* @param {String} path path of the property
1873+
* @param {String} modelName model name
18741874
* @returns {Boolean} true if the property is a structure
18751875
* @description Check if an attribute of the schema is a structure
18761876
*/
1877-
exports.isStructure = function isStructure(name, id) {
1877+
exports.isStructure = function isStructure(path, modelName) {
18781878
var result = false;
1879-
var model = store.generatedModels[id];
1880-
var attributeType = '';
1879+
var structure = null;
18811880
var type = '';
18821881

1883-
if (model[name]) {
1884-
type = store.type[model[name].type];
1885-
}
1882+
type = exports.getModelPathType(modelName, path);
1883+
structure = exports.getType(type);
18861884

1887-
if (type && type.schema) {
1885+
if (structure && structure.schema) {
18881886
result = true;
18891887
}
18901888

test/runtime/metamodel-spec.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,75 @@ describe('System Runtime metamodel component', () => {
187187
expect(person.address().city()).equal('Paris');
188188
});
189189

190+
it('can add a complex structure', () => {
191+
const metamodel = runtime.require('metamodel');
192+
193+
metamodel.type({
194+
'name': 'foods',
195+
'type': 'object',
196+
'schema': {
197+
'vegetables': { 'type': 'vegetable', 'mandatory': true }
198+
}
199+
});
200+
201+
metamodel.type({
202+
'name': 'vegetable',
203+
'type': 'object',
204+
'schema': {
205+
'kind': { 'type': ['string'], 'mandatory': true }
206+
}
207+
});
208+
209+
metamodel.schema({
210+
'_name': 'Person',
211+
'_inherit': ['_Component'],
212+
'firstName': 'property',
213+
'lastName': 'property',
214+
'likes': 'property'
215+
});
216+
217+
metamodel.model({
218+
'_name': 'Person',
219+
'_inherit': ['_Component'],
220+
'firstName': {
221+
'type': 'string',
222+
'readOnly': false,
223+
'mandatory': true,
224+
'default': ''
225+
},
226+
'lastName': {
227+
'type': 'string',
228+
'readOnly': true,
229+
'mandatory': true,
230+
'default': ''
231+
},
232+
'likes': {
233+
'type': 'foods',
234+
'readOnly': true,
235+
'mandatory': false,
236+
'default': {
237+
}
238+
}
239+
});
240+
241+
metamodel.create();
242+
243+
const Person = runtime.require('Person');
244+
245+
const yoda = new Person({
246+
'sex': 'male',
247+
'firstName': 'Yoda',
248+
'lastName': 'Master',
249+
'likes': {
250+
'vegetables': {
251+
'kind': ['all']
252+
}
253+
}
254+
});
255+
256+
expect(yoda.likes().vegetables().kind(0)).equal('all');
257+
});
258+
190259
it('can create a one to one relationship', () => {
191260
const metamodel = runtime.require('metamodel');
192261

0 commit comments

Comments
 (0)