Skip to content

Commit

Permalink
fix(core) Replace Concerto.instanceOf by ModelManager.derivesFrom
Browse files Browse the repository at this point in the history
Signed-off-by: Jerome Simeon <[email protected]>
  • Loading branch information
jeromesimeon committed Apr 23, 2021
1 parent 977f4fb commit 924889a
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 88 deletions.
2 changes: 1 addition & 1 deletion packages/concerto-core/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class Concerto {
+ void fromURI(string) throws Error
+ string getType()
+ string getNamespace()
+ boolean instanceOf(String)
}
+ object setCurrentTime()
class Factory {
Expand Down Expand Up @@ -217,6 +216,7 @@ class ModelManager {
+ Serializer getSerializer()
+ DecoratorFactory[] getDecoratorFactories()
+ void addDecoratorFactory(DecoratorFactory)
+ boolean derivesFrom(String,String)
+ boolean hasInstance(object)
}
class SecurityException extends BaseException {
Expand Down
5 changes: 3 additions & 2 deletions packages/concerto-core/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
# Note that the latest public API is documented using JSDocs and is available in api.txt.
#

Version 1.0.0-alpha.8 {e282ac1f06ae25ddd4f644bba3d133b4} 2021-04-22
Version 1.0.0-alpha.8 {08b0c809336354e7d1b17a2e44fca8b0} 2021-04-22
- Add datetimeutil & Consolidate UTC offset support
- Delete Concerto.instanceOf replace by ModelManager.derivesFrom

Version 1.0.0-alpha.7 {e791f955bbe9bdac6bcd8944d6111e1a} 2021-04-17
Version 1.0.0-alpha.7 {aec0f0d4e25be4dab03d6b94be60767b} 2021-04-17
- Remove unused systemtime options in modelfile API
- Fixes to the validation option targeting the ergo ES6 runtime

Expand Down
25 changes: 0 additions & 25 deletions packages/concerto-core/lib/concerto.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,31 +205,6 @@ class Concerto {
getNamespace(obj) {
return this.getTypeDeclaration(obj).getNamespace();
}

/**
* Check to see if this instance is an instance of the specified fully qualified
* type name.
* @param {*} obj the input object
* @param {String} fqt The fully qualified type name.
* @returns {boolean} True if this instance is an instance of the specified fully
* qualified type name, false otherwise.
*/
instanceOf(obj, fqt) {
// Check to see if this is an exact instance of the specified type.
const classDeclaration = this.getTypeDeclaration(obj);
if (classDeclaration.getFullyQualifiedName() === fqt) {
return true;
}
// Now walk the class hierachy looking to see if it's an instance of the specified type.
let superTypeDeclaration = classDeclaration.getSuperTypeDeclaration();
while (superTypeDeclaration) {
if (superTypeDeclaration.getFullyQualifiedName() === fqt) {
return true;
}
superTypeDeclaration = superTypeDeclaration.getSuperTypeDeclaration();
}
return false;
}
}

module.exports = Concerto;
19 changes: 19 additions & 0 deletions packages/concerto-core/lib/modelmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,25 @@ abstract concept Event {}
this.decoratorFactories.push(factory);
}

/**
* Checks if this fully qualified type name is derived from another.
* @param {String} fqt1 The fully qualified type name to check.
* @param {String} fqt2 The fully qualified type name it is may be derived from.
* @returns {boolean} True if this instance is an instance of the specified fully
* qualified type name, false otherwise.
*/
derivesFrom(fqt1, fqt2) {
// Check to see if this is an exact instance of the specified type.
let typeDeclaration = this.getType(fqt1);
while (typeDeclaration) {
if (typeDeclaration.getFullyQualifiedName() === fqt2) {
return true;
}
typeDeclaration = typeDeclaration.getSuperTypeDeclaration();
}
return false;
}

/**
* Alternative instanceof that is reliable across different module instances
* @see https://github.com/hyperledger/composer-concerto/issues/47
Expand Down
60 changes: 0 additions & 60 deletions packages/concerto-core/test/concerto.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
const fs = require('fs');
const chai = require('chai');

const expect = chai.expect;
// eslint-disable-next-line no-unused-vars
const should = chai.should();
chai.use(require('chai-things'));
Expand Down Expand Up @@ -229,65 +228,6 @@ describe('concerto', () => {
});
});

describe('#instanceOf', () => {

it('should get instanceOf for sub type', () => {
const obj = {
$class : 'org.accordproject.test.Customer',
ssn: '123456789',
customerId: '001',
name: 'Dan Selman'
};

const result = concerto.instanceOf(obj, 'org.accordproject.test.Person');
result.should.be.true;
});

it('should get instanceOf for sub-sub type', () => {
const obj = {
$class : 'org.accordproject.test.Manager',
ssn: '123456789',
customerId: '001',
name: 'Dan Selman'
};

const result = concerto.instanceOf(obj, 'org.accordproject.test.Person');
result.should.be.true;
});

it('should get instanceOf for type', () => {
const obj = {
$class : 'org.accordproject.test.Customer',
ssn: '123456789',
customerId: '001',
name: 'Dan Selman'
};

const result = concerto.instanceOf(obj, 'org.accordproject.test.Customer');
result.should.be.true;
});

it('should not get instanceOf for derived type', () => {
const obj = {
$class : 'org.accordproject.test.Person',
ssn: '123456789'
};

const result = concerto.instanceOf(obj, 'org.accordproject.test.Customer');
expect(result).to.be.false;
});

it('all types should be an instance of concerto.Concept', () => {
const obj = {
$class : 'org.accordproject.test.Person',
ssn: '123456789'
};

const result = concerto.instanceOf(obj, 'concerto.Concept');
expect(result).to.be.true;
});
});

describe('#validate', () => {

it('should validate data that conforms to model', () => {
Expand Down
40 changes: 40 additions & 0 deletions packages/concerto-core/test/modelmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('ModelManager', () => {
let composerModel = fs.readFileSync('./test/data/model/composer.cto', 'utf8');
let invalidModel = fs.readFileSync('./test/data/model/invalid.cto', 'utf8');
let invalidModel2 = fs.readFileSync('./test/data/model/invalid2.cto', 'utf8');
let concertoModel = fs.readFileSync('./test/data/model/concerto.cto', 'utf8');
let modelManager;
let sandbox;

Expand Down Expand Up @@ -958,4 +959,43 @@ concept Bar {
});
});

describe('#derivesFrom', () => {

it('should get derivesFrom for sub type', () => {
modelManager.addModelFile(concertoModel);
const result = modelManager.derivesFrom('org.accordproject.test.Customer', 'org.accordproject.test.Person');
result.should.be.true;
});

it('should get derivesFrom for sub-sub type', () => {
modelManager.addModelFile(concertoModel);
const result = modelManager.derivesFrom('org.accordproject.test.Manager', 'org.accordproject.test.Person');
result.should.be.true;
});

it('should get derivesFrom for type', () => {
modelManager.addModelFile(concertoModel);
const result = modelManager.derivesFrom('org.accordproject.test.Customer', 'org.accordproject.test.Customer');
result.should.be.true;
});

it('should not get derivesFrom for derived type', () => {
modelManager.addModelFile(concertoModel);
const result = modelManager.derivesFrom('org.accordproject.test.Person', 'org.accordproject.test.Customer');
result.should.be.false;
});

it('should be an instance of concerto.Participant', () => {
modelManager.addModelFile(concertoModel);
const result = modelManager.derivesFrom('org.accordproject.test.Person', 'concerto.Participant');
result.should.be.true;
});

it('all types should be an instance of concerto.Concept', () => {
modelManager.addModelFile(concertoModel);
const result = modelManager.derivesFrom('org.accordproject.test.Person', 'concerto.Concept');
result.should.be.true;
});
});

});
51 changes: 51 additions & 0 deletions packages/concerto-core/umd/concerto.js

Large diffs are not rendered by default.

0 comments on commit 924889a

Please sign in to comment.