Skip to content

Commit

Permalink
Merge pull request #115 from dselman/contract-as-composer-object
Browse files Browse the repository at this point in the history
(fix) contract data should be a Composer object
  • Loading branch information
dselman authored May 31, 2018
2 parents 86b53bc + be28047 commit ef22ae9
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 14 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/cicero-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"@accordproject/cicero-core": "0.3.14",
"@accordproject/cicero-engine": "0.3.14",
"@accordproject/ergo-compiler": "0.0.48",
"composer-common": "0.19.5",
"composer-common": "0.19.6",
"yargs": "9.0.1"
},
"license-check-config": {
Expand Down
2 changes: 1 addition & 1 deletion packages/cicero-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"nyc": "11.7.2"
},
"dependencies": {
"composer-common": "0.19.5"
"composer-common": "0.19.6"
},
"license-check-config": {
"src": [
Expand Down
25 changes: 20 additions & 5 deletions packages/cicero-core/lib/templateinstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ class TemplateInstance {
}
this.template = template;
this.data = null;
this.composerData = null;
}

/**
* Set the data for the clause
* @param {object} data - the data for the clause, must be an instance of the
* template model for the clause's template
* template model for the clause's template. This should be a plain JS object
* and will be deserialized and validated into the Composer object before assignment.
*/
setData(data) {
// verify that data is an instance of the template model
Expand All @@ -57,21 +59,34 @@ class TemplateInstance {

// downloadExternalDependencies the data using the template model
logger.debug('Setting clause data: ' + JSON.stringify(data));
const resource = this.template.getSerializer().fromJSON(data);
const resource = this.getTemplate().getSerializer().fromJSON(data);
resource.validate();

// passed validation!
// save the data
this.data = data;

// save the composer data
this.composerData = resource;
}

/**
* Get the data for the clause
* Get the data for the clause. This is a plain JS object. To retrieve the Composer
* object call getComposerData().
* @return {object} - the data for the clause, or null if it has not been set
*/
getData() {
return this.data;
}

/**
* Get the data for the clause. This is a Composer object. To retrieve the
* plain JS object suitable for serialization call toJSON() and retrieve the `data` property.
* @return {object} - the data for the clause, or null if it has not been set
*/
getDataAsComposerObject() {
return this.composerData;
}

/**
* Set the data for the clause by parsing natural language text.
* @param {string} text - the data for the clause
Expand Down Expand Up @@ -107,7 +122,7 @@ class TemplateInstance {
let hash = '';

if (this.data) {
const textToHash = JSON.stringify(this.data);
const textToHash = JSON.stringify(this.getData());
const hasher = crypto.createHash('sha256');
hasher.update(textToHash);
hash = '-' + hasher.digest('hex');
Expand Down
2 changes: 1 addition & 1 deletion packages/cicero-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"dependencies": {
"@accordproject/cicero-common": "0.3.14",
"@accordproject/ergo-compiler": "0.0.48",
"composer-common": "0.19.5",
"composer-common": "0.19.6",
"debug": "2.6.2",
"glob": "7.1.2",
"ietf-language-tag-regex": "0.0.5",
Expand Down
3 changes: 3 additions & 0 deletions packages/cicero-core/test/clause.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ describe('Clause', () => {
};
clause.setData(data);
clause.getData().should.eql(data);

// check that the composer data is really a Composer object
clause.getDataAsComposerObject().getFullyQualifiedType().should.be.equal('io.clause.latedeliveryandpenalty.TemplateModel');
});
it('should throw error for bad $class', async function() {
const template = await Template.fromDirectory('./test/data/latedeliveryandpenalty');
Expand Down
2 changes: 1 addition & 1 deletion packages/cicero-engine/lib/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class Engine {
this.compileJsClause(clause);
script = this.scripts[clause.getIdentifier()];

const validContract = clause.getData();
const validContract = clause.getDataAsComposerObject();
const factory = template.getFactory();
const vm = new VM({
timeout: 1000,
Expand Down
3 changes: 2 additions & 1 deletion packages/cicero-engine/lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class Logger {
keys.forEach(function(key) {
let element = obj[key];
if(element.getType) {
printable[key] = this.serializer.toJSON(element, {validate: false, permitResourcesForRelationships: true});
// TODO (DCS) call toJSON once that works for concepts
printable[key] = element.getFullyQualifiedType();
}
else {
printable[key] = element;
Expand Down
2 changes: 1 addition & 1 deletion packages/cicero-engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
},
"dependencies": {
"@accordproject/cicero-core": "0.3.14",
"composer-common": "0.19.5",
"composer-common": "0.19.6",
"moment": "2.20.1",
"vm2": "3.5.0"
},
Expand Down

0 comments on commit ef22ae9

Please sign in to comment.