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(*): Support Import Types for Map Values #690

Merged
merged 8 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
6 changes: 6 additions & 0 deletions packages/concerto-core/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
# Note that the latest public API is documented using JSDocs and is available in api.txt.
#

Version 3.11.1 {3bba55da8dbb522de132359415eb7eeb} 2023-08-11
- Makes getTypeDeclaration private

Version 3.11.0 {965b06a2079d06b3679a8c73034ba24b} 2023-08-11
- Add getTypeDeclaration to MapValueType

Version 3.10.0 {3bba55da8dbb522de132359415eb7eeb} 2023-08-11
- Add MetamodelException

Expand Down
25 changes: 21 additions & 4 deletions packages/concerto-core/lib/introspect/mapvaluetype.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class MapValueType extends Decorated {
constructor(parent, ast) {
super(ast);
this.parent = parent;
this.modelFile = parent.getModelFile();
this.process();
}

Expand All @@ -68,9 +69,9 @@ class MapValueType extends Decorated {
*/
validate() {
if (!ModelUtil.isPrimitiveType(this.type)) {
let decl = this.parent.getModelFile().getAllDeclarations().find(d => d.name === this.ast.type?.name);
const decl = this.getTypeDeclaration(this.ast.type.name);

// All declarations, with the exception of MapDeclarations are valid Values.
// All declarations, with the exception of MapDeclarations, are valid Values.
if(decl.isMapDeclaration?.()) {
throw new IllegalModelException(
`MapDeclaration as Map Type Value is not supported: ${this.type}`
Expand All @@ -89,7 +90,6 @@ class MapValueType extends Decorated {
let decl;
switch(this.ast.$class) {
case `${MetaModelNamespace}.ObjectMapValueType`:
decl = this.parent.getModelFile().getAllDeclarations().find(d => d.name === this.ast.type.name);

// ObjectMapValueType must have TypeIdentifier.
if (!('type' in ast)) {
Expand All @@ -106,7 +106,8 @@ class MapValueType extends Decorated {
throw new IllegalModelException(`ObjectMapValueType type $class must be of TypeIdentifier for MapDeclaration named ${this.parent.name}`);
}

this.type = decl.getName();
this.type = this.ast.type.name;

break;
case `${MetaModelNamespace}.BooleanMapValueType`:
this.type = 'Boolean';
Expand Down Expand Up @@ -184,6 +185,22 @@ class MapValueType extends Decorated {
return true;
}

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

}

module.exports = MapValueType;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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]

map Dictionary {
o String
o Asset
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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]

map Dictionary {
o String
o Concept
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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]

map Dictionary {
o String
o Event
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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]

map Dictionary {
o String
o Participant
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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]

map Dictionary {
o String
o Transaction
}
27 changes: 26 additions & 1 deletion packages/concerto-core/test/introspect/mapdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,32 @@ describe('MapDeclaration', () => {
});

it('should validate when map value is an identified concept declaration', () => {
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.goodvalue.identified.declaration.concept.cto', MapDeclaration);
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.goodvalue.declaration.identified.concept.cto', MapDeclaration);
decl.validate();
});

it('should validate when map value is an imported asset declaration', () => {
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.goodvalue.declaration.root.asset.cto', MapDeclaration);
decl.validate();
});

it('should validate when map value is an imported concept declaration', () => {
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.goodvalue.declaration.root.concept.cto', MapDeclaration);
decl.validate();
});

it('should validate when map value is an imported event declaration', () => {
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.goodvalue.declaration.root.event.cto', MapDeclaration);
decl.validate();
});

it('should validate when map value is an imported participant declaration', () => {
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.goodvalue.declaration.root.participant.cto', MapDeclaration);
decl.validate();
});

it('should validate when map value is an imported transaction declaration', () => {
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.goodvalue.declaration.root.transaction.cto', MapDeclaration);
decl.validate();
});

Expand Down
12 changes: 11 additions & 1 deletion packages/concerto-core/types/lib/introspect/mapvaluetype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ declare class MapValueType extends Decorated {
*/
constructor(parent: MapDeclaration, ast: any);
parent: MapDeclaration;
modelFile: ModelFile;
/**
* Semantic validation of the structure of this class.
*
Expand All @@ -30,7 +31,7 @@ declare class MapValueType extends Decorated {
* @private
*/
private processType;
type: string;
type: any;
/**
* Returns the owner of this property
* @public
Expand All @@ -56,6 +57,15 @@ declare class MapValueType extends Decorated {
* @return {boolean} true if the class is a Map Value
*/
isValue(): boolean;
/**
* Returns the corresponding ClassDeclaration representation of the Type
*
* @param {string} type - the Type of the Map Value
* @return {Object} the corresponding ClassDeclaration representation
* @private
*/
private getTypeDeclaration;
}
import Decorated = require("./decorated");
import MapDeclaration = require("./mapdeclaration");
import ModelFile = require("./modelfile");