Skip to content

Commit

Permalink
feat(*): Support Import Types for Map Values (#690)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
jonathan-casey authored Aug 21, 2023
1 parent b8c6256 commit 43c96e0
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 5 deletions.
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 = String(this.ast.type.name); // cast for correct type resolution in generated types.

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
10 changes: 10 additions & 0 deletions 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 Down Expand Up @@ -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");

0 comments on commit 43c96e0

Please sign in to comment.