From a183bbfe02b8b9d6b6e4d94b5f2b71bf379ee7c7 Mon Sep 17 00:00:00 2001 From: Simon Stone Date: Tue, 22 Mar 2022 16:18:54 +0000 Subject: [PATCH] fix(types): refactor metamodel object into class (contributes to #402) Signed-off-by: Simon Stone --- packages/concerto-core/api.txt | 6 +- packages/concerto-core/changelog.txt | 4 + .../concerto-core/lib/introspect/metamodel.js | 99 ++++++++++--------- .../types/lib/introspect/metamodel.d.ts | 38 ++++--- 4 files changed, 81 insertions(+), 66 deletions(-) diff --git a/packages/concerto-core/api.txt b/packages/concerto-core/api.txt index 240cd5aea9..3af0d5eae9 100644 --- a/packages/concerto-core/api.txt +++ b/packages/concerto-core/api.txt @@ -129,9 +129,11 @@ class Introspector { + ClassDeclaration[] getClassDeclarations() + ClassDeclaration getClassDeclaration(String) throws Error } +class MetaModel { + void newMetaModelManager() - + object validateMetaModel() - + object modelManagerFromMetaModel() + + object validateMetaModel(object) + + object modelManagerFromMetaModel(object,boolean?) +} class ModelFile { + void constructor(ModelManager,object,string?,string?) throws IllegalModelException + boolean isModelFile() diff --git a/packages/concerto-core/changelog.txt b/packages/concerto-core/changelog.txt index 10f9994344..da4eca69d9 100644 --- a/packages/concerto-core/changelog.txt +++ b/packages/concerto-core/changelog.txt @@ -24,6 +24,10 @@ # Note that the latest public API is documented using JSDocs and is available in api.txt. # +Version 2.0.0-alpha.2 {d2a653a4b1e77745acb98e9e31ebd72d} 2021-03-22 +- Refactor MetaModel into a class with static functions +- Refactor index.js exports for better TypeScript types + Version 2.0.0-alpha.1 {ee5fa0479fe2720d720ac500b1b3ead8} 2021-11-24 - Remove custom instanceof and add methods to check runtime type - Remove support for Node 12 diff --git a/packages/concerto-core/lib/introspect/metamodel.js b/packages/concerto-core/lib/introspect/metamodel.js index 9431436588..cdc4548018 100644 --- a/packages/concerto-core/lib/introspect/metamodel.js +++ b/packages/concerto-core/lib/introspect/metamodel.js @@ -22,59 +22,62 @@ const Serializer = require('../serializer'); const ModelFile = require('../introspect/modelfile'); /** - * Create a metamodel manager (for validation against the metamodel) - * @return {*} the metamodel manager + * Class containing static helper methods for the metamodel. */ -function newMetaModelManager() { - const metaModelManager = new ModelManager(); - const mf = new ModelFile( - metaModelManager, - MetaModelUtil.metaModelAst, - MetaModelUtil.metaModelCto, - 'concerto.metamodel', - true - ); - metaModelManager.addModelFile(mf, MetaModelUtil.metaModelCto, 'concerto.metamodel'); - return metaModelManager; -} +class MetaModel { -/** - * Validate metamodel instance against the metamodel - * @param {object} input - the metamodel instance in JSON - * @return {object} the validated metamodel instance in JSON - */ -function validateMetaModel(input) { - const metaModelManager = newMetaModelManager(); - const factory = new Factory(metaModelManager); - const serializer = new Serializer(factory, metaModelManager); - // First validate the metaModel - const object = serializer.fromJSON(input); - return serializer.toJSON(object); -} + /** + * Create a metamodel manager (for validation against the metamodel) + * @return {*} the metamodel manager + */ + static newMetaModelManager() { + const metaModelManager = new ModelManager(); + const mf = new ModelFile( + metaModelManager, + MetaModelUtil.metaModelAst, + MetaModelUtil.metaModelCto, + 'concerto.metamodel', + true + ); + metaModelManager.addModelFile(mf, MetaModelUtil.metaModelCto, 'concerto.metamodel'); + return metaModelManager; + } -/** - * Import metamodel to a model manager - * @param {object} metaModel - the metamodel - * @param {boolean} [validate] - whether to perform validation - * @return {object} the metamodel for this model manager - */ -function modelManagerFromMetaModel(metaModel, validate = true) { - // First, validate the JSON metaModel - const mm = validate ? validateMetaModel(metaModel) : metaModel; + /** + * Validate metamodel instance against the metamodel + * @param {object} input - the metamodel instance in JSON + * @return {object} the validated metamodel instance in JSON + */ + static validateMetaModel(input) { + const metaModelManager = MetaModel.newMetaModelManager(); + const factory = new Factory(metaModelManager); + const serializer = new Serializer(factory, metaModelManager); + // First validate the metaModel + const object = serializer.fromJSON(input); + return serializer.toJSON(object); + } + + /** + * Import metamodel to a model manager + * @param {object} metaModel - the metamodel + * @param {boolean} [validate] - whether to perform validation + * @return {object} the metamodel for this model manager + */ + static modelManagerFromMetaModel(metaModel, validate = true) { + // First, validate the JSON metaModel + const mm = validate ? MetaModel.validateMetaModel(metaModel) : metaModel; + + const modelManager = new ModelManager(); - const modelManager = new ModelManager(); + mm.models.forEach((mm) => { + const mf = new ModelFile(modelManager, mm, null, null, true); + modelManager.addModelFile(mf, null, null); + }); - mm.models.forEach((mm) => { - const mf = new ModelFile(modelManager, mm, null, null, true); - modelManager.addModelFile(mf, null, null); - }); + modelManager.validateModelFiles(); + return modelManager; + } - modelManager.validateModelFiles(); - return modelManager; } -module.exports = { - newMetaModelManager, - validateMetaModel, - modelManagerFromMetaModel -}; +module.exports = MetaModel; diff --git a/packages/concerto-core/types/lib/introspect/metamodel.d.ts b/packages/concerto-core/types/lib/introspect/metamodel.d.ts index 43932c0032..6dbea552d0 100644 --- a/packages/concerto-core/types/lib/introspect/metamodel.d.ts +++ b/packages/concerto-core/types/lib/introspect/metamodel.d.ts @@ -1,18 +1,24 @@ +export = MetaModel; /** - * Create a metamodel manager (for validation against the metamodel) - * @return {*} the metamodel manager + * Class containing static helper methods for the metamodel. */ -export function newMetaModelManager(): any; -/** - * Validate metamodel instance against the metamodel - * @param {object} input - the metamodel instance in JSON - * @return {object} the validated metamodel instance in JSON - */ -export function validateMetaModel(input: object): object; -/** - * Import metamodel to a model manager - * @param {object} metaModel - the metamodel - * @param {boolean} [validate] - whether to perform validation - * @return {object} the metamodel for this model manager - */ -export function modelManagerFromMetaModel(metaModel: object, validate?: boolean): object; +declare class MetaModel { + /** + * Create a metamodel manager (for validation against the metamodel) + * @return {*} the metamodel manager + */ + static newMetaModelManager(): any; + /** + * Validate metamodel instance against the metamodel + * @param {object} input - the metamodel instance in JSON + * @return {object} the validated metamodel instance in JSON + */ + static validateMetaModel(input: object): object; + /** + * Import metamodel to a model manager + * @param {object} metaModel - the metamodel + * @param {boolean} [validate] - whether to perform validation + * @return {object} the metamodel for this model manager + */ + static modelManagerFromMetaModel(metaModel: object, validate?: boolean): object; +}