Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions packages/concerto-core/src/basemodelmanager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ import decoratorModelModule from './decoratormodelhelper';
import type { ModelFileSource, ModelManagerOptions } from './types';
import type { AstNode } from './introspect/decorated';
type ModelFileInstance = InstanceType<typeof ModelFile>;
type ModelFileInput = string | ModelFileInstance;
type ModelFileInput = string | ModelFileInstance | Record<string, unknown>;

function isModelFile(obj: unknown): obj is ModelFileInstance {
return Boolean(obj) && (obj instanceof ModelFile || (typeof (obj as any).isModelFile === 'function' && (obj as any).isModelFile() === true));
}

function getFileNameFromIdentifier(fileIdentifier) {
const normalizedIdentifier = fileIdentifier.replace(/[\\/]+$/, '');
Expand Down Expand Up @@ -184,12 +188,13 @@ class BaseModelManager {
* @throws {IllegalModelException}
*/
validateModelFile(modelFile, fileName?) {
if (typeof modelFile === 'string') {
if (isModelFile(modelFile)) {
modelFile.validate();
} else {
const { ast } = this.processFile(fileName, modelFile);
let m = new ModelFile(this, ast as AstNode, modelFile, fileName);
const definitions = typeof modelFile === 'string' ? modelFile : null;
let m = new ModelFile(this, ast as AstNode, definitions, fileName);
m.validate();
} else {
modelFile.validate();
}
}

Expand Down Expand Up @@ -343,10 +348,11 @@ class BaseModelManager {
updateModelFile(modelFile, fileName?, disableValidation?) {
const NAME = 'updateModelFile';
debug(NAME, 'updateModelFile', modelFile, fileName);
if (typeof modelFile === 'string') {
if (!isModelFile(modelFile)) {
const { ast } = this.processFile(fileName, modelFile);
let m = new ModelFile(this, ast as AstNode, modelFile, fileName);
return this.updateModelFile(m,fileName,disableValidation);
const definitions = typeof modelFile === 'string' ? modelFile : null;
let m = new ModelFile(this, ast as AstNode, definitions, fileName);
return this.updateModelFile(m, fileName, disableValidation);
} else {
let existing = this.modelFiles[modelFile.getNamespace()];
if (!existing) {
Expand Down Expand Up @@ -400,11 +406,12 @@ class BaseModelManager {
}

let m: ModelFileInstance;
if (typeof modelFile === 'string') {
const { ast } = this.processFile(fileName, modelFile);
m = new ModelFile(this, ast as AstNode, modelFile, fileName);
} else {
if (isModelFile(modelFile)) {
Comment thread
Atharva15007 marked this conversation as resolved.
Resolved
m = modelFile;
} else {
const { ast } = this.processFile(fileName, modelFile);
const definitions = typeof modelFile === 'string' ? modelFile : null;
m = new ModelFile(this, ast as AstNode, definitions, fileName);
}
if (!m.getVersion()) {
throw new Error(`Cannot add an unversioned namespace: ${m.getNamespace()}`);
Expand Down
104 changes: 104 additions & 0 deletions packages/concerto-core/test/astmodelmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,108 @@ describe('AstModelManager', () => {
});
});

describe('#addModelFiles', () => {
it('should add multiple model files from AST objects', () => {
const modelContent1 = {
'$class': 'concerto.metamodel@1.0.0.Model',
'namespace': 'org.example.test1@1.0.0',
'imports': [],
'declarations': [
{
'$class': 'concerto.metamodel@1.0.0.ConceptDeclaration',
'name': 'Concept1',
'isAbstract': false,
'properties': [],
'decorators': []
}
]
};
const modelContent2 = {
'$class': 'concerto.metamodel@1.0.0.Model',
'namespace': 'org.example.test2@1.0.0',
'imports': [],
'declarations': [
{
'$class': 'concerto.metamodel@1.0.0.ConceptDeclaration',
'name': 'Concept2',
'isAbstract': false,
'properties': [],
'decorators': []
}
]
};

const added = modelManager.addModelFiles([modelContent1, modelContent2]);
added.should.have.lengthOf(2);
modelManager.getModelFile('org.example.test1@1.0.0').should.not.be.undefined;
modelManager.getModelFile('org.example.test2@1.0.0').should.not.be.undefined;
});
});

describe('#updateModelFile', () => {
it('should update a model file from an AST object', () => {
const initialContent = {
'$class': 'concerto.metamodel@1.0.0.Model',
'namespace': 'org.example.update@1.0.0',
'imports': [],
'declarations': [
{
'$class': 'concerto.metamodel@1.0.0.ConceptDeclaration',
'name': 'InitialConcept',
'isAbstract': false,
'properties': [],
'decorators': []
}
]
};

modelManager.addModel(initialContent);
modelManager.getType('org.example.update@1.0.0.InitialConcept').should.not.be.null;

const updatedContent = {
'$class': 'concerto.metamodel@1.0.0.Model',
'namespace': 'org.example.update@1.0.0',
'imports': [],
'declarations': [
{
'$class': 'concerto.metamodel@1.0.0.ConceptDeclaration',
'name': 'UpdatedConcept',
'isAbstract': false,
'properties': [],
'decorators': []
}
]
};

modelManager.updateModelFile(updatedContent);
modelManager.getType('org.example.update@1.0.0.UpdatedConcept').should.not.be.null;
(() => {
modelManager.getType('org.example.update@1.0.0.InitialConcept');
}).should.throw();
});
});

describe('#validateModelFile', () => {
it('should validate an AST object model', () => {
const modelContent = {
'$class': 'concerto.metamodel@1.0.0.Model',
'namespace': 'org.example.validate@1.0.0',
'imports': [],
'declarations': [
{
'$class': 'concerto.metamodel@1.0.0.ConceptDeclaration',
'name': 'ValidConcept',
'isAbstract': false,
'properties': [],
'decorators': []
}
]
};

(() => {
modelManager.validateModelFile(modelContent);
}).should.not.throw();
Comment thread
Atharva15007 marked this conversation as resolved.
Resolved
});
});

});
Loading