Skip to content

Commit

Permalink
feat(*): use modelfile to getType() (#742)
Browse files Browse the repository at this point in the history
* feat(map): get type from model file

Signed-off-by: Jonathan Casey <[email protected]>

* feat(map): update type def

Signed-off-by: Jonathan Casey <[email protected]>

* chore(*): rebase package-lock

Signed-off-by: Jonathan Casey <[email protected]>

* test(map): add coverage for Map Value scalar import

Signed-off-by: Jonathan Casey <[email protected]>

---------

Signed-off-by: Jonathan Casey <[email protected]>
  • Loading branch information
jonathan-casey authored Nov 7, 2023
1 parent b4140f6 commit f57148f
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 29 deletions.
3 changes: 2 additions & 1 deletion packages/concerto-core/lib/introspect/mapkeytype.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ class MapKeyType extends Decorated {
validate() {

if (!ModelUtil.isPrimitiveType(this.type)) {
const decl = ModelUtil.getTypeDeclaration(this.ast.type.name, this.modelFile);

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

if (!ModelUtil.isValidMapKeyScalar(decl)) {
throw new IllegalModelException(
Expand Down
3 changes: 2 additions & 1 deletion packages/concerto-core/lib/introspect/mapvaluetype.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ class MapValueType extends Decorated {
*/
validate() {
if (!ModelUtil.isPrimitiveType(this.type)) {
const decl = ModelUtil.getTypeDeclaration(this.ast.type.name, this.modelFile);

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

// All declarations, with the exception of MapDeclarations, are valid Values.
if(decl.isMapDeclaration?.()) {
Expand Down
15 changes: 0 additions & 15 deletions packages/concerto-core/lib/modelutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,21 +338,6 @@ class ModelUtil {
`${MetaModelNamespace}.ObjectMapValueType`
].includes(value.$class);
}

/**
* Returns the corresponding ClassDeclaration representation of the Map Type
* @param {string} type - the Type of the Map Value
* @param {ModelFile} modelFile - the ModelFile that owns the Property
* @return {Object} the corresponding ClassDeclaration representation
*/
static getTypeDeclaration(type, modelFile) {
if (modelFile.isLocalType(type)) {
return modelFile.getAllDeclarations().find(d => d.name === type);
} else {
const fqn = modelFile.resolveImport(type);
return modelFile.getModelManager().getType(fqn);
}
}
}

module.exports = ModelUtil;
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ concept Thing {
}

scalar Time extends DateTime

enum Stuff {
o Thing
o Foo
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
*/
namespace [email protected]

participant Customer identified by email {
participant CustomerParticipant identified by email {
o String email
}

map Customer {
o String
o Customer
o CustomerParticipant
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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 [email protected]

import [email protected]

map Dictionary {
o String
o Time
}
11 changes: 9 additions & 2 deletions packages/concerto-core/test/introspect/mapdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,17 @@ describe('MapDeclaration', () => {
decl.validate();
});

it('should validate when map value is imported and is of valid map key type', () => {
it('should validate when map value is imported and is of valid map key type (Concept import)', () => {
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.goodvalue.imported.thing.cto', MapDeclaration);
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.goodvalue.imported.concept.cto', MapDeclaration);
decl.validate();
});

it('should validate when map value is imported and is of valid map key type (Scalar Import)', () => {
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.goodvalue.imported.scalar.cto', MapDeclaration);
decl.validate();
});
});
Expand Down
8 changes: 0 additions & 8 deletions packages/concerto-core/types/lib/modelutil.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,4 @@ declare class ModelUtil {
* @return {boolean} true if the Value is a valid Map Value
*/
static isValidMapValue(value: any): boolean;
/**
* Returns the corresponding ClassDeclaration representation of the Map Type
* @param {string} type - the Type of the Map Value
* @param {ModelFile} modelFile - the ModelFile that owns the Property
* @return {Object} the corresponding ClassDeclaration representation
*/
static getTypeDeclaration(type: string, modelFile: ModelFile): any;
}
import ModelFile = require("../lib/introspect/modelfile");

0 comments on commit f57148f

Please sign in to comment.