Skip to content

Commit 1dad9ba

Browse files
fix(map): fix semantic validation (#841)
Signed-off-by: Jonathan Casey <[email protected]>
1 parent 3dddd1e commit 1dad9ba

File tree

4 files changed

+37
-50
lines changed

4 files changed

+37
-50
lines changed

packages/concerto-core/lib/introspect/mapkeytype.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,14 @@ class MapKeyType extends Decorated {
6969
* @protected
7070
*/
7171
validate() {
72-
7372
if (!ModelUtil.isPrimitiveType(this.type)) {
74-
7573
const decl = this.modelFile.getType(this.ast.type.name);
76-
74+
// All but StringScalar & DateTimeScalar are unsupported.
7775
if (!ModelUtil.isValidMapKeyScalar(decl)) {
7876
throw new IllegalModelException(
7977
`Scalar must be one of StringScalar, DateTimeScalar in context of MapKeyType. Invalid Scalar: ${this.type}, for MapDeclaration ${this.parent.name}`
8078
);
8179
}
82-
83-
if (decl?.isConcept?.() || decl?.isClassDeclaration?.()) {
84-
throw new IllegalModelException(
85-
`Invalid Map key type in MapDeclaration ${this.parent.name}. Only String and DateTime types are supported for Map key types`
86-
);
87-
}
8880
}
8981
}
9082

@@ -95,8 +87,7 @@ class MapKeyType extends Decorated {
9587
* @private
9688
*/
9789
processType(ast) {
98-
let decl;
99-
switch(this.ast.$class) {
90+
switch(ast.$class) {
10091
case `${MetaModelNamespace}.DateTimeMapKeyType`:
10192
this.type = 'DateTime';
10293
break;

packages/concerto-core/lib/modelutil.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,8 @@ class ModelUtil {
316316
* @return {boolean} true if the Key is a valid Map Key Scalar type
317317
*/
318318
static isValidMapKeyScalar(decl) {
319-
return (decl?.isScalarDeclaration?.() &&
320-
(decl?.ast.$class !== `${MetaModelNamespace}.StringScalar` ||
321-
decl?.ast.$class !== `${MetaModelNamespace}.DateTimeScalar`));
319+
return (decl?.isScalarDeclaration?.() && decl?.ast.$class === `${MetaModelNamespace}.StringScalar`) ||
320+
(decl?.isScalarDeclaration?.() && decl?.ast.$class === `${MetaModelNamespace}.DateTimeScalar`);
322321
}
323322

324323
/**

packages/concerto-core/test/data/parser/mapdeclaration/mapdeclaration.goodkey.declaration.enum.cto

Lines changed: 0 additions & 24 deletions
This file was deleted.

packages/concerto-core/test/introspect/mapdeclaration.js

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -298,36 +298,55 @@ describe('MapDeclaration', () => {
298298
it('should throw if ast contains illegal Map Key Type - Concept', () => {
299299
(() => {
300300
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.badkey.declaration.concept.cto', MapDeclaration);
301-
decl.validate().should.throw(IllegalModelException);
302-
});
301+
(() => {
302+
decl.validate();
303+
}).should.throw(IllegalModelException);
304+
})();
305+
});
306+
307+
it('should throw if ast contains illegal Map Key Type - Enum', () => {
308+
(() => {
309+
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.badkey.declaration.enum.cto', MapDeclaration);
310+
(() => {
311+
decl.validate();
312+
}).should.throw(IllegalModelException);
313+
})();
303314
});
304315

305316
it('should throw if ast contains illegal Map Key Type - Scalar Long', () => {
306317
(() => {
307318
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.badkey.scalar.long.cto', MapDeclaration);
308-
decl.validate();
309-
});
319+
(() => {
320+
decl.validate();
321+
}).should.throw(IllegalModelException);
322+
})();
310323
});
311324

312325
it('should throw if ast contains illegal Map Key Type - Scalar Integer', () => {
313326
(() => {
314327
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.badkey.scalar.integer.cto', MapDeclaration);
315-
decl.validate().should.throw(IllegalModelException);
316-
});
328+
(() => {
329+
decl.validate();
330+
}).should.throw(IllegalModelException);
331+
})();
317332
});
318333

319334
it('should throw if ast contains illegal Map Key Type - Scalar Double', () => {
320335
(() => {
321336
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.badkey.scalar.double.cto', MapDeclaration);
322-
decl.validate().should.throw(IllegalModelException);
323-
});
337+
(() => {
338+
decl.validate();
339+
}).should.throw(IllegalModelException);
340+
})();
324341
});
325342

326343
it('should throw if ast contains illegal Map Key Type - Scalar Boolean', () => {
327344
(() => {
328345
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.badkey.scalar.boolean.cto', MapDeclaration);
329-
decl.validate().should.throw(IllegalModelException);
330-
});
346+
(() => {
347+
decl.validate();
348+
}).should.throw(IllegalModelException);
349+
})();
331350
});
332351

333352
it('should throw if ast contains illegal Map Key Type - Boolean', () => {
@@ -414,8 +433,10 @@ describe('MapDeclaration', () => {
414433
const base_cto = fs.readFileSync('test/data/parser/mapdeclaration/base.cto', 'utf-8');
415434
introspectUtils.modelManager.addCTOModel(base_cto, 'base.cto');
416435
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.badkey.imported.thing.cto', MapDeclaration);
417-
decl.validate().should.throw(IllegalModelException);
418-
});
436+
(() => {
437+
decl.validate();
438+
}).should.throw(IllegalModelException);
439+
})();
419440
});
420441
});
421442

0 commit comments

Comments
 (0)