From 7a2549fd18d881fddb9abd1d97683a93730610d2 Mon Sep 17 00:00:00 2001 From: Jonathan Casey Date: Mon, 9 Oct 2023 14:53:17 +0100 Subject: [PATCH 1/2] feat(map): add property based flag Signed-off-by: Jonathan Casey --- .../concerto-core/lib/basemodelmanager.js | 4 ++++ .../lib/introspect/mapdeclaration.js | 5 +++-- .../test/introspect/mapdeclaration.js | 22 +++++++++++++++++-- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/packages/concerto-core/lib/basemodelmanager.js b/packages/concerto-core/lib/basemodelmanager.js index 2ea73bd58..365bd8ea0 100644 --- a/packages/concerto-core/lib/basemodelmanager.js +++ b/packages/concerto-core/lib/basemodelmanager.js @@ -93,6 +93,10 @@ class BaseModelManager { this.options = options; this.addRootModel(); + // TODO Remove on release of MapType + // Supports both env var and property based flag + this.enableMapType = !!options?.enableMapType; + // Cache a copy of the Metamodel ModelFile for use when validating the structure of ModelFiles later. this.metamodelModelFile = new ModelFile(this, MetaModelUtil.metaModelAst, undefined, MetaModelNamespace); diff --git a/packages/concerto-core/lib/introspect/mapdeclaration.js b/packages/concerto-core/lib/introspect/mapdeclaration.js index 2ddc54373..0bcc06e6c 100644 --- a/packages/concerto-core/lib/introspect/mapdeclaration.js +++ b/packages/concerto-core/lib/introspect/mapdeclaration.js @@ -45,8 +45,9 @@ class MapDeclaration extends Declaration { */ constructor(modelFile, ast) { // TODO remove on full release. - if(process.env.ENABLE_MAP_TYPE !== 'true') { - throw new Error('MapType feature is not enabled. Please set the environment variable "ENABLE_MAP_TYPE=true" to access this functionality.'); + const mm = modelFile.getModelManager(); + if(process.env.ENABLE_MAP_TYPE !== 'true' && !mm.enableMapType) { + throw new Error('MapType feature is not enabled. Please set the environment variable "ENABLE_MAP_TYPE=true", or add {enableMapType: true} to the ModelManger options, to access this functionality.'); } super(modelFile, ast); diff --git a/packages/concerto-core/test/introspect/mapdeclaration.js b/packages/concerto-core/test/introspect/mapdeclaration.js index 7b9b4bf2e..4e5c42e67 100644 --- a/packages/concerto-core/test/introspect/mapdeclaration.js +++ b/packages/concerto-core/test/introspect/mapdeclaration.js @@ -84,11 +84,29 @@ describe('MapDeclaration', () => { $class: 'concerto.metamodel@1.0.0.StringMapValueType' } }); - }).should.throw(/MapType feature is not enabled. Please set the environment variable "ENABLE_MAP_TYPE=true" to access this functionality./); + }).should.throw(/MapType feature is not enabled. Please set the environment variable "ENABLE_MAP_TYPE=true", or add {enableMapType: true} to the ModelManger options, to access this functionality/); process.env.ENABLE_MAP_TYPE = 'true'; // enable after the test run. This is necessary to ensure functioning of other tests. }); - + it('should throw if Map Type not enabled in ModelManager options', () => { + process.env.ENABLE_MAP_TYPE = 'false'; + const mm = new ModelManager({enableMapType: false}); + Util.addComposerModel(mm); + const introspectUtils = new IntrospectUtils(mm); + try { + introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.goodkey.primitive.datetime.cto', MapDeclaration); + } catch (error) { + expect(error.message).to.equal('MapType feature is not enabled. Please set the environment variable "ENABLE_MAP_TYPE=true", or add {enableMapType: true} to the ModelManger options, to access this functionality.'); + } + }); + + it('should not throw if Map Type not enabled in ModelManager options', () => { + const mm = new ModelManager({enableMapType: true}); + Util.addComposerModel(mm); + const introspectUtils = new IntrospectUtils(mm); + let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.goodkey.primitive.datetime.cto', MapDeclaration); + decl.validate(); + }); it('should throw if invalid $class provided for Map Key', () => { (() => From ff2f74b3dba5108850556b43115d255d9da77143 Mon Sep 17 00:00:00 2001 From: Jonathan Casey Date: Mon, 9 Oct 2023 17:08:49 +0100 Subject: [PATCH 2/2] fix typo Signed-off-by: Jonathan Casey --- packages/concerto-core/test/introspect/mapdeclaration.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/concerto-core/test/introspect/mapdeclaration.js b/packages/concerto-core/test/introspect/mapdeclaration.js index 4e5c42e67..641abd06c 100644 --- a/packages/concerto-core/test/introspect/mapdeclaration.js +++ b/packages/concerto-core/test/introspect/mapdeclaration.js @@ -100,7 +100,7 @@ describe('MapDeclaration', () => { } }); - it('should not throw if Map Type not enabled in ModelManager options', () => { + it('should not throw if Map Type is enabled in ModelManager options', () => { const mm = new ModelManager({enableMapType: true}); Util.addComposerModel(mm); const introspectUtils = new IntrospectUtils(mm);