-
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(map): Extend Type support (#677)
* feat(map): add to introspection API Signed-off-by: jonathan.casey <[email protected]> * feat(map): extend JsonGenerator for Map Types Signed-off-by: jonathan.casey <[email protected]> * feat(map): extend JsonPopulator for Map Types Signed-off-by: jonathan.casey <[email protected]> * feat(map): extend ResourceValidator for Map Types Signed-off-by: jonathan.casey <[email protected]> * feat(map): add test cases for Map serialisation <> deserialisation Signed-off-by: jonathan.casey <[email protected]> * feat(map): cleanup Signed-off-by: jonathan.casey <[email protected]> * feat(map): add type definitions Signed-off-by: jonathan.casey <[email protected]> * feat(map): satisfy version checker Signed-off-by: jonathan.casey <[email protected]> * feat(map): remove variable assignment for chai import Signed-off-by: jonathan.casey <[email protected]> * feat(map): satisfy version checker Signed-off-by: jonathan.casey <[email protected]> * feat(map): update test cases to match on error handling changes Signed-off-by: jonathan.casey <[email protected]> * feat(map): cleanup Signed-off-by: jonathan.casey <[email protected]> * feat(map): cleanup test cases Signed-off-by: jonathan.casey <[email protected]> * test(map): add tests for missed cases Signed-off-by: Matt Roberts <[email protected]> * feat(map): fixes metamodel parsing for MapDeclarations Signed-off-by: jonathan.casey <[email protected]> * feat(map): printer update reads new metamodel map shape Signed-off-by: jonathan.casey <[email protected]> * feat(map): updates test data for new metamodel design Signed-off-by: jonathan.casey <[email protected]> * feat(map): initial rework of introspection Signed-off-by: jonathan.casey <[email protected]> * feat(map): add more test coverage Signed-off-by: jonathan.casey <[email protected]> * feat(map): improve error messaging & cleanup Signed-off-by: jonathan.casey <[email protected]> * feat(map): adds more test coverage for MapKeyType introspection Signed-off-by: jonathan.casey <[email protected]> * feat(map): adds more test coverage for MapValueType introspection Signed-off-by: jonathan.casey <[email protected]> * feat(map): refactor serialization Signed-off-by: jonathan.casey <[email protected]> * feat(map): refactor serialization test coverage Signed-off-by: jonathan.casey <[email protected]> * feat(map): cleanup Signed-off-by: jonathan.casey <[email protected]> * feat(map): DRY up code in the JSONPopulator Signed-off-by: jonathan.casey <[email protected]> * feat(map): adds coverage to new introspection functions Signed-off-by: jonathan.casey <[email protected]> * feat(map): adds type definitions Signed-off-by: jonathan.casey <[email protected]> * feat(map): updates changelog Signed-off-by: jonathan.casey <[email protected]> * fix: Private identifiers are only available when targeting ECMAScript 2015 and higher Signed-off-by: jonathan.casey <[email protected]> * fix: updates changelog Signed-off-by: jonathan.casey <[email protected]> * feat(map): moves isValidMapKey & isValidMapValue to ModelUtils Signed-off-by: jonathan.casey <[email protected]> * feat(map): removes unreachable default case Signed-off-by: jonathan.casey <[email protected]> * feat(map): removes unreachable code Signed-off-by: jonathan.casey <[email protected]> * feat(map): adds more test coverage Signed-off-by: jonathan.casey <[email protected]> * feat(map): remove dead code Signed-off-by: jonathan.casey <[email protected]> * feat(map): adds JSDoc @Private Signed-off-by: jonathan.casey <[email protected]> * feat(map): adds type defs & change log Signed-off-by: jonathan.casey <[email protected]> * feat(map): improve error message Signed-off-by: jonathan.casey <[email protected]> * feat(map): add util method Signed-off-by: jonathan.casey <[email protected]> * fix: update type def Signed-off-by: jonathan.casey <[email protected]> --------- Signed-off-by: jonathan.casey <[email protected]> Signed-off-by: Matt Roberts <[email protected]> Co-authored-by: Matt Roberts <[email protected]>
- Loading branch information
1 parent
e55a60d
commit f8da8a9
Showing
33 changed files
with
3,459 additions
and
833 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,10 @@ | |
'use strict'; | ||
|
||
const Decorated = require('./decorated'); | ||
const { MetaModelNamespace } = require('@accordproject/concerto-metamodel'); | ||
const IllegalModelException = require('../../lib/introspect/illegalmodelexception'); | ||
const ModelUtil = require('../modelutil'); | ||
|
||
|
||
// Types needed for TypeScript generation. | ||
/* eslint-disable no-unused-vars */ | ||
|
@@ -43,7 +46,6 @@ class MapValueType extends Decorated { | |
constructor(parent, ast) { | ||
super(ast); | ||
this.parent = parent; | ||
this.type = this.ast.name; | ||
this.process(); | ||
} | ||
|
||
|
@@ -55,6 +57,7 @@ class MapValueType extends Decorated { | |
*/ | ||
process() { | ||
super.process(); | ||
this.processType(this.ast); | ||
} | ||
|
||
/** | ||
|
@@ -64,23 +67,65 @@ class MapValueType extends Decorated { | |
* @protected | ||
*/ | ||
validate() { | ||
const declarations = this.getModelFile().getAllDeclarations(); | ||
|
||
const value = declarations.find(decl => decl.name === this.type); | ||
|
||
if (!value?.isConcept?.() && | ||
!value?.isEnum?.() && | ||
!value?.isAsset?.() && | ||
!value?.isEvent?.() && | ||
!value?.isParticipant?.() && | ||
!value?.isTransaction?.() && | ||
!value?.isMapDeclaration?.() && | ||
!value?.isScalarDeclaration?.() && | ||
!['String', 'Long', 'Integer', 'Double', 'Boolean', 'DateTime'].includes(this.type)) { | ||
|
||
throw new IllegalModelException( | ||
`MapPropertyType has invalid Type: ${this.type}` | ||
); | ||
if (!ModelUtil.isPrimitiveType(this.type)) { | ||
let decl = this.parent.getModelFile().getAllDeclarations().find(d => d.name === this.ast.type?.name); | ||
|
||
// 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}` | ||
); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Sets the Type name for the Map Value | ||
* | ||
* @param {Object} ast - The AST created by the parser | ||
* @private | ||
*/ | ||
processType(ast) { | ||
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)) { | ||
throw new IllegalModelException(`ObjectMapValueType must contain property 'type', for MapDeclaration named ${this.parent.name}`); | ||
} | ||
|
||
// ObjectMapValueType TypeIdentifier must be properly formed. | ||
if (!('$class' in ast.type) || !('name' in ast.type)) { | ||
throw new IllegalModelException(`ObjectMapValueType type must contain property '$class' and property 'name', for MapDeclaration named ${this.parent.name}`); | ||
} | ||
|
||
// And the $class must be valid. | ||
if (ast.type.$class !== '[email protected]') { | ||
throw new IllegalModelException(`ObjectMapValueType type $class must be of TypeIdentifier for MapDeclaration named ${this.parent.name}`); | ||
} | ||
|
||
this.type = decl.getName(); | ||
break; | ||
case `${MetaModelNamespace}.BooleanMapValueType`: | ||
this.type = 'Boolean'; | ||
break; | ||
case `${MetaModelNamespace}.DateTimeMapValueType`: | ||
this.type = 'DateTime'; | ||
break; | ||
case `${MetaModelNamespace}.StringMapValueType`: | ||
this.type = 'String'; | ||
break; | ||
case `${MetaModelNamespace}.IntegerMapValueType`: | ||
this.type = 'Integer'; | ||
break; | ||
case `${MetaModelNamespace}.LongMapValueType`: | ||
this.type = 'Long'; | ||
break; | ||
case `${MetaModelNamespace}.DoubleMapValueType`: | ||
this.type = 'Double'; | ||
break; | ||
} | ||
} | ||
|
||
|
@@ -121,6 +166,24 @@ class MapValueType extends Decorated { | |
return 'MapValueType {id=' + this.getType() + '}'; | ||
} | ||
|
||
/** | ||
* Returns true if this class is the definition of a Map Key. | ||
* | ||
* @return {boolean} true if the class is a Map Key | ||
*/ | ||
isKey() { | ||
return false; | ||
} | ||
|
||
/** | ||
* Returns true if this class is the definition of a Map Value. | ||
* | ||
* @return {boolean} true if the class is a Map Value | ||
*/ | ||
isValue() { | ||
return true; | ||
} | ||
|
||
} | ||
|
||
module.exports = MapValueType; |
Oops, something went wrong.