diff --git a/packages/concerto-core/lib/introspect/mapkeytype.js b/packages/concerto-core/lib/introspect/mapkeytype.js index 5ffa2a3c0..41bd364be 100644 --- a/packages/concerto-core/lib/introspect/mapkeytype.js +++ b/packages/concerto-core/lib/introspect/mapkeytype.js @@ -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` - ); - } } } @@ -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; diff --git a/packages/concerto-core/lib/modelutil.js b/packages/concerto-core/lib/modelutil.js index d83210b21..1a1978d39 100644 --- a/packages/concerto-core/lib/modelutil.js +++ b/packages/concerto-core/lib/modelutil.js @@ -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`); } /** diff --git a/packages/concerto-core/test/data/parser/mapdeclaration/mapdeclaration.goodkey.declaration.enum.cto b/packages/concerto-core/test/data/parser/mapdeclaration/mapdeclaration.goodkey.declaration.enum.cto deleted file mode 100644 index b46f62cef..000000000 --- a/packages/concerto-core/test/data/parser/mapdeclaration/mapdeclaration.goodkey.declaration.enum.cto +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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. - */ - -namespace com.acme@1.0.0 - -enum Phase { - o ONE - o TWO -} -map Dictionary { - o Phase - o String -} diff --git a/packages/concerto-core/test/introspect/mapdeclaration.js b/packages/concerto-core/test/introspect/mapdeclaration.js index 49ef1af6b..aa0dc1156 100644 --- a/packages/concerto-core/test/introspect/mapdeclaration.js +++ b/packages/concerto-core/test/introspect/mapdeclaration.js @@ -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', () => { @@ -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); + })(); }); });