Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(*): use modelfile to getType() #742

Merged
merged 5 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have we got a test for enums and scalars as values?

Copy link
Member Author

@jonathan-casey jonathan-casey Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Map Key yes. Coverage for imported Scalars as Values I've just added for completeness (the same function is under test).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't support Enums as Map types (yet).

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");
Loading