Skip to content

Commit

Permalink
fix(map): fix semantic validation (#841)
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan Casey <[email protected]>
  • Loading branch information
jonathan-casey authored Apr 18, 2024
1 parent 3dddd1e commit 1dad9ba
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 50 deletions.
13 changes: 2 additions & 11 deletions packages/concerto-core/lib/introspect/mapkeytype.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,14 @@ class MapKeyType extends Decorated {
* @protected
*/
validate() {

if (!ModelUtil.isPrimitiveType(this.type)) {

const decl = this.modelFile.getType(this.ast.type.name);

// All but StringScalar & DateTimeScalar are unsupported.
if (!ModelUtil.isValidMapKeyScalar(decl)) {
throw new IllegalModelException(
`Scalar must be one of StringScalar, DateTimeScalar in context of MapKeyType. Invalid Scalar: ${this.type}, for MapDeclaration ${this.parent.name}`
);
}

if (decl?.isConcept?.() || decl?.isClassDeclaration?.()) {
throw new IllegalModelException(
`Invalid Map key type in MapDeclaration ${this.parent.name}. Only String and DateTime types are supported for Map key types`
);
}
}
}

Expand All @@ -95,8 +87,7 @@ class MapKeyType extends Decorated {
* @private
*/
processType(ast) {
let decl;
switch(this.ast.$class) {
switch(ast.$class) {
case `${MetaModelNamespace}.DateTimeMapKeyType`:
this.type = 'DateTime';
break;
Expand Down
5 changes: 2 additions & 3 deletions packages/concerto-core/lib/modelutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,8 @@ class ModelUtil {
* @return {boolean} true if the Key is a valid Map Key Scalar type
*/
static isValidMapKeyScalar(decl) {
return (decl?.isScalarDeclaration?.() &&
(decl?.ast.$class !== `${MetaModelNamespace}.StringScalar` ||
decl?.ast.$class !== `${MetaModelNamespace}.DateTimeScalar`));
return (decl?.isScalarDeclaration?.() && decl?.ast.$class === `${MetaModelNamespace}.StringScalar`) ||
(decl?.isScalarDeclaration?.() && decl?.ast.$class === `${MetaModelNamespace}.DateTimeScalar`);
}

/**
Expand Down

This file was deleted.

45 changes: 33 additions & 12 deletions packages/concerto-core/test/introspect/mapdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,36 +298,55 @@ describe('MapDeclaration', () => {
it('should throw if ast contains illegal Map Key Type - Concept', () => {
(() => {
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.badkey.declaration.concept.cto', MapDeclaration);
decl.validate().should.throw(IllegalModelException);
});
(() => {
decl.validate();
}).should.throw(IllegalModelException);
})();
});

it('should throw if ast contains illegal Map Key Type - Enum', () => {
(() => {
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.badkey.declaration.enum.cto', MapDeclaration);
(() => {
decl.validate();
}).should.throw(IllegalModelException);
})();
});

it('should throw if ast contains illegal Map Key Type - Scalar Long', () => {
(() => {
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.badkey.scalar.long.cto', MapDeclaration);
decl.validate();
});
(() => {
decl.validate();
}).should.throw(IllegalModelException);
})();
});

it('should throw if ast contains illegal Map Key Type - Scalar Integer', () => {
(() => {
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.badkey.scalar.integer.cto', MapDeclaration);
decl.validate().should.throw(IllegalModelException);
});
(() => {
decl.validate();
}).should.throw(IllegalModelException);
})();
});

it('should throw if ast contains illegal Map Key Type - Scalar Double', () => {
(() => {
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.badkey.scalar.double.cto', MapDeclaration);
decl.validate().should.throw(IllegalModelException);
});
(() => {
decl.validate();
}).should.throw(IllegalModelException);
})();
});

it('should throw if ast contains illegal Map Key Type - Scalar Boolean', () => {
(() => {
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.badkey.scalar.boolean.cto', MapDeclaration);
decl.validate().should.throw(IllegalModelException);
});
(() => {
decl.validate();
}).should.throw(IllegalModelException);
})();
});

it('should throw if ast contains illegal Map Key Type - Boolean', () => {
Expand Down Expand Up @@ -414,8 +433,10 @@ describe('MapDeclaration', () => {
const base_cto = fs.readFileSync('test/data/parser/mapdeclaration/base.cto', 'utf-8');
introspectUtils.modelManager.addCTOModel(base_cto, 'base.cto');
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.badkey.imported.thing.cto', MapDeclaration);
decl.validate().should.throw(IllegalModelException);
});
(() => {
decl.validate();
}).should.throw(IllegalModelException);
})();
});
});

Expand Down

0 comments on commit 1dad9ba

Please sign in to comment.