Skip to content

Commit 43c96e0

Browse files
feat(*): Support Import Types for Map Values (#690)
* feat(maps): support import types for Map values Signed-off-by: jonathan.casey <[email protected]> * feat(maps): adds changelog hash Signed-off-by: jonathan.casey <[email protected]> * feat(maps): makes getTypeDeclaration private Signed-off-by: jonathan.casey <[email protected]> * feat(maps): formatting Signed-off-by: jonathan.casey <[email protected]> * feat(maps): adds changelog hash Signed-off-by: jonathan.casey <[email protected]> * feat(maps): adds api.txt Signed-off-by: jonathan.casey <[email protected]> * feat(maps): explicit cast for stronger type definition Signed-off-by: jonathan.casey <[email protected]> * feat(maps): removes changelog entries Signed-off-by: jonathan.casey <[email protected]> --------- Signed-off-by: jonathan.casey <[email protected]>
1 parent b8c6256 commit 43c96e0

9 files changed

+152
-5
lines changed

packages/concerto-core/lib/introspect/mapvaluetype.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class MapValueType extends Decorated {
4646
constructor(parent, ast) {
4747
super(ast);
4848
this.parent = parent;
49+
this.modelFile = parent.getModelFile();
4950
this.process();
5051
}
5152

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

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

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

109-
this.type = decl.getName();
109+
this.type = String(this.ast.type.name); // cast for correct type resolution in generated types.
110+
110111
break;
111112
case `${MetaModelNamespace}.BooleanMapValueType`:
112113
this.type = 'Boolean';
@@ -184,6 +185,22 @@ class MapValueType extends Decorated {
184185
return true;
185186
}
186187

188+
/**
189+
* Returns the corresponding ClassDeclaration representation of the Type
190+
*
191+
* @param {string} type - the Type of the Map Value
192+
* @return {Object} the corresponding ClassDeclaration representation
193+
* @private
194+
*/
195+
getTypeDeclaration(type) {
196+
if (this.modelFile.isLocalType(this.ast.type.name)) {
197+
return this.modelFile.getAllDeclarations().find(d => d.name === this.ast.type.name);
198+
} else {
199+
const fqn = this.modelFile.resolveImport(this.ast.type.name);
200+
return this.modelFile.getModelManager().getType(fqn);
201+
}
202+
}
203+
187204
}
188205

189206
module.exports = MapValueType;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
16+
map Dictionary {
17+
o String
18+
o Asset
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
16+
map Dictionary {
17+
o String
18+
o Concept
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
16+
map Dictionary {
17+
o String
18+
o Event
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
16+
map Dictionary {
17+
o String
18+
o Participant
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
16+
map Dictionary {
17+
o String
18+
o Transaction
19+
}

packages/concerto-core/test/introspect/mapdeclaration.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,32 @@ describe('MapDeclaration', () => {
153153
});
154154

155155
it('should validate when map value is an identified concept declaration', () => {
156-
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.goodvalue.identified.declaration.concept.cto', MapDeclaration);
156+
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.goodvalue.declaration.identified.concept.cto', MapDeclaration);
157+
decl.validate();
158+
});
159+
160+
it('should validate when map value is an imported asset declaration', () => {
161+
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.goodvalue.declaration.root.asset.cto', MapDeclaration);
162+
decl.validate();
163+
});
164+
165+
it('should validate when map value is an imported concept declaration', () => {
166+
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.goodvalue.declaration.root.concept.cto', MapDeclaration);
167+
decl.validate();
168+
});
169+
170+
it('should validate when map value is an imported event declaration', () => {
171+
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.goodvalue.declaration.root.event.cto', MapDeclaration);
172+
decl.validate();
173+
});
174+
175+
it('should validate when map value is an imported participant declaration', () => {
176+
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.goodvalue.declaration.root.participant.cto', MapDeclaration);
177+
decl.validate();
178+
});
179+
180+
it('should validate when map value is an imported transaction declaration', () => {
181+
let decl = introspectUtils.loadLastDeclaration('test/data/parser/mapdeclaration/mapdeclaration.goodvalue.declaration.root.transaction.cto', MapDeclaration);
157182
decl.validate();
158183
});
159184

packages/concerto-core/types/lib/introspect/mapvaluetype.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ declare class MapValueType extends Decorated {
1616
*/
1717
constructor(parent: MapDeclaration, ast: any);
1818
parent: MapDeclaration;
19+
modelFile: ModelFile;
1920
/**
2021
* Semantic validation of the structure of this class.
2122
*
@@ -56,6 +57,15 @@ declare class MapValueType extends Decorated {
5657
* @return {boolean} true if the class is a Map Value
5758
*/
5859
isValue(): boolean;
60+
/**
61+
* Returns the corresponding ClassDeclaration representation of the Type
62+
*
63+
* @param {string} type - the Type of the Map Value
64+
* @return {Object} the corresponding ClassDeclaration representation
65+
* @private
66+
*/
67+
private getTypeDeclaration;
5968
}
6069
import Decorated = require("./decorated");
6170
import MapDeclaration = require("./mapdeclaration");
71+
import ModelFile = require("./modelfile");

0 commit comments

Comments
 (0)