Skip to content

Commit

Permalink
fix: remove custom hasInstance #47
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Selman <danscode@selman.org>
  • Loading branch information
dselman committed Nov 22, 2021
1 parent 26641e2 commit 4b51d77
Show file tree
Hide file tree
Showing 51 changed files with 1,561 additions and 2,102 deletions.
86 changes: 37 additions & 49 deletions packages/concerto-core/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,14 @@ class Factory {
+ Relationship newRelationship(String,String,String) throws TypeNotFoundException
+ Resource newTransaction(String,String,String,Object,String,boolean)
+ Resource newEvent(String,String,String,Object,String,boolean)
+ boolean hasInstance(object)
}
class AssetDeclaration extends IdentifiedDeclaration {
+ void constructor(ModelFile,Object) throws IllegalModelException
+ boolean hasInstance(object)
}
class ClassDeclaration extends Decorated {
+ void constructor(ModelFile,Object) throws IllegalModelException
+ ClassDeclaration _resolveSuperType()
+ boolean isAbstract()
+ boolean isEnum()
+ boolean isConcept()
+ boolean isEvent()
+ string getName()
+ string getNamespace()
+ string getFullyQualifiedName()
Expand All @@ -62,12 +57,16 @@ class ClassDeclaration extends Decorated {
+ Property[] getProperties()
+ Property getNestedProperty(string) throws IllegalModelException
+ String toString()
+ boolean hasInstance(object)
+ boolean isAsset()
+ boolean isParticipant()
+ boolean isTransaction()
+ boolean isEvent()
+ boolean isConcept()
+ boolean isEnum()
+ boolean isClassDeclaration()
}
class ConceptDeclaration extends ClassDeclaration {
+ void constructor(ModelFile,Object) throws IllegalModelException
+ boolean isConcept()
+ boolean hasInstance(object)
}
class Decorator {
+ void constructor(Object) throws IllegalModelException
Expand All @@ -80,22 +79,17 @@ class DecoratorFactory {
}
class EnumDeclaration extends ClassDeclaration {
+ void constructor(ModelFile,Object) throws IllegalModelException
+ boolean isEnum()
+ String toString()
+ boolean hasInstance(object)
}
class EnumValueDeclaration extends Property {
+ void constructor(ClassDeclaration,Object) throws IllegalModelException
+ boolean hasInstance(object)
+ boolean isEnumValue()
}
class EventDeclaration extends IdentifiedDeclaration {
+ void constructor(ModelFile,Object) throws IllegalModelException
+ boolean isEvent()
+ boolean hasInstance(object)
}
class IdentifiedDeclaration extends ClassDeclaration {
+ void constructor(ModelFile,Object) throws IllegalModelException
+ boolean hasInstance(object)
}
class IllegalModelException extends BaseFileException {
+ void constructor(string,ModelFile,Object,number,number,number,number,string)
Expand All @@ -110,37 +104,36 @@ class ModelFileDownloader {
+ Promise downloadExternalDependencies(ModelFile[],Object)
+ Promise runJob(Object,Object)
}
class MetaModel {
+ void createMetaModelManager()
+ object validateMetaModel(object)
+ object createNameTable(object)
+ string resolveName(string,object)
+ object resolveTypeNames(object,object)
+ object resolveMetaModel(object,object)
+ object enumPropertyToMetaModel(object)
+ object decoratorArgToMetaModel(object)
+ object decoratorToMetaModel(object)
+ object decoratorsToMetaModel(object)
+ object propertyToMetaModel(object)
+ object relationshipToMetaModel(object)
+ object enumDeclToMetaModel(object)
+ object conceptDeclToMetaModel(object)
+ object declToMetaModel(object)
+ object modelToMetaModel(object,boolean)
+ object modelFileToMetaModel(object,boolean)
+ object modelManagerToMetaModel(object,boolean,boolean)
+ string decoratorArgFromMetaModel(object)
+ string decoratorFromMetaModel(object)
+ string decoratorsFromMetaModel(object,string)
+ string propertyFromMetaModel(object)
+ string declFromMetaModel(object)
+ string ctoFromMetaModel(object,boolean)
+ object modelManagerFromMetaModel(object,boolean)
+ object ctoToMetaModel(string,boolean)
+ object ctoToMetaModelAndResolve(string,boolean)
}
+ object validateMetaModel()
+ object createNameTable()
+ string resolveName()
+ object resolveTypeNames()
+ object resolveMetaModel()
+ object enumPropertyToMetaModel()
+ object decoratorArgToMetaModel()
+ object decoratorToMetaModel()
+ object decoratorsToMetaModel()
+ object propertyToMetaModel()
+ object relationshipToMetaModel()
+ object enumDeclToMetaModel()
+ object conceptDeclToMetaModel()
+ object declToMetaModel()
+ object modelToMetaModel()
+ object modelFileToMetaModel()
+ object modelManagerToMetaModel()
+ string decoratorArgFromMetaModel()
+ string decoratorFromMetaModel()
+ string decoratorsFromMetaModel()
+ string propertyFromMetaModel()
+ string declFromMetaModel()
+ string ctoFromMetaModel()
+ object modelManagerFromMetaModel()
+ object ctoToMetaModel()
+ object ctoToMetaModelAndResolve()
class ModelFile {
+ void constructor(ModelManager,string,string) throws IllegalModelException
+ boolean isModelFile()
+ Boolean isSystemModelFile()
+ boolean isExternal()
+ ModelManager getModelManager()
Expand All @@ -164,11 +157,9 @@ class ModelFile {
+ string getDefinitions()
+ string getConcertoVersion()
+ void isCompatibleVersion()
+ boolean hasInstance(object)
}
class ParticipantDeclaration extends IdentifiedDeclaration {
+ void constructor(ModelFile,Object) throws IllegalModelException
+ boolean hasInstance(object)
}
class Property extends Decorated {
+ void constructor(ClassDeclaration,Object) throws IllegalModelException
Expand All @@ -182,16 +173,14 @@ class Property extends Decorated {
+ boolean isArray()
+ boolean isTypeEnum()
+ boolean isPrimitive()
+ boolean hasInstance(object)
}
class RelationshipDeclaration extends Property {
+ void constructor(ClassDeclaration,Object) throws IllegalModelException
+ String toString()
+ boolean hasInstance(object)
+ boolean isRelationship()
}
class TransactionDeclaration extends IdentifiedDeclaration {
+ void constructor(ModelFile,Object) throws IllegalModelException
+ boolean hasInstance(object)
}
class Identifiable extends Typed {
+ string getTimestamp()
Expand Down Expand Up @@ -234,6 +223,7 @@ class ModelLoader {
}
class ModelManager {
+ void constructor(object)
+ boolean isModelManager()
+ Object accept(Object,Object)
+ void validateModelFile(string,string) throws IllegalModelException
+ Object addModelFile(string,string,boolean) throws IllegalModelException
Expand All @@ -258,7 +248,6 @@ class ModelManager {
+ DecoratorFactory[] getDecoratorFactories()
+ void addDecoratorFactory(DecoratorFactory)
+ boolean derivesFrom(string,string)
+ boolean hasInstance(object)
}
class SecurityException extends BaseException {
+ void constructor(string)
Expand All @@ -268,7 +257,6 @@ class Serializer {
+ void setDefaultOptions(Object)
+ Object toJSON(Resource,Object,boolean,boolean,boolean,boolean,boolean,number) throws Error
+ Resource fromJSON(Object,Object,boolean,boolean,number)
+ boolean hasInstance(object)
}
class TypedStack {
+ void constructor(Object)
Expand Down
3 changes: 3 additions & 0 deletions packages/concerto-core/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
# Note that the latest public API is documented using JSDocs and is available in api.txt.
#

Version 1.2.2 {a7ba5a1d93717527babdda88941e459f} 2021-11-22
- Remove custom instanceof and add methods to check runtime type

Version 1.2.2 {8149560555027394c95a893c1e442d67} 2021-11-15
- Convert MetaModel to a class for Typescript + Webpack compatability
- Update Acorn to latest and refactor the JavaScript parser so we can use static class members
Expand Down
22 changes: 3 additions & 19 deletions packages/concerto-core/lib/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ const Relationship = require('./model/relationship');
const Resource = require('./model/resource');
const ValidatedResource = require('./model/validatedresource');

const TransactionDeclaration = require('./introspect/transactiondeclaration');
const EventDeclaration = require('./introspect/eventdeclaration');

const uuid = require('uuid');

const dayjs = require('dayjs');
Expand Down Expand Up @@ -60,7 +57,6 @@ class Factory {
*/
constructor(modelManager) {
this.modelManager = modelManager;
this._isFactory = true;
}

/**
Expand Down Expand Up @@ -120,7 +116,7 @@ class Factory {

let newObj = null;
let timestamp = null;
if (classDecl instanceof TransactionDeclaration || classDecl instanceof EventDeclaration) {
if (classDecl.isTransaction() || classDecl.isEvent()) {
timestamp = dayjs.utc();
}
if(options.disableValidation) {
Expand Down Expand Up @@ -206,7 +202,7 @@ class Factory {
let transaction = this.newResource(ns, type, id, options);
const classDeclaration = transaction.getClassDeclaration();

if (!(classDeclaration instanceof TransactionDeclaration)) {
if (!classDeclaration.isTransaction()) {
throw new Error(transaction.getClassDeclaration().getFullyQualifiedName() + ' is not a transaction');
}

Expand Down Expand Up @@ -236,7 +232,7 @@ class Factory {
let event = this.newResource(ns, type, id, options);
const classDeclaration = event.getClassDeclaration();

if (!(classDeclaration instanceof EventDeclaration)) {
if (!classDeclaration.isEvent()) {
throw new Error(event.getClassDeclaration().getFullyQualifiedName() + ' is not an event');
}

Expand Down Expand Up @@ -291,18 +287,6 @@ class Factory {

return generateParams;
}


/**
* Alternative instanceof that is reliable across different module instances
* @see https://github.com/hyperledger/composer-concerto/issues/47
*
* @param {object} object - The object to test against
* @returns {boolean} - True, if the object is an instance of a Factory
*/
static [Symbol.hasInstance](object){
return typeof object !== 'undefined' && object !== null && Boolean(object._isFactory);
}
}

module.exports = Factory;
12 changes: 0 additions & 12 deletions packages/concerto-core/lib/introspect/assetdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,6 @@ class AssetDeclaration extends IdentifiedDeclaration {
*/
constructor(modelFile, ast) {
super(modelFile, ast);
this._isAssetDeclaration = true;
}

/**
* Alternative instanceof that is reliable across different module instances
* @see https://github.com/hyperledger/composer-concerto/issues/47
*
* @param {object} object - The object to test against
* @returns {boolean} - True, if the object is an instance of a AssetDeclaration
*/
static [Symbol.hasInstance](object){
return typeof object !== 'undefined' && object !== null && Boolean(object._isAssetDeclaration);
}
}

Expand Down
92 changes: 58 additions & 34 deletions packages/concerto-core/lib/introspect/classdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class ClassDeclaration extends Decorated {
this.idField = null;
this.timestamped = false;
this.abstract = false;
this.type = this.ast.type;

if (this.ast.abstract) {
this.abstract = true;
Expand Down Expand Up @@ -107,7 +108,6 @@ class ClassDeclaration extends Decorated {
}

this.fqn = ModelUtil.getFullyQualifiedName(this.modelFile.getNamespace(), this.name);
this._isClassDeclaration = true;

if (this.fqn === 'concerto.Transaction' || this.fqn === 'concerto.Event') {
this.addTimestampField();
Expand Down Expand Up @@ -283,33 +283,6 @@ class ClassDeclaration extends Decorated {
return this.abstract;
}

/**
* Returns true if this class is an enumeration.
*
* @return {boolean} true if the class is an enumerated type
*/
isEnum() {
return false;
}

/**
* Returns true if this class is the definition of a concept.
*
* @return {boolean} true if the class is a concept
*/
isConcept() {
return false;
}

/**
* Returns true if this class is the definition of an event.
*
* @return {boolean} true if the class is an event
*/
isEvent() {
return false;
}

/**
* Returns the short name of a class. This name does not include the
* namespace from the owning ModelFile.
Expand Down Expand Up @@ -602,16 +575,67 @@ class ClassDeclaration extends Decorated {
}

/**
* Alternative instanceof that is reliable across different module instances
* @see https://github.com/hyperledger/composer-concerto/issues/47
* Returns true if this class is the definition of an asset.
*
* @return {boolean} true if the class is an asset
*/
isAsset() {
return this.type === 'AssetDeclaration';
}

/**
* Returns true if this class is the definition of a participant.
*
* @return {boolean} true if the class is an asset
*/
isParticipant() {
return this.type === 'ParticipantDeclaration';
}

/**
* Returns true if this class is the definition of a transaction.
*
* @return {boolean} true if the class is an asset
*/
isTransaction() {
return this.type === 'TransactionDeclaration';
}

/**
* Returns true if this class is the definition of an event.
*
* @param {object} object - The object to test against
* @returns {boolean} - True, if the object is an instance of a Class Declaration
* @return {boolean} true if the class is an asset
*/
static [Symbol.hasInstance](object){
return typeof object !== 'undefined' && object !== null && Boolean(object._isClassDeclaration);
isEvent() {
return this.type === 'EventDeclaration';
}

/**
* Returns true if this class is the definition of a concept.
*
* @return {boolean} true if the class is an asset
*/
isConcept() {
return this.type === 'ConceptDeclaration';
}

/**
* Returns true if this class is the definition of a enum.
*
* @return {boolean} true if the class is an asset
*/
isEnum() {
return this.type === 'EnumDeclaration';
}

/**
* Returns true if this class is the definition of a enum.
*
* @return {boolean} true if the class is an asset
*/
isClassDeclaration() {
return true;
}
}

module.exports = ClassDeclaration;
Loading

0 comments on commit 4b51d77

Please sign in to comment.